Hello!

 

I'm using the C# target of ANTLR and have some problems with it.

I'm rewriting the input to the output, but on my way I need to collect
some information about the input (to create StringTemplates on the fly).
I want the $id.text to return the rewritten text of the rule. And I
suppose this should happen, but the runtime crashes instead.

 

What am I doing wrong?

 

 

The grammar:

 

grammar Test;

 

options {

                language = CSharp2;

                output = template;

                rewrite = true;

}

 

start_rule

                :               id+                          {
System.Console.WriteLine($id.text); } // I need to access the text here

                ;

 

id            :               ID                            -> { new
StringTemplate("id") }

                ;

 

ID  :        ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*

    ;

 

WS  :   ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;}

    ;

 

The driver:

 

using System;

using Antlr.Runtime;

 

namespace RewriteTest

{

    public class Program

    {

        public static void Main(string[] args)

        {

            string input = "ab cd ef gh";

            

            var stream = new ANTLRStringStream(input);

            var lexer = new TestLexer(stream);

 

            var tokenStream = new TokenRewriteStream(lexer);

            var parser = new TestParser(tokenStream);

 

            parser.start_rule();

 

 
Console.WriteLine("----------------------------------------------");

 

            Console.WriteLine(tokenStream.ToOriginalString());

            Console.WriteLine(tokenStream.ToString());

        }

    }

}

 

The result:

 

Unhandled Exception: System.InvalidCastException: Unable to cast object
of type 'ReplaceOp' to type 'InsertBeforeOp'.

   at Antlr.Runtime.TokenRewriteStream.ToString(String programName,
Int32 start,  Int32 end)

   at Antlr.Runtime.TokenRewriteStream.ToString(Int32 start, Int32 end)

   at Antlr.Runtime.CommonTokenStream.ToString(IToken start, IToken
stop)

   at TestParser.start_rule() in C:\Tests\RewriteTest\TestParser.cs:line
147

   at RewriteTest.Program.Main(String[] args) in
C:\Tests\RewriteTest\Program.cs:line 18


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to