public void testList() { ITree root = new CommonTree((IToken)null); ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITree u = new CommonTree(new CommonToken(105)); root.AddChild(t); root.AddChild(u); CommonTreeNodeStream stream = new CommonTreeNodeStream(root); string expected = " 101 102 103 104 105"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101 2 102 2 103 3 104 3 105"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
public void test4Nodes() { // ^(101 ^(102 103) 104) CommonTree r0 = new CommonTree(new CommonToken(101)); r0.AddChild(new CommonTree(new CommonToken(102))); r0.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(new CommonTree(new CommonToken(104))); Assert.IsNull(r0.parent); Assert.AreEqual(-1, r0.childIndex); }
public void test4Nodes() { // ^(101 ^(102 103) 104) CommonTree r0 = new CommonTree(new CommonToken(101)); r0.AddChild(new CommonTree(new CommonToken(102))); r0.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(new CommonTree(new CommonToken(104))); Assert.IsNull(r0.parent); Assert.AreEqual(-1, r0.childIndex); }
/// <summary> /// Test a tree with four nodes - ^(101 ^(102 103) 104) /// </summary> public void test4Nodes() { ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITreeNodeStream stream = CreateCommonTreeNodeStream(t); string expected = " 101 102 103 104"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101 2 102 2 103 3 104 3"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
/// <summary> /// Test a tree with four nodes - ^(101 ^(102 103) 104) /// </summary> public void test4Nodes() { ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITreeNodeStream stream = CreateCommonTreeNodeStream(t); string expected = " 101 102 103 104"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101 2 102 2 103 3 104 3"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
public void testLT() { // ^(101 ^(102 103) 104) ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITreeNodeStream stream = CreateCommonTreeNodeStream(t); Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(2)).Type); Assert.AreEqual(102, ((ITree)stream.LT(3)).Type); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(4)).Type); Assert.AreEqual(103, ((ITree)stream.LT(5)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(6)).Type); Assert.AreEqual(104, ((ITree)stream.LT(7)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(8)).Type); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(9)).Type); // check way ahead Assert.AreEqual(Token.EOF, ((ITree)stream.LT(100)).Type); }
public static void ACTION(TokenRewriteStream tokens, CommonTree t) { CommonTree parent = (CommonTree)t.Parent; int ptype = parent.Type; if (ptype == ANTLRParser.SCOPE || // we have special rules for these ptype == ANTLRParser.AMPERSAND) { return; } //Console.Out.WriteLine( "ACTION: " + t.Text ); CommonTree root = (CommonTree)t.GetAncestor(ANTLRParser.RULE); if (root != null) { CommonTree rule = (CommonTree)root.GetChild(0); //Console.Out.WriteLine( "rule: " + rule ); if (!char.IsUpper(rule.Text[0])) { tokens.Delete(t.TokenStartIndex, t.TokenStopIndex); KillTrailingNewline(tokens, t.token.TokenIndex); } } }
public void testList() { ITree root = new CommonTree((IToken)null); ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITree u = new CommonTree(new CommonToken(105)); root.AddChild(t); root.AddChild(u); CommonTreeNodeStream stream = new CommonTreeNodeStream(root); string expected = " 101 102 103 104 105"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101 2 102 2 103 3 104 3 105"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
public void testLT() { // ^(101 ^(102 103) 104) ITree t = new CommonTree(new CommonToken(101)); t.AddChild(new CommonTree(new CommonToken(102))); t.GetChild(0).AddChild(new CommonTree(new CommonToken(103))); t.AddChild(new CommonTree(new CommonToken(104))); ITreeNodeStream stream = CreateCommonTreeNodeStream(t); Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(2)).Type); Assert.AreEqual(102, ((ITree)stream.LT(3)).Type); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(4)).Type); Assert.AreEqual(103, ((ITree)stream.LT(5)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(6)).Type); Assert.AreEqual(104, ((ITree)stream.LT(7)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(8)).Type); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(9)).Type); // check way ahead Assert.AreEqual(Token.EOF, ((ITree)stream.LT(100)).Type); }
public virtual void Rewrite(ITreeAdaptor adaptor, CommonTree t, string[] tokenNames) { TreeWizard wiz = new TreeWizard(adaptor, tokenNames); // ACTIONS STUFF wiz.Visit(t, ANTLRParser.ACTION, (tree) => { ACTION(tokens, (CommonTree)tree); }); // ^('@' id ACTION) rule actions wiz.Visit(t, ANTLRParser.AMPERSAND, (tree) => { CommonTree a = (CommonTree)t; CommonTree action = null; if (a.ChildCount == 2) { action = (CommonTree)a.GetChild(1); } else if (a.ChildCount == 3) { action = (CommonTree)a.GetChild(2); } if (action.Type == ANTLRParser.ACTION) { tokens.Delete(a.TokenStartIndex, a.TokenStopIndex); KillTrailingNewline(tokens, action.TokenStopIndex); } }); wiz.Visit(t, ANTLRParser.ACTION, (tree) => { }); // wipe rule arguments wiz.Visit(t, ANTLRParser.ARG, (tree) => { CommonTree a = (CommonTree)t; a = (CommonTree)a.GetChild(0); tokens.Delete(a.token.TokenIndex); KillTrailingNewline(tokens, a.token.TokenIndex); }); // wipe rule return declarations wiz.Visit(t, ANTLRParser.RET, (tree) => { CommonTree a = (CommonTree)t; CommonTree ret = (CommonTree)a.GetChild(0); tokens.Delete(a.token.TokenIndex, ret.token.TokenIndex); }); // comment out semantic predicates wiz.Visit(t, ANTLRParser.SEMPRED, (tree) => { CommonTree a = (CommonTree)t; tokens.Replace(a.token.TokenIndex, "/*" + a.Text + "*/"); }); // comment out semantic predicates wiz.Visit(t, ANTLRParser.GATED_SEMPRED, (tree) => { CommonTree a = (CommonTree)t; string text = tokens.ToString(a.TokenStartIndex, a.TokenStopIndex); tokens.Replace(a.TokenStartIndex, a.TokenStopIndex, "/*" + text + "*/"); }); // comment scope specs wiz.Visit(t, ANTLRParser.SCOPE, (tree) => { CommonTree a = (CommonTree)t; tokens.Delete(a.TokenStartIndex, a.TokenStopIndex); KillTrailingNewline(tokens, a.TokenStopIndex); }); // args r[x,y] -> ^(r [x,y]) wiz.Visit(t, ANTLRParser.ARG_ACTION, (tree) => { CommonTree a = (CommonTree)t; if (a.Parent.Type == ANTLRParser.RULE_REF) { tokens.Delete(a.TokenStartIndex, a.TokenStopIndex); } }); #if false // what is this token type in the C# ported version of the V3 grammar? // ^('=' id ^(RULE_REF [arg])), ... wiz.Visit(t, ANTLRParser.LABEL_ASSIGN, (tree) => { CommonTree a = (CommonTree)t; if (!a.HasAncestor(ANTLRParser.OPTIONS)) { // avoid options CommonTree child = (CommonTree)a.GetChild(0); // kill "id=" tokens.Delete(a.token.TokenIndex); tokens.Delete(child.token.TokenIndex); } }); #endif #if false // what is this token type in the C# ported version of the V3 grammar? // ^('+=' id ^(RULE_REF [arg])), ... wiz.Visit(t, ANTLRParser.LIST_LABEL_ASSIGN, (tree) => { CommonTree a = (CommonTree)t; CommonTree child = (CommonTree)a.GetChild(0); // kill "id+=" tokens.Delete(a.token.TokenIndex); tokens.Delete(child.token.TokenIndex); }); #endif // AST STUFF wiz.Visit(t, ANTLRParser.REWRITE, (tree) => { CommonTree a = (CommonTree)t; CommonTree child = (CommonTree)a.GetChild(0); int stop = child.TokenStopIndex; if (child.Type == ANTLRParser.SEMPRED) { CommonTree rew = (CommonTree)a.GetChild(1); stop = rew.TokenStopIndex; } tokens.Delete(a.token.TokenIndex, stop); KillTrailingNewline(tokens, stop); }); wiz.Visit(t, ANTLRParser.ROOT, (tree) => { tokens.Delete(((CommonTree)t).token.TokenIndex); }); wiz.Visit(t, ANTLRParser.BANG, (tree) => { tokens.Delete(((CommonTree)t).token.TokenIndex); }); }