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 testSeekFromStart() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); stream.Seek(7); // seek to 107 Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); }
public virtual void BuildNormalRuleFunction(Rule r, RuleFunction function) { CodeGenerator gen = @delegate.GetGenerator(); // TRIGGER factory functions for rule alts, elements GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.Token.InputStream); GrammarAST blk = (GrammarAST)r.ast.GetFirstChildWithType(ANTLRParser.BLOCK); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk); walker = new SourceGenTriggers(nodes, this); try { // walk AST of rule alts/elements function.code = DefaultOutputModelFactory.List(walker.block(null, null)); function.hasLookaheadBlock = walker.hasLookaheadBlock; } catch (Antlr.Runtime.RecognitionException e) { @delegate.GetGenerator().tool.ConsoleError.WriteLine(e.Message); @delegate.GetGenerator().tool.ConsoleError.WriteLine(e.StackTrace); } function.ctxType = @delegate.GetTarget().GetRuleFunctionContextStructName(function); function.postamble = RulePostamble(function, r); }
public virtual void ReduceBlocksToSets(GrammarAST root) { CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); BlockSetTransformer transformer = new BlockSetTransformer(nodes, g); transformer.TreeAdaptor = adaptor; transformer.Downup(root); }
public void testPushPopFromEOF() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); while (stream.LA(1) != Token.EOF) { stream.Consume(); } int indexOf102 = 2; int indexOf104 = 6; Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); // CALL 102 stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); // CALL 104 stream.Push(indexOf104); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); }
public virtual void ReduceBlocksToSets(GrammarAST root) { CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); BlockSetTransformer transformer = new BlockSetTransformer(nodes, g); transformer.TreeAdaptor = adaptor; transformer.Downup(root); }
public void testPushPop() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3"; String found = stream.ToString(); Assert.AreEqual(expecting, found); // Assume we want to hit node 107 and then "call 102" then return int indexOf102 = 2; int indexOf107 = 12; for (int k = 1; k <= indexOf107; k++) { // consume til 107 node stream.Consume(); } // CALL 102 Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN stream.Pop(); Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); }
public void testStackStretch() { // make more than INITIAL_CALL_STACK_SIZE pushes ITree r0 = new CommonTree(new CommonToken(101)); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); // go 1 over initial size for (int i = 1; i <= CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE + 1; i++) { stream.Push(i); } Assert.AreEqual(10, stream.Pop()); Assert.AreEqual(9, stream.Pop()); }
public void testMarkRewindInMiddle() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); for (int k = 1; k <= 7; k++) { // consume til middle //System.out.println(((ITree)stream.LT(1)).Type); stream.Consume(); } Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); int m = stream.Mark(); // MARK stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP stream.Consume(); // consume 104 stream.Rewind(m); // REWIND Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // now we're past rewind position Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); }
public void testListWithOneNode() { ITree root = new CommonTree((IToken)null); root.AddChild(new CommonTree(new CommonToken(101))); CommonTreeNodeStream stream = new CommonTreeNodeStream(root); string expected = " 101"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
public void testMarkRewindNested() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); int m = stream.Mark(); // MARK at start stream.Consume(); // consume 101 stream.Consume(); // consume DN int m2 = stream.Mark(); // MARK on 102 stream.Consume(); // consume 102 stream.Consume(); // consume DN stream.Consume(); // consume 103 stream.Consume(); // consume 106 stream.Rewind(m2); // REWIND to 102 Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // stop at 103 and rewind to start stream.Rewind(m); // REWIND to 101 Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); }
public virtual string ToTokenString() { ICharStream input = this.Token.InputStream; GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this); StringBuilder buf = new StringBuilder(); GrammarAST o = (GrammarAST)nodes.LT(1); int type = adaptor.GetType(o); while (type != TokenTypes.EndOfFile) { buf.Append(" "); buf.Append(o.Text); nodes.Consume(); o = (GrammarAST)nodes.LT(1); type = adaptor.GetType(o); } return(buf.ToString()); }
protected virtual void _CreateATN([NotNull] ICollection <Rule> rules) { CreateRuleStartAndStopATNStates(); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); foreach (Rule r in rules) { // find rule's block GrammarAST blk = (GrammarAST)r.ast.GetFirstChildWithType(ANTLRParser.BLOCK); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk); ATNBuilder b = new ATNBuilder(nodes, this); try { SetCurrentRuleName(r.name); Handle h = b.ruleBlock(null); Rule(r.ast, r.name, h); } catch (RecognitionException re) { g.tool.errMgr.FatalInternalError("bad grammar AST structure", re); } } }
public void testMarkRewindEntire() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); int m = stream.Mark(); // MARK for (int k = 1; k <= 13; k++) { // consume til end stream.LT(1); stream.Consume(); } Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); stream.Rewind(m); // REWIND // consume til end again :) for (int k = 1; k <= 13; k++) { // consume til end stream.LT(1); stream.Consume(); } Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); }
public void testPushPopFromEOF() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); while (stream.LA(1) != Token.EOF) { stream.Consume(); } int indexOf102 = 2; int indexOf104 = 6; Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); // CALL 102 stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); // CALL 104 stream.Push(indexOf104); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); }
public void testNestedPushPop() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); // Assume we want to hit node 107 and then "call 102", which // calls 104, then return int indexOf102 = 2; int indexOf107 = 12; for (int k = 1; k <= indexOf107; k++) { // consume til 107 node stream.Consume(); } Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); // CALL 102 stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 // CALL 104 int indexOf104 = 6; stream.Push(indexOf104); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to UP node in 102 subtree) stream.Pop(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); }
public void testPushPop() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3"; String found = stream.ToString(); Assert.AreEqual(expecting, found); // Assume we want to hit node 107 and then "call 102" then return int indexOf102 = 2; int indexOf107 = 12; for (int k = 1; k <= indexOf107; k++) { // consume til 107 node stream.Consume(); } // CALL 102 Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN stream.Pop(); Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); }
public void testSeekFromStart() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); stream.Seek(7); // seek to 107 Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); }
public void testMarkRewindNested() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); int m = stream.Mark(); // MARK at start stream.Consume(); // consume 101 stream.Consume(); // consume DN int m2 = stream.Mark(); // MARK on 102 stream.Consume(); // consume 102 stream.Consume(); // consume DN stream.Consume(); // consume 103 stream.Consume(); // consume 106 stream.Rewind(m2); // REWIND to 102 Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // stop at 103 and rewind to start stream.Rewind(m); // REWIND to 101 Assert.AreEqual(101, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); }
public void testMarkRewindInMiddle() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); for (int k = 1; k <= 7; k++) { // consume til middle //System.out.println(((ITree)stream.LT(1)).Type); stream.Consume(); } Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); int m = stream.Mark(); // MARK stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP stream.Consume(); // consume 104 stream.Rewind(m); // REWIND Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // now we're past rewind position Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); stream.Consume(); Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); }
public void testListWithOneNode() { ITree root = new CommonTree((IToken)null); root.AddChild(new CommonTree(new CommonToken(101))); CommonTreeNodeStream stream = new CommonTreeNodeStream(root); string expected = " 101"; string actual = GetStringOfEntireStreamContentsWithNodeTypesOnly(stream); Assert.AreEqual(expected, actual); expected = " 101"; actual = stream.ToString(); Assert.AreEqual(expected, actual); }
public void TranslateLeftRecursiveRule(GrammarAST ruleAST) { var input = new Antlr.Runtime.Tree.CommonTreeNodeStream(ruleAST); LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker = new LeftRecursiveRuleAnalyzer(input, this, ruleAST.enclosingRuleName); bool isLeftRec = false; try { //System.out.println("TESTING "+ruleAST.enclosingRuleName); isLeftRec = leftRecursiveRuleWalker.rec_rule(this); } catch (RecognitionException re) { ErrorManager.Error(ErrorManager.MSG_BAD_AST_STRUCTURE, re); } if (!isLeftRec) return; List<string> rules = new List<string>(); rules.Add(leftRecursiveRuleWalker.GetArtificialPrecStartRule()); rules.Add(leftRecursiveRuleWalker.GetArtificialOpPrecRule()); rules.Add(leftRecursiveRuleWalker.GetArtificialPrimaryRule()); foreach (string r in rules) { GrammarAST t = ParseArtificialRule(r); AddRule(grammarTree, t); //System.out.println(t.toStringTree()); } //precRuleInitCodeBlocks.add( precRuleWalker.getOpPrecJavaCode() ); }
public virtual void BuildNFA() { if ( nfa == null ) { CreateRuleStartAndStopNFAStates(); } if ( nfa.Complete ) { // don't let it create more than once; has side-effects return; } //[email protected]("### build "+getGrammarTypeString()+" grammar "+name+" NFAs"); if ( Rules.Count == 0 ) { return; } Antlr.Runtime.Tree.ITreeNodeStream input = new Antlr.Runtime.Tree.CommonTreeNodeStream( grammarTree ); TreeToNFAConverter nfaBuilder = new TreeToNFAConverter( this, nfa, factory, input ); try { nfaBuilder.grammar_(); } catch ( RecognitionException re ) { ErrorManager.Error( ErrorManager.MSG_BAD_AST_STRUCTURE, name, re ); } nfa.Complete = true; }
public virtual void BuildNormalRuleFunction(Rule r, RuleFunction function) { CodeGenerator gen = @delegate.GetGenerator(); // TRIGGER factory functions for rule alts, elements GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.Token.InputStream); GrammarAST blk = (GrammarAST)r.ast.GetFirstChildWithType(ANTLRParser.BLOCK); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk); walker = new SourceGenTriggers(nodes, this); try { // walk AST of rule alts/elements function.code = DefaultOutputModelFactory.List(walker.block(null, null)); function.hasLookaheadBlock = walker.hasLookaheadBlock; } catch (Antlr.Runtime.RecognitionException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); } function.ctxType = @delegate.GetTarget().GetRuleFunctionContextStructName(function); function.postamble = RulePostamble(function, r); }
public virtual string ToTokenString() { ICharStream input = this.Token.InputStream; GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this); StringBuilder buf = new StringBuilder(); GrammarAST o = (GrammarAST)nodes.LT(1); int type = adaptor.GetType(o); while (type != TokenTypes.EndOfFile) { buf.Append(" "); buf.Append(o.Text); nodes.Consume(); o = (GrammarAST)nodes.LT(1); type = adaptor.GetType(o); } return buf.ToString(); }
public void testStackStretch() { // make more than INITIAL_CALL_STACK_SIZE pushes ITree r0 = new CommonTree(new CommonToken(101)); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); // go 1 over initial size for (int i = 1; i <= CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE + 1; i++) { stream.Push(i); } Assert.AreEqual(10, stream.Pop()); Assert.AreEqual(9, stream.Pop()); }
public void testMarkRewindEntire() { // ^(101 ^(102 103 ^(106 107) ) 104 105) // stream has 7 real + 6 nav nodes // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r0.AddChild(r1); r1.AddChild(new CommonTree(new CommonToken(103))); ITree r2 = new CommonTree(new CommonToken(106)); r2.AddChild(new CommonTree(new CommonToken(107))); r1.AddChild(r2); r0.AddChild(new CommonTree(new CommonToken(104))); r0.AddChild(new CommonTree(new CommonToken(105))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); int m = stream.Mark(); // MARK for (int k = 1; k <= 13; k++) { // consume til end stream.LT(1); stream.Consume(); } Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); stream.Rewind(m); // REWIND // consume til end again :) for (int k = 1; k <= 13; k++) { // consume til end stream.LT(1); stream.Consume(); } Assert.AreEqual(Token.EOF, ((ITree)stream.LT(1)).Type); Assert.AreEqual(Token.UP, ((ITree)stream.LT(-1)).Type); }
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 testNestedPushPop() { // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109) // stream has 9 real + 8 nav nodes // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP ITree r0 = new CommonTree(new CommonToken(101)); ITree r1 = new CommonTree(new CommonToken(102)); r1.AddChild(new CommonTree(new CommonToken(103))); r0.AddChild(r1); ITree r2 = new CommonTree(new CommonToken(104)); r2.AddChild(new CommonTree(new CommonToken(105))); r0.AddChild(r2); ITree r3 = new CommonTree(new CommonToken(106)); r3.AddChild(new CommonTree(new CommonToken(107))); r0.AddChild(r3); r0.AddChild(new CommonTree(new CommonToken(108))); r0.AddChild(new CommonTree(new CommonToken(109))); CommonTreeNodeStream stream = new CommonTreeNodeStream(r0); // Assume we want to hit node 107 and then "call 102", which // calls 104, then return int indexOf102 = 2; int indexOf107 = 12; for (int k = 1; k <= indexOf107; k++) { // consume til 107 node stream.Consume(); } Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); // CALL 102 stream.Push(indexOf102); Assert.AreEqual(102, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(103, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 // CALL 104 int indexOf104 = 6; stream.Push(indexOf104); Assert.AreEqual(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 102 Assert.AreEqual(Token.DOWN, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume DN Assert.AreEqual(105, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 103 Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to UP node in 102 subtree) stream.Pop(); Assert.AreEqual(Token.UP, ((ITree)stream.LT(1)).Type); // RETURN (to empty stack) stream.Pop(); Assert.AreEqual(107, ((ITree)stream.LT(1)).Type); }