Exemple #1
0
		public ParserError(ParserMatch noMatch, string errorId, string errorMessage)
		{
		  if (errorId == null)
		  {
			throw new ArgumentNullException("errorId");
		  }

		  if (errorMessage == null)
		  {
			throw new ArgumentNullException("errorMessage");
		  }

			long errorOffset = noMatch.Offset + noMatch.Length;

			IScanner scanner = noMatch.Scanner;
			long originalOffset = scanner.Offset;
			long lastLineOffset = 0;
			scanner.Offset = 0;
			Parser eol = Prims.Eol;
			Parser notEol = new CharParser(delegate(Char c)
					{
						return c != '\r' && c != '\n';
					});
			_line=1; // 1 based not 0 based
			while (!scanner.AtEnd)
			{
				notEol.Parse(scanner);
				if (scanner.AtEnd)
				{
				  break;
				}
				ParserMatch match = eol.Parse(scanner);
				if (scanner.Offset > errorOffset)
				{
					break;
				}
				if (match.Success)
				{
					++_line;
					lastLineOffset = scanner.Offset;
				}
			}

			_column = errorOffset - lastLineOffset + 1; // 1 based not 0 based
			scanner.Offset = originalOffset;
			_errorText = errorMessage;
			_errorId = errorId;
		}
		public void Setup() {
			this._letterOrDigit = Prims.LetterOrDigit;
			this._letterOrDigit.Act += OnLetterOrDigitParserSuccess;
			this._letter = Prims.Letter;
			this._letter.Act += OnLetterParserSuccess;
			this._symbol = Prims.Symbol;
			this._symbol.Act += OnSymbolParserSuccess;

			this._LetterOrDigitButNotLetterDifferenceParser = new DifferenceParser(this._letterOrDigit, this._letter);
			this._LetterOrDigitButNotLetterDifferenceParser.Act += OnDifferenceParserSuccess;

			this._symbolParserSuccess = false;
			this._letterParserSuccess = false;
			this._letterOrDigitParserSuccess = false;
			this._differenceParserSuccess = false;

		}
			public SimpleCollationRuleParser(bool useDebugger)
			{
				// creating sub parsers
				Parser character = new CharParser(IsCharacter);
				Parser hexDigit = Prims.HexDigit;
				Parser newLine = Prims.Eol;
				Parser whiteSpace = Ops.ZeroOrMore(Prims.WhiteSpace - Prims.Eol);

				Parser unicodeEscapeCharacter = Ops.Sequence('\\',
															 Ops.Expect("scr0001",
																		"Invalid unicode character escape sequence: missing 'u' after '\\'",
																		'u'),
															 Ops.Expect("scr0002",
																		"Invalid unicode character escape sequence: missing hexadecimal digit after '\\u'",
																		hexDigit),
															 Ops.Expect("scr0002",
																		"Invalid unicode character escape sequence: missing hexadecimal digit after '\\u'",
																		hexDigit),
															 Ops.Expect("scr0002",
																		"Invalid unicode character escape sequence: missing hexadecimal digit after '\\u'",
																		hexDigit),
															 Ops.Expect("scr0002",
																		"Invalid unicode character escape sequence: missing hexadecimal digit after '\\u'",
																		hexDigit));

				// creating rules and assigning names (for debugging)
				_collationElement = new Rule("collationElement");
				_collationGroup = new Rule("collationGroup");
				_collationLine = new Rule("collationLine");
				_collationRules = new Rule("collationRules");

				// assigning parsers to rules
				// collationElement ::= (unicodeEscapeCharacter | character)+
				_collationElement.Parser = Ops.Expect(CollationElementIsUnique, "scr0100", "Duplicate collation element",
													  Ops.OneOrMore(unicodeEscapeCharacter | character));

				// collationGroup ::= '(' WS* collationElement WS+ (collationElement WS?)+ ')'
				_collationGroup.Parser = Ops.Sequence('(',
													  whiteSpace,
													  Ops.Expect("scr0003",
																 "Expected: 2 or more collation elements in collation group (nested collation groups are not allowed)",
																 Ops.Sequence(_collationElement,
																			  whiteSpace,
																			  _collationElement,
																			  whiteSpace)),
													  Ops.Expect("scr0004",
																 "Expected: collation element or close group ')'",
																 Ops.ZeroOrMore(
																		 Ops.Sequence(_collationElement, whiteSpace))),
													  Ops.Expect("scr0005", "Expected: group close ')'", ')'));

				// collationLine ::= (collationElement WS? | collationGroup WS?)+
				_collationLine.Parser = Ops.OneOrMore(Ops.Sequence(_collationGroup | _collationElement,
																   whiteSpace));

				// collationRules ::= WS? collationLine? (NewLine WS? collationLine?)* EOF
				_collationRules.Parser = Ops.Expect("scr0006",
													"Invalid character",
													Ops.Sequence(whiteSpace,
																 Ops.Optional(_collationLine),
																 Ops.ZeroOrMore(Ops.Sequence(newLine,
																							 whiteSpace,
																							 Ops.Optional(_collationLine))),
																 Prims.End));

				character.Act += new ActionHandler(OnCharacter);
				unicodeEscapeCharacter.Act += OnUnicodeEscapeCharacter;
				_collationElement.Act += new ActionHandler(OnCollationElement);
				_collationGroup.Act += new ActionHandler(OnCollationGroup);
				_collationGroup.PreParse += new PreParseEventHandler(OnEnterCollationGroup);
				_collationGroup.PostParse += new PostParseEventHandler(OnExitCollationGroup);
				_collationLine.Act += new ActionHandler(OnCollationLine);
				_collationRules.Act += new ActionHandler(OnCollationRules);
				if (useDebugger)
				{
					// debuggger
					debug = new Debugger(Console.Out);
					debug += _collationRules;
					debug += _collationLine;
					debug += _collationGroup;
					debug += _collationElement;
				}
			}
Exemple #4
0
		public static void Test(CharParser test, Char success, Char failed)
		{
			Assert.IsTrue(test.Accepts(success));
			Assert.IsFalse(test.Accepts(failed));
		}