-
Notifications
You must be signed in to change notification settings - Fork 86
Closed
Description
Hi
I am trying to call this lib from other maven project, since i am using antlr 4.7.2, so i manually changed your pom.xml from 4.5.3 to 4.7.2, it build successfully. But when i run my code, it throw exception:
/*
* Copyright 2019 Peter <peter@quantr.hk>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hk.quantr.antlrformatter;
import java.io.File;
import java.util.List;
import java.util.Map;
import org.antlr.codebuff.Corpus;
import org.antlr.codebuff.Formatter;
import org.antlr.codebuff.InputDocument;
import static org.antlr.codebuff.Tool.format;
import static org.antlr.codebuff.Tool.getFilenames;
import static org.antlr.codebuff.Tool.getLexer;
import static org.antlr.codebuff.Tool.load;
import static org.antlr.codebuff.Tool.parse;
import static org.antlr.codebuff.Trainer.FEATURES_HPOS;
import static org.antlr.codebuff.Trainer.FEATURES_INJECT_WS;
import static org.antlr.codebuff.misc.BuffUtils.filter;
import org.antlr.codebuff.misc.LangDescriptor;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.misc.Utils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
/**
*
* @author Peter <peter@quantr.hk>
*/
public class Test1 {
@Test
public void test() throws Exception {
// String s = IOUtils.toString(AntlrFormatter.class.getResourceAsStream("Hello.g4"), "utf8");
// String s = IOUtils.toString(AntlrFormatter.class.getResourceAsStream("AssemblerLexer.g4"), "utf8");
String s = IOUtils.toString(AntlrFormatter.class.getResourceAsStream("AssemblerParser.g4"), "utf8");
// System.out.println(AntlrFormatter.format(s).substring(0, 10000));
String grammarName = "org.antlr.codebuff.ANTLRv4";
String startRule = "grammarSpec";
String fileExtension = "g4";
String fileRegex = ".*\\." + fileExtension;
String corpusDir = "corpus/antlr4/training";
int indentSize = 4;
int singleLineCommentType = -1;
String commentS = "LINE_COMMENT";
String parserClassName = grammarName + "Parser";
String lexerClassName = grammarName + "Lexer";
Class<? extends Parser> parserClass = null;
Class<? extends Lexer> lexerClass = null;
Lexer lexer = null;
try {
parserClass = (Class<? extends Parser>) Class.forName(parserClassName);
lexerClass = (Class<? extends Lexer>) Class.forName(lexerClassName);
} catch (Exception e) {
System.err.println("Can't load " + parserClassName + " or maybe " + lexerClassName);
System.err.println("Make sure they are generated by ANTLR, compiled, and in CLASSPATH");
e.printStackTrace(System.err);
}
if (commentS != null) {
try {
lexer = getLexer(lexerClass, null);
} catch (Exception e) {
System.err.println("Can't instantiate lexer " + lexerClassName);
e.printStackTrace(System.err);
}
if (lexer == null) {
return;
}
Map<String, Integer> tokenTypeMap = lexer.getTokenTypeMap();
if (tokenTypeMap.containsKey(commentS)) {
singleLineCommentType = tokenTypeMap.get(commentS);
}
}
LangDescriptor language = new LangDescriptor(grammarName, corpusDir, fileRegex,
lexerClass, parserClass, startRule,
indentSize, singleLineCommentType);
format(language, "/Users/peter/workspace/antlr-formatter/src/test/resources/hk/quantr/antlrformatter/AssemblerParser.g4", "/Users/peter/Desktop/AssemblerParser.g4");
}
public static void format(LangDescriptor language,
String testFileName,
String outputFileName)
throws Exception {
// load all files up front
List<String> allFiles = getFilenames(new File(language.corpusDir), language.fileRegex);
List<InputDocument> documents = load(allFiles, language);
// if in corpus, don't include in corpus
final String path = new File(testFileName).getAbsolutePath();
List<InputDocument> others = filter(documents, d -> !d.fileName.equals(path));
InputDocument testDoc = parse(testFileName, language);
Corpus corpus = new Corpus(others, language);
corpus.train();
Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K,
FEATURES_INJECT_WS, FEATURES_HPOS);
String output = formatter.format(testDoc, false);
if (outputFileName != null) {
Utils.writeFile(outputFileName, output);
} else {
System.out.print(output);
}
}
}
Exception:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running hk.quantr.antlrformatter.Test1
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.587 sec <<< FAILURE!
test(hk.quantr.antlrformatter.Test1) Time elapsed: 0.521 sec <<< FAILURE!
java.lang.AssertionError
at org.antlr.v4.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:148)
at org.antlr.v4.runtime.BufferedTokenStream.previousTokenOnChannel(BufferedTokenStream.java:343)
at org.antlr.codebuff.misc.CodeBuffTokenStream.getPreviousRealToken(CodeBuffTokenStream.java:45)
at org.antlr.codebuff.Formatter.getFeatures(Formatter.java:348)
at org.antlr.codebuff.Formatter.processToken(Formatter.java:189)
at org.antlr.codebuff.Formatter.format(Formatter.java:161)
at hk.quantr.antlrformatter.Test1.format(Test1.java:111)
at hk.quantr.antlrformatter.Test1.test(Test1.java:92)
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:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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:567)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Results :
Failed tests: test(hk.quantr.antlrformatter.Test1)
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 4.607 s
Finished at: 2019-10-13T15:46:58+08:00
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-cli) on project antlr-formatter: There are test failures.
Please refer to /Users/peter/workspace/antlr-formatter/target/surefire-reports for the individual test results.
-> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Please help
Thanks
Peter
Metadata
Metadata
Assignees
Labels
No labels