antlr / antlr4test-maven-plugin Goto Github PK
View Code? Open in Web Editor NEWA maven plugin used to test the grammars-v4 repo grammars
License: BSD 3-Clause "New" or "Revised" License
A maven plugin used to test the grammars-v4 repo grammars
License: BSD 3-Clause "New" or "Revised" License
grammars
or includes
- <grammarName>.g4
. If not found than <grammarName>Lexer.g4
and <grammarName>Parser.g4
.entryPoint
- the first rule in separated or combined grammar.exampleFiles
- examples
directory in root grammar dir (baseDir).We can test a parser output via *.tree
files but it's not possible yet to test a lexer output.
I have created a new scenario and configured a GrammarInitializer implemented inside my src/test/java root. A ClassNotFound exception was raised as at the end.
I investigated the problem and it is caused because of this snippet inside com.khubla.antlr.antlr4test.Scenario class:
public ClassLoader getClassLoader() throws MalformedURLException, ClassNotFoundException {
final URL antlrGeneratedURL = new File(baseDir + "/target/classes").toURI().toURL();
final URL[] urls = new URL[] { antlrGeneratedURL };
return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}
This is the code used to get a classloader to load the Lexer and Parser. It points explicitly to "/target/classes". But when the GrammarInitializer is put inside /src/test/java, the corresponding .class gets compiled to "/target/test-classes", so it cannot be found.
Until I fix this, a possible workaround is to put the GrammarInitializer implementation inside the main source project root: src/main/java
This workaround solved the ClassNotFoundException. Its drawback is that GrammarInitializer class would be distributed with the .jar of the project.
java.lang.ClassNotFoundException: io.trustep.grammars.mysql.SmokeInitializer
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammar(ScenarioExecutor.java:112)
at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammars(ScenarioExecutor.java:72)
at com.khubla.antlr.antlr4test.GrammarTestMojo.testScenarios(GrammarTestMojo.java:294)
at com.khubla.antlr.antlr4test.GrammarTestMojo.execute(GrammarTestMojo.java:169)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Error I'm getting:
baseDir: /tmp/testproject
exampleFiles: src/main/plsql/
Lexer classname is: PlSqlLexer
Parser classname is: PlSqlParser
java.lang.ClassNotFoundException: PlSqlLexer
I'm building an internal project with pom.xml like this and I'm wondering how to get a Lexer/Parser into the classpath of the test.
(We are building the standard PL/SQL grammar/lexer jar internally and uploading to an internal maven repo, thus the dependency below...)
<build>
<plugins>
<plugin>
<groupId>com.khubla.antlr</groupId>
<artifactId>antlr4test-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<verbose>true</verbose>
<showTree>false</showTree>
<entryPoint>sql_script</entryPoint>
<grammarName>PlSql</grammarName>
<caseInsensitiveType>UPPER</caseInsensitiveType>
<packageName></packageName>
<exampleFiles>src/main/plsql/</exampleFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.antlr.grammarsv4</groupId>
<artifactId>plsql</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
Thank you for your time,
Stephen
Some people still use this plugin for testing grammars-v4. Please update the pom.xml with a new version 1.23, and publish to Maven Central, like it was done previously. 9981c93. Thanks.
If you start a build which contains antlr4test-maven-plugin with multi thread support (parallel build) you get the following warning:
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ cql ---
[INFO] Building jar: ..../grammars-v4/cql/target/cql-1.0-SNAPSHOT.jar
[INFO]
[INFO] ----------------------< org.antlr.grammars:cql3 >-----------------------
[INFO] Building Apache Cassandra CQL 3 grammar 1.0-SNAPSHOT [34/256]
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] *****************************************************************
[WARNING] * Your build is requesting parallel execution, but project *
[WARNING] * contains the following plugin(s) that have goals not marked *
[WARNING] * as @threadSafe to support parallel building. *
[WARNING] * While this /may/ work fine, please look for plugin updates *
[WARNING] * and/or request plugins be made thread-safe. *
[WARNING] * If reporting an issue, report it against the plugin in *
[WARNING] * question, not against maven-core *
[WARNING] *****************************************************************
[WARNING] The following plugins are not marked @threadSafe in Apache Cassandra CQL 3 grammar:
[WARNING] com.khubla.antlr:antlr4test-maven-plugin:1.17
[WARNING] org.antlr:antlr4-maven-plugin:4.9.1
[WARNING] Enable debug to see more precisely which goals are not marked @threadSafe.
[WARNING] *****************************************************************
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cql3 ---
It would be nice to mark the antlr4-maven-plugin threadSafe = true
to suppress this warning.
It'd be nice if this plug-in was skipped when specifying the -DskipTests=true option.
When I try to test my grammar I get this error exception:
[ERROR] Failed to execute goal com.khubla.antlr:antlr4test-maven-plugin:1.11:test (VisualBasic6) on project matcher-core: Unable execute mojo: VB6_BACKDOOR_TIMEBOMB.vb.errors : expected (line 24:19 no viable alternative at input '
[ERROR]
[ERROR] If Timer() < 1234 Then
[ERROR]
[ERROR] End If
[ERROR]
[ERROR] If Year(date)=2017 Then
[ERROR] \tactivateBackdoor();'), but was (line 24:19 no viable alternative at input '\n\nIf Timer() < 1234 Then\n \nEnd If\n\nIf Year(date)=2017 Then\n\tactivateBackdoor();')
I don't understand how is it possible. '\n''s encoding like a newline but '\t''s not. And adding '\' before symbol '\n' in ".errors" file does't give any successful result (printed like '\').
I think something is wrong. Encoding file is 'utf-8', default value encoding in this plugin is 'utf-8' too.
I want to use not only lower case. but UPPER CASE for case insensitive lexers (such as SQL languages). So, I suggest to use the following options for caseInsensitive parameter:
File encoding has not been set
warning during build. For example:
[WARNING] Using platform encoding (UTF-8 actually) to read mojo source files, i.e. build is platform dependent!
It's necessary to include travis-ci to ANTLR organization and use https://travis-ci.org/antlr/antlr4test-maven-plugin
instead of old https://travis-ci.org/teverett/antlr4test-maven-plugin
.
Using the new scenario configuration, I have found a NPE only when using the verbose=true option. Digging into the code, I have found that baseDir does not get its default value if using the new Scenario configuration, as shown in log messages. Two possible workarounds: first, to setup manually the baseDir with . within the scenario; or second, to disable verbose option.
The problematic Scenario configuration:
<scenario>
<scenarioName>Basic</scenarioName>
<enabled>true</enabled>
<verbose>false</verbose>
<showTree>true</showTree>
<entryPoint>query</entryPoint>
<grammarName>MySQL</grammarName>
<packageName>io.trustep.grammars.mysql</packageName>
<caseInsensitiveType>None</caseInsensitiveType>
<exampleFiles>src/test/antlr4/io/trustep/grammars/mysql/basic</exampleFiles>
<fileEncoding>ISO-8859-1</fileEncoding>
<testFileExtension>sql</testFileExtension>
</scenario>
The logged error:
Evaluating Scenario: Basic
baseDir: null
exampleFiles: src/test/antlr4/io/trustep/grammars/mysql/basic
java.lang.NullPointerException
at com.khubla.antlr.antlr4test.GrammarTestMojo.testScenarios(GrammarTestMojo.java:284)
at com.khubla.antlr.antlr4test.GrammarTestMojo.execute(GrammarTestMojo.java:169)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Can you please add some releases?
Both Lexer and Parser have setInputStream
method so they can be reused. We could add an option to reuse them between tests of the same grammar to make sure it works properly.
When verbose is set to true, the plugin could generate in the Maven log output all the parsing errors. This feature is born by the necessity to check specific error conditions when parsing complex grammars.
It reduces the number of "symbol operator conflicts with generated code in target language or runtime" errors for non-java parsers. See antlr/antlr4#1851 as a sample.
I suggest checking generation for all runtimes (Java, C#, JavaScript, Python2, Python3, C++, Go, Swift) by default.
Looking through the sources, I suppose it's possible to specify the expected tree outputs and expected errors for some test files. It would be very useful to have a documentation with examples describing how to use that files.
Also, when some of the test files contains an error (does not match grammar syntax), the plugin tries to open .errors file and throws an error if it doesn't exist:
Caused by: java.io.FileNotFoundException: /home/scadge/IdeaProjects/querysyntax/query-syntax/examples/dml_select.txt.errors (No such file or directory)
Though it's probably a subject to another issue, I thought it would be useful to also mention it here.
It improves and simplifies diff for .tree files.
See also antlr/grammars-v4#748 and antlr/antlr4#1502.
It looks like ANTLRFileStream.LA(int) does not work on grammars-v4/php/examples/identifiers.php. See this. It should be replaced with CharStreams.fromFileName.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.