public void TestSeekFromStart() /*throws Exception*/ { // ^(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))); ITreeNodeStream stream = newStream(r0); stream.Seek(7); // seek to 107 assertEquals(107, ((ITree)stream.LT(1)).Type); stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP assertEquals(104, ((ITree)stream.LT(1)).Type); }
/** <summary> * Match '.' in tree parser has special meaning. Skip node or * entire tree if node has children. If children, scan until * corresponding UP node. * </summary> */ public override void MatchAny(IIntStream ignore) { state.errorRecovery = false; state.failed = false; // always consume the current node input.Consume(); // if the next node is DOWN, then the current node is a subtree: // skip to corresponding UP. must count nesting level to get right UP int look = input.LA(1); if (look == DOWN) { input.Consume(); int level = 1; while (level > 0) { switch (input.LA(1)) { case DOWN: level++; break; case UP: level--; break; case TokenTypes.EndOfFile: return; default: break; } input.Consume(); } } }
public void TestMarkRewindEntire() /*throws Exception*/ { // ^(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))); ITreeNodeStream stream = newStream(r0); int m = stream.Mark(); // MARK for (int k = 1; k <= 13; k++) { // consume til end stream.LT(1); stream.Consume(); } assertEquals(TokenTypes.EndOfFile, ((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(); } assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(1)).Type); }
protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input, int k) { int mark = input.Mark(); try { for (int i = 0; i < k - 1; i++) { input.Consume(); } ExtractInformationFromTreeNodeStream(input); } finally { input.Rewind(mark); } }
public string ToNodesOnlyString(ITreeNodeStream nodes) { ITreeAdaptor adaptor = nodes.TreeAdaptor; StringBuilder buf = new StringBuilder(); object o = nodes.LT(1); int type = adaptor.GetType(o); while (o != null && type != TokenTypes.EndOfFile) { if (!(type == TokenTypes.Down || type == TokenTypes.Up)) { buf.Append(" "); buf.Append(type); } nodes.Consume(); o = nodes.LT(1); type = adaptor.GetType(o); } return(buf.ToString()); }
protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input, int k) { int mark = input.Mark(); try { for (int i = 0; i < k - 1; i++) input.Consume(); ExtractInformationFromTreeNodeStream(input); } finally { input.Rewind(mark); } }
public string ToNodesOnlyString( ITreeNodeStream nodes ) { ITreeAdaptor adaptor = nodes.TreeAdaptor; StringBuilder buf = new StringBuilder(); object o = nodes.LT( 1 ); int type = adaptor.GetType( o ); while ( o != null && type != TokenTypes.EndOfFile ) { if ( !( type == TokenTypes.Down || type == TokenTypes.Up ) ) { buf.Append( " " ); buf.Append( type ); } nodes.Consume(); o = nodes.LT( 1 ); type = adaptor.GetType( o ); } return buf.ToString(); }
public void TestMarkRewindNested() /*throws Exception*/ { // ^(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))); ITreeNodeStream stream = newStream(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 assertEquals(102, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type); stream.Consume(); // stop at 103 and rewind to start stream.Rewind(m); // REWIND to 101 assertEquals(101, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(102, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Down, ((ITree)stream.LT(1)).Type); }
public void TestMarkRewindInMiddle() /*throws Exception*/ { // ^(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))); ITreeNodeStream stream = newStream(r0); for (int k = 1; k <= 7; k++) { // consume til middle //System.out.println(((Tree)stream.LT(1)).getType()); stream.Consume(); } assertEquals(107, ((ITree)stream.LT(1)).Type); stream.Mark(); // MARK stream.Consume(); // consume 107 stream.Consume(); // consume UP stream.Consume(); // consume UP stream.Consume(); // consume 104 stream.Rewind(); // REWIND stream.Mark(); // keep saving nodes though assertEquals(107, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(104, ((ITree)stream.LT(1)).Type); stream.Consume(); // now we're past rewind position assertEquals(105, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.Up, ((ITree)stream.LT(1)).Type); stream.Consume(); assertEquals(TokenTypes.EndOfFile, ((ITree)stream.LT(1)).Type); assertEquals(TokenTypes.Up, ((ITree)stream.LT(-1)).Type); }