congo-cc / congo-parser-generator

The CongoCC Parser Generator, the Next Generation of JavaCC 21, which in turn was the next generation of JavaCC
https://discuss.congocc.org/
Other
33 stars 9 forks source link

C# grammar doesn't handle compiler pragmas #150

Closed vsajip closed 3 months ago

vsajip commented 5 months ago

While not part of the C# language itself, it might be expected that a parser for C# would support handling compiler pragmas. This source:

#pragma warning disable 168
namespace Foo {
    public class Parser {
    }
}

results in the following AST, which is missing the pragma:

<CompilationUnit (2, 1)-(5, 2)> 'namespace Foo {\n    public cla...'
  <NamespaceDeclaration (2, 1)-(5, 1)> 'namespace Foo {\n    public cla...'
    <KeyWord (2, 1)-(2, 9)> 'namespace'
    <Identifier (2, 11)-(2, 13)> 'Foo'
    <NamespaceBody (2, 15)-(5, 1)> '{\n    public class Parser {\n  ...'
      <Delimiter (2, 15)-(2, 15)> '{'
      <ClassDeclaration (3, 5)-(4, 5)> 'public class Parser {\n    }'
        <KeyWord (3, 5)-(3, 10)> 'public'
        <KeyWord (3, 12)-(3, 16)> 'class'
        <Identifier (3, 18)-(3, 23)> 'Parser'
        <Delimiter (3, 25)-(3, 25)> '{'
        <Delimiter (4, 5)-(4, 5)> '}'
      <Delimiter (5, 1)-(5, 1)> '}'
  <CSToken (6, 1)-(5, 2)> ''

I'm not sure whether this due to something in the preprocessor (since pragmas have the same form as preprocessor directives).

revusky commented 5 months ago

Well, currently, as regards preprocessing, it just ignores everything that is not #if-#elif-#endif.

revusky commented 5 months ago

I guess, in principle, we shouldn't be throwing away any info that occurs in the source file, so it probably makes sense for the #pragma to be available in the AST as a comment, i.e. an UNPARSED token.

So, naturally, if you want to do something about this, by all means. It's not something I would classify as very urgent.

vsajip commented 5 months ago

not something I would classify as very urgent

Agreed. I was trying to turn off the many "unreachable code" warnings that you get as an inevitable result of how the code generation works.

vsajip commented 3 months ago

Confirmed fixed in #190.