diff --git a/antlr4-maven-plugin/pom.xml b/antlr4-maven-plugin/pom.xml index de47aa3f7..ef6f6dcea 100644 --- a/antlr4-maven-plugin/pom.xml +++ b/antlr4-maven-plugin/pom.xml @@ -88,7 +88,7 @@ org.codehaus.plexus plexus-compiler-api - 2.2 + 2.4 diff --git a/antlr4-testgen-maven-plugin/nb-configuration.xml b/antlr4-testgen-maven-plugin/nb-configuration.xml new file mode 100644 index 000000000..b13e72df8 --- /dev/null +++ b/antlr4-testgen-maven-plugin/nb-configuration.xml @@ -0,0 +1,42 @@ + + + + + + project + 4 + 4 + 4 + true + 80 + none + false + 4 + 4 + 4 + false + 80 + none + 4 + false + false + *;java + test + none + 4 + 4 + 4 + 80 + false + + diff --git a/antlr4-testgen-maven-plugin/pom.xml b/antlr4-testgen-maven-plugin/pom.xml new file mode 100644 index 000000000..a01bc32eb --- /dev/null +++ b/antlr4-testgen-maven-plugin/pom.xml @@ -0,0 +1,210 @@ + + + + + + + 4.0.0 + + + org.antlr + antlr4-master + 4.4-SNAPSHOT + + + antlr4-testgen-maven-plugin + maven-plugin + + ANTLR 4 Test Generator Maven plugin + Maven plugin for generating ANTLR 4 runtime tests + http://www.antlr.org + + + 3.0 + + + + 2009 + + + + + + + + + org.apache.maven + maven-plugin-api + 3.0.5 + compile + + + + org.apache.maven + maven-project + 2.2.1 + + + + org.codehaus.plexus + plexus-compiler-api + 2.2 + + + + org.sonatype.plexus + plexus-build-api + 0.0.7 + + + + org.antlr + ST4 + 4.0.8 + + + + + junit + junit + 4.11 + test + + + + org.apache.maven.shared + maven-plugin-testing-harness + 1.1 + test + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + provided + + + + + + + install + + + resources + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.3 + + + true + + + + mojo-descriptor + + descriptor + + + + help-goal + + helpmojo + + + + + + + org.apache.maven.plugins + maven-site-plugin + 3.3 + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.7 + + false + + + + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.3 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9 + + true + + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.3 + + + + diff --git a/antlr4-testgen-maven-plugin/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/antlr4-testgen-maven-plugin/resources/META-INF/m2e/lifecycle-mapping-metadata.xml new file mode 100644 index 000000000..de806eee9 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/META-INF/m2e/lifecycle-mapping-metadata.xml @@ -0,0 +1,18 @@ + + + + + + + antlr4 + + + + + true + true + + + + + diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/Index.stg new file mode 100644 index 000000000..c7e56f595 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/Index.stg @@ -0,0 +1,7 @@ +//TestFolders ::= [ +//] + +TestTemplates ::= [ + "LexerDelegatorInvokesDelegateRule": [], + "LexerDelegatorRuleOverridesDelegate": [] +] diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorInvokesDelegateRule.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorInvokesDelegateRule.stg new file mode 100644 index 000000000..b8c374c43 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorInvokesDelegateRule.stg @@ -0,0 +1,31 @@ +TestType() ::= "CompositeLexer" + +Grammars ::= [ + "M.g4": {}, + "S.g4": {} +] + +Input() ::= "abc" + +Output() ::= << +S.A +[@0,0:0='a',\<3>,1:0] +[@1,1:1='b',\<1>,1:1] +[@2,2:2='c',\<4>,1:2] +[@3,3:2='\',\<-1>,1:3]<\n> +>> + +Errors() ::= "" + +masterGrammar(grammarName, slaveGrammarName) ::= << +lexer grammar ; +import ; +B : 'b'; +WS : (' '|'\n') -> skip ; +>> + +slaveGrammar(grammarName) ::= << +lexer grammar ; +A : 'a' {}; +C : 'c' ; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorRuleOverridesDelegate.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorRuleOverridesDelegate.stg new file mode 100644 index 000000000..3d68b3124 --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/CompositeLexers/LexerDelegatorRuleOverridesDelegate.stg @@ -0,0 +1,31 @@ +TestType() ::= "CompositeLexer" + +Grammars ::= [ + "M.g4": {}, + "S.g4": {} +] + +Input() ::= "abc" + +Output() ::= << +S.A +[@0,0:0='a',\<3>,1:0] +[@1,1:1='b',\<1>,1:1] +[@2,2:2='c',\<4>,1:2] +[@3,3:2='\',\<-1>,1:3]<\n> +>> + +Errors() ::= "" + +masterGrammar(grammarName, slaveGrammarName) ::= << +lexer grammar ; +import ; +A : 'a' B {}; +WS : (' '|'\n') -> skip ; +>> + +slaveGrammar(grammarName) ::= << +lexer grammar ; +A : 'a' {}; +B : 'b' {}; +>> diff --git a/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg new file mode 100644 index 000000000..fc7033c0a --- /dev/null +++ b/antlr4-testgen-maven-plugin/resources/org/antlr4/runtime/test/templates/Index.stg @@ -0,0 +1,6 @@ +TestFolders ::= [ + "CompositeLexers": [] +] + +//TestTemplates ::= [ +//] diff --git a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java new file mode 100644 index 000000000..49d53c5b6 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/Antlr4TestGeneratorMojo.java @@ -0,0 +1,156 @@ +/* + [The "BSD license"] + Copyright (c) 2014 Sam Harwell + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package org.antlr.mojo.antlr4.testgen; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.STGroup; +import org.stringtemplate.v4.STGroupFile; +import org.stringtemplate.v4.gui.STViz; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +@Mojo( + name = "antlr4.testgen", + defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES, + requiresDependencyResolution = ResolutionScope.TEST, + requiresProject = true) +public class Antlr4TestGeneratorMojo extends AbstractMojo { + + // This project uses UTF-8, but the plugin might be used in another project + // which is not. Always load templates with UTF-8, but write using the + // specified encoding. + @Parameter(property = "project.build.sourceEncoding") + private String encoding; + + @Parameter(property = "project", required = true, readonly = true) + private MavenProject project; + + @Parameter(required = true) + private File runtimeTemplates; + + @Parameter(defaultValue = "${project.build.directory}/generated-test-sources/antlr4-tests") + private File outputDirectory; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + STGroup targetGroup = new STGroupFile(runtimeTemplates.getPath()); + targetGroup.registerModelAdaptor(STGroup.class, new STGroupModelAdaptor()); + targetGroup.defineDictionary("escape", new JavaEscapeStringMap()); + + String rootFolder = "org/antlr4/runtime/test/templates"; + STGroup index = new STGroupFile(rootFolder + "/Index.stg"); + generateCodeForFolder(targetGroup, rootFolder, index); + + project.addTestCompileSourceRoot(outputDirectory.getPath()); + } + + private void generateCodeForFolder(STGroup targetGroup, String folder, STGroup index) { + // make sure the index group is loaded since we call rawGetDictionary + index.load(); + + Map folders = index.rawGetDictionary("TestFolders"); + if (folders != null) { + for (String key : folders.keySet()) { + String subfolder = folder + "/" + key; + STGroup subindex = new STGroupFile(subfolder + "/Index.stg"); + generateCodeForFolder(targetGroup, folder + "/" + key, subindex); + } + } + + Map templates = index.rawGetDictionary("TestTemplates"); + if (templates != null && !templates.isEmpty()) { + generateTestFile(targetGroup, folder.substring(folder.lastIndexOf('/') + 1), folder, new ArrayList(templates.keySet())); + } + } + + private void generateTestFile(STGroup targetGroup, String testFile, String templateFolder, Collection testTemplates) { + List templates = new ArrayList(); + for (String template : testTemplates) { + STGroup testGroup = new STGroupFile(templateFolder + "/" + template + STGroup.GROUP_FILE_EXTENSION); + testGroup.importTemplates(targetGroup); + ST testType = testGroup.getInstanceOf("TestType"); + if (testType == null) { + getLog().warn(String.format("Unable to generate tests for %s: no TestType specified.", template)); + continue; + } + + ST testMethodTemplate = targetGroup.getInstanceOf(testType.render() + "TestMethod"); + if (testMethodTemplate == null) { + getLog().warn(String.format("Unable to generate tests for %s: TestType '%s' is not supported by the current runtime.", template, testType.render())); + continue; + } + + testMethodTemplate.add(testMethodTemplate.impl.formalArguments.keySet().iterator().next(), testGroup); + templates.add(testMethodTemplate); + } + + ST testFileTemplate = targetGroup.getInstanceOf("TestFile"); + testFileTemplate.addAggr("file.{importErrorQueue,importGrammar,name,tests}", false, false, testFile, templates); + STViz viz = testFileTemplate.inspect(); + try { + viz.waitForClose(); + } catch (InterruptedException ex) { + } + + File targetFolder = new File(outputDirectory, templateFolder.substring(0, templateFolder.indexOf("/templates"))); + File targetFile = new File(targetFolder, "Test" + testFile + ".java"); + try { + writeFile(targetFile, testFileTemplate.render()); + } catch (IOException ex) { + getLog().error(String.format("Failed to write output file: %s", targetFile), ex); + } + } + + public void writeFile(File file, String content) throws IOException { + file.getParentFile().mkdirs(); + + FileOutputStream fos = new FileOutputStream(file); + OutputStreamWriter osw = new OutputStreamWriter(fos, encoding != null ? encoding : "UTF-8"); + try { + osw.write(content); + } + finally { + osw.close(); + } + } +} diff --git a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/JavaEscapeStringMap.java b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/JavaEscapeStringMap.java new file mode 100644 index 000000000..cbdff41e0 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/JavaEscapeStringMap.java @@ -0,0 +1,63 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.antlr.mojo.antlr4.testgen; + +import java.util.AbstractMap; +import java.util.Collections; +import java.util.Set; + +/** + * + * @author Sam Harwell + */ +public class JavaEscapeStringMap extends AbstractMap { + + @Override + public Object get(Object key) { + if (key instanceof String) { + String str = (String)key; + StringBuilder builder = new StringBuilder(str.length()); + for (int i = 0; i < str.length(); i++) { + switch (str.charAt(i)) { + case '\\': + builder.append("\\\\"); + break; + + case '\r': + // normalize \r\n to just \n + break; + + case '\n': + builder.append("\\n"); + break; + + case '"': + builder.append("\\\""); + break; + + default: + builder.append(str.charAt(i)); + break; + } + } + + return builder.toString(); + } + + return super.get(key); + } + + @Override + public boolean containsKey(Object key) { + return key instanceof String; + } + + @Override + public Set> entrySet() { + return Collections.emptySet(); + } + +} diff --git a/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java new file mode 100644 index 000000000..ca8424f07 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/main/java/org/antlr/mojo/antlr4/testgen/STGroupModelAdaptor.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.antlr.mojo.antlr4.testgen; + +import org.stringtemplate.v4.Interpreter; +import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.STGroup; +import org.stringtemplate.v4.misc.ObjectModelAdaptor; +import org.stringtemplate.v4.misc.STNoSuchPropertyException; + +/** + * + * @author sam + */ +public class STGroupModelAdaptor extends ObjectModelAdaptor { + + @Override + public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException { + STGroup group = (STGroup)o; + if (group.isDictionary(propertyName)) { + return group.rawGetDictionary(propertyName); + } + + ST template = group.getInstanceOf(propertyName); + if (template != null) { + return template; + } + + return super.getProperty(interp, self, o, property, propertyName); + } + +} diff --git a/antlr4-testgen-maven-plugin/src/site/apt/examples/import.apt b/antlr4-testgen-maven-plugin/src/site/apt/examples/import.apt new file mode 100644 index 000000000..4bdfd46ab --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/examples/import.apt @@ -0,0 +1,8 @@ +Imported Grammar Files + + In order to have the ANTLR plugin automatically locate and use grammars used + as imports in your main <<<.g4>>> files, you need to place the imported grammar + files in the <<>> directory beneath the root directory of your grammar + files (which is <<>> by default of course). + + For a default layout, place your import grammars in the directory: <<>> diff --git a/antlr4-testgen-maven-plugin/src/site/apt/examples/libraries.apt.vm b/antlr4-testgen-maven-plugin/src/site/apt/examples/libraries.apt.vm new file mode 100644 index 000000000..9a4e94d34 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/examples/libraries.apt.vm @@ -0,0 +1,46 @@ +Libraries + + The introduction of the import directive in a grammar allows reuse of common grammar files + as well as the ability to divide up functional components of large grammars. However it has + caused some confusion in regard to the fact that generated vocabulary files (<<<*.tokens>>>) can also + be searched for with the <<<>>> directive. + + This has confused two separate functions and imposes a structure upon the layout of + your grammar files in certain cases. If you have grammars that both use the import + directive and also require the use of a vocabulary file then you will need to locate + the grammar that generates the <<<.tokens>>> file alongside the grammar that uses it. This + is because you will need to use the <<<>>> directive to specify the + location of your imported grammars and ANTLR will not find any vocabulary files in + this directory. + + The <<<.tokens>>> files for any grammars are generated within the same output directory structure + as the <<<.java>>> files. So, wherever the <<<.java>>> files are generated, you will also find the <<<.tokens>>> + files. ANTLR looks for <<<.tokens>>> files in both the <<<>>> and the output directory + where it is placing the generated <<<.java>>> files. Hence when you locate the grammars that generate + <<<.tokens>>> files in the same source directory as the ones that use the <<<.tokens>>> files, then + the Maven plugin will find the expected <<<.tokens>>> files. + + The <<<>>> is specified like any other directory parameter in Maven. Here is an + example: + ++-- + + org.antlr + antlr4-maven-plugin + ${project.version} + + + + + + antlr4 + + src/main/antlr4_imports + + + + ++-- + + + diff --git a/antlr4-testgen-maven-plugin/src/site/apt/examples/simple.apt.vm b/antlr4-testgen-maven-plugin/src/site/apt/examples/simple.apt.vm new file mode 100644 index 000000000..a6f6a0427 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/examples/simple.apt.vm @@ -0,0 +1,40 @@ +Simple configuration + + If your grammar files are organized into the default locations as described in the {{{../index.html}introduction}}, + then configuring the <<>> file for your project is as simple as adding this to it + ++-- + + + org.antlr + antlr4-maven-plugin + ${project.version} + + + + antlr4 + + + + +... + ++-- + + When the <<>> command is executed all grammar files under <<>>, except any + import grammars under <<>> will be analyzed and converted to + Java source code in the output directory <<>>. + + Your input files under <<>> should be stored in sub directories that + reflect the package structure of your java parsers. If your grammar file <<>> contains: + ++--- +@header { +package org.jimi.themuss; +} ++--- + + Then the <<<.g4>>> file should be stored in: <<>>. This way + the generated <<<.java>>> files will correctly reflect the package structure in which they will + finally rest as classes. + diff --git a/antlr4-testgen-maven-plugin/src/site/apt/faq.apt.vm b/antlr4-testgen-maven-plugin/src/site/apt/faq.apt.vm new file mode 100644 index 000000000..d6360a1c1 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/faq.apt.vm @@ -0,0 +1 @@ +FAQ diff --git a/antlr4-testgen-maven-plugin/src/site/apt/index.apt b/antlr4-testgen-maven-plugin/src/site/apt/index.apt new file mode 100644 index 000000000..cfbfaf293 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/index.apt @@ -0,0 +1,62 @@ + ------------- + ANTLR v4 Maven Plugin + ------------- + Jim Idle + ------------- + March 2009 + ------------- + +ANTLR v4 Maven plugin + + The ANTLR v4 Maven plugin is completely re-written as of version 4.0; if you are familiar + with prior versions, you should note that there are some behavioral differences that make + it worthwhile reading this documentation. + + The job of the plugin is essentially to tell the standard ANTLR parser generator where the + input grammar files are and where the output files should be generated. As with all Maven + plugins, there are defaults, which you are advised to comply to, but are not forced to + comply to. + + This version of the plugin allows full control over ANTLR and allows configuration of all + options that are useful for a build system. The code required to calculate dependencies, + check the build order, and otherwise work with your grammar files is built into the ANTLR + tool as of version 4.0 of ANTLR and this plugin. + +* Plugin Versioning + + The plugin version tracks the version of the ANTLR tool that it controls. Hence if you + use version 4.0 of the plugin, you will build your grammars using version 4.0 of the + ANTLR tool, version 4.2 of the plugin will use version 4.2 of the ANTLR tool and so on. + + You may also find that there are patch versions of the plugin such as 4.0-1 4.0-2 and + so on. Use the latest patch release of the plugin. + + The current version of the plugin is shown at the top of this page after the <> date. + + +* Default directories + + As with all Maven plugins, this plugin will automatically default to standard locations + for your grammar and import files. Organizing your source code to reflect this standard + layout will greatly reduce the configuration effort required. The standard layout lookd + like this: + ++-- + src/main/ + | + +--- antlr4/... .g4 files organized in the required package structure + | + +--- imports/ .g4 files that are imported by other grammars. ++-- + + If your grammar is intended to be part of a package called <<>> then you would + place it in the directory <<>>. The plugin will then produce + <<<.java>>> and <<<.tokens>>> files in the output directory <<>> + When the Java files are compiled they will be in the correct location for the Javac + compiler without any special configuration. The generated java files are automatically + submitted for compilation by the plugin. + + The <<>> directory is treated in a special way. It should contain + any grammar files that are imported by other grammar files (do not make subdirectories here.) + Such files are never built on their own, but the plugin will automatically tell the ANTLR + tool to look in this directory for library files. diff --git a/antlr4-testgen-maven-plugin/src/site/apt/usage.apt.vm b/antlr4-testgen-maven-plugin/src/site/apt/usage.apt.vm new file mode 100644 index 000000000..5d3a62875 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/apt/usage.apt.vm @@ -0,0 +1,59 @@ +Usage + + The ANTLR 4 plugin for Maven can generate parsers for any number of grammars in + your project. + +* Compiling Grammars into Parsers + + By default, the <<<{{{./antlr4-mojo.html}antlr4}}>>> goal will search for grammar + files in the directory <<<$\{basedir\}/src/main/antlr4>>> and any additional + <<<.tokens>>> files in the directory <<<$\{basedir\}/src/main/antlr4/imports>>>. + This can be configured to search other directories using the plugin configuration + parameters as described in the <<<{{{./antlr4-mojo.html}antlr4}}>>> goal + documentation. + + The following figure shows the expected layout of files for the default + configuration of this plugin. + ++-- + src/main/ + | + +--- antlr4/... .g4 files organized in the required package structure + | + +--- imports/ user-created .tokens files and .g4 files that are imported by other grammars ++-- + + The next step is to configure your POM to call the plugin. The goals will + normally run during the generate-sources phase of the build. Examples of how to + configure your POM can be found on the various examples pages, reachable via + the page menu. If you stick with the default values, the snippet below will + suffice: + ++-- + + ... + + + + org.antlr + antlr4-maven-plugin + ${project.version} + + + antlr + + antlr4 + + + + + + ... + + ... + ++-- + + Note that you can create multiple executions, and thus build some grammars with + different options to others (such as setting the <<>> option for + instance). diff --git a/antlr4-testgen-maven-plugin/src/site/site.xml b/antlr4-testgen-maven-plugin/src/site/site.xml new file mode 100644 index 000000000..0cf1aa278 --- /dev/null +++ b/antlr4-testgen-maven-plugin/src/site/site.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bild.py b/bild.py deleted file mode 100755 index 65e94f698..000000000 --- a/bild.py +++ /dev/null @@ -1,292 +0,0 @@ -# !/usr/bin/env python -import os -import string - -""" -This script uses my experimental build tool http://www.bildtool.org - -In order to build the complete ANTLR4 product with Java, CSharp, Python 2/3, and JavaScript -targets, do the following from a UNIX command line. Windows build using this script -is not yet supported. Please use the mvn build or ant build. - -You will also need python 2.7, python 3.4, node.js and mono (on Mac/Linux) - -!!!You must set path values in test_properties dictionary below to ensure non Java targets tests run.!!! - -mkdir -p /usr/local/antlr # somewhere appropriate where you want to install stuff -cd /usr/local/antlr -git clone git@github.com:antlr/antlr4.git -git clone git@github.com:antlr/antlr4-python3.git -git clone git@github.com:antlr/antlr4-python2.git -git clone git@github.com:antlr/antlr4-csharp.git -git clone git@github.com:antlr/antlr4-javascript.git -cd antlr4 -./bild.py tests -""" - -# bootstrap by downloading bilder.py if not found -import urllib -import os - -if not os.path.exists("bilder.py"): - print "bootstrapping; downloading bilder.py" - urllib.urlretrieve( - "https://raw.githubusercontent.com/parrt/bild/master/src/python/bilder.py", - "bilder.py") - -# assumes bilder.py is in current directory -from bilder import * - -VERSION = "4.4" -JAVA_TARGET = "." -PYTHON2_TARGET = "../antlr4-python2" -PYTHON3_TARGET = "../antlr4-python3" -CSHARP_TARGET = "../antlr4-csharp" -JAVASCRIPT_TARGET = "../antlr4-javascript" - -# Properties needed to run Python[23] tests -test_properties = { -"antlr-python2-runtime": uniformpath(PYTHON2_TARGET) + "/src", -"antlr-python3-runtime": uniformpath(PYTHON3_TARGET) + "/src", -"antlr-javascript-runtime": uniformpath(JAVASCRIPT_TARGET) + "/src", -"antlr-csharp-runtime-project": uniformpath(CSHARP_TARGET) + "/runtime/CSharp/Antlr4.Runtime/Antlr4.Runtime.mono.csproj" -} - -TARGETS = { - "Java": uniformpath(JAVA_TARGET), - "CSharp":uniformpath(CSHARP_TARGET), - "Python2": uniformpath(PYTHON2_TARGET), - "Python3": uniformpath(PYTHON3_TARGET), - "JavaScript":uniformpath(JAVASCRIPT_TARGET) -} - - -def parsers(): - antlr3("tool/src/org/antlr/v4/parse", "gen3", package="org.antlr.v4.parse") - antlr3("tool/src/org/antlr/v4/codegen", "gen3", package="org.antlr.v4.codegen", - args=["-lib", uniformpath("gen3/org/antlr/v4/parse")]) - antlr4("runtime/Java/src/org/antlr/v4/runtime/tree/xpath", "gen4", package="org.antlr.v4.runtime.tree.xpath") - -def compile(): - require(parsers) - cp = uniformpath("out") + os.pathsep + \ - os.path.join(JARCACHE, "antlr-3.5.1-complete.jar") + os.pathsep + \ - "runtime/Java/lib/org.abego.treelayout.core.jar" + os.pathsep - if os.path.exists(JARCACHE + "/antlr-" + VERSION + "-complete.jar"): - cp += JARCACHE + "/antlr-" + VERSION + "-complete.jar" - srcpath = ["gen3", "gen4", "runtime/JavaAnnotations/src", "runtime/Java/src", "tool/src"] - args = ["-Xlint", "-Xlint:-serial", "-g", "-sourcepath", string.join(srcpath, os.pathsep)] - for sp in srcpath: - javac(sp, "out", version="1.6", cp=cp, args=args) - # pull in targets - for t in TARGETS: - javac(TARGETS[t] + "/tool/src", "out", version="1.6", cp=cp, args=args) - - -def mkjar_complete(): - require(compile) - copytree(src="tool/resources", trg="out") # messages, Java code gen, etc... - manifest = \ - "Main-Class: org.antlr.v4.Tool\n" +\ - "Implementation-Title: ANTLR 4 Tool\n" +\ - "Implementation-Version: %s\n" +\ - "Implementation-Vendor: ANTLR\n" +\ - "Implementation-Vendor-Id: org.antlr\n" +\ - "Built-By: %s\n" +\ - "Build-Jdk: 1.6\n" +\ - "Created-By: http://www.bildtool.org\n" +\ - "\n" - manifest = manifest % (VERSION, os.getlogin()) - # unjar required libraries - unjar("runtime/Java/lib/org.abego.treelayout.core.jar", trgdir="out") - download("http://www.antlr3.org/download/antlr-3.5.1-runtime.jar", JARCACHE) - unjar(os.path.join(JARCACHE, "antlr-3.5.1-runtime.jar"), trgdir="out") - download("http://www.stringtemplate.org/download/ST-4.0.8.jar", JARCACHE) - unjar(os.path.join(JARCACHE, "ST-4.0.8.jar"), trgdir="out") - # pull in target templates - for t in TARGETS: - trgdir = "out/org/antlr/v4/tool/templates/codegen/" + t - mkdir(trgdir) - copyfile(TARGETS[t] + "/tool/resources/org/antlr/v4/tool/templates/codegen/" + t + "/" + t + ".stg", - trgdir) - jarfile = "dist/antlr4-" + VERSION + "-complete.jar" - jar(jarfile, srcdir="out", manifest=manifest) - print "Generated " + jarfile - - -def mkjar_runtime(): - # out/... dir is full of tool-related stuff, make special dir out/runtime - # unjar required library - unjar("runtime/Java/lib/org.abego.treelayout.core.jar", trgdir="out/runtime") - cp = uniformpath("out/runtime") + os.pathsep + \ - "runtime/Java/lib/org.abego.treelayout.core.jar" - srcpath = ["gen4", "runtime/JavaAnnotations/src", "runtime/Java/src"] - args = ["-nowarn", "-Xlint", "-Xlint:-serial", "-g", "-sourcepath", string.join(srcpath, os.pathsep)] - for sp in srcpath: - javac(sp, "out/runtime", version="1.6", cp=cp, args=args) - manifest = \ - "Implementation-Vendor: ANTLR\n" +\ - "Implementation-Vendor-Id: org.antlr\n" +\ - "Implementation-Title: ANTLR 4 Runtime\n" +\ - "Implementation-Version: %s\n" +\ - "Built-By: %s\n" +\ - "Build-Jdk: 1.6\n" +\ - "Created-By: http://www.bildtool.org\n" +\ - "\n" - manifest = manifest % (VERSION, os.getlogin()) - jarfile = "dist/antlr4-" + VERSION + ".jar" - jar(jarfile, srcdir="out/runtime", manifest=manifest) - print "Generated " + jarfile - -def mkjar(): - mkjar_complete() - # put it in JARCARCHE too so bild can find it during antlr4() - copyfile(src="dist/antlr4-" + VERSION + "-complete.jar", trg=JARCACHE) - # rebuild/bootstrap XPath with 4.4 so it can use 4.4 runtime (gen'd with 4.3 at this point) - rmdir("gen4/org/antlr/v4/runtime/tree/xpath") # kill 4.3-generated version - antlr4("runtime/Java/src/org/antlr/v4/runtime/tree/xpath", "gen4", version=VERSION, - package="org.antlr.v4.runtime.tree.xpath") - compile() - mkjar_complete() # make it again with up to date XPath lexer - mkjar_runtime() # now build the runtime jar - - -def tests(): - require(mkjar) - junit_jar, hamcrest_jar = load_junitjars() - cp = uniformpath("dist/antlr4-" + VERSION + "-complete.jar") \ - + os.pathsep + uniformpath("out/test/Java") \ - + os.pathsep + junit_jar \ - + os.pathsep + hamcrest_jar - juprops = ["-D%s=%s" % (p, test_properties[p]) for p in test_properties] - args = ["-nowarn", "-Xlint", "-Xlint:-serial", "-g"] - # don't compile generator - skip = [ uniformpath(TARGETS['Java'] + "/tool/test/org/antlr/v4/test/rt/gen/") ] - javac("tool/test", "out/test/Java", version="1.6", cp=cp, args=args, skip=skip) # all targets can use org.antlr.v4.test.* - for t in TARGETS: - print "Testing %s ..." % t - try: - test(t, cp, juprops, args) - print t + " tests complete" - except: - print t + " tests failed" - -def test(t, cp, juprops, args): - srcdir = uniformpath(TARGETS[t] + "/tool/test") - dstdir = uniformpath( "out/test/" + t) - # Prefix CLASSPATH with individual target tests - thiscp = dstdir + os.pathsep + cp - skip = [] - if t=='Java': - # don't test generator - skip = [ "/org/antlr/v4/test/rt/gen/", "TestPerformance" ] - elif t=='Python2': - # need BaseTest located in Py3 target - base = uniformpath(TARGETS['Python3'] + "/tool/test") - skip = [ "/org/antlr/v4/test/rt/py3/" ] - javac(base, "out/test/" + t, version="1.6", cp=thiscp, args=args, skip=skip) - skip = [] - elif t=='JavaScript': - # don't test browsers automatically, this is overkilling and unreliable - browsers = ["safari","chrome","firefox","explorer"] - skip = [ uniformpath(srcdir + "/org/antlr/v4/test/rt/js/" + b) for b in browsers ] - javac(srcdir, "out/test/" + t, version="1.6", cp=thiscp, args=args, skip=skip) - # copy resource files required for testing - files = allfiles(srcdir) - for src in files: - if not ".java" in src and not ".stg" in src and not ".DS_Store" in src: - dst = src.replace(srcdir, uniformpath("out/test/" + t)) - # only copy files from test dirs - if os.path.exists(os.path.split(dst)[0]): - shutil.copyfile(src, dst) - junit("out/test/" + t, cp=thiscp, verbose=False, args=juprops) - -def all(): - clean(True) - mkjar() - tests() - mkdoc() - mksrc() - install() - clean() - -def install(): - mvn_install("dist/antlr4-" + VERSION + "-complete.jar", - "dist/antlr4-" + VERSION + "-complete-sources.jar", - "dist/antlr4-" + VERSION + "-complete-javadoc.jar", - "org.antlr", - "antlr4", - VERSION) - mvn_install("dist/antlr4-" + VERSION + ".jar", - "dist/antlr4-" + VERSION + "-sources.jar", - "dist/antlr4-" + VERSION + "-javadoc.jar", - "org.antlr", - "antlr4-runtime", - VERSION) - -def clean(dist=False): - if dist: - rmdir("dist") - rmdir("out") - rmdir("gen3") - rmdir("gen4") - rmdir("doc") - - -def mksrc(): - srcpath = "runtime/Java/src/org" - jarfile = "dist/antlr4-" + VERSION + "-sources.jar" - zip(jarfile, srcpath) - print "Generated " + jarfile - jarfile = "dist/antlr4-" + VERSION + "-complete-sources.jar" - srcpaths = [ srcpath, "gen3/org", "gen4/org", "runtime/JavaAnnotations/src/org", "tool/src/org"] - zip(jarfile, srcpaths) - print "Generated " + jarfile - - -def mkdoc(): - # add a few source dirs to reduce the number of javadoc errors - # JavaDoc needs antlr annotations source code - mkdir("out/Annotations") - download("http://search.maven.org/remotecontent?filepath=org/antlr/antlr4-annotations/4.3/antlr4-annotations-4.3-sources.jar", "out/Annotations") - unjar("out/Annotations/antlr4-annotations-4.3-sources.jar", trgdir="out/Annotations") - # JavaDoc needs abego treelayout source code - mkdir("out/TreeLayout") - download("http://search.maven.org/remotecontent?filepath=org/abego/treelayout/org.abego.treelayout.core/1.0.1/org.abego.treelayout.core-1.0.1-sources.jar", "out/TreeLayout") - unjar("out/TreeLayout/org.abego.treelayout.core-1.0.1-sources.jar", trgdir="out/TreeLayout") - # JavaDoc needs antlr runtime 3.5.2 source code - mkdir("out/Antlr352Runtime") - download("http://search.maven.org/remotecontent?filepath=org/antlr/antlr-runtime/3.5.2/antlr-runtime-3.5.2-sources.jar", "out/Antlr352Runtime") - unjar("out/Antlr352Runtime/antlr-runtime-3.5.2-sources.jar", trgdir="out/Antlr352Runtime") - # JavaDoc needs antlr ST4 source code - mkdir("out/ST4") - download("http://search.maven.org/remotecontent?filepath=org/antlr/ST4/4.0.8/ST4-4.0.8-sources.jar", "out/ST4") - unjar("out/ST4/ST4-4.0.8-sources.jar", trgdir="out/ST4") - # go! - mkdir("doc/Java") - mkdir("doc/JavaTool") - dirs = ["runtime/Java/src"] - dirs += ["out/Annotations"] - dirs += ["out/TreeLayout"] - exclude = ["org/antlr/runtime", - "org/abego", - "org/stringtemplate", - "org/antlr/stringtemplate"] - javadoc(srcdir=dirs, trgdir="doc/Java", packages="org.antlr.v4.runtime", exclude=exclude) - dirs += ["gen3"] - dirs += [TARGETS[t] + "/tool/src" for t in TARGETS] - dirs += ["out/Antlr352Runtime"] - dirs += ["out/ST4"] - javadoc(srcdir=dirs, trgdir="doc/JavaTool", packages="org.antlr.v4", exclude=exclude) - # build stack merge PredictionContext and ATNState images from DOT - # DOT Images are in runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/ - # Gen into E.g., doc/Java/org/antlr/v4/runtime/atn/images/SingletonMerge_DiffRootSamePar.svg - mkdir("doc/Java/org/antlr/v4/runtime/atn/images") - for f in glob.glob("runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/*.dot"): - dot(f, "doc/Java/org/antlr/v4/runtime/atn/images", format="svg") - zip("dist/antlr4-" + VERSION + "-javadoc.jar", "doc/Java") - zip("dist/antlr4-" + VERSION + "-complete-javadoc.jar", "doc/JavaTool") - - - -processargs(globals()) # E.g., "python bild.py all" diff --git a/build.xml b/build.xml deleted file mode 100644 index 2505ce4e1..000000000 --- a/build.xml +++ /dev/null @@ -1,285 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pom.xml b/pom.xml index 05d2332be..707eadbc8 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ runtime/JavaAnnotations tool antlr4-maven-plugin + antlr4-testgen-maven-plugin diff --git a/runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/Block.dot b/runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/Block.dot index 73962b99b..f97636847 100644 --- a/runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/Block.dot +++ b/runtime/Java/src/main/dot/org/antlr/v4/runtime/atn/images/Block.dot @@ -1,35 +1,20 @@ digraph "" { - graph[fontname="CourierNew";rankdir="LR";pad="0.25"]; - node[fontname="CourierNew";target="_parent"]; - edge[fontname="CourierNew"]; - subgraph clusterA { - color="invis"; - { node[shape="record"]; - BasicBlockStartState[URL="../BasicBlockStartState.html";label="{BasicBlockStartState | { alt 1 | alt 2 | ·\n·\n· | alt n}}"]; - BlockEndState[URL="../BlockEndState.html"]; - } + graph[fontname="Consolas";rankdir="LR";pad="0.25"]; + node[fontname="Consolas";target="_parent"]; + edge[fontname="Consolas"]; - { node[style="dashed"]; - content1[label="alt 1"]; - content2[label="alt 2"]; - more[label="alt n"]; - } - } + net35client[label="net35-client"] + net40client[label="net40-client"] + portablenet40[label="portable-net40"] + portablenet45[label="portable-net45"] - { node[style="dashed"]; - begin; - end; - } + net20 -> net30 + net30 -> net35client + net35client -> net40client[style="dashed"] + net40client -> net45 - begin -> BasicBlockStartState[style="dashed"]; - BlockEndState -> end[label="ε"]; - - BasicBlockStartState:alt1 -> content1[label="ε"]; - content1 -> BlockEndState[style="dashed"]; - - BasicBlockStartState:alt2 -> content2[label="ε"]; - content2 -> BlockEndState[style="dashed"]; - - BasicBlockStartState:altn -> more[label="ε"]; - more -> BlockEndState[style="dashed"]; + portablenet40 -> net40client + portablenet40 -> portablenet45 + portablenet45 -> win + win -> net45 } diff --git a/tool/pom.xml b/tool/pom.xml index 3ca4acc28..5eb38a89b 100644 --- a/tool/pom.xml +++ b/tool/pom.xml @@ -154,6 +154,22 @@ + + org.antlr + antlr4-testgen-maven-plugin + ${project.version} + + + + antlr4.testgen + + + test/org/antlr/v4/test/runtime/java/Java.test.stg + + + + + org.apache.maven.plugins maven-jar-plugin diff --git a/tool/test/org/antlr/v4/test/rt/gen/Generator.java b/tool/test/org/antlr/v4/test/rt/gen/Generator.java index f3fc9c2a4..d34060ea0 100644 --- a/tool/test/org/antlr/v4/test/rt/gen/Generator.java +++ b/tool/test/org/antlr/v4/test/rt/gen/Generator.java @@ -162,105 +162,9 @@ public class Generator { list.add(buildParseTrees()); list.add(buildSemPredEvalLexer()); list.add(buildSemPredEvalParser()); - list.add(buildSets()); return list; } - private JUnitTestFile buildSets() throws Exception { - JUnitTestFile file = new JUnitTestFile("Sets"); - // this must return A not I to the parser; calling a nonfragment rule - // from a nonfragment rule does not set the overall token. - file.addParserTest(input, "SeqDoesNotBecomeSet", "T", "a", - "34", - "34\n", - null); - file.addParserTest(input, "ParserSet", "T", "a", - "x", - "x\n", - null); - file.addParserTest(input, "ParserNotSet", "T", "a", - "zz", - "z\n", - null); - file.addParserTest(input, "ParserNotToken", "T", "a", - "zz", - "zz\n", - null); - file.addParserTest(input, "ParserNotTokenWithLabel", "T", "a", - "zz", - "z\n", - null); - file.addParserTest(input, "RuleAsSet", "T", "a", - "b", - "b\n", - null); - file.addParserTest(input, "NotChar", "T", "a", - "x", - "x\n", - null); - file.addParserTest(input, "OptionalSingleElement", "T", "a", - "bc", - "bc\n", - null); - file.addParserTest(input, "OptionalLexerSingleElement", "T", "a", - "bc", - "bc\n", - null); - file.addParserTests(input, "StarLexerSingleElement", "T", "a", - "bbbbc", "bbbbc\n", - "c", "c\n"); - file.addParserTest(input, "PlusLexerSingleElement", "T", "a", - "bbbbc", - "bbbbc\n", - null); - file.addParserTest(input, "OptionalSet", "T", "a", - "ac", - "ac\n", - null); - file.addParserTest(input, "StarSet", "T", "a", - "abaac", - "abaac\n", - null); - file.addParserTest(input, "PlusSet", "T", "a", - "abaac", - "abaac\n", - null); - file.addParserTest(input, "LexerOptionalSet", "T", "a", - "ac", - "ac\n", - null); - file.addParserTest(input, "LexerStarSet", "T", "a", - "abaac", - "abaac\n", - null); - file.addParserTest(input, "LexerPlusSet", "T", "a", - "abaac", - "abaac\n", - null); - file.addParserTest(input, "NotCharSet", "T", "a", - "x", - "x\n", - null); - file.addParserTest(input, "NotCharSetWithLabel", "T", "a", - "x", - "x\n", - null); - file.addParserTest(input, "NotCharSetWithRuleRef3", "T", "a", - "x", - "x\n", - null); - file.addParserTest(input, "CharSetLiteral", "T", "a", - "A a B b", - "A\n" + "a\n" + "B\n" + "b\n", - null); - file.addParserTest(input, "ComplementSet", "T", "parse", - "a", - "", - "line 1:0 token recognition error at: 'a'\n" + - "line 1:1 missing {} at ''\n"); - return file; - } - private JUnitTestFile buildSemPredEvalParser() throws Exception { JUnitTestFile file = new JUnitTestFile("SemPredEvalParser"); JUnitTestMethod tm = file.addParserTest(input, "SimpleValidate", "T", "s", diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/CharSetLiteral.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/CharSetLiteral.st deleted file mode 100644 index 91f68f149..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/CharSetLiteral.st +++ /dev/null @@ -1,4 +0,0 @@ -grammar ; -a : (A {})+ ; -A : [AaBb] ; -WS : (' '|'\n')+ -> skip ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ComplementSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ComplementSet.st deleted file mode 100644 index 5873c96de..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ComplementSet.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -parse : ~NEW_LINE; -NEW_LINE: '\\r'? '\\n'; \ No newline at end of file diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerOptionalSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerOptionalSet.st deleted file mode 100644 index 090b5c7e4..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerOptionalSet.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : ('a'|'b')? 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerPlusSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerPlusSet.st deleted file mode 100644 index 0f00f8416..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerPlusSet.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : ('a'|'b')+ 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerStarSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerStarSet.st deleted file mode 100644 index 9407e8a03..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/LexerStarSet.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : ('a'|'b')* 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotChar.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotChar.st deleted file mode 100644 index 1bbdffaf1..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotChar.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : ~'b' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSet.st deleted file mode 100644 index 878839aeb..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSet.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : ~('b'|'c') ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithLabel.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithLabel.st deleted file mode 100644 index 9f2025cb0..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithLabel.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : h=~('b'|'c') ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithRuleRef3.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithRuleRef3.st deleted file mode 100644 index 107e6bb85..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/NotCharSetWithRuleRef3.st +++ /dev/null @@ -1,5 +0,0 @@ -grammar ; -a : A {} ; -A : ('a'|B) ; // this doesn't collapse to set but works -fragment -B : ~('a'|'c') ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalLexerSingleElement.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalLexerSingleElement.st deleted file mode 100644 index 7f00dba67..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalLexerSingleElement.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : 'b'? 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSet.st deleted file mode 100644 index 128433b14..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : ('a'|'b')? 'c' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSingleElement.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSingleElement.st deleted file mode 100644 index 88b7bc673..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/OptionalSingleElement.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A? 'c' {} ; -A : 'b' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotSet.st deleted file mode 100644 index eca2f1b3d..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : t=~('x'|'y') 'z' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotToken.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotToken.st deleted file mode 100644 index f5b1a914d..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotToken.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : ~'x' 'z' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotTokenWithLabel.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotTokenWithLabel.st deleted file mode 100644 index 6dc068aab..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserNotTokenWithLabel.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : t=~'x' 'z' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserSet.st deleted file mode 100644 index 528eca4b4..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/ParserSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : t=('x'|'y') {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusLexerSingleElement.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusLexerSingleElement.st deleted file mode 100644 index a7e8bca8f..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusLexerSingleElement.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : 'b'+ 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusSet.st deleted file mode 100644 index 5027cd639..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/PlusSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : ('a'|'b')+ 'c' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/RuleAsSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/RuleAsSet.st deleted file mode 100644 index feced0155..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/RuleAsSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a @after {} : 'a' | 'b' |'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/SeqDoesNotBecomeSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/SeqDoesNotBecomeSet.st deleted file mode 100644 index 1bab7fe17..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/SeqDoesNotBecomeSet.st +++ /dev/null @@ -1,5 +0,0 @@ -grammar ; -a : C {} ; -fragment A : '1' | '2'; -fragment B : '3' '4'; -C : A | B; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarLexerSingleElement.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarLexerSingleElement.st deleted file mode 100644 index 8811e8019..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarLexerSingleElement.st +++ /dev/null @@ -1,3 +0,0 @@ -grammar ; -a : A {} ; -A : 'b'* 'c' ; diff --git a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarSet.st b/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarSet.st deleted file mode 100644 index 0f0b2a0f9..000000000 --- a/tool/test/org/antlr/v4/test/rt/gen/grammars/Sets/StarSet.st +++ /dev/null @@ -1,2 +0,0 @@ -grammar ; -a : ('a'|'b')* 'c' {} ; diff --git a/tool/test/org/antlr/v4/test/rt/java/TestSets.java b/tool/test/org/antlr/v4/test/rt/java/TestSets.java deleted file mode 100644 index 14b4e0967..000000000 --- a/tool/test/org/antlr/v4/test/rt/java/TestSets.java +++ /dev/null @@ -1,261 +0,0 @@ -package org.antlr.v4.test.rt.java; - -import org.junit.Test; -import static org.junit.Assert.*; - -public class TestSets extends BaseTest { - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testSeqDoesNotBecomeSet() throws Exception { - String grammar = "grammar T;\n" + - "a : C {System.out.println(this._input.getText());} ;\n" + - "fragment A : '1' | '2';\n" + - "fragment B : '3' '4';\n" + - "C : A | B;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "34", false); - assertEquals("34\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testParserSet() throws Exception { - String grammar = "grammar T;\n" + - "a : t=('x'|'y') {System.out.println($t.text);} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false); - assertEquals("x\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testParserNotSet() throws Exception { - String grammar = "grammar T;\n" + - "a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", false); - assertEquals("z\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testParserNotToken() throws Exception { - String grammar = "grammar T;\n" + - "a : ~'x' 'z' {System.out.println(this._input.getText());} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", false); - assertEquals("zz\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testParserNotTokenWithLabel() throws Exception { - String grammar = "grammar T;\n" + - "a : t=~'x' 'z' {System.out.println($t.text);} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "zz", false); - assertEquals("z\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testRuleAsSet() throws Exception { - String grammar = "grammar T;\n" + - "a @after {System.out.println(this._input.getText());} : 'a' | 'b' |'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "b", false); - assertEquals("b\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testNotChar() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println($A.text);} ;\n" + - "A : ~'b' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false); - assertEquals("x\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testOptionalSingleElement() throws Exception { - String grammar = "grammar T;\n" + - "a : A? 'c' {System.out.println(this._input.getText());} ;\n" + - "A : 'b' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", false); - assertEquals("bc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testOptionalLexerSingleElement() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : 'b'? 'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", false); - assertEquals("bc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - String testStarLexerSingleElement(String input) throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : 'b'* 'c' ;"; - return execParser("T.g4", grammar, "TParser", "TLexer", "a", input, false); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testStarLexerSingleElement_1() throws Exception { - String found = testStarLexerSingleElement("bbbbc"); - assertEquals("bbbbc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testStarLexerSingleElement_2() throws Exception { - String found = testStarLexerSingleElement("c"); - assertEquals("c\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPlusLexerSingleElement() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : 'b'+ 'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bbbbc", false); - assertEquals("bbbbc\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testOptionalSet() throws Exception { - String grammar = "grammar T;\n" + - "a : ('a'|'b')? 'c' {System.out.println(this._input.getText());} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false); - assertEquals("ac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testStarSet() throws Exception { - String grammar = "grammar T;\n" + - "a : ('a'|'b')* 'c' {System.out.println(this._input.getText());} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false); - assertEquals("abaac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testPlusSet() throws Exception { - String grammar = "grammar T;\n" + - "a : ('a'|'b')+ 'c' {System.out.println(this._input.getText());} ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false); - assertEquals("abaac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testLexerOptionalSet() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : ('a'|'b')? 'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false); - assertEquals("ac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testLexerStarSet() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : ('a'|'b')* 'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false); - assertEquals("abaac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testLexerPlusSet() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println(this._input.getText());} ;\n" + - "A : ('a'|'b')+ 'c' ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false); - assertEquals("abaac\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testNotCharSet() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println($A.text);} ;\n" + - "A : ~('b'|'c') ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false); - assertEquals("x\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testNotCharSetWithLabel() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println($A.text);} ;\n" + - "A : h=~('b'|'c') ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false); - assertEquals("x\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testNotCharSetWithRuleRef3() throws Exception { - String grammar = "grammar T;\n" + - "a : A {System.out.println($A.text);} ;\n" + - "A : ('a'|B) ; // this doesn't collapse to set but works\n" + - "fragment\n" + - "B : ~('a'|'c') ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false); - assertEquals("x\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testCharSetLiteral() throws Exception { - String grammar = "grammar T;\n" + - "a : (A {System.out.println($A.text);})+ ;\n" + - "A : [AaBb] ;\n" + - "WS : (' '|'\\n')+ -> skip ;"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "A a B b", false); - assertEquals("A\na\nB\nb\n", found); - assertNull(this.stderrDuringParse); - } - - /* this file and method are generated, any edit will be overwritten by the next generation */ - @Test - public void testComplementSet() throws Exception { - String grammar = "grammar T;\n" + - "parse : ~NEW_LINE;\n" + - "NEW_LINE: '\\r'? '\\n';"; - String found = execParser("T.g4", grammar, "TParser", "TLexer", "parse", "a", false); - assertEquals("", found); - assertEquals("line 1:0 token recognition error at: 'a'\nline 1:1 missing {} at ''\n", this.stderrDuringParse); - } - - -} \ No newline at end of file diff --git a/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg b/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg new file mode 100644 index 000000000..e4c0aadc7 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/java/Java.test.stg @@ -0,0 +1,353 @@ +TestFile(file) ::= << +package org.antlr.v4.test.rt.java; + +import org.junit.Test; +import static org.junit.Assert.*; + + +import org.antlr.v4.test.tool.ErrorQueue; + + +import org.antlr.v4.tool.Grammar; + + +public class Test extends BaseTest { + + }; separator="\n", wrap, anchor> + +} +>> + +LexerTestMethod(test) ::= << +@Test +public void test() throws Exception { + = };separator="\\n\" +\n", wrap, anchor>"; + mkdir(tmpdir); + writeFile(tmpdir, ".g4", slave_); + };separator="\n", wrap, anchor> + StringBuilder sb = new StringBuilder(); + \\n");};separator="\n", wrap, anchor> + String grammar = sb.toString(); + + String found = execLexer(".g4", grammar, "Lexer", "", ); + assertEquals(\\n"};separator=" + \n", wrap, anchor>, found); + + assertEquals("", this.stderrDuringParse); + + assertNull(this.stderrDuringParse); + +} + +>> + +CompositeLexerTestMethod(test) ::= << + +>> + + +ParserTestMethod(test) ::= << +@Test +public void test() throws Exception { + = };separator="\\n\" +\n", wrap, anchor>"; + + rawGenerateAndBuildRecognizer(".g4", slave_, null, ""); + + mkdir(tmpdir); + writeFile(tmpdir, ".g4", slave_); + + };separator="\n", wrap, anchor>!> + + mkdir(tmpdir); + ", );}; separator="\n", anchor> + writeFile(tmpdir, "" + + String grammar = };separator="\\n\" +\n", wrap, anchor>"; + + String found = execParser(".g4", grammar, "Parser", "Lexer", "", "", ); + assertEquals("", found); + + assertEquals("", this.stderrDuringParse); + + assertNull(this.stderrDuringParse); + +} + +>> + +string(text) ::= << +"" +>> + +CompositeParserTestMethod(test) ::= << + +>> + +AbstractParserTestMethod(test) ::= << +String test(String input) throws Exception { + String grammar = };separator="\\n\" +\n", wrap, anchor>"; + return execParser(".g4", grammar, "Parser", "Lexer", "", input, ); +} + +>> + +ConcreteParserTestMethod(test) ::= << +@Test +public void test() throws Exception { + String found = test(""); + assertEquals("", found); + + assertEquals("", this.stderrDuringParse); + + assertNull(this.stderrDuringParse); + +} + +>> + +writeln(s) ::= <);>> + +write(s) ::= <);>> + +False() ::= "false" + +True() ::= "true" + +Not(v) ::= "!" + +Assert(s) ::= <);>> + +Cast(t,v) ::= "(())" + +Append(a,b) ::= " + " + +Concat(a,b) ::= "" + +DeclareLocal(s,v) ::= "Object = ;" + +AssignLocal(s,v) ::= " = ;" + +InitIntMember(n,v) ::= <%int = ;%> + +InitBooleanMember(n,v) ::= <%boolean = ;%> + +GetMember(n) ::= <%this.%> + +SetMember(n,v) ::= <%this. = ;%> + +AddMember(n,v) ::= <%this. += ;%> + +PlusMember(v,n) ::= <% + this.%> + +MemberEquals(n,v) ::= <%this. == %> + +ModMemberEquals(n,m,v) ::= <%this. % == %> + +ModMemberNotEquals(n,m,v) ::= <%this. % != %> + +DumpDFA() ::= "this.dumpDFA();" + +Pass() ::= "" + +StringList() ::= "List\" + +BuildParseTrees() ::= "setBuildParseTree(true);" + +BailErrorStrategy() ::= <%setErrorHandler(new BailErrorStrategy());%> + +ToStringTree(s) ::= <%.toStringTree(this)%> + +Column() ::= "this.getCharPositionInLine()" + +Text() ::= "this.getText()" + +ValEquals(a,b) ::= <%==%> + +TextEquals(a) ::= <%this.getText().equals("")%> + +PlusText(a) ::= <%"" + this.getText()%> + +InputText() ::= "this._input.getText()" + +LTEquals(i, v) ::= <%this._input.LT().getText().equals()%> + +LANotEquals(i, v) ::= <%this._input.LA()!=%> + +TokenStartColumnEquals(i) ::= <%this._tokenStartCharPositionInLine==%> + +ImportListener(X) ::= "" + +GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)" + +RuleInvocationStack() ::= "getRuleInvocationStack()" + +LL_EXACT_AMBIG_DETECTION() ::= <<_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);>> + +PositionAdjustingLexer() ::= << + +@Override +public Token nextToken() { + if (!(_interp instanceof PositionAdjustingLexerATNSimulator)) { + _interp = new PositionAdjustingLexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } + + return super.nextToken(); +} + +@Override +public Token emit() { + switch (_type) { + case TOKENS: + handleAcceptPositionForKeyword("tokens"); + break; + + case LABEL: + handleAcceptPositionForIdentifier(); + break; + + default: + break; + } + + return super.emit(); +} + +private boolean handleAcceptPositionForIdentifier() { + String tokenText = getText(); + int identifierLength = 0; + while (identifierLength \< tokenText.length() && isIdentifierChar(tokenText.charAt(identifierLength))) { + identifierLength++; + } + + if (getInputStream().index() > _tokenStartCharIndex + identifierLength) { + int offset = identifierLength - 1; + getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset); + return true; + } + + return false; +} + +private boolean handleAcceptPositionForKeyword(String keyword) { + if (getInputStream().index() > _tokenStartCharIndex + keyword.length()) { + int offset = keyword.length() - 1; + getInterpreter().resetAcceptPosition(getInputStream(), _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset); + return true; + } + + return false; +} + +@Override +public PositionAdjustingLexerATNSimulator getInterpreter() { + return (PositionAdjustingLexerATNSimulator)super.getInterpreter(); +} + +private static boolean isIdentifierChar(char c) { + return Character.isLetterOrDigit(c) || c == '_'; +} + +protected static class PositionAdjustingLexerATNSimulator extends LexerATNSimulator { + + public PositionAdjustingLexerATNSimulator(Lexer recog, ATN atn, + DFA[] decisionToDFA, + PredictionContextCache sharedContextCache) + { + super(recog, atn, decisionToDFA, sharedContextCache); + } + + protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) { + input.seek(index); + this.line = line; + this.charPositionInLine = charPositionInLine; + consume(input); + } + +} + +>> + +BasicListener(X) ::= << +public static class LeafListener extends TBaseListener { + public void visitTerminal(TerminalNode node) { + System.out.println(node.getSymbol().getText()); + } +} +>> + +WalkListener(s) ::= << +ParseTreeWalker walker = new ParseTreeWalker(); +walker.walk(new LeafListener(), ); +>> + +TokenGetterListener(X) ::= << +public static class LeafListener extends TBaseListener { + public void exitA(TParser.AContext ctx) { + if (ctx.getChildCount()==2) + System.out.printf("%s %s %s",ctx.INT(0).getSymbol().getText(), + ctx.INT(1).getSymbol().getText(),ctx.INT()); + else + System.out.println(ctx.ID().getSymbol()); + } +} +>> + +RuleGetterListener(X) ::= << +public static class LeafListener extends TBaseListener { + public void exitA(TParser.AContext ctx) { + if (ctx.getChildCount()==2) { + System.out.printf("%s %s %s",ctx.b(0).start.getText(), + ctx.b(1).start.getText(),ctx.b().get(0).start.getText()); + } else + System.out.println(ctx.b(0).start.getText()); + } +} +>> + + +LRListener(X) ::= << +public static class LeafListener extends TBaseListener { + public void exitE(TParser.EContext ctx) { + if (ctx.getChildCount()==3) { + System.out.printf("%s %s %s\n",ctx.e(0).start.getText(), + ctx.e(1).start.getText(), ctx.e().get(0).start.getText()); + } else + System.out.println(ctx.INT().getSymbol().getText()); + } +} +>> + +LRWithLabelsListener(X) ::= << +public static class LeafListener extends TBaseListener { + public void exitCall(TParser.CallContext ctx) { + System.out.printf("%s %s",ctx.e().start.getText(),ctx.eList()); + } + public void exitInt(TParser.IntContext ctx) { + System.out.println(ctx.INT().getSymbol().getText()); + } +} +>> + +DeclareContextListGettersFunction() ::= << +void foo() { + SContext s = null; + List\ a = s.a(); + List\ b = s.b(); +} +>> + +Declare_foo() ::= <> + +Invoke_foo() ::= "this.foo();" + +Declare_pred() ::= <} +] + +Rule() ::= "a" + +Input() ::= "A a B b" + +Output() ::= << +A +a +B +b<\n> +>> + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : (A {})+ ; +A : [AaBb] ; +WS : (' '|'\n')+ -> skip ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/ComplementSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/ComplementSet.stg new file mode 100644 index 000000000..9b8607a59 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/ComplementSet.stg @@ -0,0 +1,22 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "parse" + +Input() ::= "a" + +Output() ::= "" + +Errors() ::= << +line 1:0 token recognition error at: 'a' +line 1:1 missing {} at '\'<\n> +>> + +grammar(grammarName) ::= << +grammar ; + : ~NEW_LINE; +NEW_LINE: '\\r'? '\\n'; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerOptionalSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerOptionalSet.stg new file mode 100644 index 000000000..542e34a45 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerOptionalSet.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "ac" + +Output() ::= "ac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ('a'|'b')? 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerPlusSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerPlusSet.stg new file mode 100644 index 000000000..c17142dfb --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerPlusSet.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "abaac" + +Output() ::= "abaac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ('a'|'b')+ 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerStarSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerStarSet.stg new file mode 100644 index 000000000..75f976931 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/LexerStarSet.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "abaac" + +Output() ::= "abaac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ('a'|'b')* 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/NotChar.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotChar.stg new file mode 100644 index 000000000..bbe94776f --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotChar.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "x" + +Output() ::= "x<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ~'b' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSet.stg new file mode 100644 index 000000000..e43de3e9f --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSet.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "x" + +Output() ::= "x<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ~('b'|'c') ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithLabel.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithLabel.stg new file mode 100644 index 000000000..bf5b2ce0c --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithLabel.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "x" + +Output() ::= "x<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : h=~('b'|'c') ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithRuleRef3.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithRuleRef3.stg new file mode 100644 index 000000000..34a480c06 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/NotCharSetWithRuleRef3.stg @@ -0,0 +1,21 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "x" + +Output() ::= "x<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : ('a'|B) ; // this doesn't collapse to set but works +fragment +B : ~('a'|'c') ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalLexerSingleElement.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalLexerSingleElement.stg new file mode 100644 index 000000000..038b5757b --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalLexerSingleElement.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "bc" + +Output() ::= "bc<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : 'b'? 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSet.stg new file mode 100644 index 000000000..705ca6775 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "ac" + +Output() ::= "ac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : ('a'|'b')? 'c' {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSingleElement.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSingleElement.stg new file mode 100644 index 000000000..6b574bdf3 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/OptionalSingleElement.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "bc" + +Output() ::= "bc<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A? 'c' {} ; +A : 'b' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotSet.stg new file mode 100644 index 000000000..bf198a384 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "zz" + +Output() ::= "z<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : t=~('x'|'y') 'z' {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotToken.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotToken.stg new file mode 100644 index 000000000..80c234002 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotToken.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "zz" + +Output() ::= "zz<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : ~'x' 'z' {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotTokenWithLabel.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotTokenWithLabel.stg new file mode 100644 index 000000000..2a9107e6c --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserNotTokenWithLabel.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "zz" + +Output() ::= "z<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : t=~'x' 'z' {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserSet.stg new file mode 100644 index 000000000..bccf2c060 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/ParserSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "x" + +Output() ::= "x<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : t=('x'|'y') {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusLexerSingleElement.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusLexerSingleElement.stg new file mode 100644 index 000000000..e01d2d72a --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusLexerSingleElement.stg @@ -0,0 +1,19 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "bbbbc" + +Output() ::= "bbbbc<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : 'b'+ 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusSet.stg new file mode 100644 index 000000000..d4c782195 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/PlusSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "abaac" + +Output() ::= "abaac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : ('a'|'b')+ 'c' {} ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/RuleAsSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/RuleAsSet.stg new file mode 100644 index 000000000..07cb81ca1 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/RuleAsSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "b" + +Output() ::= "b<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + @after {} : 'a' | 'b' |'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/SeqDoesNotBecomeSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/SeqDoesNotBecomeSet.stg new file mode 100644 index 000000000..44036e4bc --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/SeqDoesNotBecomeSet.stg @@ -0,0 +1,21 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "34" + +Output() ::= "34<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : C {} ; +fragment A : '1' | '2'; +fragment B : '3' '4'; +C : A | B; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/StarLexerSingleElement.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/StarLexerSingleElement.stg new file mode 100644 index 000000000..55f6123f3 --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/StarLexerSingleElement.stg @@ -0,0 +1,28 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input ::= [ + "1": "bbbbc", + "2": "c" +] + +Output ::= [ + "1": {bbbbc<\n>}, + "2": {c<\n>} +] + +Errors ::= [ + "1": "", + "2": "" +] + +grammar(grammarName) ::= << +grammar ; + : A {} ; +A : 'b'* 'c' ; +>> diff --git a/tool/test/org/antlr/v4/test/runtime/templates/sets/StarSet.stg b/tool/test/org/antlr/v4/test/runtime/templates/sets/StarSet.stg new file mode 100644 index 000000000..af7193b4b --- /dev/null +++ b/tool/test/org/antlr/v4/test/runtime/templates/sets/StarSet.stg @@ -0,0 +1,18 @@ +TestType() ::= "Parser" + +Grammars ::= [ + "T.g4": {} +] + +Rule() ::= "a" + +Input() ::= "abaac" + +Output() ::= "abaac<\n>" + +Errors() ::= "" + +grammar(grammarName) ::= << +grammar ; + : ('a'|'b')* 'c' {} ; +>>