public void TestOverlappingReplace2() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(0, 3, "bar"); tokens.Replace(1, 2, "foo"); // cannot split earlier replace Exception exc = null; try { tokens.ToString(); } catch (ArgumentException iae) { exc = iae; } string expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<5>,1:1]..[@2,2:2='c',<6>,1:2]:\"foo\"> overlap with previous <ReplaceOp@[@0,0:0='a',<4>,1:0]..[@3,3:3='c',<6>,1:3]:\"bar\">"; Assert.IsNotNull(exc); Assert.AreEqual(expecting, exc.Message); }
public void TestReplaceThenReplaceLowerIndexedSuperset() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 4, "xyz"); tokens.Replace(1, 3, "foo"); // overlap, error Exception exc = null; try { tokens.ToString(); } catch (ArgumentException iae) { exc = iae; } string expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<5>,1:1]..[@3,3:3='c',<6>,1:3]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<6>,1:2]..[@4,4:4='c',<6>,1:4]:\"xyz\">"; Assert.IsNotNull(exc); Assert.AreEqual(expecting, exc.Message); }
public void TestInsertInPriorReplace() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(0, 2, "x"); tokens.InsertBefore(1, "0"); Exception exc = null; try { tokens.ToString(); } catch (ArgumentException iae) { exc = iae; } string expecting = "insert op <InsertBeforeOp@[@1,1:1='b',<5>,1:1]:\"0\"> within boundaries of previous <ReplaceOp@[@0,0:0='a',<4>,1:0]..[@2,2:2='c',<6>,1:2]:\"x\">"; Assert.IsNotNull(exc); Assert.AreEqual(expecting, exc.Message); }
public void TestReplaceRangeThenInsertAtRightEdge() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 4, "x"); tokens.InsertBefore(4, "y"); // no effect; within range of a replace Exception exc = null; try { tokens.ToString(); } catch (ArgumentException iae) { exc = iae; } string expecting = "insert op <InsertBeforeOp@[@4,4:4='c',<6>,1:4]:\"y\"> within boundaries of previous <ReplaceOp@[@2,2:2='c',<6>,1:2]..[@4,4:4='c',<6>,1:4]:\"x\">"; Assert.IsNotNull(exc); Assert.AreEqual(expecting, exc.Message); }
private static void KillTrailingNewline(TokenRewriteStream tokens, int index) { IList <IToken> all = tokens.GetTokens(); IToken tok = all[index]; IToken after = all[index + 1]; string ws = after.Text; if (ws.StartsWith("\n")) { //Console.Out.WriteLine( "killing WS after action" ); if (ws.Length > 1) { int space = ws.IndexOf(' '); int tab = ws.IndexOf('\t'); if (ws.StartsWith("\n") && space >= 0 || tab >= 0) { return; // do nothing if \n + indent } // otherwise kill all \n ws = ws.Replace("\n", ""); tokens.Replace(after.TokenIndex, ws); } else { tokens.Delete(after.TokenIndex); } } }
public void Test2ReplaceMiddleIndex() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(1, "x"); tokens.Replace(1, "y"); string result = tokens.ToString(); string expecting = "ayc"; Assert.AreEqual(expecting, result); }
public void TestReplaceSingleMiddleThenOverlappingSuperset() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 2, "xyz"); tokens.Replace(0, 3, "foo"); string result = tokens.ToString(); string expecting = "fooa"; Assert.AreEqual(expecting, result); }
public void TestOverlappingReplace4() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(1, 2, "foo"); tokens.Replace(1, 3, "bar"); // wipes prior nested replace string result = tokens.ToString(); string expecting = "abar"; Assert.AreEqual(expecting, result); }
public void TestDropIdenticalReplace() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(1, 2, "foo"); tokens.Replace(1, 2, "foo"); // drop previous, identical string result = tokens.ToString(); string expecting = "afooc"; Assert.AreEqual(expecting, result); }
public void TestToStringStartStop2() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n"); // Tokens: 012345678901234567 // Input: x = 3 * 0 + 2 * 0; ICharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0 + 2 * 0;"; Assert.AreEqual(expecting, result); tokens.Replace(4, 8, "0"); // replace 3 * 0 with 0 result = tokens.ToString(); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 17); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(4, 8); expecting = "0"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 8); expecting = "x = 0"; Assert.AreEqual(expecting, result); result = tokens.ToString(12, 16); expecting = "2 * 0"; Assert.AreEqual(expecting, result); tokens.InsertAfter(17, "// comment"); result = tokens.ToString(12, 18); expecting = "2 * 0;// comment"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 8); // try again after insert at end expecting = "x = 0"; Assert.AreEqual(expecting, result); }
public void TestReplaceAll() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(0, 6, "x"); string result = tokens.ToString(); string expecting = "x"; assertEquals(expecting, result); }
public void TestReplaceSubsetThenFetch() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 4, "xyz"); string result = tokens.ToString(0, 6); string expecting = "abxyzba"; Assert.AreEqual(expecting, result); }
public void TestLeaveAloneDisjointInsert2() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 3, "foo"); tokens.InsertBefore(1, "x"); string result = tokens.ToString(); string expecting = "axbfoo"; Assert.AreEqual(expecting, result); }
public void TestInsertThenReplaceSameIndex() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.InsertBefore(0, "0"); tokens.Replace(0, "x"); // supercedes insert at 0 string result = tokens.ToString(); string expecting = "0xbc"; Assert.AreEqual(expecting, result); }
public void TestCombineInsertOnLeftWithReplace() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(0, 2, "foo"); tokens.InsertBefore(0, "z"); // combine with left edge of rewrite string result = tokens.ToString(); string expecting = "zfoo"; Assert.AreEqual(expecting, result); }
public void TestReplaceRangeThenInsertAtLeftEdge() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abcccba"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(2, 4, "x"); tokens.InsertBefore(2, "y"); string result = tokens.ToString(); string expecting = "abyxba"; Assert.AreEqual(expecting, result); }
public void TestDropPrevCoveredInsert() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n"); ICharStream input = new ANTLRStringStream("abc"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.InsertBefore(1, "foo"); tokens.Replace(1, 2, "foo"); // kill prev insert string result = tokens.ToString(); string expecting = "afoofoo"; Assert.AreEqual(expecting, result); }
public void TestToStringStartStop() /*throws Exception*/ { Grammar g = new Grammar( "lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "MUL : '*';\n" + "ASSIGN : '=';\n" + "WS : ' '+;\n"); // Tokens: 0123456789 // Input: x = 3 * 0; ICharStream input = new ANTLRStringStream("x = 3 * 0;"); Interpreter lexEngine = new Interpreter(g, input); TokenRewriteStream tokens = new TokenRewriteStream(lexEngine); tokens.Fill(); tokens.Replace(4, 8, "0"); // replace 3 * 0 with 0 string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(); expecting = "x = 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(0, 9); expecting = "x = 0;"; Assert.AreEqual(expecting, result); result = tokens.ToString(4, 8); expecting = "0"; Assert.AreEqual(expecting, result); }
public void TestReplaceRangeThenInsertAtRightEdge() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "x" ); tokens.InsertBefore( 4, "y" ); // no effect; within range of a replace Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "insert op <InsertBeforeOp@[@4,4:4='c',<6>,1:4]:\"y\"> within boundaries of previous <ReplaceOp@[@2,2:2='c',<6>,1:2]..[@4,4:4='c',<6>,1:4]:\"x\">"; Assert.IsNotNull( exc ); Assert.AreEqual( expecting, exc.Message ); }
public void TestDropPrevCoveredInsert() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.InsertBefore( 1, "foo" ); tokens.Replace( 1, 2, "foo" ); // kill prev insert string result = tokens.ToString(); string expecting = "afoofoo"; Assert.AreEqual( expecting, result ); }
public void TestReplaceSubsetThenFetch() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "xyz" ); string result = tokens.ToString( 0, 6 ); string expecting = "abxyzba"; Assert.AreEqual( expecting, result ); }
public void TestReplaceThenReplaceSuperset() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "xyz" ); tokens.Replace( 3, 5, "foo" ); // overlaps, error Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "replace op boundaries of <[email protected]:\"foo\"> overlap with previous <[email protected]:\"xyz\">"; assertNotNull( exc ); assertEquals( expecting, exc.Message ); }
public void TestOverlappingReplace2() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 0, 3, "bar" ); tokens.Replace( 1, 2, "foo" ); // cannot split earlier replace Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "replace op boundaries of <[email protected]:\"foo\"> overlap with previous <[email protected]:\"bar\">"; assertNotNull( exc ); assertEquals( expecting, exc.Message ); }
public void Test2InsertThenReplaceIndex0() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.InsertBefore( 0, "x" ); tokens.InsertBefore( 0, "y" ); tokens.Replace( 0, "z" ); string result = tokens.ToString(); string expecting = "yxzbc"; Assert.AreEqual( expecting, result ); }
public void TestToStringStartStop() { Grammar g = new Grammar( "lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "MUL : '*';\n" + "ASSIGN : '=';\n" + "WS : ' '+;\n" ); // Tokens: 0123456789 // Input: x = 3 * 0; ICharStream input = new ANTLRStringStream( "x = 3 * 0;" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 4, 8, "0" ); // replace 3 * 0 with 0 string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString(); expecting = "x = 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 9 ); expecting = "x = 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 4, 8 ); expecting = "0"; Assert.AreEqual( expecting, result ); }
public void TestReplaceThenReplaceSuperset() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "xyz" ); tokens.Replace( 3, 5, "foo" ); // overlaps, error Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "replace op boundaries of <ReplaceOp@[@3,3:3='c',<6>,1:3]..[@5,5:5='b',<5>,1:5]:\"foo\"> overlap with previous <ReplaceOp@[@2,2:2='c',<6>,1:2]..[@4,4:4='c',<6>,1:4]:\"xyz\">"; Assert.IsNotNull( exc ); Assert.AreEqual( expecting, exc.Message ); }
public void TestReplaceThenDeleteMiddleIndex() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 1, "x" ); tokens.Delete( 1 ); string result = tokens.ToString(); string expecting = "ac"; Assert.AreEqual( expecting, result ); }
public void TestDropIdenticalReplace() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 1, 2, "foo" ); tokens.Replace( 1, 2, "foo" ); // drop previous, identical string result = tokens.ToString(); string expecting = "afooc"; Assert.AreEqual( expecting, result ); }
public void TestOverlappingReplace2() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 0, 3, "bar" ); tokens.Replace( 1, 2, "foo" ); // cannot split earlier replace Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "replace op boundaries of <ReplaceOp@[@1,1:1='b',<5>,1:1]..[@2,2:2='c',<6>,1:2]:\"foo\"> overlap with previous <ReplaceOp@[@0,0:0='a',<4>,1:0]..[@3,3:3='c',<6>,1:3]:\"bar\">"; Assert.IsNotNull( exc ); Assert.AreEqual( expecting, exc.Message ); }
public void TestCombineInsertOnLeftWithReplace() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 0, 2, "foo" ); tokens.InsertBefore( 0, "z" ); // combine with left edge of rewrite string result = tokens.ToString(); string expecting = "zfoo"; Assert.AreEqual( expecting, result ); }
public void TestLeaveAloneDisjointInsert2() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 3, "foo" ); tokens.InsertBefore( 1, "x" ); string result = tokens.ToString(); string expecting = "axbfoo"; Assert.AreEqual( expecting, result ); }
private static void KillTrailingNewline( TokenRewriteStream tokens, int index ) { IList<IToken> all = tokens.GetTokens(); IToken tok = all[index]; IToken after = all[index + 1]; string ws = after.Text; if ( ws.StartsWith( "\n" ) ) { //Console.Out.WriteLine( "killing WS after action" ); if ( ws.Length > 1 ) { int space = ws.IndexOf( ' ' ); int tab = ws.IndexOf( '\t' ); if ( ws.StartsWith( "\n" ) && space >= 0 || tab >= 0 ) { return; // do nothing if \n + indent } // otherwise kill all \n ws = ws.Replace( "\n", "" ); tokens.Replace( after.TokenIndex, ws ); } else { tokens.Delete( after.TokenIndex ); } } }
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); }); }
public void TestReplaceThenInsertBeforeLastIndex() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, "x" ); tokens.InsertBefore( 2, "y" ); string result = tokens.ToString(); string expecting = "abyx"; assertEquals( expecting, result ); }
public void TestReplaceRangeThenInsertAtLeftEdge() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcccba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 4, "x" ); tokens.InsertBefore( 2, "y" ); string result = tokens.ToString(); string expecting = "abyxba"; Assert.AreEqual( expecting, result ); }
public void TestInsertInPriorReplace() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 0, 2, "x" ); tokens.InsertBefore( 1, "0" ); Exception exc = null; try { tokens.ToString(); } catch ( ArgumentException iae ) { exc = iae; } string expecting = "insert op <InsertBeforeOp@[@1,1:1='b',<5>,1:1]:\"0\"> within boundaries of previous <ReplaceOp@[@0,0:0='a',<4>,1:0]..[@2,2:2='c',<6>,1:2]:\"x\">"; Assert.IsNotNull( exc ); Assert.AreEqual( expecting, exc.Message ); }
public void TestReplaceSingleMiddleThenOverlappingSuperset() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcba" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 2, 2, "xyz" ); tokens.Replace( 0, 3, "foo" ); string result = tokens.ToString(); string expecting = "fooa"; Assert.AreEqual( expecting, result ); }
public void TestToStringStartStop2() { Grammar g = new Grammar( "lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n" ); // Tokens: 012345678901234567 // Input: x = 3 * 0 + 2 * 0; ICharStream input = new ANTLRStringStream( "x = 3 * 0 + 2 * 0;" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); string result = tokens.ToOriginalString(); string expecting = "x = 3 * 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); tokens.Replace( 4, 8, "0" ); // replace 3 * 0 with 0 result = tokens.ToString(); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 17 ); expecting = "x = 0 + 2 * 0;"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 4, 8 ); expecting = "0"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 8 ); expecting = "x = 0"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 12, 16 ); expecting = "2 * 0"; Assert.AreEqual( expecting, result ); tokens.InsertAfter( 17, "// comment" ); result = tokens.ToString( 12, 18 ); expecting = "2 * 0;// comment"; Assert.AreEqual( expecting, result ); result = tokens.ToString( 0, 8 ); // try again after insert at end expecting = "x = 0"; Assert.AreEqual( expecting, result ); }
public void TestOverlappingReplace4() { Grammar g = new Grammar( "lexer grammar t;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n" ); ICharStream input = new ANTLRStringStream( "abcc" ); Interpreter lexEngine = new Interpreter( g, input ); TokenRewriteStream tokens = new TokenRewriteStream( lexEngine ); tokens.Fill(); tokens.Replace( 1, 2, "foo" ); tokens.Replace( 1, 3, "bar" ); // wipes prior nested replace string result = tokens.ToString(); string expecting = "abar"; Assert.AreEqual( expecting, result ); }