Quantcast

Parboiled and reflection

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parboiled and reflection

roberto
Hi,

I started using parboiled a few days ago, since I had to create a rather simple parser for my graduation project.
The parser is now done and works fine, but as soon as I completed it I got to know that it has to run inside a GWT user interface.

The main problem with this is that GWT, having to translate from java to javaScript, does not allow the use of reflection.
Since I would hate to rewrite the parser using another tool, I am trying everything possible to avoid that.

After a brief look at the source code, I found that parboiled-java is mainly responsible for the creation of the MyParser$$parboiled class, so I am asking if it is possible to have that class generated once and for all, so that instead of calling every time:

MyParser parser = Parboiled.createParser(MyParser.class);

I can call the actual class as:

MyParser parser = new MyParser$$parboiled();

Is something like that even possible? That would solve my problem and possibly help any other parser-for-GWT writer out there.

Thank you for your support.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parboiled and reflection

mathias
Administrator
Roberto,

it is possible, even though currently not directly supported.
(I just added an issue to track this: https://github.com/sirthias/parboiled/issues/25)

However, the patch required for a quick and dirty solution is not hard at all, so that you should be able to simply do it yourself.
Here is how:

You need to augment the method "loadClass" of the org.parboiled.transform.AsmUtils class. It loads a generated class using the class name, class file content and class loader. If you add a line saving the class file content to an actual file, you can then put that file on the class path and load it directly.
You can easily save the class code with something like this:

        import org.parboiled.common.FileUtils;
        FileUtils.writeAllBytes(code, "/some/path/" + className);

Of course you would have to rebuild parboiled from source after you have applied this patch.
See the "building parboiled" page for information on how to do this (https://github.com/sirthias/parboiled/wiki/Building-parboiled).

HTH and cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 04.07.2011, at 21:02, roberto [via parboiled users] wrote:

> Hi,
>
> I started using parboiled a few days ago, since I had to create a rather simple parser for my graduation project.
> The parser is now done and works fine, but as soon as I completed it I got to know that it has to run inside a GWT user interface.
>
> The main problem with this is that GWT, having to translate from java to javaScript, does not allow the use of reflection.
> Since I would hate to rewrite the parser using another tool, I am trying everything possible to avoid that.
>
> After a brief look at the source code, I found that parboiled-java is mainly responsible for the creation of the MyParser$$parboiled class, so I am asking if it is possible to have that class generated once and for all, so that instead of calling every time:
>
> MyParser parser = Parboiled.createParser(MyParser.class);
>
> I can call the actual class as:
>
> MyParser parser = new MyParser$$parboiled();
>
> Is something like that even possible? That would solve my problem and possibly help any other parser-for-GWT writer out there.
>
> Thank you for your support.
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Parboiled-and-reflection-tp3138548p3138548.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parboiled and reflection

roberto
Thank you,

I will let you know how it goes
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parboiled and reflection

roberto
Dear Mathias,

I have encountered a couple of problems attempting to do what you suggested.

First, GWT needs the source code of the class to translate it in javaScript, so I tried to decompile
the resulting .class file.
What I noticed is that it contains several methods completely commented out, resulting in compilation errors.
Some other method contains errors of different kinds, maybe due to the decompiler.
However I don't know how to fix these in any way.

The other thing is that not just one .class file is written, but actually one for the parser and one for each action.
Decompiling these action.class files, they result empty, i.e. they consist only in an empty constructor and an empty run(Context) method.
Oddly enough I can't seem to find the actual code of my actions anywhere, I guess it is invoked directly from the not expanded parser class.

Is there a way to obtain a compilable version of that source code I need?
Thank you again
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parboiled and reflection

mathias
Administrator
Roberto,

unfortunately, if GWT requires the java sources of all classes, you are at a loss.
parboiled generates the extended parser and action classes directly at the bytecode level, with quite some optimization, so no decompiler will be able to generate proper Java sources for these (which is probably why you see empty run methods).

I think the only solution in your case would be to keep the parsing part on the server and interface to this logic from the client via GWT-RPC...

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 05.07.2011, at 23:41, roberto [via parboiled users] wrote:

> Dear Mathias,
>
> I have encountered a couple of problems attempting to do what you suggested.
>
> First, GWT needs the source code of the class to translate it in javaScript, so I tried to decompile
> the resulting .class file.
> What I noticed is that it contains several methods completely commented out, resulting in compilation errors.
> Some other method contains errors of different kinds, maybe due to the decompiler.
> However I don't know how to fix these in any way.
>
> The other thing is that not just one .class file is written, but actually one for the parser and one for each action.
> Decompiling these action.class files, they result empty, i.e. they consist only in an empty constructor and an empty run(Context) method.
> Oddly enough I can't seem to find the actual code of my actions anywhere, I guess it is invoked directly from the not expanded parser class.
>
> Is there a way to obtain a compilable version of that source code I need?
> Thank you again
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Parboiled-and-reflection-tp3138548p3142639.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parboiled and GWT

Gary Miller
I was looking to help port a Java an open source application that uses Parboiled to being GWT compatible.
Want to get an idea about how difficult it would be to make Parboiled generate Java code instead of byte code?
eg.
  Is an AST build and the byte code generated by walking the AST?
  How dependance is the logic and the code generation?

I know that the parsing code could be left on the server-side and accessed via RPC.
This is just a scoping exercise.

Regards
Gary
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parboiled and GWT

mathias
Administrator
Gary,

> how difficult it would be to make Parboiled generate Java code instead of byte code?

Unfortunately this would be quite difficult and require a basic change in parboileds DNA.
parboiled is not a classical parser generator and doesn't want to be one.

I'm afraid I'll have to point you in the direction of ANTLR, JavaCC and the likes...

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 22.11.2011, at 23:23, Gary Miller [via parboiled users] wrote:

> I was looking to help port a Java an open source application that uses Parboiled to being GWT compatible.
> Want to get an idea about how difficult it would be to make Parboiled generate Java code instead of byte code?
> eg.
>   Is an AST build and the byte code generated by walking the AST?
>   How dependance is the logic and the code generation?
>
> I know that the parsing code could be left on the server-side and accessed via RPC.
> This is just a scoping exercise.
>
> Regards
> Gary
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Parboiled-and-reflection-tp3138548p3529193.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...