This project has moved and is read-only. For the latest updates, please go here.

expected namespace when parsing c# code

Nov 7, 2013 at 11: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.


Nov 7, 2013 at 2: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 3: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.


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


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 3:24 PM
Edited Nov 8, 2013 at 3:37 PM
Never mind.