craigmjohnston / grunsharp

C# implementation of grun, the ANTLR grammar test rig. Not being actively worked on right now.
MIT License
6 stars 4 forks source link

Support for combined grammars #3

Open Eternal21 opened 5 years ago

Eternal21 commented 5 years ago

I'm not sure what am I doing wrong. I'm going through the examples in 'Definitive ANTLR 4 Reference' book, and generating my C# parser/lexer files using the following command:

antlr4 -Dlanguage=CSharp PropertyFile.g4

The grammar name is: PropertyFile, and the root rule is 'file'. Here's the grammar file "PropertyFile.g4":

grammar PropertyFile;
file : prop+ ;
prop : ID '=' STRING '\n' ;
ID   : [a-z]+ ;
STRING : '"' .*? '"' ;

Here's my test file t.properties:

user="parrt"
machine="maniac"

I end up with the following C# generated files: PropertyFileLexer.cs PropertyFileParser.cs

I wanted to use your tool to generate the gui tree directly from C# files (instead of having to compile java lexer/parser first, and then using: grun PropertyFile file -tree). The problem is, no matter what combination of input args I try, I end up with exceptions during startup (looks like grunsharp can't find parser file). I tried the following command arguments:

PropertyFile file t.properties --gui

based on readme it should be: GrunCS.exe <grammar name> <start rule> <input files>... [--gui] [--tokens] [--config=<path>]

What am I doing wrong?

craigmjohnston commented 5 years ago

Hey, sorry for the delay in replying! Thanks for trying this out, it's kind of rough at the moment and it's been a while since I worked on it, but it should work.

So you should be running the following from within the folder that holds your t.properties file: /path/to/GrunCS.exe PropertyFile file t.properties --gui

If you're doing that properly and it's still showing errors, then there must be a bug. I don't have the time today to test this out for you but I'll have a go over the weekend and see if I can reproduce your issue. In the meantime, if you try this again could you paste any exceptions you're getting, just so I have them to go through.

Eternal21 commented 5 years ago

Thanks. Here's what I'm doing:

I downloaded grunsharp_0.0.1_windows.zip and unzipped it to a folder. Then I copied the following files to it (attached to this post as well):

  1. PropertyFile.g4
  2. t.Properties
  3. PropertyFileLexer.cs
  4. PropertyFileParser.cs

I then ran the following command after cd'ing to that location in cmd:

C:\_TEST\antlr\grunsharp_0.0.1_windows>GrunCS PropertyFile file t.properties --gui
There was an error when trying to parse.
Can't find PropertyFileParserListener parser listener file

Unhandled Exception: System.IO.FileNotFoundException: Can't find PropertyFileParserListener parser listener file
   at GrunCS.Loader.FindFiles(String grammarName, String startRule, String directoryPath, String[] includes) in E:\Projects\GrunCS2\GrunCS\Loader.cs:line 70
   at GrunCS.Main.Process(String workingDirectory, String grammarName, String startRule, String testFile) in E:\Projects\GrunCS2\GrunCS\Main.cs:line 31
   at GrunCS.App.OnStartup(StartupEventArgs e) in E:\Projects\GrunCS2\GrunCS\App.xaml.cs:line 83
   at System.Windows.Application.<.ctor>b__1_0(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at GrunCS.App.Main()

So it looks like the program can't find the following file: PropertyFileParserListener. The problem is, no such file is ever being generated by anltr4 tool. The only other genereated cs files, apart from Lexer and Parser are (also attached for reference): PropertyFileBaseListener.cs PropertyFileListener.cs

PropertyFiles.zip

craigmjohnston commented 5 years ago

Aha! It doesn't look like grunsharp handles combined grammars, just split ones. I guess I didn't realise that the output files are different if you use a combined grammar. I'll fix that this weekend.

You should be able to get it working if you split your grammar into parser and lexer, like it mentions here: https://github.com/antlr/antlr4/blob/master/doc/grammars.md

Eternal21 commented 5 years ago

I tried splitting the file into two parts, but antlr still didn't generate the other required file (PropertyFileParserListener). Is there a special naming convention I should be using for the g4 files?

craigmjohnston commented 5 years ago

I've uploaded a fix so that your combined grammar should work, try out the latest release and let me know if you have any other issues.

I'd assumed that the .cs files generated had a certain naming format, but it turns out they're just named based on the name of your grammars. I've changed the executable's name to "grunsharp.exe" also, so you'll need to modify the command you were using (I'm going to update the readme now).

Hope that helps!

Eternal21 commented 5 years ago

Still can't get it to work. No exceptions this time, but nothing would happen. Downloaded source code, and ran from visual studio, setting Debug Command line arguments to the following: PropertyFile file t.properties --gui

I placed attached files inside /bin/Debug folder: PropertyFiles.zip

The code throws an exception inside Main.cs, line 41:

var lexer = this.GetLexer(assembly, inputStream);

with the following message: {"Value does not fall within the expected range."}

because the following line does not find a lexer type:

var lexerType = types.FirstOrDefault(t => typeof(Lexer).IsAssignableFrom(t));

Here's the types array: lexertypes

craigmjohnston commented 5 years ago

Hmm, okay. I'll look into that.