1

I am having some tricky problems with Irony which I do not understand...

The first parsing I do in the runtime of my application succeeds.

string src = "" // this is the file to parse
Grammar g = new CSharpGrammar();
LanguageData language = new LanguageData(g);
Parser parser = new Parser(language);
ParseTree parseTree = parser.Parse(src);
ParseRoot = parseTree.Root;

Then I followed one hint to move the LanguageData variable to the global context. Still everything ok. But now I wanted to call the Irony parser inside of custom functions, parsing multiple files with the c# grammar v3.5 provided by Irony (LINQ queries are not important to me, so that seems sufficient). Same approach:

Parser parser = new Parser(language);
ParseTree parseTree = parser.Parse(file);
ParseRoot = parseTree.Root;

But now the parseroot is and remains "null". And I have absolutely no idea why. I also checked the parser errors a moment ago, there I recognized an error I cannot comprehend.

"Syntax error, expected: statement, member declaration, namespace"

But my file looks like this:

using System;
using Microsoft.SharePoint.WebControls;

namespace WebParts.Layouts.Ordering
{
    public partial class ConfirmDelete : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMsg.Text = "Are you sure you want to delete the entry?";

            if (!IsPostBack)
            {
                BtnYes.Attributes.Add("onclick", "OnYes(); return false;");
                BtnNo.Attributes.Add("onclick", "OnNo(); return false;");
            }
        }
    }
}

I hope you can see what's wrong with my code... I'm getting desperate with this thing...

Markus Köhler
  • 764
  • 2
  • 8
  • 20
  • 1
    This would be enlightening to know the purpose you want to achieve when parsing c#, could you please tell us? – Julien Ch. Jul 27 '12 at 08:45
  • "Then I followed one hint to move the LanguageData variable to the global context." More explanation on that please. – mathieu Jul 27 '12 at 08:57
  • the purpose is to write the code structure of a project (namespaces, classes, methods, properties, members) to a sql database for further debugging purposes. – Markus Köhler Sep 10 '12 at 09:58
  • with "moving to global context" i mean that i do not initialize the grammar and languagedata variables each time i use the parser... – Markus Köhler Sep 10 '12 at 10:00

1 Answers1

2

You have to provide a filename as the second argument of parser.Parse:

static void Main(string[] args)
{
    Grammar g = new CSharpGrammar();
    LanguageData language = new LanguageData(g);
    Parser parser = new Parser(language);
    ParseTree parseTree = parser.Parse("", "class1.cs");

    var r = parseTree.Root;
}
mathieu
  • 30,974
  • 4
  • 64
  • 90