1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
/*
* Created by SharpDevelop.
* User: lextm
* Date: 2008/5/17
* Time: 16:50
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Lextm.SharpSnmpLib.Mib.Elements.Types;
namespace Lextm.SharpSnmpLib.Mib
{
/// <summary>
/// Lexer class that parses MIB files into symbol list.
/// </summary>
public sealed class Lexer
{
private readonly SymbolList _symbols = new SymbolList();
public Lexer(string file)
: this(file, new StreamReader(file))
{
}
public Lexer(string file, TextReader stream)
{
this.Parse(file, stream);
}
public ISymbolEnumerator GetEnumerator()
{
return _symbols.GetSymbolEnumerator();
}
#region Parsing of MIB File
private class ParseParams
{
public string File;
public StringBuilder Temp = new StringBuilder();
public bool StringSection = false;
public bool AssignSection = false;
public bool AssignAhead = false;
public bool DotSection = false;
}
/// <summary>
/// Parses MIB file to symbol list.
/// </summary>
/// <param name="file">File</param>
/// <param name="stream">File stream</param>
private void Parse(string file, TextReader stream)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
ParseParams pp = new ParseParams();
pp.File = file;
string line;
int row = 0;
while ((line = stream.ReadLine()) != null)
{
if (!pp.StringSection && line.TrimStart().StartsWith("--", StringComparison.Ordinal))
{
row++;
continue; // commented line
}
ParseLine(pp, line, row);
row++;
}
}
private void ParseLine(ParseParams pp, string line, int row)
{
line = line + "\n";
int count = line.Length;
for (int i = 0; i < count; i++)
{
char current = line[i];
bool moveNext = Parse(pp, current, row, i);
if (moveNext)
{
break;
}
}
}
private bool Parse(ParseParams pp, char current, int row, int column)
{
switch (current)
{
case '\n':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case ';':
case ',':
case '|':
if (!pp.StringSection)
{
bool moveNext = ParseLastSymbol(pp, row, column);
if (moveNext)
{
_symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column));
return true;
}
_symbols.Add(CreateSpecialSymbol(pp.File, current, row, column));
return false;
}
break;
case '"':
pp.StringSection = !pp.StringSection;
break;
case '\r':
return false;
default:
if ((int)current == 0x1A)
{
// IMPORTANT: ignore invisible characters such as SUB.
return false;
}
if (Char.IsWhiteSpace(current) && !pp.AssignSection && !pp.StringSection)
{
bool moveNext = ParseLastSymbol(pp, row, column);
if (moveNext)
{
_symbols.Add(CreateSpecialSymbol(pp.File, '\n', row, column));
return true;
}
return false;
}
if (pp.AssignAhead)
{
pp.AssignAhead = false;
ParseLastSymbol(pp, row, column);
break;
}
if (pp.DotSection && current != '.')
{
ParseLastSymbol(pp, row, column);
pp.DotSection = false;
}
if (current == '.' && !pp.StringSection)
{
if (!pp.DotSection)
{
ParseLastSymbol(pp, row, column);
pp.DotSection = true;
}
}
if (current == ':' && !pp.StringSection)
{
if (!pp.AssignSection)
{
ParseLastSymbol(pp, row, column);
}
pp.AssignSection = true;
}
if (current == '=' && !pp.StringSection)
{
pp.AssignSection = false;
pp.AssignAhead = true;
}
break;
}
pp.Temp.Append(current);
return false;
}
private bool ParseLastSymbol(ParseParams pp, int row, int column)
{
if (pp.Temp.Length > 0)
{
Symbol s = new Symbol(pp.File, pp.Temp.ToString(), row, column);
pp.Temp.Length = 0;
if (s.ToString().StartsWith(Symbol.Comment.ToString()))
{
// ignore the rest symbols on this line because they are in comment.
return true;
}
_symbols.Add(s);
}
return false;
}
private static Symbol CreateSpecialSymbol(string file, char value, int row, int column)
{
string str;
switch (value)
{
case '\n':
str = Environment.NewLine;
break;
case '{':
str = "{";
break;
case '}':
str = "}";
break;
case '(':
str = "(";
break;
case ')':
str = ")";
break;
case '[':
str = "[";
break;
case ']':
str = "]";
break;
case ';':
str = ";";
break;
case ',':
str = ",";
break;
case '|':
str = "|";
break;
default:
throw new ArgumentException("value is not a special character");
}
return new Symbol(file, str, row, column);
}
#endregion
#region Static Parse Helper
public static ITypeAssignment ParseBasicTypeDef(IModule module, string name, ISymbolEnumerator symbols, bool isMacroSyntax = false)
{
Symbol current = symbols.NextNonEOLSymbol();
if (current == Symbol.Bits)
{
return new BitsType(module, name, symbols);
}
if (IntegerType.IsIntegerType(current))
{
return new IntegerType(module, name, current, symbols);
}
if (UnsignedType.IsUnsignedType(current))
{
return new UnsignedType(module, name, current, symbols);
}
if (current == Symbol.Opaque)
{
return new OpaqueType(module, name, symbols);
}
if (current == Symbol.IpAddress)
{
return new IpAddressType(module, name, symbols);
}
if (current == Symbol.TextualConvention)
{
return new TextualConvention(module, name, symbols);
}
if (current == Symbol.Octet)
{
Symbol next = symbols.NextNonEOLSymbol();
if (next == Symbol.String)
{
return new OctetStringType(module, name, symbols);
}
symbols.PutBack(next);
}
if (current == Symbol.Object)
{
Symbol next = symbols.NextNonEOLSymbol();
if (next == Symbol.Identifier)
{
return new ObjectIdentifierType(module, name, symbols);
}
symbols.PutBack(next);
}
if (current == Symbol.Sequence)
{
Symbol next = symbols.NextNonEOLSymbol();
if (next == Symbol.Of)
{
return new SequenceOf(module, name, symbols);
}
else
{
symbols.PutBack(next);
return new Sequence(module, name, symbols);
}
}
if (current == Symbol.Choice)
{
return new Choice(module, name, symbols);
}
return new TypeAssignment(module, name, current, symbols, isMacroSyntax);
}
public static void ParseOidValue(ISymbolEnumerator symbols, out string parent, out uint value)
{
parent = null;
value = 0;
Symbol current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.OpenBracket);
Symbol previous = null;
StringBuilder longParent = new StringBuilder();
current = symbols.NextNonEOLSymbol();
longParent.Append(current);
while ((current = symbols.NextNonEOLSymbol()) != null)
{
bool succeeded;
if (current == Symbol.OpenParentheses)
{
longParent.Append(current);
current = symbols.NextNonEOLSymbol();
succeeded = UInt32.TryParse(current.ToString(), out value);
current.Assert(succeeded, "not a decimal");
longParent.Append(current);
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.CloseParentheses);
longParent.Append(current);
continue;
}
if (current == Symbol.CloseBracket)
{
parent = longParent.ToString();
return;
}
succeeded = UInt32.TryParse(current.ToString(), out value);
if (succeeded)
{
// numerical way
while ((current = symbols.NextNonEOLSymbol()) != Symbol.CloseBracket)
{
longParent.Append(".").Append(value);
succeeded = UInt32.TryParse(current.ToString(), out value);
current.Assert(succeeded, "not a decimal");
}
current.Expect(Symbol.CloseBracket);
parent = longParent.ToString();
return;
}
longParent.Append(".");
longParent.Append(current);
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.OpenParentheses);
longParent.Append(current);
current = symbols.NextNonEOLSymbol();
succeeded = UInt32.TryParse(current.ToString(), out value);
current.Assert(succeeded, "not a decimal");
longParent.Append(current);
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.CloseParentheses);
longParent.Append(current);
previous = current;
}
throw MibException.Create("end of file reached", previous);
}
public static ValueRanges DecodeRanges(ISymbolEnumerator symbols)
{
ValueRanges result = new ValueRanges();
Symbol startSymbol = symbols.NextNonEOLSymbol();
Symbol current = startSymbol;
current.Expect(Symbol.OpenParentheses);
while (current != Symbol.CloseParentheses)
{
Symbol value1Symbol = symbols.NextNonEOLSymbol();
if ((value1Symbol == Symbol.Size) && !result.IsSizeDeclaration)
{
result.IsSizeDeclaration = true;
symbols.NextNonEOLSymbol().Expect(Symbol.OpenParentheses);
continue;
}
// check for valid number
Int64? value1 = DecodeNumber(value1Symbol);
if (!value1.HasValue)
{
value1Symbol.Assert(false, "Invalid range declaration!");
}
// process next symbol
ValueRange range;
current = symbols.NextNonEOLSymbol();
if (current == Symbol.DoubleDot)
{
// its a continuous range
Symbol value2Symbol = symbols.NextNonEOLSymbol();
Int64? value2 = DecodeNumber(value2Symbol);
value2Symbol.Assert(value2.HasValue && (value2.Value >= value1.Value), "Invalid range declaration!");
if (value2.Value == value1.Value)
{
range = new ValueRange(value1.Value, null);
}
else
{
range = new ValueRange(value1.Value, value2.Value);
}
current = symbols.NextNonEOLSymbol();
}
else
{
// its a single number
range = new ValueRange(value1.Value, null);
}
// validate range
if (result.IsSizeDeclaration)
{
value1Symbol.Assert(range.Start >= 0, "Invalid range declaration! Size must be greater than 0");
}
result.Add(range);
// check next symbol
current.Expect(Symbol.Pipe, Symbol.CloseParentheses);
}
if (result.IsSizeDeclaration)
{
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.CloseParentheses);
}
// validate ranges in between
for (int i=0; i<result.Count; i++)
{
for (int k=i+1; k<result.Count; k++)
{
startSymbol.Assert(!result[i].IntersectsWith(result[k]), "Invalid range declaration! Overlapping of ranges!");
}
}
return result;
}
public static Int64? DecodeNumber(Symbol number)
{
Int64 result;
string numString = (number != null) ? number.ToString() : null;
if (!String.IsNullOrEmpty(numString))
{
if (numString.StartsWith("'") && (numString.Length > 3))
{
// search second apostrophe
int end = numString.IndexOf('\'', 1);
if (end == (numString.Length - 2))
{
try
{
switch (numString[numString.Length - 1])
{
case 'b':
case 'B':
result = Convert.ToInt64(numString.Substring(1, numString.Length - 3), 2);
return result;
case 'h':
case 'H':
result = Convert.ToInt64(numString.Substring(1, numString.Length - 3), 16);
return result;
}
}
catch
{
}
}
}
else if (Int64.TryParse(numString, out result))
{
return result;
}
}
return null;
}
public static ValueMap DecodeEnumerations(ISymbolEnumerator symbols)
{
Symbol current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.OpenBracket);
ValueMap map = new ValueMap();
do
{
current = symbols.NextNonEOLSymbol();
string identifier = current.ToString();
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.OpenParentheses);
current = symbols.NextNonEOLSymbol();
Int64 enumValue;
if (Int64.TryParse(current.ToString(), out enumValue))
{
try
{
// Have to include the number as it seems repeated identifiers are allowed ??
map.Add(enumValue, String.Format("{0}({1})", identifier, enumValue));
}
catch (ArgumentException ex)
{
current.Assert(false, ex.Message);
}
}
else
{
// Need to get "DefinedValue".
}
current = symbols.NextNonEOLSymbol();
current.Expect(Symbol.CloseParentheses);
current = symbols.NextNonEOLSymbol();
} while (current == Symbol.Comma);
current.Expect(Symbol.CloseBracket);
return map;
}
#endregion
}
}
|