public void Terminal_Any() { var input = "ijk"; var bytes = Encoding.UTF8.GetBytes(input); var iterator = new ByteInputIterator(bytes); AExpression any = new Sequence(new AnyCharacter(), new AnyCharacter()); var visitor = new NpegParserVisitor(iterator); any.Accept(visitor); Assert.IsTrue(visitor.IsMatch); Assert.IsTrue(iterator.Index == 2, "Expected two characters to be consumed and Iterator updated by 2. 0, 1 .. points to 2"); input = "ij"; bytes = Encoding.UTF8.GetBytes(input); iterator = new ByteInputIterator(bytes); visitor = new NpegParserVisitor(iterator); any.Accept(visitor); Assert.IsTrue(visitor.IsMatch); Assert.IsTrue(iterator.Index == 2, "Expected two characters to be consumed and Iterator updated by 2. 0, 1 .. points to 2"); input = ""; bytes = Encoding.UTF8.GetBytes(input); iterator = new ByteInputIterator(bytes); any = new AnyCharacter(); visitor = new NpegParserVisitor(iterator); any.Accept(visitor); Assert.IsFalse(visitor.IsMatch); Assert.IsTrue(iterator.Index == 0, "Expected no characters to be consumed and index stay at zero."); var number = new Sequence( new OneOrMore(new CharacterClass {ClassExpression = "[0-9]"}), new NotPredicate( new AnyCharacter() ) ); input = "012345."; bytes = Encoding.UTF8.GetBytes(input); iterator = new ByteInputIterator(bytes); visitor = new NpegParserVisitor(iterator); number.Accept(visitor); Assert.IsFalse(visitor.IsMatch); }
public void CompositeVisitor_CapturingGroup_SandBoxTest_PriorityChoice1() { PrioritizedChoice newline = new PrioritizedChoice( new Literal {MatchText = "\r\n"}, // windows new Literal {MatchText = "\r\r"} // old macs ) .Or(new Literal {MatchText = "\n"}); // linux // Single Line Comment var singleLineComment = new Sequence( new Literal {MatchText = "//"}, new Sequence( new NotPredicate(newline), new AnyCharacter() ) .Star() ); // Multiline Comment var multiLineComment = new Sequence( new Literal {MatchText = "/*"}, new Sequence( new NotPredicate(new Literal {MatchText = "*/"}), new AnyCharacter() ) .Star() .Sequence(new Literal {MatchText = "*/"}) ); var comment = new PrioritizedChoice(singleLineComment, multiLineComment); var whitespace = new PrioritizedChoice( new CharacterClass {ClassExpression = "[ \t\r\n\v]"}, comment ); var label = new CapturingGroup("Label", new Sequence( new CharacterClass {ClassExpression = "[a-zA-Z_]"}, // must start with alpha character new ZeroOrMore(new CharacterClass {ClassExpression = "[a-zA-Z0-9_]"}) ) ); var backreference = new CapturingGroup("DynamicBackReferencing", new Sequence( new Literal {MatchText = @"\k<"}, new Sequence(new ZeroOrMore(whitespace), label).Sequence( new ZeroOrMore(whitespace)) ) .Sequence( new Optional( new Sequence( new Sequence( new Literal {MatchText = "["}, new CapturingGroup("CaseSensitive", new Literal {MatchText = @"\i"} ) ), new Literal {MatchText = "]"} ) ) ) .Sequence( new Sequence(new ZeroOrMore(whitespace), new Literal {MatchText = ">"}) ) ); var root = new CapturingGroup("Test", new Sequence( backreference, new NotPredicate(new AnyCharacter()) ) ); var input = @"\k< CapturedLabelVariableName >"; var bytes = Encoding.UTF8.GetBytes(input); var iterator = new ByteInputIterator(bytes); var visitor = new NpegParserVisitor(iterator); root.Accept(visitor); Assert.IsTrue(visitor.IsMatch); AstNode node = visitor.AST; Assert.IsTrue(node.Token.Name == "Test"); Assert.IsTrue(node.Children[0].Token.Name == "DynamicBackReferencing"); Assert.IsTrue(node.Children[0].Children[0].Token.Name == "Label"); Assert.IsTrue(node.Children[0].Children[0].Token.ValueAsString(iterator) == "CapturedLabelVariableName"); }
public override void VisitLeave(Sequence expression) { IsMatchPredicate localRight = _matchStack.Pop(); IsMatchPredicate localLeft = _matchStack.Pop(); _matchStack.Push( delegate(IInputIterator iterator) { Boolean result = true; Int32 savePosition = iterator.Index; if (localLeft(iterator) && localRight(iterator)) { result &= true; } else { iterator.Index = savePosition; result &= false; } return result; } ); }
public override void VisitExecute(Sequence expression) { }
public override void VisitEnter(Sequence expression) { }
public override void VisitLeave(Sequence expression) { //String input = terminal.Pop().ToString(); //if (!this.uniqueBranches.ContainsKey(input)) //{ // String nodename = "node" + branchcount++; // this.uniqueBranches.Add(input, nodename); // this.statements.Add(String.Format("{0}: {1};", nodename, input)); //} //// remember last node is always a captured group so peek should not throw exceptions //// insert terminal name //terminal.Peek().Append(this.uniqueBranches[input]); }
public override void VisitEnter(Sequence expression) { //this.terminal.Push(new StringBuilder()); }
public override void VisitExecute(Sequence expression) { terminal.Peek().Append(" "); }
public abstract void VisitEnter(Sequence expression);
public abstract void VisitLeave(Sequence expression);
public abstract void VisitExecute(Sequence expression);
private static AExpression mComment() { // Single Line Comment AExpression singleLineComment = new Sequence( new Literal {MatchText = "//"}, new Sequence( new NotPredicate(mNewLine()), new AnyCharacter() ) .Star() ); // Multiline Comment AExpression multiLineComment = new Sequence( new Literal {MatchText = "/*"}, new Sequence( new NotPredicate(new Literal {MatchText = "*/"}), new AnyCharacter() ) .Star() .Sequence(new Literal {MatchText = "*/"}) ); return new PrioritizedChoice(singleLineComment, multiLineComment); }
public void PracticalExample_MathematicalFormula() { #region Composite var VALUE = new PrioritizedChoice( new CapturingGroup("VALUE", new OneOrMore(new CharacterClass {ClassExpression = "[0-9]"}) ) , new Sequence( new Literal {MatchText = "("}, new RecursionCall("ParethesisFunction") ) .Sequence(new Literal {MatchText = ")"}) ); var PRODUCT = new Sequence( VALUE, new Sequence( new CapturingGroup("SYMBOL", new PrioritizedChoice( new Literal {MatchText = "*"}, new Literal {MatchText = "/"} ) ), VALUE ).Star() ); var SUM = new Sequence( PRODUCT, new Sequence( new CapturingGroup("SYMBOL", new PrioritizedChoice( new Literal {MatchText = "+"}, new Literal {MatchText = "-"} ) ), PRODUCT ).Star() ); AExpression EXPRESSION = new RecursionCreate("ParethesisFunction", new CapturingGroup("EXPRESSION", SUM)); #endregion var input = "((((12/3)+5-2*(81/9))+1))"; var bytes = Encoding.UTF8.GetBytes(input); var iterator = new ByteInputIterator(bytes); var visitor = new NpegParserVisitor(iterator); EXPRESSION.Accept(visitor); Assert.IsTrue(visitor.IsMatch); AstNode node = visitor.AST; Assert.IsTrue(node.Token.ValueAsString(iterator) == input); }