Ejemplo n.º 1
0
		public void Simple()
		{
			var input = "  hello ( parsing world )  ";

			// optional repeating whitespace
			var ws = Terminals.WhiteSpace.Repeat(0);

			// parse a value with or without brackets
			var valueParser = Terminals.Set('(')
				.Then(Terminals.Set(')').Inverse().Repeat().Until(ws.Then(')')).Named("value"))
				.Then(Terminals.Set(')'))
				.SeparatedBy(ws)
				.Or(Terminals.WhiteSpace.Inverse().Repeat().Named("value"));

			// our grammar
			var grammar = new Grammar(
				ws
				.Then(valueParser.Named("first"))
				.Then(valueParser.Named("second"))
				.Then(Terminals.End)
				.SeparatedBy(ws)
			);

			var match = grammar.Match(input);
			Assert.IsTrue(match.Success, match.ErrorMessage);
			Assert.AreEqual("hello", match["first"]["value"].Text);
			Assert.AreEqual("parsing world", match["second"]["value"].Text);
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Initializes a new copy of the <see cref="Eto.Parse.Grammar"/> class
		/// </summary>
		/// <param name="other">Other object to copy</param>
		/// <param name="args">Arguments for the copy</param>
		protected Grammar(Grammar other, ParserCloneArgs args)
		{
			this.EnableMatchEvents = other.EnableMatchEvents;
			this.Separator = args.Clone(other.Separator);
			this.CaseSensitive = other.CaseSensitive;
			this.Optimizations = other.Optimizations;
		}
Ejemplo n.º 3
0
		public void Except()
		{
			var ch = Terminals.AnyChar.Except(Terminals.WhiteSpace).Repeat();

			var input = "abc def 1234";

			var repeat = new Grammar(ch) { AllowPartialMatch = true };

			var match = repeat.Match(input);
			Assert.IsTrue(match.Success);
			Assert.AreEqual("abc", match.Text);
		}
Ejemplo n.º 4
0
		public void RepeatUntil()
		{
			var input = "abc def 1234";

			// optional repeating whitespace
			var ws = Terminals.WhiteSpace.Repeat(0);

			// repeat until we get a digit, and exclude any whitespace inbetween
			var repeat = new Grammar(Terminals.AnyChar.Repeat().Until(ws.Then(Terminals.Digit))) { AllowPartialMatch = true };

			var match = repeat.Match(input);
			Assert.IsTrue(match.Success);
			Assert.AreEqual("abc def", match.Text);
		}
Ejemplo n.º 5
0
		public void RepeatUntil()
		{
			var input = "abc def 1234";

			// optional repeating whitespace
			var ws = -Terminals.WhiteSpace;

			// repeat until we get a digit, and exclude any whitespace inbetween
			var repeat = +Terminals.AnyChar ^ (ws & Terminals.Digit);

			var match = new Grammar(repeat) { AllowPartialMatch = true }.Match(input);
			Assert.IsTrue(match.Success, match.ErrorMessage);
			Assert.AreEqual("abc def", match.Text);
		}
Ejemplo n.º 6
0
		public void Simple()
		{
			var input = "  hello ( parsing world )  ";

			// optional repeating whitespace
			var ws = -Terminals.WhiteSpace;

			// parse a value with or without brackets
			Parser valueParser = 
				('(' & ws & (+Terminals.AnyChar ^ (ws & ')')).Named("value") & ws & ')')
				| (+!Terminals.WhiteSpace).Named("value");

			// our grammar
			var grammar = new Grammar(ws & valueParser.Named("first") & ws & valueParser.Named("second") & ws & Terminals.End);

			var match = grammar.Match(input);
			Assert.IsTrue(match.Success);
			Assert.AreEqual("hello", match["first"]["value"].Text);
			Assert.AreEqual("parsing world", match["second"]["value"].Text);
		}
Ejemplo n.º 7
0
 public Parser(string textGrammar, string root_rule, EbnfStyle style)
 {
     var ebnfGrammar = new Eto.Parse.Grammars.EbnfGrammar( style	);
     myGrammar = ebnfGrammar.Build(textGrammar, root_rule);
     myGrammar.CheckAbsentRules ();
 }
Ejemplo n.º 8
0
		internal ParseArgs(Grammar grammar, Scanner scanner)
		{
			Grammar = grammar;
			Scanner = scanner;
		}
Ejemplo n.º 9
0
		public static void TestAddress(Grammar addressParser)
		{
			TestAddress(addressParser.Match(Address));
		}
Ejemplo n.º 10
0
		void SetEbnfRules(Grammar myEbnf)
		{
			// EBNF spec does not identify terminals and other special rules, so we define them here so we can round
			// trip the original defintion.
			// For you own grammars, there is an extension of the EbnfGrammar that allows you to define terminals
			// by using := instead of = for your rule.
			myEbnf["meta identifier"].SeparateChildrenBy(-Terminals.SingleLineWhiteSpace);
			myEbnf["integer"].SeparateChildrenBy(null);
			myEbnf["terminal string"].SeparateChildrenBy(null);
		}
Ejemplo n.º 11
0
 public IfBlocksParser()
 {
     _grammar = BuildGrammar();
 }