expected namespace when parsing c# code

Nov 7, 2013 at 10:38 AM
I'm trying to parse an existing c# code base, because I want to automatically instrument it with extra code.

The issue that I'm running into is that it gives a syntax error on code that compiles against .Net or Unity Mono.

This simple code gives syntax error expected: namespace

using System;

class TestClass
{
}

If I look at the example c# grammar I find this bit:

namespace_declarations_opt.Rule = MakeStarRule(namespace_declarations_opt, null, namespace_declaration);

It looks like the namespace is indeed optional, as it should be, so I'm wondering why I'm getting the syntax error message.

How do I modify the grammar to make this parse correctly?

Any help would be appreciated.

Grtz,

Floris
Nov 7, 2013 at 1:27 PM
I did some further checking and it looks like the problem lies somewhere in the grammar for using clauses.
Nov 7, 2013 at 2:36 PM
Changed the grammar to fix the problem. I initially misunderstood the above grammar rule to mean the namespace declaration itself was optional. It meant the entire thing including the namespace body was optional.

Changed

namespace_declarations_opt.Rule = MakeStarRule(namespace_declarations_opt, null, namespace_declaration_opt);

into

namespace_declaration_opt.Rule = namespace_declaration | no_namespace_declaration;
no_namespace_declaration.Rule = namespace_member_declarations;
namespace_declarations_opt.Rule = MakeStarRule(namespace_declarations_opt, null, namespace_declaration_opt);
Nov 8, 2013 at 2:24 PM
Edited Nov 8, 2013 at 2:37 PM
Never mind.