[antlr-interest] New partial methods in the C# 3 generated code

Sam Harwell sharwell at pixelminegames.com
Thu Apr 8 09:52:50 PDT 2010


I've made a few changes locally in the generated code for the CSharp3
target. Partial methods are new to C# 3, and were added so generated
code can add extensibility points. The partial methods and all calls to
them in generated code are *completely* removed by the compiler if there
is no implementation provided in another file.

 

1.       The partial methods "void EnterRule(string ruleName, int
ruleIndex)" and "void LeaveRule(string ruleName, int ruleIndex)" are
generated, and a call to each is placed at the beginning and end of each
rule.

 

2.       For each rule, the partial methods "void Enter[RuleName]()" and
"void Leave[RuleName]()" are generated. They are called *outside* of the
calls to EnterRule and LeaveRule (Enter[RuleName] before EnterRule,
Leave[RuleName] after LeaveRule).

 

3.       The -trace flag no longer affects the generated code. Calls to
BaseRecognizer.TraceIn and BaseRecognizer.TraceOut are always included
in the generated code, and the methods are now marked
[Conditional("ANTLR_TRACE")]. The calls are *inside* the calls to
EnterRule and LeaveRule.

 

4.       The partial method "void OnCreated()" is called at the end of
the constructor.

 

5.       The partial method "void CreateTreeAdaptor(ref ITreeAdaptor
adaptor)" is generated for tree parsers. Implement this method if you
want to use a tree adaptor other than CommonTreeAdaptor - no more need
to place conditional initialization inside your rules.

 

I'm also open to suggestions for other partial methods to add, which is
why I included this on the antlr-interest list.

 

Thanks,

Sam



More information about the antlr-interest mailing list