I am undergoing trouble writing the custom protoc plugin. I posted this question but no response. Atleast Kindly please let me know answers to few questions. I really need to do this. I havent gone past first step itself.
From this question, how do they link shell script with plugin name protoc-gen-code.
java -cp ./codegen.jar CodeGeneratorMain "$#"
With respect to the above implementation in the question and google proto buffer documentation, what exactly would be there in the path.. is it the path of the Shell Script? would the shell script be named as protoc-gen-code?
Can someone please respond to these queries.
protoc can generate code for several different languages with one invocation. The way you specify which languages you want is to use the command line arguments of the form --LANG_out where LANG is the language you want. So --cpp_out gives you C++ code, directory, --js_out gives you JavaScript etc. If protoc doesn't recognize LANG then it looks for a plugin called protoc-gen-LANG and uses it as the code generator.
The shell script can be called whatever. Let's say you call it mygen.sh and you decide you want to use mylang as LANG. Then the protoc invocation looks like:
protoc --plugin=protoc-gen-mylang=/path/to/mygen.sh --mylang_out=/some/dir some.proto
Related
So I spend the whole day trying to figure out how to work with ANTLR. I have a bunch of Java source code files. My goal is to use ANTLR in order to create an AST for each one of those files. In the end, the AST will be converted to JSON so it will be easier to parse each one of them with a dynamic language like python. The conversion will use the solution in this topic (although if someone have a better solution I will be glad to hear). I have managed to run the basic example in the getting-started page. Now I'm trying to figure out how to create an AST for one of my Java source code files. As it is said in the getting-started page, I can use the Java grammar for that. I have downloaded JavaParser.g4 and JavaLexer.g4. I tried to run:
java -Xmx500M -cp "/p/antlr-4.8-complete.jar:$CLASSPATH" org.antlr.v4.Tool JavaParser.g4
But I get the error:
JavaParser.g4:32:21: cannot find tokens file ./JavaLexer.tokens
I guess I need to create that file somehow but I could not understand how, from the docs. So I tried to find that file in Github and I found it here. I have downloaded it and it looked ok:
java -Xmx500M -cp "/p/antlr-4.8-complete.jar:$CLASSPATH" org.antlr.v4.Tool JavaParser.g4
javac JavaP*.java // Also tried javac Java*.java
java -Xmx500M -cp "/p/antlr-4.8-complete.jar:$CLASSPATH" org.antlr.v4.gui.TestRig JavaParser r -gui
But I get:
Exception in thread "main" java.lang.ClassCastException: class JavaParser
at java.lang.Class.asSubclass(Class.java:3404)
at org.antlr.v4.gui.TestRig.process(TestRig.java:135)
at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
Not sure what I do wrong or whether I'm doing the right thing in order to solve my problem.
All I want to do is to create an AST of the file /tmp/file.java and I'm not even sure where I should include it in the commands.
So my questions are:
How do I create an AST for the file /tmp/file.java?
Is it possible to create an AST file without actually writing Java code? I saw many places where they suggest to write Java code and compile it wtih Maven or some other tool. I prefer not to do it if possible, rather use the command-line option.
It's been a while since the JSON topic (mentioned above) was updated. Have something changed? Is it possible to create a JSON report without actually running Java?
As you can see I'm really confused so I appreciate any help!
EDIT: A small clarification - I don't need the AST image, rather to create a JSON report. If there is no easy way to do it, I'm sure that ANTLR has some raw file with that info that I could parse and convert it into JSON. Although, where is that raw data located?
I hope your classpath is correct. It needs the current directory in addition to the ANTLR tool jar. So something like .;C:\...\antlr.jar. As for using ANTLR on the Java grammar, you can do this:
$ antlr4 JavaLexer.g4 JavaParser.g4
$ javac Java*.java
$ grun Java compilationUnit -tree -gui
-gui will pop a window which may take some time. Remember to send an EOF to the ANTLR runtime after you type something to stdin. (CTLR+Z on Windows, CTLR+D on Linux) Alternatively, you can input a file as an extra argument to grun.
From the two .g4 files, I couldn't tell that the grammar name was Java. I just guessed it out based on the context. compilationUnit is the name of the first grammar rule all java files start with. This can be deduced by opening the JavaParser.g4 and reading the first rule.
Here are the aliases I used on Git Bash.
$ alias
alias antlr4='java -Xmx500M org.antlr.v4.Tool'
alias grun='java -Xmx500M org.antlr.v4.gui.TestRig'
I work on a Scala project that uses c++ code, using sbt. Once compiled, this c++ code is imported into Scala through Java code that uses jna.
Now, currently the Java wrapper are manually written, and I like to automatize this. I've found jnaerator that can do that, but I don't know how I should use it in sbt.
I see two general approaches:
use command line, such as java -jar jnaerator ... but I don't know how to setup such command line task in sbt? Also, I would need to know the typical project structure to follow: where to output the jna generated code?
Use jnaerator maven plugin through sbt, if it is possible?
This might take some iteration until we get it do what you need.
For the first approach, here is how you can run custom system command on sbt (you essentially solve this using Scala code). Add the following to your build.sbt file:
lazy val runJnaerator= taskKey[Unit]("This task generates libraries from native code")
runJnaerator := {
import sys.process._
Seq("java" , "-jar", "jnaerator", "..." ).!
}
To execute:
>sbt runJnaerator
Now the question is where do you need these files files to go? Finally, how do you want to invoke everything?
I'm struggling trying to get remote actors setup in Scala. I'm running Scala 2.10.2 and Akka 2.2.1.
I compile using [I've shortened the paths on the classpath arg for clarity sake]:
$ scalac -classpath "akka-2.2.1/lib:akka-2.2.1/lib/scala-library.jar:akka-2.2.1/lib/akka:akka-2.2.1/lib/akka/scala-reflect-2.10.1.jar:akka-2.2.1/lib/akka/config-1.0.2.jar:akka-2.2.1/lib/akka/akka-remote_2.10-2.2.1.jar:akka-2.2.1/lib/akka/akka-kernel_2.10-2.2.1.jar:akka-2.2.1/lib/akka/akka-actor_2.10-2.2.1.jar:." [file.scala]
I've continuously added new libraries trying to debug this - I'm pretty sure all I really need to include is akka-remote, but the others shouldn't hurt.
No issues compiling.
I attempt to run like this:
$ scala -classpath "[same as above]" [application]
And I receive a NSM exception:
java.lang.NoSuchMethodException: akka.remote.RemoteActorRefProvider.<init>(java.lang.String, akka.actor.ActorSystem$Settings, akka.event.EventStream, akka.actor.Scheduler, akka.actor.DynamicAccess)
at java.lang.Class.getConstructor0(Class.java:2810)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
...
Looking into the source code, it appears that Akka 2.2.X's flavor of this constructor takes 4 arguments (the Scheduler is removed). But in Akka < 2.2.X, the constructor takes 5 args.
Thus, I'm thinking my classpath isn't setup quite right. At compile-time, Scala must be finding the <2.2.X flavor. I don't even know where it would be finding it, since I only have Akka 2.2.1 installed.
Any suggestions!? Thanks! (Please don't say to use SBT).
The problem here is that the Scala distribution contains akka-actor 2.1.0 and helpfully puts that in the boot class path for you. We very strongly recommend using a dependency manager like sbt or maven when building anything which goes beyond the most trivial projects.
As noted in another answer, the problem is that scala puts a different version of Akka into the bootclasspath.
To more directly answer your question (as you said you don't want to use sbt): you can execute your program with java instead of scala. You just have to put the appropriate Scala jars into the classpath.
Here is a spark-dev message about the problem. The important part is: "the workaround is to use java to launch the application instead of scala. All you need to do is to include the right Scala jars (scala-library and scala-compiler) in the classpath."
I want to run an ANTLR jar from the command line to run a parser, as described at the bottom of http://www.antlr.org/wiki/pages/viewpage.action?pageId=729, where it says,
What if I want to test my parser without generating code? Easy. Just
run ANTLR in interpreter mode. It can't execute your actions, but it
can create a parse tree from your input to show you how it would be
matched. Use the org.antlr.tool.Interp main class. In the following, I
interpret simplec.g on t.c, which contains "int x;"
and follows that with
$ java org.antlr.tool.Interp simplec.g WS program t.c
Which jar do I download, and where can i get it? I tried antlr-3.4-complete.jar, but the output suggests that that may be only the parser generator.
Thanks.
Which jar do I download
The sentence following the command says this:
"In above command, we are considering ANTLR3 jar lies in your classpath."
where ANTLR3 jar presumably means the antlr-3.1.2.jar file mentioned earlier in the page.
... and where can i get it?
The ANTLR FAQ answers this one:
"Where do I find this damn thing"
I just started using ANTLR and found developing in it and java was quick and straightforward in Eclipse with the ANTLR IDE plug-in. This will give you an idea of what that process is like: Testing ANTLR Grammar.
I have a java program that I'm required to compile into a Linux native program using gcj-4.3. This program requires serial port access. The javax.comm api provides serial port access but I'm not sure how to get my compiled java program to use it.
The target box has Java installed, but of course my compiled program isn't running in the JRE...so I'm not exactly sure how I can link in the comm.jar file or how that file can find the .properties file it requires.
I wonder if I can just compile the comm.jar allong with my .jar file and link the two object files together. Can my code then reference the classes in comm.jar?
Thanks in advance for your help!
I'm not an GCJ expert but I have some suggestions (I'm not providing the syntax, this will require some exploration that I didn't perform):
first, I think that you'll have to compile comm.jar into a (shared) library,
then, you'll have to link your code against the library,
finally, use the GCJ_PROPERTIES environment variable to pass properties to the program at invocation time.
The following pointers might be helpful to implement this:
GCJ---The GNU Compiler for Java (great resources IMO, covers all the steps mentioned above)
GCJ – Getting Started (more an intro but still nice)
Compile ActiveMQ with GCJ (more use cases but I don't think they apply here)
And of course, the official documentation :)