public virtual object ParseNode() { // "%label:" prefix string label = null; if (ttype == TreePatternLexer.Percent) { ttype = tokenizer.NextToken(); if (ttype != TreePatternLexer.Id) { return(null); } label = tokenizer.sval.ToString(); ttype = tokenizer.NextToken(); if (ttype != TreePatternLexer.Colon) { return(null); } ttype = tokenizer.NextToken(); // move to ID following colon } // Wildcard? if (ttype == TreePatternLexer.Dot) { ttype = tokenizer.NextToken(); IToken wildcardPayload = new CommonToken(0, "."); TreeWizard.TreePattern node = new TreeWizard.WildcardTreePattern(wildcardPayload); if (label != null) { node.label = label; } return(node); } // "ID" or "ID[arg]" if (ttype != TreePatternLexer.Id) { return(null); } string tokenName = tokenizer.sval.ToString(); ttype = tokenizer.NextToken(); if (tokenName.Equals("nil")) { return(adaptor.Nil()); } string text = tokenName; // check for arg string arg = null; if (ttype == TreePatternLexer.Arg) { arg = tokenizer.sval.ToString(); text = arg; ttype = tokenizer.NextToken(); } // create node int treeNodeType = wizard.GetTokenType(tokenName); if (treeNodeType == TokenTypes.Invalid) { return(null); } object node2; node2 = adaptor.Create(treeNodeType, text); if (label != null && node2.GetType() == typeof(TreeWizard.TreePattern)) { ((TreeWizard.TreePattern)node2).label = label; } if (arg != null && node2.GetType() == typeof(TreeWizard.TreePattern)) { ((TreeWizard.TreePattern)node2).hasTextArg = true; } return(node2); }
public void TestRepeatsVisitWithNullParentAndContext() { TreeWizard wiz = new TreeWizard( adaptor, tokens ); CommonTree t = (CommonTree)wiz.Create( "(A B (A C B) B D D)" ); IList elements = new List<object>(); wiz.Visit( t, wiz.GetTokenType( "A" ), new testRepeatsVisitWithContext_TreeWizard_Visitor( elements ) ); string found = elements.ToElementString(); string expecting = "[A@nil[0], A@A[1]]"; Assert.AreEqual( expecting, found ); }
public object ParseNode() { // "%label:" prefix string label = null; if (ttype == TreePatternLexer.PERCENT) { ttype = tokenizer.NextToken(); if (ttype != TreePatternLexer.ID) { return(null); } label = tokenizer.sval.ToString(); ttype = tokenizer.NextToken(); if (ttype != TreePatternLexer.COLON) { return(null); } ttype = tokenizer.NextToken(); // move to ID following colon } // Wildcard? if (ttype == TreePatternLexer.DOT) { ttype = tokenizer.NextToken(); IToken wildcardPayload = new CommonToken(0, "."); TreeWizard.TreePattern node = new TreeWizard.WildcardTreePattern(wildcardPayload); if (label != null) { node.label = label; } return(node); } // "ID" or "ID[arg]" if (ttype != TreePatternLexer.ID) { return(null); } string tokenName = tokenizer.sval.ToString(); ttype = tokenizer.NextToken(); if (tokenName.Equals("nil")) { return(adaptor.GetNilNode()); } string text = tokenName; // check for arg string arg = null; if (ttype == TreePatternLexer.ARG) { arg = tokenizer.sval.ToString(); text = arg; ttype = tokenizer.NextToken(); } // create node int treeNodeType = wizard.GetTokenType(tokenName); if (treeNodeType == Token.INVALID_TOKEN_TYPE) { return(null); } object nodeObj; nodeObj = adaptor.Create(treeNodeType, text); if ((label != null) && (nodeObj.GetType() == typeof(TreeWizard.TreePattern))) { ((TreeWizard.TreePattern)nodeObj).label = label; } if ((arg != null) && (nodeObj.GetType() == typeof(TreeWizard.TreePattern))) { ((TreeWizard.TreePattern)nodeObj).hasTextArg = true; } return(nodeObj); }
public void TestRepeatsVisit() { TreeWizard wiz = new TreeWizard( adaptor, tokens ); CommonTree t = (CommonTree)wiz.Create( "(A B (A C B) B D D)" ); IList elements = new List<object>(); wiz.Visit( t, wiz.GetTokenType( "B" ), new testNoRepeatsVisit_TreeWizard_Visitor( elements ) ); string found = elements.ToElementString(); string expecting = "[B, B, B]"; Assert.AreEqual( expecting, found ); }
public void TestRepeatsVisitWithContext() { TreeWizard wiz = new TreeWizard( adaptor, tokens ); CommonTree t = (CommonTree)wiz.Create( "(A B (A C B) B D D)" ); IList elements = new List<object>(); wiz.Visit( t, wiz.GetTokenType( "B" ), new testRepeatsVisitWithContext_TreeWizard_Visitor( elements ) ); string found = elements.ToElementString(); string expecting = "[B@A[0], B@A[1], B@A[2]]"; assertEquals( expecting, found ); }