Question

So, I'm trying to parse some simple C# code to learn how to use Irony. I'm using the C# grammar included with the Irony samples and using the sample assembly loading code from there as well. There seems to be very little if any documentation on Irony. Here's the full source I'm trying to parse:

using System;

namespace N
{
    class Test
    {
       public int A(int x)
       {
           return x+1;
       }
    }
    class Test2
    {
        public string hello = "world";
        private int _T = 0;
        private Test _actor = new Test();
        public void B()
        {
            Console.WriteLine(hello+": "+_actor.A(_T++));
        }
    }
}

And the parsing code:

 string src = File.ReadAllText("testcs.cs");
 LanguageData language = new LanguageData(g);
 Parser parser = new Parser(language);
 ParseTree parseTree = parser.Parse(src);
 ParseTreeNode root = parseTree.Root;
 Console.WriteLine((root==null?"Parse tree unsuccessful!":"Parse tree created!"));

...where g is the C# grammar from Irony. Has anyone experienced these problems with parsing C# with Irony's grammar before? I'm sorry I can't provide any more information. There aren't any excep

Was it helpful?

Solution

When I tried your code, the parsing indeed was unsuccessful. And the error (in parseTree.ParserMessages) was on the end of the first line with the error message Invalid character: ''., where the character between the quotes is \r.

Hmm, it seems \r is not considered whitespace, so we need to modify the grammar. In the grammar, there is a method SkipWhitespace(), that contains a list of whitespace characters in a switch. If you just add case '\r': there, the parsing should succeed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top