I am writing a C# grammar in Java using Antlr 4.5. When I am dealing with a C# source code having Preprocessor Directives. sample code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hansa
{
class Program
{
public static void Main()
{
int b = 5;
#if true
int c = 0;
#if false
union {
internal struct {
uint pmclass;
ushort pmenum;
};
ushort bseg;
byte[] Sym;
internal struct {
uint index;
string name;
} btype;
} pbase;
#endif
printMe(c);
#endif
printMe(b);
Console.ReadLine();
}
public static void printMe(int val)
{
Console.WriteLine(val);
}
}
}
In the above code sample, codes between "#if false" and next "#endif" generates an error as "no viable alternative at input 'union {'".
I need to ignore this error in code level or ignore lines through grammar.
In the above code, if I do not have any errors, I can get namespace name, class name, method names, method signatures and lines of statements. When I got an error near by "union {", I am able to reach namespace name, class name, Main() method name but I am unable to reach to printMe() method declaration.
My requirement is, when an error occurred, grammar parsing should not be terminated. It should be continued from next line till EOF.
How can I achieve that ?