示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        public void TestCombineInserts() /*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, "x");
            tokens.InsertBefore(0, "y");
            string result    = tokens.ToString();
            string expecting = "yxabc";

            Assert.AreEqual(expecting, result);
        }
 public void Test2InsertBeforeAfterMiddleIndex()
 {
     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, "x" );
     tokens.InsertAfter( 1, "x" );
     string result = tokens.ToString();
     string expecting = "axbxc";
     Assert.AreEqual( expecting, result );
 }
        public void TestInsertBeforeIndex0() /*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.InsertBefore(0, "0");
            string result    = tokens.ToString();
            string expecting = "0abc";

            assertEquals(expecting, result);
        }
示例#6
0
        public void TestInsertBeforeTokenThenDeleteThatToken()
        {
            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(2, "y");
            tokens.Delete(2);
            string result    = tokens.ToString();
            string expecting = "aby";

            Assert.AreEqual(expecting, result);
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
示例#10
0
        public void TestCombineInsertOnLeftWithDelete() /*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.Delete(0, 2);
            tokens.InsertBefore(0, "z"); // combine with left edge of rewrite
            string result    = tokens.ToString();
            string expecting = "z";      // make sure combo is not znull

            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";

            assertEquals(expecting, result);
        }
示例#12
0
        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 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 );
 }
示例#14
0
 public void TestCombineInsertOnLeftWithDelete()
 {
     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.Delete( 0, 2 );
     tokens.InsertBefore( 0, "z" ); // combine with left edge of rewrite
     string result = tokens.ToString();
     string expecting = "z"; // make sure combo is not znull
     assertEquals( expecting, result );
 }
示例#15
0
 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 = "zbc";
     assertEquals( expecting, result );
 }
 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 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 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 );
 }
 public void TestInsertThenReplaceSameIndex()
 {
     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 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 TestInsertBeforeTokenThenDeleteThatToken()
 {
     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(2, "y");
     tokens.Delete(2);
     string result = tokens.ToString();
     string expecting = "aby";
     Assert.AreEqual(expecting, result);
 }