diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java index 17c0d5e96c..1604c69870 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTest.java @@ -111,11 +111,6 @@ public BaseRuntimeTest(RuntimeTestDescriptor descriptor, RuntimeTestSupport dele this.delegate = delegate; } - public static void mkdir(String dir) { - File f = new File(dir); - f.mkdirs(); - } - @Before public void setUp() throws Exception { // From http://junit.sourceforge.net/javadoc/org/junit/Assume.html @@ -159,7 +154,7 @@ public void testOne() throws Exception { } public void testParser(RuntimeTestDescriptor descriptor) throws Exception { - mkdir(delegate.getTmpDir()); + RuntimeTestUtils.mkdir(delegate.getTempParserDirPath()); Pair pair = descriptor.getGrammar(); @@ -176,7 +171,7 @@ public void testParser(RuntimeTestDescriptor descriptor) throws Exception { g.registerRenderer(String.class, new StringRenderer()); g.importTemplates(targetTemplates); ST grammarST = new ST(g, spair.b); - writeFile(delegate.getTmpDir(), spair.a+".g4", grammarST.render()); + writeFile(delegate.getTempParserDirPath(), spair.a+".g4", grammarST.render()); } } @@ -201,7 +196,7 @@ public void testParser(RuntimeTestDescriptor descriptor) throws Exception { } public void testLexer(RuntimeTestDescriptor descriptor) throws Exception { - mkdir(delegate.getTmpDir()); + RuntimeTestUtils.mkdir(delegate.getTempParserDirPath()); Pair pair = descriptor.getGrammar(); @@ -218,7 +213,7 @@ public void testLexer(RuntimeTestDescriptor descriptor) throws Exception { g.registerRenderer(String.class, new StringRenderer()); g.importTemplates(targetTemplates); ST grammarST = new ST(g, spair.b); - writeFile(delegate.getTmpDir(), spair.a+".g4", grammarST.render()); + writeFile(delegate.getTempParserDirPath(), spair.a+".g4", grammarST.render()); } } @@ -242,7 +237,7 @@ public static ErrorQueue antlrOnString(String workdir, boolean defaultListener, String... extraOptions) { - mkdir(workdir); + RuntimeTestUtils.mkdir(workdir); writeFile(workdir, grammarFileName, grammarStr); return antlrOnString(workdir, targetName, grammarFileName, defaultListener, extraOptions); } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java new file mode 100644 index 0000000000..fb6d8571dc --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/BaseRuntimeTestSupport.java @@ -0,0 +1,215 @@ +package org.antlr.v4.test.runtime; + +import org.antlr.v4.Tool; +import org.antlr.v4.automata.LexerATNFactory; +import org.antlr.v4.automata.ParserATNFactory; +import org.antlr.v4.runtime.atn.ATN; +import org.antlr.v4.runtime.atn.ATNDeserializer; +import org.antlr.v4.runtime.atn.ATNSerializer; +import org.antlr.v4.semantics.SemanticPipeline; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.LexerGrammar; +import org.junit.rules.TestRule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; + +import java.io.File; +import java.util.Locale; +import java.util.logging.Logger; + +import static org.junit.Assert.assertEquals; + +public abstract class BaseRuntimeTestSupport implements RuntimeTestSupport { + + // -J-Dorg.antlr.v4.test.BaseTest.level=FINE + protected static final Logger logger = Logger.getLogger(BaseRuntimeTestSupport.class.getName()); + + public static final String NEW_LINE = System.getProperty("line.separator"); + public static final String PATH_SEP = System.getProperty("path.separator"); + + private File tempTestDir = null; + + /** If error during parser execution, store stderr here; can't return + * stdout and stderr. This doesn't trap errors from running antlr. + */ + private String parseErrors; + + /** Errors found while running antlr */ + private StringBuilder antlrToolErrors; + + @org.junit.Rule + public final TestRule testWatcher = new TestWatcher() { + + @Override + protected void succeeded(Description description) { + testSucceeded(description); + } + + }; + + protected void testSucceeded(Description description) { + // remove tmpdir if no error. + eraseTempDir(); + } + + @Override + public File getTempParserDir() { + return getTempTestDir(); + } + + @Override + public String getTempParserDirPath() { + return getTempParserDir() == null ? null : getTempParserDir().getAbsolutePath(); + } + + @Override + public final File getTempTestDir() { + return tempTestDir; + } + + @Override + public final String getTempDirPath() { + return tempTestDir ==null ? null : tempTestDir.getAbsolutePath(); + } + + + public void setParseErrors(String errors) { + this.parseErrors = errors; + } + + public String getParseErrors() { + return parseErrors; + } + + public String getANTLRToolErrors() { + if ( antlrToolErrors.length()==0 ) { + return null; + } + return antlrToolErrors.toString(); + } + + protected abstract String getPropertyPrefix(); + + @Override + public void testSetUp() throws Exception { + createTempDir(); + antlrToolErrors = new StringBuilder(); + } + + private void createTempDir() { + // new output dir for each test + String propName = getPropertyPrefix() + "-test-dir"; + String prop = System.getProperty(propName); + if(prop!=null && prop.length()>0) { + tempTestDir = new File(prop); + } + else { + String dirName = getClass().getSimpleName() + "-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis(); + tempTestDir = new File(System.getProperty("java.io.tmpdir"), dirName); + } + } + + @Override + public void testTearDown() throws Exception { + } + + @Override + public void beforeTest(RuntimeTestDescriptor descriptor) { + } + + @Override + public void afterTest(RuntimeTestDescriptor descriptor) { + } + + public void eraseTempDir() { + if(shouldEraseTempDir()) { + eraseDirectory(getTempTestDir()); + } + } + + protected boolean shouldEraseTempDir() { + if(tempTestDir == null) + return false; + String propName = getPropertyPrefix() + "-erase-test-dir"; + String prop = System.getProperty(propName); + if (prop != null && prop.length() > 0) + return Boolean.getBoolean(prop); + else + return true; + } + + public static void eraseDirectory(File dir) { + if ( dir.exists() ) { + eraseFilesInDir(dir); + dir.delete(); + } + } + + + public static void eraseFilesInDir(File dir) { + String[] files = dir.list(); + for(int i = 0; files!=null && i < files.length; i++) { + new File(dir,files[i]).delete(); + } + } + + private static String detectedOS; + + public static String getOS() { + if (detectedOS == null) { + String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); + if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) { + detectedOS = "mac"; + } + else if (os.indexOf("win") >= 0) { + detectedOS = "windows"; + } + else if (os.indexOf("nux") >= 0) { + detectedOS = "linux"; + } + else { + detectedOS = "unknown"; + } + } + return detectedOS; + } + + + public static boolean isWindows() { + return getOS().equalsIgnoreCase("windows"); + } + + protected ATN createATN(Grammar g, boolean useSerializer) { + if ( g.atn==null ) { + semanticProcess(g); + assertEquals(0, g.tool.getNumErrors()); + + ParserATNFactory f = g.isLexer() ? new LexerATNFactory((LexerGrammar) g) : new ParserATNFactory(g); + + g.atn = f.createATN(); + assertEquals(0, g.tool.getNumErrors()); + } + + ATN atn = g.atn; + if ( useSerializer ) { + char[] serialized = ATNSerializer.getSerializedAsChars(atn); + return new ATNDeserializer().deserialize(serialized); + } + + return atn; + } + protected void semanticProcess(Grammar g) { + if ( g.ast!=null && !g.ast.hasErrors ) { +// System.out.println(g.ast.toStringTree()); + Tool antlr = new Tool(); + SemanticPipeline sem = new SemanticPipeline(g); + sem.process(); + if ( g.getImportedGrammars()!=null ) { // process imported grammars (if any) + for (Grammar imp : g.getImportedGrammars()) { + antlr.processNonCombinedGrammar(imp, false); + } + } + } + } + +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/MockIntTokenStream.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/MockIntTokenStream.java new file mode 100644 index 0000000000..06eacddfb1 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/MockIntTokenStream.java @@ -0,0 +1,91 @@ +package org.antlr.v4.test.runtime; + +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.IntegerList; +import org.antlr.v4.runtime.misc.Interval; + +public class MockIntTokenStream implements TokenStream { + + public IntegerList types; + int p=0; + + public MockIntTokenStream(IntegerList types) { this.types = types; } + + @Override + public void consume() { p++; } + + @Override + public int LA(int i) { return LT(i).getType(); } + + @Override + public int mark() { + return index(); + } + + @Override + public int index() { return p; } + + @Override + public void release(int marker) { + seek(marker); + } + + @Override + public void seek(int index) { + p = index; + } + + @Override + public int size() { + return types.size(); + } + + @Override + public String getSourceName() { + return UNKNOWN_SOURCE_NAME; + } + + @Override + public Token LT(int i) { + CommonToken t; + int rawIndex = p + i - 1; + if ( rawIndex>=types.size() ) t = new CommonToken(Token.EOF); + else t = new CommonToken(types.get(rawIndex)); + t.setTokenIndex(rawIndex); + return t; + } + + @Override + public Token get(int i) { + return new org.antlr.v4.runtime.CommonToken(types.get(i)); + } + + @Override + public TokenSource getTokenSource() { + return null; + } + + + @Override + public String getText() { + throw new UnsupportedOperationException("can't give strings"); + } + + + @Override + public String getText(Interval interval) { + throw new UnsupportedOperationException("can't give strings"); + } + + + @Override + public String getText(RuleContext ctx) { + throw new UnsupportedOperationException("can't give strings"); + } + + + @Override + public String getText(Token start, Token stop) { + throw new UnsupportedOperationException("can't give strings"); + } +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestSupport.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestSupport.java index ca09509867..87fcc763e7 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestSupport.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestSupport.java @@ -6,6 +6,8 @@ package org.antlr.v4.test.runtime; +import java.io.File; + /** This interface describes functionality needed to execute a runtime test. * Unfortunately the Base*Test.java files are big junk drawers. This is * an attempt to make it more obvious what new target implementers have to @@ -14,13 +16,22 @@ * @since 4.6 */ public interface RuntimeTestSupport { - void testSetUp() throws Exception; - void testTearDown() throws Exception; + + // dir containing grammar input and output + File getTempParserDir(); + String getTempParserDirPath(); + + // dir containing test input and output + File getTempTestDir(); + String getTempDirPath(); void eraseTempDir(); - String getTmpDir(); + void testSetUp() throws Exception; + void testTearDown() throws Exception; + + void beforeTest(RuntimeTestDescriptor descriptor); + void afterTest(RuntimeTestDescriptor descriptor); - String getStdout(); String getParseErrors(); String getANTLRToolErrors(); @@ -40,6 +51,4 @@ String execParser(String grammarFileName, String input, boolean showDiagnosticErrors); - void beforeTest(RuntimeTestDescriptor descriptor); - void afterTest(RuntimeTestDescriptor descriptor); } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestUtils.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestUtils.java new file mode 100644 index 0000000000..d2c30800cb --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTestUtils.java @@ -0,0 +1,89 @@ +package org.antlr.v4.test.runtime; + +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.ATN; +import org.antlr.v4.runtime.atn.LexerATNSimulator; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.IntegerList; +import org.antlr.v4.tool.LexerGrammar; + +import java.io.*; +import java.util.*; + +public abstract class RuntimeTestUtils { + + /** Sort a list */ + public static > List sort(List data) { + List dup = new ArrayList(data); + dup.addAll(data); + Collections.sort(dup); + return dup; + } + + /** Return map sorted by key */ + public static ,V> LinkedHashMap sort(Map data) { + LinkedHashMap dup = new LinkedHashMap(); + List keys = new ArrayList(data.keySet()); + Collections.sort(keys); + for (K k : keys) { + dup.put(k, data.get(k)); + } + return dup; + } + + public static List getTokenTypes(LexerGrammar lg, + ATN atn, + CharStream input) { + LexerATNSimulator interp = new LexerATNSimulator(atn, new DFA[]{new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE))}, null); + List tokenTypes = new ArrayList(); + int ttype; + boolean hitEOF = false; + do { + if ( hitEOF ) { + tokenTypes.add("EOF"); + break; + } + int t = input.LA(1); + ttype = interp.match(input, Lexer.DEFAULT_MODE); + if ( ttype==Token.EOF ) { + tokenTypes.add("EOF"); + } + else { + tokenTypes.add(lg.typeToTokenList.get(ttype)); + } + + if ( t== IntStream.EOF ) { + hitEOF = true; + } + } while ( ttype!=Token.EOF ); + return tokenTypes; + } + + public static IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { + ANTLRInputStream in = new ANTLRInputStream(input); + IntegerList tokenTypes = new IntegerList(); + int ttype; + do { + ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); + tokenTypes.add(ttype); + } while ( ttype!= Token.EOF ); + return tokenTypes; + } + + public static void copyFile(File source, File dest) throws IOException { + InputStream is = new FileInputStream(source); + OutputStream os = new FileOutputStream(dest); + byte[] buf = new byte[4 << 10]; + int l; + while ((l = is.read(buf)) > -1) { + os.write(buf, 0, l); + } + is.close(); + os.close(); + } + + public static void mkdir(String dir) { + File f = new File(dir); + f.mkdirs(); + } +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java index 136c320cd7..e6a4526c2a 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java @@ -5,68 +5,19 @@ */ package org.antlr.v4.test.runtime.cpp; -import org.antlr.v4.Tool; -import org.antlr.v4.automata.ATNFactory; -import org.antlr.v4.automata.ATNPrinter; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.codegen.CodeGenerator; -import org.antlr.v4.runtime.ANTLRInputStream; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CommonToken; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.IntStream; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.Parser; -import org.antlr.v4.runtime.RuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenSource; -import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.WritableToken; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; -import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.LexerATNSimulator; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntegerList; -import org.antlr.v4.runtime.misc.Interval; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.semantics.SemanticPipeline; -import org.antlr.v4.test.runtime.ErrorQueue; -import org.antlr.v4.test.runtime.RuntimeTestDescriptor; -import org.antlr.v4.test.runtime.RuntimeTestSupport; -import org.antlr.v4.test.runtime.StreamVacuum; -import org.antlr.v4.tool.ANTLRMessage; -import org.antlr.v4.tool.DOTGenerator; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.GrammarSemanticsMessage; -import org.antlr.v4.tool.LexerGrammar; -import org.antlr.v4.tool.Rule; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; +import org.antlr.v4.test.runtime.*; import org.stringtemplate.v4.ST; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STGroupString; import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.TreeMap; import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; @@ -76,268 +27,12 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -public class BaseCppTest implements RuntimeTestSupport { - // -J-Dorg.antlr.v4.test.BaseTest.level=FINE - // private static final Logger LOGGER = Logger.getLogger(BaseTest.class.getName()); - public static final String newline = System.getProperty("line.separator"); - public static final String pathSep = System.getProperty("path.separator"); +public class BaseCppTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { - public String tmpdir = null; - - /** If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** Errors found while running antlr */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - private String getPropertyPrefix() { + protected String getPropertyPrefix() { return "antlr-" + getLanguage().toLowerCase(); } - @Override - public void testSetUp() throws Exception { - // new output dir for each test - String propName = getPropertyPrefix() + "-test-dir"; - String prop = System.getProperty(propName); - if(prop!=null && prop.length()>0) { - tmpdir = prop; - } - else { - tmpdir = new File(System.getProperty("java.io.tmpdir"), - getClass().getSimpleName()+"-"+Thread.currentThread().getName()+"-"+System.currentTimeMillis()).getAbsolutePath(); - } - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - System.out.println(descriptor.getTestName()); - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if ( antlrToolErrors.length()==0 ) { - return null; - } - return antlrToolErrors.toString(); - } - - protected org.antlr.v4.Tool newTool(String[] args) { - Tool tool = new Tool(args); - return tool; - } - - protected Tool newTool() { - org.antlr.v4.Tool tool = new Tool(new String[] {"-o", tmpdir}); - return tool; - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if ( g.atn==null ) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if ( g.isLexer() ) { - f = new LexerATNFactory((LexerGrammar)g); - } - else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if ( g.ast!=null && !g.ast.hasErrors ) { - System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if ( g.getImportedGrammars()!=null ) { // process imported grammars (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } - } - - public DFA createDFA(Grammar g, DecisionState s) { -// PredictionDFAFactory conv = new PredictionDFAFactory(g, s); -// DFA dfa = conv.createDFA(); -// conv.issueAmbiguityWarnings(); -// System.out.print("DFA="+dfa); -// return dfa; - return null; - } - -// public void minimizeDFA(DFA dfa) { -// DFAMinimizer dmin = new DFAMinimizer(dfa); -// dfa.minimized = dmin.minimize(); -// } - - IntegerList getTypesFromString(Grammar g, String expecting) { - IntegerList expectingTokenTypes = new IntegerList(); - if ( expecting!=null && !expecting.trim().isEmpty() ) { - for (String tname : expecting.replace(" ", "").split(",")) { - int ttype = g.getTokenType(tname); - expectingTokenTypes.add(ttype); - } - } - return expectingTokenTypes; - } - - public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { - ANTLRInputStream in = new ANTLRInputStream(input); - IntegerList tokenTypes = new IntegerList(); - int ttype; - do { - ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); - tokenTypes.add(ttype); - } while ( ttype!= Token.EOF ); - return tokenTypes; - } - - public List getTokenTypes(LexerGrammar lg, - ATN atn, - CharStream input) - { - LexerATNSimulator interp = new LexerATNSimulator(atn,new DFA[] { new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null); - List tokenTypes = new ArrayList(); - int ttype; - boolean hitEOF = false; - do { - if ( hitEOF ) { - tokenTypes.add("EOF"); - break; - } - int t = input.LA(1); - ttype = interp.match(input, Lexer.DEFAULT_MODE); - if ( ttype == Token.EOF ) { - tokenTypes.add("EOF"); - } - else { - tokenTypes.add(lg.typeToTokenList.get(ttype)); - } - - if ( t== IntStream.EOF ) { - hitEOF = true; - } - } while ( ttype!=Token.EOF ); - return tokenTypes; - } - - List checkRuleDFA(String gtext, String ruleName, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - ATNState s = atn.ruleToStartState[g.getRule(ruleName).index]; - if ( s==null ) { - System.err.println("no such rule: "+ruleName); - return null; - } - ATNState t = s.transition(0).target; - if ( !(t instanceof DecisionState) ) { - System.out.println(ruleName+" has no decision"); - return null; - } - DecisionState blk = (DecisionState)t; - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - List checkRuleDFA(String gtext, int decision, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - DecisionState blk = atn.decisionToState.get(decision); - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - void checkRuleDFA(Grammar g, DecisionState blk, String expecting) - throws Exception - { - DFA dfa = createDFA(g, blk); - String result = null; - if ( dfa!=null ) result = dfa.toString(); - assertEquals(expecting, result); - } - - List checkLexerDFA(String gtext, String expecting) - throws Exception - { - return checkLexerDFA(gtext, LexerGrammar.DEFAULT_MODE_NAME, expecting); - } - - List checkLexerDFA(String gtext, String modeName, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - LexerGrammar g = new LexerGrammar(gtext, equeue); - g.atn = createATN(g, false); -// LexerATNToDFAConverter conv = new LexerATNToDFAConverter(g); -// DFA dfa = conv.createDFA(modeName); -// g.setLookaheadDFA(0, dfa); // only one decision to worry about -// -// String result = null; -// if ( dfa!=null ) result = dfa.toString(); -// assertEquals(expecting, result); -// -// return equeue.all; - return null; - } - protected String getLanguage() { return "Cpp"; } @@ -362,44 +57,13 @@ public String execLexer(String grammarFileName, null, lexerName,"-no-listener"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); String output = execModule("Test.cpp"); return output; } - public ParseTree execStartRule(String startRuleName, Parser parser) - throws IllegalAccessException, InvocationTargetException, - NoSuchMethodException - { - Method startRule = null; - Object[] args = null; - try { - startRule = parser.getClass().getMethod(startRuleName); - } - catch (NoSuchMethodException nsme) { - // try with int _p arg for recursive func - startRule = parser.getClass().getMethod(startRuleName, int.class); - args = new Integer[] {0}; - } - ParseTree result = (ParseTree)startRule.invoke(parser, args); -// System.out.println("parse tree = "+result.toStringTree(parser)); - return result; - } -// protected String execParser(String grammarFileName, -// String grammarStr, -// String parserName, -// String lexerName, -// String listenerName, -// String visitorName, -// String startRuleName, -// String input, -// boolean debug) { -// return execParser(grammarFileName, grammarStr, parserName, lexerName, -// listenerName, visitorName, startRuleName, input, debug); -// } -// @Override public String execParser(String grammarFileName, String grammarStr, @@ -417,7 +81,7 @@ public String execParser(String grammarFileName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); rawBuildRecognizerTestFile(parserName, lexerName, listenerName, @@ -447,7 +111,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String... extraOptions) { ErrorQueue equeue = - antlrOnString(getTmpDir(), "Cpp", grammarFileName, grammarStr, defaultListener, extraOptions); + antlrOnString(getTempDirPath(), "Cpp", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; } @@ -481,7 +145,7 @@ protected void rawBuildRecognizerTestFile(String parserName, boolean debug, boolean trace) { - this.stderrDuringParse = null; + setParseErrors(null); if ( parserName==null ) { writeLexerTestFile(lexerName, false); } @@ -500,26 +164,6 @@ public String execRecognizer() { } - private static String detectedOS; - public static String getOS() { - if (detectedOS == null) { - String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); - if ((os.indexOf("mac") >= 0) || (os.indexOf("darwin") >= 0)) { - detectedOS = "mac"; - } - else if (os.indexOf("win") >= 0) { - detectedOS = "windows"; - } - else if (os.indexOf("nux") >= 0) { - detectedOS = "linux"; - } - else { - detectedOS = "unknown"; - } - } - return detectedOS; - } - public List allCppFiles(String path) { ArrayList files = new ArrayList(); File folder = new File(path); @@ -545,16 +189,16 @@ private String runProcess(ProcessBuilder builder, String description, boolean sh stderrVacuum.join(); String output = stdoutVacuum.toString(); if ( stderrVacuum.toString().length()>0 ) { - this.stderrDuringParse = stderrVacuum.toString(); - if ( showStderr ) System.err.println(this.stderrDuringParse); + setParseErrors(stderrVacuum.toString()); + if ( showStderr ) System.err.println(getParseErrors()); } if (errcode != 0) { String err = "execution of '"+description+"' failed with error code: "+errcode; - if ( this.stderrDuringParse!=null ) { - this.stderrDuringParse += err; + if ( getParseErrors()!=null ) { + setParseErrors(getParseErrors() + err); } else { - this.stderrDuringParse = err; + setParseErrors(err); } } @@ -621,15 +265,15 @@ private boolean buildRuntime() { public String execModule(String fileName) { String runtimePath = locateRuntime(); String includePath = runtimePath + "/runtime/src"; - String binPath = new File(new File(tmpdir), "a.out").getAbsolutePath(); - String inputPath = new File(new File(tmpdir), "input").getAbsolutePath(); + String binPath = new File(getTempTestDir(), "a.out").getAbsolutePath(); + String inputPath = new File(getTempTestDir(), "input").getAbsolutePath(); // Build runtime using cmake once. synchronized (runtimeBuiltOnce) { if ( !runtimeBuiltOnce ) { try { String command[] = {"clang++", "--version"}; - String output = runCommand(command, tmpdir, "printing compiler version", false); + String output = runCommand(command, getTempDirPath(), "printing compiler version", false); System.out.println("Compiler version is: "+output); } catch (Exception e) { @@ -649,7 +293,7 @@ public String execModule(String fileName) { String libExtension = (getOS().equals("mac")) ? "dylib" : "so"; try { String command[] = { "ln", "-s", runtimePath + "/dist/libantlr4-runtime." + libExtension }; - if (runCommand(command, tmpdir, "sym linking C++ runtime", true) == null) + if (runCommand(command, getTempDirPath(), "sym linking C++ runtime", true) == null) return null; } catch (Exception e) { @@ -660,8 +304,8 @@ public String execModule(String fileName) { try { List command2 = new ArrayList(Arrays.asList("clang++", "-std=c++11", "-I", includePath, "-L.", "-lantlr4-runtime", "-o", "a.out")); - command2.addAll(allCppFiles(tmpdir)); - if (runCommand(command2.toArray(new String[0]), tmpdir, "building test binary", true) == null) { + command2.addAll(allCppFiles(getTempDirPath())); + if (runCommand(command2.toArray(new String[0]), getTempDirPath(), "building test binary", true) == null) { return null; } } @@ -672,10 +316,10 @@ public String execModule(String fileName) { } // Now run the newly minted binary. Reset the error output, as we could have got compiler warnings which are not relevant here. - this.stderrDuringParse = null; + setParseErrors(null); try { ProcessBuilder builder = new ProcessBuilder(binPath, inputPath); - builder.directory(new File(tmpdir)); + builder.directory(getTempTestDir()); Map env = builder.environment(); env.put("LD_PRELOAD", runtimePath + "/dist/libantlr4-runtime." + libExtension); String output = runProcess(builder, "running test binary", false); @@ -717,151 +361,6 @@ protected String locateRuntime() { return p; } - List getMessagesOfType(List msgs, Class c) { - List filtered = new ArrayList(); - for (ANTLRMessage m : msgs) { - if ( m.getClass() == c ) filtered.add(m); - } - return filtered; - } - - void checkRuleATN(Grammar g, String ruleName, String expecting) { - ParserATNFactory f = new ParserATNFactory(g); - ATN atn = f.createATN(); - - DOTGenerator dot = new DOTGenerator(g); - System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule(ruleName).index])); - - Rule r = g.getRule(ruleName); - ATNState startState = atn.ruleToStartState[r.index]; - ATNPrinter serializer = new ATNPrinter(g, startState); - String result = serializer.asString(); - - //System.out.print(result); - assertEquals(expecting, result); - } - - public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException { - int lp = templates.indexOf('('); - String name = templates.substring(0, lp); - STGroup group = new STGroupString(templates); - ST st = group.getInstanceOf(name); - st.add(actionName, action); - String grammar = st.render(); - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(grammar, equeue); - if ( g.ast!=null && !g.ast.hasErrors ) { - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - - ATNFactory factory = new ParserATNFactory(g); - if ( g.isLexer() ) factory = new LexerATNFactory((LexerGrammar)g); - g.atn = factory.createATN(); - - CodeGenerator gen = new CodeGenerator(g); - ST outputFileST = gen.generateParser(); - String output = outputFileST.render(); - //System.out.println(output); - String b = "#" + actionName + "#"; - int start = output.indexOf(b); - String e = "#end-" + actionName + "#"; - int end = output.indexOf(e); - String snippet = output.substring(start+b.length(),end); - assertEquals(expected, snippet); - } - if ( equeue.size()>0 ) { - System.err.println(equeue.toString()); - } - } - - protected void checkGrammarSemanticsError(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception - { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if ( equeue.size()!=1 ) { - System.err.println(equeue); - } - } - - protected void checkGrammarSemanticsWarning(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception - { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.warnings.size(); i++) { - ANTLRMessage m = equeue.warnings.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if ( equeue.size()!=1 ) { - System.err.println(equeue); - } - } - - protected void checkError(ErrorQueue equeue, - ANTLRMessage expectedMessage) - throws Exception - { - //System.out.println("errors="+equeue); - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertTrue("no error; "+expectedMessage.getErrorType()+" expected", !equeue.errors.isEmpty()); - assertTrue("too many errors; "+equeue.errors, equeue.errors.size()<=1); - assertNotNull("couldn't find expected error: "+expectedMessage.getErrorType(), foundMsg); - /* - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - */ - assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs()); - } - - public static class FilteringTokenStream extends CommonTokenStream { - public FilteringTokenStream(TokenSource src) { super(src); } - Set hide = new HashSet(); - @Override - protected boolean sync(int i) { - if (!super.sync(i)) { - return false; - } - - Token t = get(i); - if ( hide.contains(t.getType()) ) { - ((WritableToken)t).setChannel(Token.HIDDEN_CHANNEL); - } - - return true; - } - public void setTokenTypeChannel(int ttype, int channel) { - hide.add(ttype); - } - } - - protected void mkdir(String dir) { - File f = new File(dir); - f.mkdirs(); - } - protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) { @@ -922,7 +421,7 @@ protected void writeParserTestFile(String parserName, String lexerName, outputFileST.add("listenerName", listenerName); outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.cpp", outputFileST.render()); + writeFile(getTempDirPath(), "Test.cpp", outputFileST.render()); } protected void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -947,215 +446,8 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { + " return 0;\n" + "}\n"); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.cpp", outputFileST.render()); + writeFile(getTempDirPath(), "Test.cpp", outputFileST.render()); } - public void writeRecognizer(String parserName, String lexerName, - String listenerName, String visitorName, - String parserStartRuleName, boolean debug, boolean trace) { - if ( parserName==null ) { - writeLexerTestFile(lexerName, debug); - } - else { - writeParserTestFile(parserName, - lexerName, - listenerName, - visitorName, - parserStartRuleName, - debug, - trace); - } - } - - - protected void eraseFiles(final String filesEndingWith) { - File tmpdirF = new File(tmpdir); - String[] files = tmpdirF.list(); - for(int i = 0; files!=null && i < files.length; i++) { - if ( files[i].endsWith(filesEndingWith) ) { - new File(tmpdir+"/"+files[i]).delete(); - } - } - } - - protected void eraseFiles(File dir) { - String[] files = dir.list(); - for(int i = 0; files!=null && i < files.length; i++) { - new File(dir,files[i]).delete(); - } - } - - @Override - public void eraseTempDir() { - if (shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseFiles(tmpdirF); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - if(tmpdir==null) - return false; - String propName = getPropertyPrefix() + "-erase-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) - return Boolean.getBoolean(prop); - else - return true; - } - - public String getFirstLineOfException() { - if ( this.stderrDuringParse ==null ) { - return null; - } - String[] lines = this.stderrDuringParse.split("\n"); - String prefix="Exception in thread \"main\" "; - return lines[0].substring(prefix.length(),lines[0].length()); - } - - /** - * When looking at a result set that consists of a Map/HashTable - * we cannot rely on the output order, as the hashing algorithm or other aspects - * of the implementation may be different on different JDKs or platforms. Hence - * we take the Map, convert the keys to a List, sort them and Stringify the Map, which is a - * bit of a hack, but guarantees that we get the same order on all systems. We assume that - * the keys are strings. - * - * @param m The Map that contains keys we wish to return in sorted order - * @return A string that represents all the keys in sorted order. - */ - public String sortMapToString(Map m) { - // Pass in crap, and get nothing back - // - if (m == null) { - return null; - } - - System.out.println("Map toString looks like: " + m.toString()); - - // Sort the keys in the Map - // - TreeMap nset = new TreeMap(m); - - System.out.println("Tree map looks like: " + nset.toString()); - return nset.toString(); - } - - public List realElements(List elements) { - return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size()); - } - - public void assertNotNullOrEmpty(String message, String text) { - assertNotNull(message, text); - assertFalse(message, text.isEmpty()); - } - - public void assertNotNullOrEmpty(String text) { - assertNotNull(text); - assertFalse(text.isEmpty()); - } - - public static class IntTokenStream implements TokenStream { - IntegerList types; - int p=0; - public IntTokenStream(IntegerList types) { this.types = types; } - - @Override - public void consume() { p++; } - - @Override - public int LA(int i) { return LT(i).getType(); } - - @Override - public int mark() { - return index(); - } - - @Override - public int index() { return p; } - - @Override - public void release(int marker) { - seek(marker); - } - - @Override - public void seek(int index) { - p = index; - } - - @Override - public int size() { - return types.size(); - } - - @Override - public String getSourceName() { - return null; - } - - @Override - public Token LT(int i) { - CommonToken t; - int rawIndex = p + i - 1; - if ( rawIndex>=types.size() ) t = new CommonToken(Token.EOF); - else t = new CommonToken(types.get(rawIndex)); - t.setTokenIndex(rawIndex); - return t; - } - - @Override - public Token get(int i) { - return new org.antlr.v4.runtime.CommonToken(types.get(i)); - } - - @Override - public TokenSource getTokenSource() { - return null; - } - - @Override - public String getText() { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Interval interval) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(RuleContext ctx) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Token start, Token stop) { - throw new UnsupportedOperationException("can't give strings"); - } - } - - /** Sort a list */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - - /** Return map sorted by key */ - public ,V> LinkedHashMap sort(Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java index 2cd12d015d..f6c4ab5c52 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java @@ -5,36 +5,14 @@ */ package org.antlr.v4.test.runtime.csharp; -import org.antlr.v4.Tool; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenSource; -import org.antlr.v4.runtime.WritableToken; import org.antlr.v4.runtime.misc.Utils; import org.antlr.v4.test.runtime.*; -import org.antlr.v4.tool.ANTLRMessage; -import org.antlr.v4.tool.GrammarSemanticsMessage; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.stringtemplate.v4.ST; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathFactory; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.nio.file.Path; @@ -53,134 +31,11 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -public class BaseCSharpTest implements RuntimeTestSupport { - public static final String newline = System.getProperty("line.separator"); - - /** - * When the {@code antlr.preserve-test-dir} runtime property is set to - * {@code true}, the temporary directories created by the test run will not - * be removed at the end of the test run, even for tests that completed - * successfully. - * - *

- * The default behavior (used in all other cases) is removing the temporary - * directories for all tests which completed successfully, and preserving - * the directories for tests which failed.

- */ - public static final boolean PRESERVE_TEST_DIR = Boolean.parseBoolean(System.getProperty("antlr-preserve-csharp-test-dir")); - - /** - * The base test directory is the directory where generated files get placed - * during unit test execution. - * - *

- * The default value for this property is the {@code java.io.tmpdir} system - * property, and can be overridden by setting the - * {@code antlr.java-test-dir} property to a custom location. Note that the - * {@code antlr.java-test-dir} property directly affects the - * {@link #CREATE_PER_TEST_DIRECTORIES} value as well.

- */ - public static final String BASE_TEST_DIR; - - /** - * When {@code true}, a temporary directory will be created for each test - * executed during the test run. - * - *

- * This value is {@code true} when the {@code antlr.java-test-dir} system - * property is set, and otherwise {@code false}.

- */ - public static final boolean CREATE_PER_TEST_DIRECTORIES; - - static { - String baseTestDir = System.getProperty("antlr-csharp-test-dir"); - boolean perTestDirectories = false; - if (baseTestDir == null || baseTestDir.isEmpty()) { - baseTestDir = System.getProperty("java.io.tmpdir"); - perTestDirectories = true; - } - - if (!new File(baseTestDir).isDirectory()) { - throw new UnsupportedOperationException("The specified base test directory does not exist: " + baseTestDir); - } - - BASE_TEST_DIR = baseTestDir; - CREATE_PER_TEST_DIRECTORIES = perTestDirectories; - } - - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** - * Errors found while running antlr - */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - @Override - public void testSetUp() throws Exception { - if (CREATE_PER_TEST_DIRECTORIES) { - // new output dir for each test - String testDirectory = getClass().getSimpleName() + "-" + Thread.currentThread().getName() + "-" + System.currentTimeMillis(); - tmpdir = new File(BASE_TEST_DIR, testDirectory).getAbsolutePath(); - } else { - tmpdir = new File(BASE_TEST_DIR).getAbsolutePath(); - if (!PRESERVE_TEST_DIR && new File(tmpdir).exists()) { - eraseDirectory(new File(tmpdir)); - } - } - antlrToolErrors = new StringBuilder(); - } +public class BaseCSharpTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - System.out.println(descriptor.getTestName()); - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if (antlrToolErrors.length() == 0) { - return null; - } - return antlrToolErrors.toString(); + protected String getPropertyPrefix() { + return "antlr4-csharp"; } protected String execLexer(String grammarFileName, @@ -201,12 +56,12 @@ public String execLexer(String grammarFileName, null, lexerName); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); addSourceFiles("Test.cs"); if (!compile()) { System.err.println("Failed to compile!"); - return stderrDuringParse; + return getParseErrors(); } String output = execTest(); if (output != null && output.length() == 0) { @@ -218,8 +73,7 @@ public String execLexer(String grammarFileName, Set sourceFiles = new HashSet<>(); private void addSourceFiles(String... files) { - for (String file : files) - this.sourceFiles.add(file); + Collections.addAll(sourceFiles, files); } @Override @@ -238,7 +92,7 @@ public String execParser(String grammarFileName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); return rawExecRecognizer(parserName, lexerName, startRuleName, @@ -265,7 +119,7 @@ protected boolean rawGenerateRecognizer(String grammarFileName, String lexerName, boolean defaultListener, String... extraOptions) { - ErrorQueue equeue = antlrOnString(getTmpDir(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions); + ErrorQueue equeue = antlrOnString(getTempDirPath(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; } @@ -295,7 +149,7 @@ protected String rawExecRecognizer(String parserName, String lexerName, String parserStartRuleName, boolean debug) { - this.stderrDuringParse = null; + setParseErrors(null); if (parserName == null) { writeLexerTestFile(lexerName, false); } else { @@ -330,14 +184,14 @@ public boolean compile() { } private String locateExec() { - return new File(tmpdir, "bin/Release/netcoreapp3.1/Test.dll").getAbsolutePath(); + return new File(getTempTestDir(), "bin/Release/netcoreapp3.1/Test.dll").getAbsolutePath(); } public boolean buildProject() { try { // save auxiliary files String pack = BaseCSharpTest.class.getPackage().getName().replace(".", "/") + "/"; - saveResourceAsFile(pack + "Antlr4.Test.csproj", new File(tmpdir, "Antlr4.Test.csproj")); + saveResourceAsFile(pack + "Antlr4.Test.csproj", new File(getTempTestDir(), "Antlr4.Test.csproj")); // find runtime package final ClassLoader loader = Thread.currentThread().getContextClassLoader(); @@ -356,7 +210,7 @@ public boolean buildProject() { "reference", runtimeProjPath }; - boolean success = runProcess(args, tmpdir); + boolean success = runProcess(args, getTempDirPath()); assertTrue(success); // build test @@ -367,7 +221,7 @@ public boolean buildProject() { "-c", "Release" }; - success = runProcess(args, tmpdir); + success = runProcess(args, getTempDirPath()); assertTrue(success); } catch (Exception e) { e.printStackTrace(System.err); @@ -395,11 +249,11 @@ private boolean runProcess(String[] args, String path, int retries) throws Excep int exitValue = process.exitValue(); boolean success = (exitValue == 0); if (!success) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); System.err.println("runProcess command: " + Utils.join(args, " ")); System.err.println("runProcess exitValue: " + exitValue); System.err.println("runProcess stdoutVacuum: " + stdoutVacuum.toString()); - System.err.println("runProcess stderrVacuum: " + stderrDuringParse); + System.err.println("runProcess stderrVacuum: " + getParseErrors()); } if (exitValue == 132) { // Retry after SIGILL. We are seeing this intermittently on @@ -434,7 +288,7 @@ private void saveResourceAsFile(String resourceName, File file) throws IOExcepti public String execTest() { String exec = locateExec(); try { - File tmpdirFile = new File(tmpdir); + File tmpdirFile = new File(getTempDirPath()); Path output = tmpdirFile.toPath().resolve("output"); Path errorOutput = tmpdirFile.toPath().resolve("error-output"); String[] args = getExecTestArgs(exec, output, errorOutput); @@ -449,7 +303,7 @@ public String execTest() { stdoutVacuum.join(); stderrVacuum.join(); String writtenOutput = TestOutputReading.read(output); - this.stderrDuringParse = TestOutputReading.read(errorOutput); + setParseErrors(TestOutputReading.read(errorOutput)); int exitValue = process.exitValue(); String stdoutString = stdoutVacuum.toString().trim(); String stderrString = stderrVacuum.toString().trim(); @@ -473,7 +327,7 @@ public String execTest() { private String[] getExecTestArgs(String exec, Path output, Path errorOutput) { return new String[]{ - "dotnet", exec, new File(tmpdir, "input").getAbsolutePath(), + "dotnet", exec, new File(getTempTestDir(), "input").getAbsolutePath(), output.toAbsolutePath().toString(), errorOutput.toAbsolutePath().toString() }; @@ -533,7 +387,7 @@ protected void writeParserTestFile(String parserName, outputFileST.add("parserName", parserName); outputFileST.add("lexerName", lexerName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.cs", outputFileST.render()); + writeFile(getTempDirPath(), "Test.cs", outputFileST.render()); } protected void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -562,36 +416,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { ); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.cs", outputFileST.render()); - } - - protected void eraseDirectory(File dir) { - File[] files = dir.listFiles(); - if (files != null) { - for (File file : files) { - if (file.isDirectory()) { - eraseDirectory(file); - } else { - file.delete(); - } - } - } - dir.delete(); - } - - @Override - public void eraseTempDir() { - if (shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseDirectory(tmpdirF); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - return tmpdir!=null && !PRESERVE_TEST_DIR; + writeFile(getTempDirPath(), "Test.cs", outputFileST.render()); } /** @@ -599,8 +424,7 @@ private boolean shouldEraseTempDir() { */ public , V> LinkedHashMap sort(Map data) { LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); + List keys = new ArrayList(data.keySet()); Collections.sort(keys); for (K k : keys) { dup.put(k, data.get(k)); diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java index 5bedebde29..1848cfa46d 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/dart/BaseDartTest.java @@ -6,39 +6,14 @@ package org.antlr.v4.test.runtime.dart; -import org.antlr.v4.Tool; -import org.antlr.v4.analysis.AnalysisPipeline; -import org.antlr.v4.automata.ATNFactory; -import org.antlr.v4.automata.ATNPrinter; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.misc.Utils; -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntegerList; -import org.antlr.v4.runtime.misc.Interval; -import org.antlr.v4.runtime.misc.Pair; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.*; import org.antlr.v4.test.runtime.descriptors.LexerExecDescriptors; import org.antlr.v4.test.runtime.descriptors.PerformanceDescriptors; -import org.antlr.v4.tool.*; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.stringtemplate.v4.ST; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STGroupString; import java.io.*; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.URL; -import java.net.URLClassLoader; import java.util.*; import static junit.framework.TestCase.*; @@ -47,352 +22,19 @@ import static org.junit.Assert.assertArrayEquals; -public class BaseDartTest implements RuntimeTestSupport { +public class BaseDartTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { + private static final List AOT_COMPILE_TESTS = Arrays.asList( new PerformanceDescriptors.DropLoopEntryBranchInLRRule_4().input, new LexerExecDescriptors.LargeLexer().input ); - public static final String newline = System.getProperty("line.separator"); - public static final String pathSep = System.getProperty("path.separator"); - - - /** - * When the {@code antlr.preserve-test-dir} runtime property is set to - * {@code true}, the temporary directories created by the test run will not - * be removed at the end of the test run, even for tests that completed - * successfully. - *

- *

- * The default behavior (used in all other cases) is removing the temporary - * directories for all tests which completed successfully, and preserving - * the directories for tests which failed.

- */ - public static final boolean PRESERVE_TEST_DIR = Boolean.parseBoolean(System.getProperty("antlr.preserve-test-dir", "false")); - - /** - * The base test directory is the directory where generated files get placed - * during unit test execution. - *

- *

- * The default value for this property is the {@code java.io.tmpdir} system - * property, and can be overridden by setting the - * {@code antlr.java-test-dir} property to a custom location. Note that the - * {@code antlr.java-test-dir} property directly affects the - * {@link #CREATE_PER_TEST_DIRECTORIES} value as well.

- */ - public static final String BASE_TEST_DIR; - - /** - * When {@code true}, a temporary directory will be created for each test - * executed during the test run. - *

- *

- * This value is {@code true} when the {@code antlr.java-test-dir} system - * property is set, and otherwise {@code false}.

- */ - public static final boolean CREATE_PER_TEST_DIRECTORIES; - - static { - String baseTestDir = System.getProperty("antlr.dart-test-dir"); - boolean perTestDirectories = false; - if (baseTestDir == null || baseTestDir.isEmpty()) { - baseTestDir = System.getProperty("java.io.tmpdir"); - perTestDirectories = true; - } - - if (!new File(baseTestDir).isDirectory()) { - throw new UnsupportedOperationException("The specified base test directory does not exist: " + baseTestDir); - } - - BASE_TEST_DIR = baseTestDir; - CREATE_PER_TEST_DIRECTORIES = perTestDirectories; - } - - /** - * Build up the full classpath we need, including the surefire path (if present) - */ - public static final String CLASSPATH = System.getProperty("java.class.path"); - - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** - * Errors found while running antlr - */ - protected StringBuilder antlrToolErrors; - private static String cacheDartPackages; - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - private String getPropertyPrefix() { + public String getPropertyPrefix() { return "antlr-dart"; } - @Override - public void testSetUp() throws Exception { - if (CREATE_PER_TEST_DIRECTORIES) { - // new output dir for each test - String threadName = Thread.currentThread().getName(); - String testDirectory = getClass().getSimpleName() + "-" + threadName + "-" + System.nanoTime(); - tmpdir = new File(BASE_TEST_DIR, testDirectory).getAbsolutePath(); - } else { - tmpdir = new File(BASE_TEST_DIR).getAbsolutePath(); - if (!PRESERVE_TEST_DIR && new File(tmpdir).exists()) { - eraseFiles(); - } - } - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if (antlrToolErrors.length() == 0) { - return null; - } - return antlrToolErrors.toString(); - } - - protected Tool newTool(String[] args) { - Tool tool = new Tool(args); - return tool; - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if (g.atn == null) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if (g.isLexer()) { - f = new LexerATNFactory((LexerGrammar) g); - } else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if (g.ast != null && !g.ast.hasErrors) { -// System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if (g.getImportedGrammars() != null) { // process imported grammars (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } - } - - public DFA createDFA(Grammar g, DecisionState s) { -// PredictionDFAFactory conv = new PredictionDFAFactory(g, s); -// DFA dfa = conv.createDFA(); -// conv.issueAmbiguityWarnings(); -// System.out.print("DFA="+dfa); -// return dfa; - return null; - } - -// public void minimizeDFA(DFA dfa) { -// DFAMinimizer dmin = new DFAMinimizer(dfa); -// dfa.minimized = dmin.minimize(); -// } - - IntegerList getTypesFromString(Grammar g, String expecting) { - IntegerList expectingTokenTypes = new IntegerList(); - if (expecting != null && !expecting.trim().isEmpty()) { - for (String tname : expecting.replace(" ", "").split(",")) { - int ttype = g.getTokenType(tname); - expectingTokenTypes.add(ttype); - } - } - return expectingTokenTypes; - } - - public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { - ANTLRInputStream in = new ANTLRInputStream(input); - IntegerList tokenTypes = new IntegerList(); - int ttype; - do { - ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); - tokenTypes.add(ttype); - } while (ttype != Token.EOF); - return tokenTypes; - } - - public List getTokenTypes(LexerGrammar lg, - ATN atn, - CharStream input) { - LexerATNSimulator interp = new LexerATNSimulator(atn, new DFA[]{new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE))}, null); - List tokenTypes = new ArrayList(); - int ttype; - boolean hitEOF = false; - do { - if (hitEOF) { - tokenTypes.add("EOF"); - break; - } - int t = input.LA(1); - ttype = interp.match(input, Lexer.DEFAULT_MODE); - if (ttype == Token.EOF) { - tokenTypes.add("EOF"); - } else { - tokenTypes.add(lg.typeToTokenList.get(ttype)); - } - - if (t == IntStream.EOF) { - hitEOF = true; - } - } while (ttype != Token.EOF); - return tokenTypes; - } - - List checkRuleDFA(String gtext, String ruleName, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - ATNState s = atn.ruleToStartState[g.getRule(ruleName).index]; - if (s == null) { - System.err.println("no such rule: " + ruleName); - return null; - } - ATNState t = s.transition(0).target; - if (!(t instanceof DecisionState)) { - System.out.println(ruleName + " has no decision"); - return null; - } - DecisionState blk = (DecisionState) t; - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - List checkRuleDFA(String gtext, int decision, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - DecisionState blk = atn.decisionToState.get(decision); - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - void checkRuleDFA(Grammar g, DecisionState blk, String expecting) - throws Exception { - DFA dfa = createDFA(g, blk); - String result = null; - if (dfa != null) result = dfa.toString(); - assertEquals(expecting, result); - } - - List checkLexerDFA(String gtext, String expecting) - throws Exception { - return checkLexerDFA(gtext, LexerGrammar.DEFAULT_MODE_NAME, expecting); - } - - List checkLexerDFA(String gtext, String modeName, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - LexerGrammar g = new LexerGrammar(gtext, equeue); - g.atn = createATN(g, false); -// LexerATNToDFAConverter conv = new LexerATNToDFAConverter(g); -// DFA dfa = conv.createDFA(modeName); -// g.setLookaheadDFA(0, dfa); // only one decision to worry about -// -// String result = null; -// if ( dfa!=null ) result = dfa.toString(); -// assertEquals(expecting, result); -// -// return equeue.all; - return null; - } - - protected String load(String fileName, String encoding) - throws IOException { - if (fileName == null) { - return null; - } - - String fullFileName = getClass().getPackage().getName().replace('.', '/') + '/' + fileName; - int size = 65000; - InputStreamReader isr; - InputStream fis = getClass().getClassLoader().getResourceAsStream(fullFileName); - if (encoding != null) { - isr = new InputStreamReader(fis, encoding); - } else { - isr = new InputStreamReader(fis); - } - try { - char[] data = new char[size]; - int n = isr.read(data); - return new String(data, 0, n); - } finally { - isr.close(); - } - } - - protected String execLexer(String grammarFileName, - String grammarStr, - String lexerName, - String input) { - return execLexer(grammarFileName, grammarStr, lexerName, input, false); - } - @Override public String execLexer(String grammarFileName, String grammarStr, @@ -404,71 +46,12 @@ public String execLexer(String grammarFileName, null, lexerName); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); String output = execClass("Test", AOT_COMPILE_TESTS.contains(input)); return output; } - public ParseTree execParser(String startRuleName, String input, - String parserName, String lexerName) - throws Exception { - Pair pl = getParserAndLexer(input, parserName, lexerName); - Parser parser = pl.a; - return execStartRule(startRuleName, parser); - } - - public ParseTree execStartRule(String startRuleName, Parser parser) - throws IllegalAccessException, InvocationTargetException, - NoSuchMethodException { - Method startRule = null; - Object[] args = null; - try { - startRule = parser.getClass().getMethod(startRuleName); - } catch (NoSuchMethodException nsme) { - // try with int _p arg for recursive func - startRule = parser.getClass().getMethod(startRuleName, int.class); - args = new Integer[]{0}; - } - ParseTree result = (ParseTree) startRule.invoke(parser, args); -// System.out.println("parse tree = "+result.toStringTree(parser)); - return result; - } - - public Pair getParserAndLexer(String input, - String parserName, String lexerName) - throws Exception { - final Class lexerClass = loadLexerClassFromTempDir(lexerName); - final Class parserClass = loadParserClassFromTempDir(parserName); - - ANTLRInputStream in = new ANTLRInputStream(new StringReader(input)); - - Class c = lexerClass.asSubclass(Lexer.class); - Constructor ctor = c.getConstructor(CharStream.class); - Lexer lexer = ctor.newInstance(in); - - Class pc = parserClass.asSubclass(Parser.class); - Constructor pctor = pc.getConstructor(TokenStream.class); - CommonTokenStream tokens = new CommonTokenStream(lexer); - Parser parser = pctor.newInstance(tokens); - return new Pair(parser, lexer); - } - - public Class loadClassFromTempDir(String name) throws Exception { - ClassLoader loader = - new URLClassLoader(new URL[]{new File(tmpdir).toURI().toURL()}, - ClassLoader.getSystemClassLoader()); - return loader.loadClass(name); - } - - public Class loadLexerClassFromTempDir(String name) throws Exception { - return loadClassFromTempDir(name).asSubclass(Lexer.class); - } - - public Class loadParserClassFromTempDir(String name) throws Exception { - return loadClassFromTempDir(name).asSubclass(Parser.class); - } - @Override public String execParser(String grammarFileName, String grammarStr, @@ -499,7 +82,7 @@ public String execParser(String grammarFileName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); return rawExecRecognizer(parserName, lexerName, startRuleName, @@ -529,7 +112,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, boolean defaultListener, String... extraOptions) { ErrorQueue equeue = - BaseRuntimeTest.antlrOnString(getTmpDir(), "Dart", grammarFileName, grammarStr, defaultListener, extraOptions); + BaseRuntimeTest.antlrOnString(getTempDirPath(), "Dart", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; } @@ -553,14 +136,14 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, } String runtime = locateRuntime(); - writeFile(tmpdir, "pubspec.yaml", + writeFile(getTempDirPath(), "pubspec.yaml", "name: \"test\"\n" + "dependencies:\n" + " antlr4:\n" + " path: " + runtime + "\n"); if (cacheDartPackages == null) { try { - final Process process = Runtime.getRuntime().exec(new String[]{locatePub(), "get"}, null, new File(tmpdir)); + final Process process = Runtime.getRuntime().exec(new String[]{locatePub(), "get"}, null, getTempTestDir()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stderrVacuum.start(); Timer timer = new Timer(); @@ -585,9 +168,9 @@ public void run() { e.printStackTrace(); return false; } - cacheDartPackages = readFile(tmpdir, ".packages"); + cacheDartPackages = readFile(getTempDirPath(), ".packages"); } else { - writeFile(tmpdir, ".packages", cacheDartPackages); + writeFile(getTempDirPath(), ".packages", cacheDartPackages); } return true; // allIsWell: no compile } @@ -598,7 +181,7 @@ protected String rawExecRecognizer(String parserName, boolean debug, boolean profile, boolean aotCompile) { - this.stderrDuringParse = null; + setParseErrors(null); if (parserName == null) { writeLexerTestFile(lexerName, false); } else { @@ -622,7 +205,7 @@ public String execClass(String className, boolean compile) { String cmdLine = Utils.join(args, " "); System.err.println("Compile: " + cmdLine); final Process process = - Runtime.getRuntime().exec(args, null, new File(tmpdir)); + Runtime.getRuntime().exec(args, null, getTempTestDir()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stderrVacuum.start(); Timer timer = new Timer(); @@ -647,18 +230,18 @@ public void run() { String[] args; if (compile) { args = new String[]{ - new File(tmpdir, className).getAbsolutePath(), new File(tmpdir, "input").getAbsolutePath() + new File(getTempTestDir(), className).getAbsolutePath(), new File(getTempTestDir(), "input").getAbsolutePath() }; } else { args = new String[]{ locateDart(), - className + ".dart", new File(tmpdir, "input").getAbsolutePath() + className + ".dart", new File(getTempTestDir(), "input").getAbsolutePath() }; } //String cmdLine = Utils.join(args, " "); //System.err.println("execParser: " + cmdLine); final Process process = - Runtime.getRuntime().exec(args, null, new File(tmpdir)); + Runtime.getRuntime().exec(args, null, getTempTestDir()); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stdoutVacuum.start(); @@ -683,7 +266,7 @@ public void run() { output = null; } if (stderrVacuum.toString().length() > 0) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } catch (Exception e) { @@ -786,187 +369,6 @@ private String locateRuntime() { return runtimeSrc.getPath(); } - private boolean isWindows() { - return System.getProperty("os.name").toLowerCase().contains("windows"); - } - -// void ambig(List msgs, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// ambig(msgs, 0, expectedAmbigAlts, expectedAmbigInput); -// } - -// void ambig(List msgs, int i, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, AmbiguityMessage.class); -// AmbiguityMessage a = (AmbiguityMessage)amsgs.get(i); -// if ( a==null ) assertNull(expectedAmbigAlts); -// else { -// assertEquals(a.conflictingAlts.toString(), Arrays.toString(expectedAmbigAlts)); -// } -// assertEquals(expectedAmbigInput, a.input); -// } - -// void unreachable(List msgs, int[] expectedUnreachableAlts) -// throws Exception -// { -// unreachable(msgs, 0, expectedUnreachableAlts); -// } - -// void unreachable(List msgs, int i, int[] expectedUnreachableAlts) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, UnreachableAltsMessage.class); -// UnreachableAltsMessage u = (UnreachableAltsMessage)amsgs.get(i); -// if ( u==null ) assertNull(expectedUnreachableAlts); -// else { -// assertEquals(u.conflictingAlts.toString(), Arrays.toString(expectedUnreachableAlts)); -// } -// } - - List getMessagesOfType(List msgs, Class c) { - List filtered = new ArrayList(); - for (ANTLRMessage m : msgs) { - if (m.getClass() == c) filtered.add(m); - } - return filtered; - } - - public void checkRuleATN(Grammar g, String ruleName, String expecting) { -// DOTGenerator dot = new DOTGenerator(g); -// System.out.println(dot.getDOT(g.atn.ruleToStartState[g.getRule(ruleName).index])); - - Rule r = g.getRule(ruleName); - ATNState startState = g.getATN().ruleToStartState[r.index]; - ATNPrinter serializer = new ATNPrinter(g, startState); - String result = serializer.asString(); - - //System.out.print(result); - assertEquals(expecting, result); - } - - public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException { - int lp = templates.indexOf('('); - String name = templates.substring(0, lp); - STGroup group = new STGroupString(templates); - ST st = group.getInstanceOf(name); - st.add(actionName, action); - String grammar = st.render(); - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(grammar, equeue); - if (g.ast != null && !g.ast.hasErrors) { - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - - ATNFactory factory = new ParserATNFactory(g); - if (g.isLexer()) factory = new LexerATNFactory((LexerGrammar) g); - g.atn = factory.createATN(); - - AnalysisPipeline anal = new AnalysisPipeline(g); - anal.process(); - - CodeGenerator gen = new CodeGenerator(g); - ST outputFileST = gen.generateParser(false); - String output = outputFileST.render(); - //System.out.println(output); - String b = "#" + actionName + "#"; - int start = output.indexOf(b); - String e = "#end-" + actionName + "#"; - int end = output.indexOf(e); - String snippet = output.substring(start + b.length(), end); - assertEquals(expected, snippet); - } - if (equeue.size() > 0) { -// System.err.println(equeue.toString()); - } - } - - protected void checkGrammarSemanticsError(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertNotNull("no error; " + expectedMessage.getErrorType() + " expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if (equeue.size() != 1) { - System.err.println(equeue); - } - } - - protected void checkGrammarSemanticsWarning(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.warnings.size(); i++) { - ANTLRMessage m = equeue.warnings.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertNotNull("no error; " + expectedMessage.getErrorType() + " expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if (equeue.size() != 1) { - System.err.println(equeue); - } - } - - protected void checkError(ErrorQueue equeue, - ANTLRMessage expectedMessage) - throws Exception { - //System.out.println("errors="+equeue); - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertTrue("no error; " + expectedMessage.getErrorType() + " expected", !equeue.errors.isEmpty()); - assertTrue("too many errors; " + equeue.errors, equeue.errors.size() <= 1); - assertNotNull("couldn't find expected error: " + expectedMessage.getErrorType(), foundMsg); - /* - * assertTrue("error is not a GrammarSemanticsMessage", foundMsg - * instanceof GrammarSemanticsMessage); - */ - assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs()); - } - - public static class FilteringTokenStream extends CommonTokenStream { - public FilteringTokenStream(TokenSource src) { - super(src); - } - - Set hide = new HashSet(); - - @Override - protected boolean sync(int i) { - if (!super.sync(i)) { - return false; - } - - Token t = get(i); - if (hide.contains(t.getType())) { - ((WritableToken) t).setChannel(Token.HIDDEN_CHANNEL); - } - - return true; - } - - public void setTokenTypeChannel(int ttype, int channel) { - hide.add(ttype); - } - } - protected void writeTestFile(String parserName, String lexerName, String parserStartRuleName, @@ -1026,7 +428,7 @@ protected void writeTestFile(String parserName, outputFileST.add("parserName", parserName); outputFileST.add("lexerName", lexerName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.dart", outputFileST.render()); + writeFile(getTempDirPath(), "Test.dart", outputFileST.render()); } protected void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -1050,197 +452,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { ); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.dart", outputFileST.render()); - } - - protected void eraseFiles() { - if (tmpdir == null) { - return; - } - - File tmpdirF = new File(tmpdir); - String[] files = tmpdirF.list(); - for (int i = 0; files != null && i < files.length; i++) { - new File(tmpdir + "/" + files[i]).delete(); - } - } - - @Override - public void eraseTempDir() { - if(shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseFiles(); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - return tmpdir!=null; - } - - public String getFirstLineOfException() { - if (this.stderrDuringParse == null) { - return null; - } - String[] lines = this.stderrDuringParse.split("\n"); - String prefix = "Exception in thread \"main\" "; - return lines[0].substring(prefix.length(), lines[0].length()); - } - - /** - * When looking at a result set that consists of a Map/HashTable - * we cannot rely on the output order, as the hashing algorithm or other aspects - * of the implementation may be different on differnt JDKs or platforms. Hence - * we take the Map, convert the keys to a List, sort them and Stringify the Map, which is a - * bit of a hack, but guarantees that we get the same order on all systems. We assume that - * the keys are strings. - * - * @param m The Map that contains keys we wish to return in sorted order - * @return A string that represents all the keys in sorted order. - */ - public String sortMapToString(Map m) { - // Pass in crap, and get nothing back - // - if (m == null) { - return null; - } - - System.out.println("Map toString looks like: " + m.toString()); - - // Sort the keys in the Map - // - TreeMap nset = new TreeMap(m); - - System.out.println("Tree map looks like: " + nset.toString()); - return nset.toString(); - } - - public List realElements(List elements) { - return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size()); - } - - public void assertNotNullOrEmpty(String message, String text) { - assertNotNull(message, text); - assertFalse(message, text.isEmpty()); - } - - public void assertNotNullOrEmpty(String text) { - assertNotNull(text); - assertFalse(text.isEmpty()); + writeFile(getTempDirPath(), "Test.dart", outputFileST.render()); } - public static class IntTokenStream implements TokenStream { - public IntegerList types; - int p = 0; - - public IntTokenStream(IntegerList types) { - this.types = types; - } - - @Override - public void consume() { - p++; - } - - @Override - public int LA(int i) { - return LT(i).getType(); - } - - @Override - public int mark() { - return index(); - } - - @Override - public int index() { - return p; - } - - @Override - public void release(int marker) { - seek(marker); - } - - @Override - public void seek(int index) { - p = index; - } - - @Override - public int size() { - return types.size(); - } - - @Override - public String getSourceName() { - return UNKNOWN_SOURCE_NAME; - } - - @Override - public Token LT(int i) { - CommonToken t; - int rawIndex = p + i - 1; - if (rawIndex >= types.size()) t = new CommonToken(Token.EOF); - else t = new CommonToken(types.get(rawIndex)); - t.setTokenIndex(rawIndex); - return t; - } - - @Override - public Token get(int i) { - return new CommonToken(types.get(i)); - } - - @Override - public TokenSource getTokenSource() { - return null; - } - - @Override - public String getText() { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Interval interval) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(RuleContext ctx) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Token start, Token stop) { - throw new UnsupportedOperationException("can't give strings"); - } - } - - /** - * Sort a list - */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - - /** - * Return map sorted by key - */ - public , V> LinkedHashMap sort(Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java index 1696d5307b..1ff93d1fae 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java @@ -5,107 +5,36 @@ */ package org.antlr.v4.test.runtime.go; -import org.antlr.v4.Tool; -import org.antlr.v4.automata.ATNFactory; -import org.antlr.v4.automata.ATNPrinter; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.codegen.CodeGenerator; -import org.antlr.v4.runtime.ANTLRInputStream; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CommonToken; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.IntStream; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.RuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenSource; -import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.WritableToken; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; -import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.LexerATNSimulator; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntegerList; -import org.antlr.v4.runtime.misc.Interval; -import org.antlr.v4.semantics.SemanticPipeline; -import org.antlr.v4.test.runtime.ErrorQueue; -import org.antlr.v4.test.runtime.RuntimeTestDescriptor; -import org.antlr.v4.test.runtime.RuntimeTestSupport; -import org.antlr.v4.test.runtime.StreamVacuum; -import org.antlr.v4.tool.ANTLRMessage; -import org.antlr.v4.tool.DOTGenerator; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.GrammarSemanticsMessage; -import org.antlr.v4.tool.LexerGrammar; -import org.antlr.v4.tool.Rule; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; + +import org.antlr.v4.test.runtime.*; import org.stringtemplate.v4.ST; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STGroupString; import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertNotNull; -import static junit.framework.TestCase.assertTrue; + +import static junit.framework.TestCase.*; import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; -import static org.junit.Assert.assertArrayEquals; -public class BaseGoTest implements RuntimeTestSupport { - public File overall_tmpdir = null; - public File tmpdir = null; // this is where the parser package is stored, typically inside the tmpdir +public class BaseGoTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { + private static File tmpGopath = null; private static final String GO_RUNTIME_IMPORT_PATH = "github.com/antlr/antlr4/runtime/Go/antlr"; // TODO: Change this before merging with upstream - /** - * If error during parser execution, store stderr here; can't return stdout - * and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** Errors found while running antlr */ - protected StringBuilder antlrToolErrors; + private File parserTempDir; // "parser" with tempDir - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; + @Override + protected String getPropertyPrefix() { + return "antlr4-go"; + } /** * Copies all files from go runtime to a temporary folder that is inside a valid GOPATH project structure. */ public static void groupSetUp() throws Exception { - tmpGopath = new File(System.getProperty("java.io.tmpdir"), "antlr-goruntime-tmpgopath-" - + Long.toHexString(System.currentTimeMillis())); + tmpGopath = new File(System.getProperty("java.io.tmpdir"), "antlr-goruntime-tmpgopath-" + Long.toHexString(System.currentTimeMillis())); ArrayList pathsegments = new ArrayList(); pathsegments.add("src"); @@ -125,47 +54,12 @@ public static void groupSetUp() throws Exception { } for (File runtimeFile : runtimeFiles) { File dest = new File(tmpPackageDir, runtimeFile.getName()); - copyFile(runtimeFile, dest); + RuntimeTestUtils.copyFile(runtimeFile, dest); } cacheGoRuntime(tmpPackageDir); } - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir.getPath(); - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if ( antlrToolErrors.length()==0 ) { - return null; - } - return antlrToolErrors.toString(); - } - public static void groupTearDown() throws Exception { eraseDirectory(tmpGopath); } @@ -186,139 +80,22 @@ private static void cacheGoRuntime(File tmpPackageDir) throws Exception { } } - private static void copyFile(File source, File dest) throws IOException { - InputStream is = new FileInputStream(source); - OutputStream os = new FileOutputStream(dest); - byte[] buf = new byte[4 << 10]; - int l; - while ((l = is.read(buf)) > -1) { - os.write(buf, 0, l); - } - is.close(); - os.close(); - } - public void testSetUp() throws Exception { - // new output dir for each test - String prop = System.getProperty("antlr-go-test-dir"); - if (prop != null && prop.length() > 0) { - overall_tmpdir = new File(prop); - } - else { - String threadName = Thread.currentThread().getName(); - overall_tmpdir = new File(System.getProperty("java.io.tmpdir"), - getClass().getSimpleName()+"-"+threadName+"-"+System.currentTimeMillis()); - } - - if ( overall_tmpdir.exists()) - this.eraseDirectory(overall_tmpdir); - - tmpdir = new File(overall_tmpdir, "parser"); - - if ( tmpdir.exists()) { - this.eraseDirectory(tmpdir); - } - antlrToolErrors = new StringBuilder(); - } - - protected org.antlr.v4.Tool newTool(String[] args) { - return new Tool(args); - } - - protected Tool newTool() { - return new Tool(new String[]{"-o", tmpdir.getPath()}); + eraseParserTempDir(); + super.testSetUp(); + parserTempDir = new File(getTempTestDir(), "parser"); } - protected ATN createATN(Grammar g, boolean useSerializer) { - if (g.atn == null) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if (g.isLexer()) { - f = new LexerATNFactory((LexerGrammar) g); - } - else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if (g.ast != null && !g.ast.hasErrors) { - System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if (g.getImportedGrammars() != null) { // process imported grammars - // (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } + @Override + public File getTempParserDir() { + return parserTempDir; } - IntegerList getTypesFromString(Grammar g, String expecting) { - IntegerList expectingTokenTypes = new IntegerList(); - if (expecting != null && !expecting.trim().isEmpty()) { - for (String tname : expecting.replace(" ", "").split(",")) { - int ttype = g.getTokenType(tname); - expectingTokenTypes.add(ttype); - } + private void eraseParserTempDir() { + if(parserTempDir != null) { + eraseDirectory(parserTempDir); + parserTempDir = null; } - return expectingTokenTypes; - } - - public IntegerList getTokenTypesViaATN(String input, - LexerATNSimulator lexerATN) { - ANTLRInputStream in = new ANTLRInputStream(input); - IntegerList tokenTypes = new IntegerList(); - int ttype; - do { - ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); - tokenTypes.add(ttype); - } while (ttype != Token.EOF); - return tokenTypes; - } - - public List getTokenTypes(LexerGrammar lg, ATN atn, CharStream input) { - LexerATNSimulator interp = new LexerATNSimulator(atn, - new DFA[] { new DFA( - atn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, null); - List tokenTypes = new ArrayList(); - int ttype; - boolean hitEOF = false; - do { - if (hitEOF) { - tokenTypes.add("EOF"); - break; - } - int t = input.LA(1); - ttype = interp.match(input, Lexer.DEFAULT_MODE); - if (ttype == Token.EOF) { - tokenTypes.add("EOF"); - } - else { - tokenTypes.add(lg.typeToTokenList.get(ttype)); - } - - if (t == IntStream.EOF) { - hitEOF = true; - } - } while (ttype != Token.EOF); - return tokenTypes; } protected String execLexer(String grammarFileName, String grammarStr, @@ -332,25 +109,10 @@ public String execLexer(String grammarFileName, String grammarStr, boolean success = rawGenerateAndBuildRecognizer(grammarFileName, grammarStr, null, lexerName, "-no-listener"); assertTrue(success); - writeFile(overall_tmpdir.toString(), "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); - String output = execModule("Test.go"); - return output; + return execModule("Test.go"); } -// -// public String execParser(String grammarFileName, String grammarStr, -// String parserName, String lexerName, String listenerName, -// String visitorName, String startRuleName, String input, -// boolean debug) -// { -// boolean success = rawGenerateAndBuildRecognizer(grammarFileName, -// grammarStr, parserName, lexerName, "-visitor"); -// assertTrue(success); -// writeFile(overall_tmpdir, "input", input); -// rawBuildRecognizerTestFile(parserName, lexerName, listenerName, -// visitorName, startRuleName, debug); -// return execRecognizer(); -// } @Override public String execParser(String grammarFileName, String grammarStr, @@ -361,7 +123,7 @@ public String execParser(String grammarFileName, String grammarStr, boolean success = rawGenerateAndBuildRecognizer(grammarFileName, grammarStr, parserName, lexerName, "-visitor"); assertTrue(success); - writeFile(overall_tmpdir.toString(), "input", input); + writeFile(getTempDirPath(), "input", input); rawBuildRecognizerTestFile(parserName, lexerName, listenerName, visitorName, startRuleName, showDiagnosticErrors); return execRecognizer(); @@ -379,7 +141,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) { - ErrorQueue equeue = antlrOnString(getTmpDir(), "Go", grammarFileName, grammarStr, + ErrorQueue equeue = antlrOnString(getTempParserDirPath(), "Go", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; @@ -390,7 +152,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, protected void rawBuildRecognizerTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) { - this.stderrDuringParse = null; + setParseErrors(null); if (parserName == null) { writeLexerTestFile(lexerName, false); } @@ -406,12 +168,12 @@ public String execRecognizer() { public String execModule(String fileName) { String goExecutable = locateGo(); - String modulePath = new File(overall_tmpdir, fileName).getAbsolutePath(); - String inputPath = new File(overall_tmpdir, "input").getAbsolutePath(); + String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath(); + String inputPath = new File(getTempTestDir(), "input").getAbsolutePath(); try { ProcessBuilder builder = new ProcessBuilder(goExecutable, "run", modulePath, inputPath); builder.environment().put("GOPATH", tmpGopath.getPath()); - builder.directory(overall_tmpdir); + builder.directory(getTempTestDir()); Process process = builder.start(); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); @@ -425,7 +187,7 @@ public String execModule(String fileName) { output = null; } if (stderrVacuum.toString().length() > 0) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } @@ -492,203 +254,6 @@ private static File locateRuntime() { return runtimeDir; } - // void ambig(List msgs, int[] expectedAmbigAlts, String - // expectedAmbigInput) - // throws Exception - // { - // ambig(msgs, 0, expectedAmbigAlts, expectedAmbigInput); - // } - - // void ambig(List msgs, int i, int[] expectedAmbigAlts, String - // expectedAmbigInput) - // throws Exception - // { - // List amsgs = getMessagesOfType(msgs, AmbiguityMessage.class); - // AmbiguityMessage a = (AmbiguityMessage)amsgs.get(i); - // if ( a==null ) assertNull(expectedAmbigAlts); - // else { - // assertEquals(a.conflictingAlts.toString(), - // Arrays.toString(expectedAmbigAlts)); - // } - // assertEquals(expectedAmbigInput, a.input); - // } - - // void unreachable(List msgs, int[] expectedUnreachableAlts) - // throws Exception - // { - // unreachable(msgs, 0, expectedUnreachableAlts); - // } - - // void unreachable(List msgs, int i, int[] - // expectedUnreachableAlts) - // throws Exception - // { - // List amsgs = getMessagesOfType(msgs, - // UnreachableAltsMessage.class); - // UnreachableAltsMessage u = (UnreachableAltsMessage)amsgs.get(i); - // if ( u==null ) assertNull(expectedUnreachableAlts); - // else { - // assertEquals(u.conflictingAlts.toString(), - // Arrays.toString(expectedUnreachableAlts)); - // } - // } - - List getMessagesOfType(List msgs, - Class c) { - List filtered = new ArrayList(); - for (ANTLRMessage m : msgs) { - if (m.getClass() == c) - filtered.add(m); - } - return filtered; - } - - void checkRuleATN(Grammar g, String ruleName, String expecting) { - ParserATNFactory f = new ParserATNFactory(g); - ATN atn = f.createATN(); - - DOTGenerator dot = new DOTGenerator(g); - System.out - .println(dot.getDOT(atn.ruleToStartState[g.getRule(ruleName).index])); - - Rule r = g.getRule(ruleName); - ATNState startState = atn.ruleToStartState[r.index]; - ATNPrinter serializer = new ATNPrinter(g, startState); - String result = serializer.asString(); - - // System.out.print(result); - assertEquals(expecting, result); - } - - public void testActions(String templates, String actionName, String action, - String expected) throws org.antlr.runtime.RecognitionException { - int lp = templates.indexOf('('); - String name = templates.substring(0, lp); - STGroup group = new STGroupString(templates); - ST st = group.getInstanceOf(name); - st.add(actionName, action); - String grammar = st.render(); - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(grammar, equeue); - if (g.ast != null && !g.ast.hasErrors) { - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - - ATNFactory factory = new ParserATNFactory(g); - if (g.isLexer()) - factory = new LexerATNFactory((LexerGrammar) g); - g.atn = factory.createATN(); - - CodeGenerator gen = new CodeGenerator(g); - ST outputFileST = gen.generateParser(); - String output = outputFileST.render(); - // System.out.println(output); - String b = "#" + actionName + "#"; - int start = output.indexOf(b); - String e = "#end-" + actionName + "#"; - int end = output.indexOf(e); - String snippet = output.substring(start + b.length(), end); - assertEquals(expected, snippet); - } - if (equeue.size() > 0) { - System.err.println(equeue.toString()); - } - } - - protected void checkGrammarSemanticsError(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) throws Exception { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertNotNull("no error; " + expectedMessage.getErrorType() - + " expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), - Arrays.toString(foundMsg.getArgs())); - if (equeue.size() != 1) { - System.err.println(equeue); - } - } - - protected void checkGrammarSemanticsWarning(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) throws Exception { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.warnings.size(); i++) { - ANTLRMessage m = equeue.warnings.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertNotNull("no error; " + expectedMessage.getErrorType() - + " expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), - Arrays.toString(foundMsg.getArgs())); - if (equeue.size() != 1) { - System.err.println(equeue); - } - } - - protected void checkError(ErrorQueue equeue, ANTLRMessage expectedMessage) - throws Exception { - // System.out.println("errors="+equeue); - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType() == expectedMessage.getErrorType()) { - foundMsg = m; - } - } - assertTrue("no error; " + expectedMessage.getErrorType() + " expected", - !equeue.errors.isEmpty()); - assertTrue("too many errors; " + equeue.errors, - equeue.errors.size() <= 1); - assertNotNull( - "couldn't find expected error: " - + expectedMessage.getErrorType(), foundMsg); - /* - * assertTrue("error is not a GrammarSemanticsMessage", foundMsg - * instanceof GrammarSemanticsMessage); - */ - assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs()); - } - - public static class FilteringTokenStream extends CommonTokenStream { - public FilteringTokenStream(TokenSource src) { - super(src); - } - - Set hide = new HashSet(); - - @Override - protected boolean sync(int i) { - if (!super.sync(i)) { - return false; - } - - Token t = get(i); - if (hide.contains(t.getType())) { - ((WritableToken) t).setChannel(Token.HIDDEN_CHANNEL); - } - - return true; - } - - public void setTokenTypeChannel(int ttype, int channel) { - hide.add(ttype); - } - } - - protected void mkdir(File dir) { - dir.mkdirs(); - } - protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) { @@ -746,7 +311,7 @@ protected void writeParserTestFile(String parserName, String lexerName, outputFileST.add("listenerName", listenerName); outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName.substring(0, 1).toUpperCase() + parserStartRuleName.substring(1) ); - writeFile(overall_tmpdir.toString(), "Test.go", outputFileST.render()); + writeFile(getTempDirPath(), "Test.go", outputFileST.render()); } @@ -778,228 +343,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { + "}\n" + "\n"); outputFileST.add("lexerName", lexerName); - writeFile(overall_tmpdir.toString(), "Test.go", outputFileST.render()); - } - - public void writeRecognizer(String parserName, String lexerName, - String listenerName, String visitorName, - String parserStartRuleName, boolean debug) { - if (parserName == null) { - writeLexerTestFile(lexerName, debug); - } - else { - writeParserTestFile(parserName, lexerName, listenerName, - visitorName, parserStartRuleName, debug); - } + writeFile(getTempDirPath(), "Test.go", outputFileST.render()); } - protected void eraseFilesEndingWith(final String filesEndingWith) { - File[] files = overall_tmpdir.listFiles(new FileFilter() { - @Override - public boolean accept(File pathname) { - return pathname.getName().endsWith(filesEndingWith); - } - }); - for (File file : files) { - file.delete(); - } - } - - protected static void eraseDirectory(File dir) { - File[] files = dir.listFiles(); - if (files != null) { - for (File file : files) { - if (file.isDirectory()) { - eraseDirectory(file); - } - else { - file.delete(); - } - } - } - dir.delete(); - } - - public void eraseTempDir() { - if (shouldEraseTempDir()) { - if ( overall_tmpdir.exists()) { - eraseDirectory(overall_tmpdir); - } - } - } - - private boolean shouldEraseTempDir() { - if(overall_tmpdir==null) - return false; - String propName = "antlr-go-erase-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) - return Boolean.getBoolean(prop); - else - return true; - } - - public String getFirstLineOfException() { - if (this.stderrDuringParse == null) { - return null; - } - String[] lines = this.stderrDuringParse.split("\n"); - String prefix = "Exception in thread \"main\" "; - return lines[0].substring(prefix.length(), lines[0].length()); - } - - /** - * When looking at a result set that consists of a Map/HashTable we cannot - * rely on the output order, as the hashing algorithm or other aspects of - * the implementation may be different on differnt JDKs or platforms. Hence - * we take the Map, convert the keys to a List, sort them and Stringify the - * Map, which is a bit of a hack, but guarantees that we get the same order - * on all systems. We assume that the keys are strings. - * - * @param m - * The Map that contains keys we wish to return in sorted order - * @return A string that represents all the keys in sorted order. - */ - public String sortMapToString(Map m) { - // Pass in crap, and get nothing back - // - if (m == null) { - return null; - } - - System.out.println("Map toString looks like: " + m.toString()); - - // Sort the keys in the Map - // - TreeMap nset = new TreeMap(m); - - System.out.println("Tree map looks like: " + nset.toString()); - return nset.toString(); - } - - public List realElements(List elements) { - return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size()); - } - - public void assertNotNullOrEmpty(String message, String text) { - assertNotNull(message, text); - assertFalse(message, text.isEmpty()); - } - - public void assertNotNullOrEmpty(String text) { - assertNotNull(text); - assertFalse(text.isEmpty()); - } - - public static class IntTokenStream implements TokenStream { - IntegerList types; - int p = 0; - - public IntTokenStream(IntegerList types) { - this.types = types; - } - - @Override - public void consume() { - p++; - } - - @Override - public int LA(int i) { - return LT(i).getType(); - } - - @Override - public int mark() { - return index(); - } - - @Override - public int index() { - return p; - } - - @Override - public void release(int marker) { - seek(marker); - } - - @Override - public void seek(int index) { - p = index; - } - - @Override - public int size() { - return types.size(); - } - - @Override - public String getSourceName() { - return null; - } - - @Override - public Token LT(int i) { - CommonToken t; - int rawIndex = p + i - 1; - if (rawIndex >= types.size()) - t = new CommonToken(Token.EOF); - else - t = new CommonToken(types.get(rawIndex)); - t.setTokenIndex(rawIndex); - return t; - } - - @Override - public Token get(int i) { - return new org.antlr.v4.runtime.CommonToken(types.get(i)); - } - - @Override - public TokenSource getTokenSource() { - return null; - } - - @Override - public String getText() { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Interval interval) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(RuleContext ctx) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Token start, Token stop) { - throw new UnsupportedOperationException("can't give strings"); - } - } - - /** Sort a list */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - - /** Return map sorted by key */ - public , V> LinkedHashMap sort( - Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java index 606cdede2c..cc03bdc116 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java @@ -5,7 +5,6 @@ */ package org.antlr.v4.test.runtime.java; -import org.antlr.v4.Tool; import org.antlr.v4.analysis.AnalysisPipeline; import org.antlr.v4.automata.ATNFactory; import org.antlr.v4.automata.ATNPrinter; @@ -14,25 +13,12 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.IntStream; import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Parser; -import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenSource; import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.WritableToken; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.LexerATNSimulator; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntegerList; -import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Pair; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.semantics.SemanticPipeline; @@ -42,9 +28,6 @@ import org.antlr.v4.tool.GrammarSemanticsMessage; import org.antlr.v4.tool.LexerGrammar; import org.antlr.v4.tool.Rule; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; @@ -68,13 +51,9 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.TreeMap; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertFalse; @@ -83,9 +62,7 @@ import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; import static org.junit.Assert.assertArrayEquals; -public class BaseJavaTest implements RuntimeTestSupport { - public static final String newline = System.getProperty("line.separator"); - public static final String pathSep = System.getProperty("path.separator"); +public class BaseJavaTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { /** * When the {@code antlr.testinprocess} runtime property is set to @@ -103,308 +80,14 @@ public class BaseJavaTest implements RuntimeTestSupport { */ public static final boolean TEST_IN_SAME_PROCESS = Boolean.parseBoolean(System.getProperty("antlr.testinprocess")); - /** - * When the {@code antlr.preserve-test-dir} runtime property is set to - * {@code true}, the temporary directories created by the test run will not - * be removed at the end of the test run, even for tests that completed - * successfully. - *

- *

- * The default behavior (used in all other cases) is removing the temporary - * directories for all tests which completed successfully, and preserving - * the directories for tests which failed.

- */ - public static final boolean PRESERVE_TEST_DIR = true; //Boolean.parseBoolean(System.getProperty("antlr.preserve-test-dir")); - - /** - * The base test directory is the directory where generated files get placed - * during unit test execution. - *

- *

- * The default value for this property is the {@code java.io.tmpdir} system - * property, and can be overridden by setting the - * {@code antlr.java-test-dir} property to a custom location. Note that the - * {@code antlr.java-test-dir} property directly affects the - * {@link #CREATE_PER_TEST_DIRECTORIES} value as well.

- */ - public static final String BASE_TEST_DIR; - - /** - * When {@code true}, a temporary directory will be created for each test - * executed during the test run. - *

- *

- * This value is {@code true} when the {@code antlr.java-test-dir} system - * property is set, and otherwise {@code false}.

- */ - public static final boolean CREATE_PER_TEST_DIRECTORIES; - - static { - String baseTestDir = System.getProperty("antlr.java-test-dir"); - boolean perTestDirectories = false; - if ( baseTestDir==null || baseTestDir.isEmpty() ) { - baseTestDir = System.getProperty("java.io.tmpdir"); - perTestDirectories = true; - } - - if ( !new File(baseTestDir).isDirectory() ) { - throw new UnsupportedOperationException("The specified base test directory does not exist: "+baseTestDir); - } - - BASE_TEST_DIR = baseTestDir; - CREATE_PER_TEST_DIRECTORIES = perTestDirectories; - } - /** * Build up the full classpath we need, including the surefire path (if present) */ public static final String CLASSPATH = System.getProperty("java.class.path"); - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** - * Errors found while running antlr - */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - @Override - public void testSetUp() throws Exception { -// STGroup.verbose = true; -// System.err.println("testSetUp "+Thread.currentThread().getName()); - if ( CREATE_PER_TEST_DIRECTORIES ) { - // new output dir for each test - String threadName = Thread.currentThread().getName(); - String testDirectory = getClass().getSimpleName()+"-"+threadName+"-"+System.nanoTime(); - tmpdir = new File(BASE_TEST_DIR, testDirectory).getAbsolutePath(); - } - else { - tmpdir = new File(BASE_TEST_DIR).getAbsolutePath(); - if ( !PRESERVE_TEST_DIR && new File(tmpdir).exists() ) { - eraseFiles(); - } - } - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if ( antlrToolErrors.length()==0 ) { - return null; - } - return antlrToolErrors.toString(); - } - - protected org.antlr.v4.Tool newTool(String[] args) { - Tool tool = new Tool(args); - return tool; - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if ( g.atn==null ) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if ( g.isLexer() ) { - f = new LexerATNFactory((LexerGrammar) g); - } - else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if ( useSerializer ) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if ( g.ast!=null && !g.ast.hasErrors ) { -// System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if ( g.getImportedGrammars()!=null ) { // process imported grammars (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } - } - - public DFA createDFA(Grammar g, DecisionState s) { -// PredictionDFAFactory conv = new PredictionDFAFactory(g, s); -// DFA dfa = conv.createDFA(); -// conv.issueAmbiguityWarnings(); -// System.out.print("DFA="+dfa); -// return dfa; - return null; - } - -// public void minimizeDFA(DFA dfa) { -// DFAMinimizer dmin = new DFAMinimizer(dfa); -// dfa.minimized = dmin.minimize(); -// } - - IntegerList getTypesFromString(Grammar g, String expecting) { - IntegerList expectingTokenTypes = new IntegerList(); - if ( expecting!=null && !expecting.trim().isEmpty() ) { - for (String tname : expecting.replace(" ", "").split(",")) { - int ttype = g.getTokenType(tname); - expectingTokenTypes.add(ttype); - } - } - return expectingTokenTypes; - } - - public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { - ANTLRInputStream in = new ANTLRInputStream(input); - IntegerList tokenTypes = new IntegerList(); - int ttype; - do { - ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); - tokenTypes.add(ttype); - } while ( ttype!=Token.EOF ); - return tokenTypes; - } - - public List getTokenTypes(LexerGrammar lg, - ATN atn, - CharStream input) { - LexerATNSimulator interp = new LexerATNSimulator(atn, new DFA[]{new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE))}, null); - List tokenTypes = new ArrayList(); - int ttype; - boolean hitEOF = false; - do { - if ( hitEOF ) { - tokenTypes.add("EOF"); - break; - } - int t = input.LA(1); - ttype = interp.match(input, Lexer.DEFAULT_MODE); - if ( ttype==Token.EOF ) { - tokenTypes.add("EOF"); - } - else { - tokenTypes.add(lg.typeToTokenList.get(ttype)); - } - - if ( t==IntStream.EOF ) { - hitEOF = true; - } - } while ( ttype!=Token.EOF ); - return tokenTypes; - } - - List checkRuleDFA(String gtext, String ruleName, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - ATNState s = atn.ruleToStartState[g.getRule(ruleName).index]; - if ( s==null ) { - System.err.println("no such rule: "+ruleName); - return null; - } - ATNState t = s.transition(0).target; - if ( !(t instanceof DecisionState) ) { - System.out.println(ruleName+" has no decision"); - return null; - } - DecisionState blk = (DecisionState) t; - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - List checkRuleDFA(String gtext, int decision, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - DecisionState blk = atn.decisionToState.get(decision); - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - void checkRuleDFA(Grammar g, DecisionState blk, String expecting) - throws Exception { - DFA dfa = createDFA(g, blk); - String result = null; - if ( dfa!=null ) result = dfa.toString(); - assertEquals(expecting, result); - } - - List checkLexerDFA(String gtext, String expecting) - throws Exception { - return checkLexerDFA(gtext, LexerGrammar.DEFAULT_MODE_NAME, expecting); - } - - List checkLexerDFA(String gtext, String modeName, String expecting) - throws Exception { - ErrorQueue equeue = new ErrorQueue(); - LexerGrammar g = new LexerGrammar(gtext, equeue); - g.atn = createATN(g, false); -// LexerATNToDFAConverter conv = new LexerATNToDFAConverter(g); -// DFA dfa = conv.createDFA(modeName); -// g.setLookaheadDFA(0, dfa); // only one decision to worry about -// -// String result = null; -// if ( dfa!=null ) result = dfa.toString(); -// assertEquals(expecting, result); -// -// return equeue.all; - return null; + protected String getPropertyPrefix() { + return "antrl4-java"; } protected String load(String fileName, String encoding) @@ -439,7 +122,7 @@ protected String load(String fileName, String encoding) protected boolean compile(String... fileNames) { List files = new ArrayList(); for (String fileName : fileNames) { - File f = new File(tmpdir, fileName); + File f = new File(getTempTestDir(), fileName); files.add(f); } @@ -454,7 +137,7 @@ protected boolean compile(String... fileNames) { fileManager.getJavaFileObjectsFromFiles(files); Iterable compileOptions = - Arrays.asList("-g", "-source", "1.6", "-target", "1.6", "-implicit:class", "-Xlint:-options", "-d", tmpdir, "-cp", tmpdir+pathSep+CLASSPATH); + Arrays.asList("-g", "-source", "1.6", "-target", "1.6", "-implicit:class", "-Xlint:-options", "-d", getTempDirPath(), "-cp", getTempDirPath() + PATH_SEP + CLASSPATH); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, compileOptions, null, @@ -488,11 +171,10 @@ public String execLexer(String grammarFileName, null, lexerName); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); compile("Test.java"); - String output = execClass("Test"); - return output; + return execClass("Test"); } public ParseTree execParser(String startRuleName, String input, @@ -542,7 +224,7 @@ public Pair getParserAndLexer(String input, public Class loadClassFromTempDir(String name) throws Exception { ClassLoader loader = - new URLClassLoader(new URL[]{new File(tmpdir).toURI().toURL()}, + new URLClassLoader(new URL[]{getTempTestDir().toURI().toURL()}, ClassLoader.getSystemClassLoader()); return loader.loadClass(name); } @@ -590,7 +272,7 @@ public String execParser(String grammarFileName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); return rawExecRecognizer(parserName, lexerName, startRuleName, @@ -617,7 +299,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String... extraOptions) { ErrorQueue equeue = - BaseRuntimeTest.antlrOnString(getTmpDir(), "Java", grammarFileName, grammarStr, defaultListener, extraOptions); + BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; } @@ -639,8 +321,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, files.add(grammarName+"BaseVisitor.java"); } } - boolean allIsWell = compile(files.toArray(new String[files.size()])); - return allIsWell; + return compile(files.toArray(new String[0])); } protected String rawExecRecognizer(String parserName, @@ -649,7 +330,7 @@ protected String rawExecRecognizer(String parserName, boolean debug, boolean profile) { - this.stderrDuringParse = null; + setParseErrors(null); if ( parserName==null ) { writeLexerTestFile(lexerName, false); } @@ -672,7 +353,7 @@ public String execRecognizer() { public String execClass(String className) { if (TEST_IN_SAME_PROCESS) { try { - ClassLoader loader = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader()); + ClassLoader loader = new URLClassLoader(new URL[] { getTempTestDir().toURI().toURL() }, ClassLoader.getSystemClassLoader()); final Class mainClass = (Class)loader.loadClass(className); final Method mainMethod = mainClass.getDeclaredMethod("main", String[].class); PipedInputStream stdoutIn = new PipedInputStream(); @@ -690,7 +371,7 @@ public String execClass(String className) { System.setErr(new PrintStream(stderrOut)); stdoutVacuum.start(); stderrVacuum.start(); - mainMethod.invoke(null, (Object)new String[] { new File(tmpdir, "input").getAbsolutePath() }); + mainMethod.invoke(null, (Object)new String[] { new File(getTempTestDir(), "input").getAbsolutePath() }); } finally { System.setErr(originalErr); @@ -709,7 +390,7 @@ public String execClass(String className) { output = null; } if ( stderrVacuum.toString().length()>0 ) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } @@ -720,14 +401,14 @@ public String execClass(String className) { try { String[] args = new String[] { - "java", "-classpath", tmpdir+pathSep+CLASSPATH, + "java", "-classpath", getTempDirPath() + PATH_SEP + CLASSPATH, "-Dfile.encoding=UTF-8", - className, new File(tmpdir, "input").getAbsolutePath() + className, new File(getTempTestDir(), "input").getAbsolutePath() }; // String cmdLine = Utils.join(args, " "); // System.err.println("execParser: "+cmdLine); Process process = - Runtime.getRuntime().exec(args, null, new File(tmpdir)); + Runtime.getRuntime().exec(args, null, getTempTestDir()); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stdoutVacuum.start(); @@ -740,7 +421,7 @@ className, new File(tmpdir, "input").getAbsolutePath() output = null; } if ( stderrVacuum.toString().length()>0 ) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } @@ -751,49 +432,6 @@ className, new File(tmpdir, "input").getAbsolutePath() return null; } -// void ambig(List msgs, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// ambig(msgs, 0, expectedAmbigAlts, expectedAmbigInput); -// } - -// void ambig(List msgs, int i, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, AmbiguityMessage.class); -// AmbiguityMessage a = (AmbiguityMessage)amsgs.get(i); -// if ( a==null ) assertNull(expectedAmbigAlts); -// else { -// assertEquals(a.conflictingAlts.toString(), Arrays.toString(expectedAmbigAlts)); -// } -// assertEquals(expectedAmbigInput, a.input); -// } - -// void unreachable(List msgs, int[] expectedUnreachableAlts) -// throws Exception -// { -// unreachable(msgs, 0, expectedUnreachableAlts); -// } - -// void unreachable(List msgs, int i, int[] expectedUnreachableAlts) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, UnreachableAltsMessage.class); -// UnreachableAltsMessage u = (UnreachableAltsMessage)amsgs.get(i); -// if ( u==null ) assertNull(expectedUnreachableAlts); -// else { -// assertEquals(u.conflictingAlts.toString(), Arrays.toString(expectedUnreachableAlts)); -// } -// } - - List getMessagesOfType(List msgs, Class c) { - List filtered = new ArrayList(); - for (ANTLRMessage m : msgs) { - if ( m.getClass() == c ) filtered.add(m); - } - return filtered; - } - public void checkRuleATN(Grammar g, String ruleName, String expecting) { // DOTGenerator dot = new DOTGenerator(g); // System.out.println(dot.getDOT(g.atn.ruleToStartState[g.getRule(ruleName).index])); @@ -843,25 +481,7 @@ public void testActions(String templates, String actionName, String action, Stri } } - protected void checkGrammarSemanticsError(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception - { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if ( equeue.size()!=1 ) { - System.err.println(equeue); - } - } + protected void checkGrammarSemanticsWarning(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) @@ -883,49 +503,6 @@ protected void checkGrammarSemanticsWarning(ErrorQueue equeue, } } - protected void checkError(ErrorQueue equeue, - ANTLRMessage expectedMessage) - throws Exception - { - //System.out.println("errors="+equeue); - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertTrue("no error; "+expectedMessage.getErrorType()+" expected", !equeue.errors.isEmpty()); - assertTrue("too many errors; "+equeue.errors, equeue.errors.size()<=1); - assertNotNull("couldn't find expected error: "+expectedMessage.getErrorType(), foundMsg); - /* - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - */ - assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs()); - } - - public static class FilteringTokenStream extends CommonTokenStream { - public FilteringTokenStream(TokenSource src) { super(src); } - Set hide = new HashSet(); - @Override - protected boolean sync(int i) { - if (!super.sync(i)) { - return false; - } - - Token t = get(i); - if ( hide.contains(t.getType()) ) { - ((WritableToken)t).setChannel(Token.HIDDEN_CHANNEL); - } - - return true; - } - public void setTokenTypeChannel(int ttype, int channel) { - hide.add(ttype); - } - } - protected void writeTestFile(String parserName, String lexerName, String parserStartRuleName, @@ -988,7 +565,7 @@ protected void writeTestFile(String parserName, outputFileST.add("parserName", parserName); outputFileST.add("lexerName", lexerName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.java", outputFileST.render()); + writeFile(getTempDirPath(), "Test.java", outputFileST.render()); } protected void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -1009,219 +586,13 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { ); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.java", outputFileST.render()); - } - - public void writeRecognizerAndCompile(String parserName, String lexerName, - String parserStartRuleName, - boolean debug, - boolean profile) { - if ( parserName==null ) { - writeLexerTestFile(lexerName, debug); - } - else { - writeTestFile(parserName, - lexerName, - parserStartRuleName, - debug, - profile); - } - - compile("Test.java"); - } - - - protected void eraseFiles(final String filesEndingWith) { - if (tmpdir == null) { - return; - } - File tmpdirF = new File(tmpdir); - String[] files = tmpdirF.list(); - for(int i = 0; files!=null && i < files.length; i++) { - if ( files[i].endsWith(filesEndingWith) ) { - new File(tmpdir+"/"+files[i]).delete(); - } - } - } - - protected void eraseFiles() { - if (tmpdir == null) { - return; - } - File tmpdirF = new File(tmpdir); - String[] files = tmpdirF.list(); - for(int i = 0; files!=null && i < files.length; i++) { - new File(tmpdir+"/"+files[i]).delete(); - } - } - - public void eraseTempDir() { - if (shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseFiles(); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - return tmpdir != null; + writeFile(getTempDirPath(), "Test.java", outputFileST.render()); } - public String getFirstLineOfException() { - if ( this.stderrDuringParse ==null ) { - return null; - } - String[] lines = this.stderrDuringParse.split("\n"); - String prefix="Exception in thread \"main\" "; - return lines[0].substring(prefix.length(),lines[0].length()); - } - - /** - * When looking at a result set that consists of a Map/HashTable - * we cannot rely on the output order, as the hashing algorithm or other aspects - * of the implementation may be different on differnt JDKs or platforms. Hence - * we take the Map, convert the keys to a List, sort them and Stringify the Map, which is a - * bit of a hack, but guarantees that we get the same order on all systems. We assume that - * the keys are strings. - * - * @param m The Map that contains keys we wish to return in sorted order - * @return A string that represents all the keys in sorted order. - */ - public String sortMapToString(Map m) { - // Pass in crap, and get nothing back - // - if (m == null) { - return null; - } - - System.out.println("Map toString looks like: " + m.toString()); - - // Sort the keys in the Map - // - TreeMap nset = new TreeMap(m); - - System.out.println("Tree map looks like: " + nset.toString()); - return nset.toString(); - } - public List realElements(List elements) { return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size()); } - public void assertNotNullOrEmpty(String message, String text) { - assertNotNull(message, text); - assertFalse(message, text.isEmpty()); - } - - public void assertNotNullOrEmpty(String text) { - assertNotNull(text); - assertFalse(text.isEmpty()); - } - - public static class IntTokenStream implements TokenStream { - public IntegerList types; - int p=0; - public IntTokenStream(IntegerList types) { this.types = types; } - - @Override - public void consume() { p++; } - - @Override - public int LA(int i) { return LT(i).getType(); } - - @Override - public int mark() { - return index(); - } - - @Override - public int index() { return p; } - - @Override - public void release(int marker) { - seek(marker); - } - - @Override - public void seek(int index) { - p = index; - } - - @Override - public int size() { - return types.size(); - } - - @Override - public String getSourceName() { - return UNKNOWN_SOURCE_NAME; - } - - @Override - public Token LT(int i) { - CommonToken t; - int rawIndex = p + i - 1; - if ( rawIndex>=types.size() ) t = new CommonToken(Token.EOF); - else t = new CommonToken(types.get(rawIndex)); - t.setTokenIndex(rawIndex); - return t; - } - - @Override - public Token get(int i) { - return new org.antlr.v4.runtime.CommonToken(types.get(i)); - } - - @Override - public TokenSource getTokenSource() { - return null; - } - - - @Override - public String getText() { - throw new UnsupportedOperationException("can't give strings"); - } - - - @Override - public String getText(Interval interval) { - throw new UnsupportedOperationException("can't give strings"); - } - - - @Override - public String getText(RuleContext ctx) { - throw new UnsupportedOperationException("can't give strings"); - } - - - @Override - public String getText(Token start, Token stop) { - throw new UnsupportedOperationException("can't give strings"); - } - } - - /** Sort a list */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - /** Return map sorted by key */ - public ,V> LinkedHashMap sort(Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/BaseNodeTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/BaseNodeTest.java index 99b0631587..f58bd075ca 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/BaseNodeTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/BaseNodeTest.java @@ -5,19 +5,7 @@ */ package org.antlr.v4.test.runtime.javascript; -import org.antlr.v4.Tool; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; -import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.*; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.LexerGrammar; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.stringtemplate.v4.ST; import java.io.File; @@ -27,131 +15,13 @@ import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -public class BaseNodeTest implements RuntimeTestSupport { - // -J-Dorg.antlr.v4.test.BaseTest.level=FINE - // private static final Logger LOGGER = - // Logger.getLogger(BaseTest.class.getName()); - - public static final String newline = System.getProperty("line.separator"); - public static final String pathSep = System.getProperty("path.separator"); - - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return stdout - * and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** Errors found while running antlr */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - @Override - public void testSetUp() throws Exception { - // new output dir for each test - String prop = System.getProperty("antlr-javascript-test-dir"); - if (prop != null && prop.length() > 0) { - tmpdir = prop; - } - else { - tmpdir = new File(System.getProperty("java.io.tmpdir"), getClass() - .getSimpleName()+"-"+Thread.currentThread().getName()+"-"+System.currentTimeMillis()) - .getAbsolutePath(); - } - File dir = new File(tmpdir); - if (dir.exists()) - this.eraseFiles(dir); - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } +public class BaseNodeTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if ( antlrToolErrors.length()==0 ) { - return null; - } - return antlrToolErrors.toString(); - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if (g.atn == null) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if (g.isLexer()) { - f = new LexerATNFactory((LexerGrammar) g); - } - else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if (g.ast != null && !g.ast.hasErrors) { - System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if (g.getImportedGrammars() != null) { // process imported grammars - // (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } + protected String getPropertyPrefix() { + return "antlr4-javascript"; } protected String execLexer(String grammarFileName, String grammarStr, @@ -165,9 +35,9 @@ public String execLexer(String grammarFileName, String grammarStr, boolean success = rawGenerateAndBuildRecognizer(grammarFileName, grammarStr, null, lexerName, "-no-listener"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); - writeFile(tmpdir, "package.json", "{\"type\": \"module\"}"); + writeFile(getTempDirPath(), "package.json", "{\"type\": \"module\"}"); String output = execModule("Test.js"); if ( output!=null && output.length()==0 ) { output = null; @@ -184,10 +54,10 @@ public String execParser(String grammarFileName, String grammarStr, boolean success = rawGenerateAndBuildRecognizer(grammarFileName, grammarStr, parserName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); rawBuildRecognizerTestFile(parserName, lexerName, listenerName, visitorName, startRuleName, showDiagnosticErrors); - writeFile(tmpdir, "package.json", "{\"type\": \"module\"}"); + writeFile(getTempDirPath(), "package.json", "{\"type\": \"module\"}"); return execRecognizer(); } @@ -203,7 +73,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) { - ErrorQueue equeue = antlrOnString(getTmpDir(), "JavaScript", grammarFileName, grammarStr, + ErrorQueue equeue = antlrOnString(getTempDirPath(), "JavaScript", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; @@ -234,7 +104,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, protected void rawBuildRecognizerTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) { - this.stderrDuringParse = null; + setParseErrors(null); if (parserName == null) { writeLexerTestFile(lexerName, false); } @@ -255,16 +125,16 @@ public String execModule(String fileName) { installRuntime(npmPath); registerRuntime(npmPath); } - String modulePath = new File(new File(tmpdir), fileName) + String modulePath = new File(getTempTestDir(), fileName) .getAbsolutePath(); linkRuntime(npmPath); String nodejsPath = locateNodeJS(); - String inputPath = new File(new File(tmpdir), "input") + String inputPath = new File(getTempTestDir(), "input") .getAbsolutePath(); ProcessBuilder builder = new ProcessBuilder(nodejsPath, modulePath, inputPath); - builder.environment().put("NODE_PATH", tmpdir); - builder.directory(new File(tmpdir)); + builder.environment().put("NODE_PATH", getTempDirPath()); + builder.directory(getTempTestDir()); Process process = builder.start(); StreamVacuum stdoutVacuum = new StreamVacuum( process.getInputStream()); @@ -283,7 +153,7 @@ public String execModule(String fileName) { output = null; } if (stderrVacuum.toString().length() > 0) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } catch (Exception e) { @@ -298,8 +168,8 @@ private void installRuntime(String npmPath) throws IOException, InterruptedExcep String runtimePath = locateRuntime(); ProcessBuilder builder = new ProcessBuilder(npmPath, "install"); builder.directory(new File(runtimePath)); - builder.redirectError(new File(tmpdir, "error.txt")); - builder.redirectOutput(new File(tmpdir, "output.txt")); + builder.redirectError(new File(getTempTestDir(), "error.txt")); + builder.redirectOutput(new File(getTempTestDir(), "output.txt")); Process process = builder.start(); // TODO switch to jdk 8 process.waitFor(); @@ -314,8 +184,8 @@ private void registerRuntime(String npmPath) throws IOException, InterruptedExce String runtimePath = locateRuntime(); ProcessBuilder builder = new ProcessBuilder(npmPath, "link"); builder.directory(new File(runtimePath)); - builder.redirectError(new File(tmpdir, "error.txt")); - builder.redirectOutput(new File(tmpdir, "output.txt")); + builder.redirectError(new File(getTempTestDir(), "error.txt")); + builder.redirectOutput(new File(getTempTestDir(), "output.txt")); Process process = builder.start(); // TODO switch to jdk 8 process.waitFor(); @@ -332,9 +202,9 @@ private void linkRuntime(String npmPath) throws IOException, InterruptedExceptio args.add("sudo"); args.addAll(Arrays.asList(npmPath, "link", "antlr4")); ProcessBuilder builder = new ProcessBuilder(args.toArray(new String[0])); - builder.directory(new File(tmpdir)); - builder.redirectError(new File(tmpdir, "error.txt")); - builder.redirectOutput(new File(tmpdir, "output.txt")); + builder.directory(getTempTestDir()); + builder.redirectError(new File(getTempTestDir(), "error.txt")); + builder.redirectOutput(new File(getTempTestDir(), "output.txt")); Process process = builder.start(); // TODO switch to jdk 8 process.waitFor(); @@ -396,11 +266,6 @@ private String locateRuntime() { return runtimeSrc.getPath(); } - private boolean isWindows() { - return System.getProperty("os.name").toLowerCase().contains("windows"); - } - - protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) { @@ -451,7 +316,7 @@ protected void writeParserTestFile(String parserName, String lexerName, outputFileST.add("listenerName", listenerName); outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.js", outputFileST.render()); + writeFile(getTempDirPath(), "Test.js", outputFileST.render()); } protected void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -471,54 +336,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { : "") + "}\n" + "\n" + "main(process.argv);\n" + "\n"); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.js", outputFileST.render()); - } - - protected void eraseFiles(File dir) { - String[] files = dir.list(); - for (int i = 0; files != null && i < files.length; i++) { - new File(dir, files[i]).delete(); - } - } - - @Override - public void eraseTempDir() { - if (shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseFiles(tmpdirF); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - if(tmpdir==null) - return false; - String propName = "antlr-javascript-erase-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) - return Boolean.getBoolean(prop); - else - return true; + writeFile(getTempDirPath(), "Test.js", outputFileST.render()); } - /** Sort a list */ - public > List sort(List data) { - List dup = new ArrayList(data); - Collections.sort(dup); - return dup; - } - - /** Return map sorted by key */ - public , V> LinkedHashMap sort( - Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java index deef632879..dd5af7cb90 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/php/BasePHPTest.java @@ -11,28 +11,11 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Set; -import org.antlr.v4.Tool; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; -import org.antlr.v4.semantics.SemanticPipeline; -import org.antlr.v4.test.runtime.ErrorQueue; -import org.antlr.v4.test.runtime.RuntimeTestDescriptor; -import org.antlr.v4.test.runtime.RuntimeTestSupport; -import org.antlr.v4.test.runtime.StreamVacuum; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.LexerGrammar; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; + +import org.antlr.v4.test.runtime.*; import org.stringtemplate.v4.ST; import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; @@ -40,143 +23,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class BasePHPTest implements RuntimeTestSupport { - public static final String newline = System.getProperty("line.separator"); - - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** - * Errors found while running antlr - */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; +public class BasePHPTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { - private String getPropertyPrefix() { + public String getPropertyPrefix() { return "antlr-php"; } - @Override - public void testSetUp() throws Exception { - // new output dir for each test - String propName = getPropertyPrefix() + "-test-dir"; - String prop = System.getProperty(propName); - - if (prop != null && prop.length() > 0) { - tmpdir = prop; - } else { - String classSimpleName = getClass().getSimpleName(); - String threadName = Thread.currentThread().getName(); - String childPath = String.format("%s-%s-%s", classSimpleName, threadName, System.currentTimeMillis()); - tmpdir = new File(System.getProperty("java.io.tmpdir"), childPath).getAbsolutePath(); - } - - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if (antlrToolErrors.length() == 0) { - return null; - } - - return antlrToolErrors.toString(); - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if (g.atn == null) { - semanticProcess(g); - - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - - if (g.isLexer()) { - f = new LexerATNFactory((LexerGrammar) g); - } else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if (g.ast != null && !g.ast.hasErrors) { - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - - if (g.getImportedGrammars() != null) { - for (Grammar imp: g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } - } - - protected String execLexer( - String grammarFileName, - String grammarStr, - String lexerName, - String input - ) { - return execLexer(grammarFileName, grammarStr, lexerName, input, false); - } - @Override public String execLexer( String grammarFileName, @@ -193,11 +45,9 @@ public String execLexer( "-no-listener" ); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); - String output = execModule("Test.php"); - - return output; + return execModule("Test.php"); } public String execParser( @@ -247,7 +97,7 @@ public String execParser_( assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); rawBuildRecognizerTestFile( parserName, @@ -293,7 +143,7 @@ protected boolean rawGenerateAndBuildRecognizer( boolean defaultListener, String... extraOptions ) { - ErrorQueue equeue = antlrOnString(getTmpDir(), "PHP", grammarFileName, grammarStr, defaultListener, extraOptions); + ErrorQueue equeue = antlrOnString(getTempDirPath(), "PHP", grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; @@ -330,7 +180,7 @@ protected void rawBuildRecognizerTestFile( boolean debug, boolean trace ) { - this.stderrDuringParse = null; + setParseErrors(null); if (parserName == null) { writeLexerTestFile(lexerName, false); } else { @@ -354,15 +204,14 @@ public String execModule(String fileName) { String phpPath = locatePhp(); String runtimePath = locateRuntime(); - File tmpdirFile = new File(tmpdir); - String modulePath = new File(tmpdirFile, fileName).getAbsolutePath(); - String inputPath = new File(tmpdirFile, "input").getAbsolutePath(); - Path outputPath = tmpdirFile.toPath().resolve("output").toAbsolutePath(); + String modulePath = new File(getTempTestDir(), fileName).getAbsolutePath(); + String inputPath = new File(getTempTestDir(), "input").getAbsolutePath(); + Path outputPath = getTempTestDir().toPath().resolve("output").toAbsolutePath(); try { ProcessBuilder builder = new ProcessBuilder(phpPath, modulePath, inputPath, outputPath.toString()); builder.environment().put("RUNTIME", runtimePath); - builder.directory(tmpdirFile); + builder.directory(getTempTestDir()); Process process = builder.start(); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); @@ -378,7 +227,7 @@ public String execModule(String fileName) { } if (stderrVacuum.toString().length() > 0) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; @@ -487,7 +336,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.php", outputFileST.render()); + writeFile(getTempDirPath(), "Test.php", outputFileST.render()); } protected void writeParserTestFile( @@ -569,59 +418,7 @@ protected void writeParserTestFile( outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.php", outputFileST.render()); - } - - protected void eraseFiles(File dir) { - String[] files = dir.list(); - for (int i = 0; files != null && i < files.length; i++) { - new File(dir, files[i]).delete(); - } - } - - @Override - public void eraseTempDir() { - if (shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if (tmpdirF.exists()) { - eraseFiles(tmpdirF); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - if(tmpdir==null) - return false; - String propName = getPropertyPrefix() + "-erase-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) - return Boolean.getBoolean(prop); - else - return true; + writeFile(getTempDirPath(), "Test.php", outputFileST.render()); } - /** - * Sort a list - */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - - /** - * Return map sorted by key - */ - public , V> LinkedHashMap sort(Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k: keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java index a3ea85b1b1..c129678698 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java @@ -5,344 +5,38 @@ */ package org.antlr.v4.test.runtime.python; -import org.antlr.v4.Tool; -import org.antlr.v4.automata.ATNFactory; -import org.antlr.v4.automata.ATNPrinter; -import org.antlr.v4.automata.LexerATNFactory; -import org.antlr.v4.automata.ParserATNFactory; -import org.antlr.v4.codegen.CodeGenerator; -import org.antlr.v4.runtime.ANTLRInputStream; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CommonToken; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.IntStream; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.Parser; -import org.antlr.v4.runtime.RuleContext; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenSource; -import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.WritableToken; -import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.runtime.atn.ATNDeserializer; -import org.antlr.v4.runtime.atn.ATNSerializer; -import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.atn.DecisionState; -import org.antlr.v4.runtime.atn.LexerATNSimulator; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.IntegerList; -import org.antlr.v4.runtime.misc.Interval; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.*; -import org.antlr.v4.tool.ANTLRMessage; -import org.antlr.v4.tool.DOTGenerator; -import org.antlr.v4.tool.Grammar; -import org.antlr.v4.tool.GrammarSemanticsMessage; -import org.antlr.v4.tool.LexerGrammar; -import org.antlr.v4.tool.Rule; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; import org.junit.runner.Description; -import org.stringtemplate.v4.ST; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STGroupString; import java.io.File; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.TreeMap; import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -public abstract class BasePythonTest implements RuntimeTestSupport { - // -J-Dorg.antlr.v4.test.BaseTest.level=FINE - // private static final Logger LOGGER = Logger.getLogger(BaseTest.class.getName()); - public static final String newline = System.getProperty("line.separator"); - public static final String pathSep = System.getProperty("path.separator"); - - public String tmpdir = null; - - /** If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - protected String stderrDuringParse; - - /** Errors found while running antlr */ - protected StringBuilder antlrToolErrors; - - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempPyCache(); - eraseTempDir(); - } - - }; - - private String getPropertyPrefix() { - return "antlr-" + getLanguage().toLowerCase(); - } - - @Override - public void testSetUp() throws Exception { - // new output dir for each test - String propName = getPropertyPrefix() + "-test-dir"; - String prop = System.getProperty(propName); - if(prop!=null && prop.length()>0) { - tmpdir = prop; - } - else { - tmpdir = new File(System.getProperty("java.io.tmpdir"), getClass().getSimpleName()+ - "-"+Thread.currentThread().getName()+"-"+System.currentTimeMillis()).getAbsolutePath(); - } - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public String getTmpDir() { - return tmpdir; - } +public abstract class BasePythonTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { @Override - public String getStdout() { - return null; + protected void testSucceeded(Description description) { + eraseTempPyCache(); + eraseTempDir(); } @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if ( antlrToolErrors.length()==0 ) { - return null; - } - return antlrToolErrors.toString(); - } - - protected org.antlr.v4.Tool newTool(String[] args) { - Tool tool = new Tool(args); - return tool; - } - - protected Tool newTool() { - org.antlr.v4.Tool tool = new Tool(new String[] {"-o", tmpdir}); - return tool; - } - - protected ATN createATN(Grammar g, boolean useSerializer) { - if ( g.atn==null ) { - semanticProcess(g); - assertEquals(0, g.tool.getNumErrors()); - - ParserATNFactory f; - if ( g.isLexer() ) { - f = new LexerATNFactory((LexerGrammar)g); - } - else { - f = new ParserATNFactory(g); - } - - g.atn = f.createATN(); - assertEquals(0, g.tool.getNumErrors()); - } - - ATN atn = g.atn; - if (useSerializer) { - char[] serialized = ATNSerializer.getSerializedAsChars(atn); - return new ATNDeserializer().deserialize(serialized); - } - - return atn; - } - - protected void semanticProcess(Grammar g) { - if ( g.ast!=null && !g.ast.hasErrors ) { - System.out.println(g.ast.toStringTree()); - Tool antlr = new Tool(); - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - if ( g.getImportedGrammars()!=null ) { // process imported grammars (if any) - for (Grammar imp : g.getImportedGrammars()) { - antlr.processNonCombinedGrammar(imp, false); - } - } - } - } - - public DFA createDFA(Grammar g, DecisionState s) { -// PredictionDFAFactory conv = new PredictionDFAFactory(g, s); -// DFA dfa = conv.createDFA(); -// conv.issueAmbiguityWarnings(); -// System.out.print("DFA="+dfa); -// return dfa; - return null; - } - -// public void minimizeDFA(DFA dfa) { -// DFAMinimizer dmin = new DFAMinimizer(dfa); -// dfa.minimized = dmin.minimize(); -// } - - IntegerList getTypesFromString(Grammar g, String expecting) { - IntegerList expectingTokenTypes = new IntegerList(); - if ( expecting!=null && !expecting.trim().isEmpty() ) { - for (String tname : expecting.replace(" ", "").split(",")) { - int ttype = g.getTokenType(tname); - expectingTokenTypes.add(ttype); - } - } - return expectingTokenTypes; - } - - public IntegerList getTokenTypesViaATN(String input, LexerATNSimulator lexerATN) { - ANTLRInputStream in = new ANTLRInputStream(input); - IntegerList tokenTypes = new IntegerList(); - int ttype; - do { - ttype = lexerATN.match(in, Lexer.DEFAULT_MODE); - tokenTypes.add(ttype); - } while ( ttype!= Token.EOF ); - return tokenTypes; - } - - public List getTokenTypes(LexerGrammar lg, - ATN atn, - CharStream input) - { - LexerATNSimulator interp = new LexerATNSimulator(atn,new DFA[] { new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null); - List tokenTypes = new ArrayList(); - int ttype; - boolean hitEOF = false; - do { - if ( hitEOF ) { - tokenTypes.add("EOF"); - break; - } - int t = input.LA(1); - ttype = interp.match(input, Lexer.DEFAULT_MODE); - if ( ttype == Token.EOF ) { - tokenTypes.add("EOF"); - } - else { - tokenTypes.add(lg.typeToTokenList.get(ttype)); - } - - if ( t== IntStream.EOF ) { - hitEOF = true; - } - } while ( ttype!=Token.EOF ); - return tokenTypes; - } - - List checkRuleDFA(String gtext, String ruleName, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - ATNState s = atn.ruleToStartState[g.getRule(ruleName).index]; - if ( s==null ) { - System.err.println("no such rule: "+ruleName); - return null; - } - ATNState t = s.transition(0).target; - if ( !(t instanceof DecisionState) ) { - System.out.println(ruleName+" has no decision"); - return null; - } - DecisionState blk = (DecisionState)t; - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - List checkRuleDFA(String gtext, int decision, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(gtext, equeue); - ATN atn = createATN(g, false); - DecisionState blk = atn.decisionToState.get(decision); - checkRuleDFA(g, blk, expecting); - return equeue.all; - } - - void checkRuleDFA(Grammar g, DecisionState blk, String expecting) - throws Exception - { - DFA dfa = createDFA(g, blk); - String result = null; - if ( dfa!=null ) result = dfa.toString(); - assertEquals(expecting, result); - } - - List checkLexerDFA(String gtext, String expecting) - throws Exception - { - return checkLexerDFA(gtext, LexerGrammar.DEFAULT_MODE_NAME, expecting); + protected String getPropertyPrefix() { + return "antlr-" + getLanguage().toLowerCase(); } - List checkLexerDFA(String gtext, String modeName, String expecting) - throws Exception - { - ErrorQueue equeue = new ErrorQueue(); - LexerGrammar g = new LexerGrammar(gtext, equeue); - g.atn = createATN(g, false); -// LexerATNToDFAConverter conv = new LexerATNToDFAConverter(g); -// DFA dfa = conv.createDFA(modeName); -// g.setLookaheadDFA(0, dfa); // only one decision to worry about -// -// String result = null; -// if ( dfa!=null ) result = dfa.toString(); -// assertEquals(expecting, result); -// -// return equeue.all; - return null; - } protected abstract String getLanguage(); - protected String execLexer(String grammarFileName, - String grammarStr, - String lexerName, - String input) - { - return execLexer(grammarFileName, grammarStr, lexerName, input, false); - } - @Override public String execLexer(String grammarFileName, String grammarStr, @@ -355,31 +49,12 @@ public String execLexer(String grammarFileName, null, lexerName,"-no-listener"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); - String output = execModule("Test.py"); - return output; - } - - public ParseTree execStartRule(String startRuleName, Parser parser) - throws IllegalAccessException, InvocationTargetException, - NoSuchMethodException - { - Method startRule = null; - Object[] args = null; - try { - startRule = parser.getClass().getMethod(startRuleName); - } - catch (NoSuchMethodException nsme) { - // try with int _p arg for recursive func - startRule = parser.getClass().getMethod(startRuleName, int.class); - args = new Integer[] {0}; - } - ParseTree result = (ParseTree)startRule.invoke(parser, args); -// System.out.println("parse tree = "+result.toStringTree(parser)); - return result; + return execModule("Test.py"); } + @Override public String execParser(String grammarFileName, String grammarStr, String parserName, @@ -410,7 +85,7 @@ public String execParser_(String grammarFileName, lexerName, "-visitor"); assertTrue(success); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); rawBuildRecognizerTestFile(parserName, lexerName, listenerName, @@ -439,8 +114,7 @@ protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, boolean defaultListener, String... extraOptions) { - ErrorQueue equeue = - antlrOnString(getTmpDir(), getLanguage(), grammarFileName, grammarStr, defaultListener, extraOptions); + ErrorQueue equeue = antlrOnString(getTempDirPath(), getLanguage(), grammarFileName, grammarStr, defaultListener, extraOptions); if (!equeue.errors.isEmpty()) { return false; } @@ -470,7 +144,7 @@ protected void rawBuildRecognizerTestFile(String parserName, boolean debug, boolean trace) { - this.stderrDuringParse = null; + setParseErrors(null); if ( parserName==null ) { writeLexerTestFile(lexerName, false); } @@ -491,7 +165,7 @@ public String execRecognizer() { public String execModule(String fileName) { String pythonPath = locatePython(); String runtimePath = locateRuntime(); - File tmpdirFile = new File(tmpdir); + File tmpdirFile = new File(getTempDirPath()); String modulePath = new File(tmpdirFile, fileName).getAbsolutePath(); String inputPath = new File(tmpdirFile, "input").getAbsolutePath(); Path outputPath = tmpdirFile.toPath().resolve("output").toAbsolutePath(); @@ -507,7 +181,7 @@ public String execModule(String fileName) { stderrVacuum.join(); String output = TestOutputReading.read(outputPath); if ( stderrVacuum.toString().length()>0 ) { - this.stderrDuringParse = stderrVacuum.toString(); + setParseErrors(stderrVacuum.toString()); } return output; } @@ -560,190 +234,6 @@ protected String locateRuntime(String targetName) { return runtimeSrc.getPath(); } - private boolean isWindows() { - return System.getProperty("os.name").toLowerCase().contains("windows"); - } - -// void ambig(List msgs, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// ambig(msgs, 0, expectedAmbigAlts, expectedAmbigInput); -// } - -// void ambig(List msgs, int i, int[] expectedAmbigAlts, String expectedAmbigInput) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, AmbiguityMessage.class); -// AmbiguityMessage a = (AmbiguityMessage)amsgs.get(i); -// if ( a==null ) assertNull(expectedAmbigAlts); -// else { -// assertEquals(a.conflictingAlts.toString(), Arrays.toString(expectedAmbigAlts)); -// } -// assertEquals(expectedAmbigInput, a.input); -// } - -// void unreachable(List msgs, int[] expectedUnreachableAlts) -// throws Exception -// { -// unreachable(msgs, 0, expectedUnreachableAlts); -// } - -// void unreachable(List msgs, int i, int[] expectedUnreachableAlts) -// throws Exception -// { -// List amsgs = getMessagesOfType(msgs, UnreachableAltsMessage.class); -// UnreachableAltsMessage u = (UnreachableAltsMessage)amsgs.get(i); -// if ( u==null ) assertNull(expectedUnreachableAlts); -// else { -// assertEquals(u.conflictingAlts.toString(), Arrays.toString(expectedUnreachableAlts)); -// } -// } - - List getMessagesOfType(List msgs, Class c) { - List filtered = new ArrayList(); - for (ANTLRMessage m : msgs) { - if ( m.getClass() == c ) filtered.add(m); - } - return filtered; - } - - void checkRuleATN(Grammar g, String ruleName, String expecting) { - ParserATNFactory f = new ParserATNFactory(g); - ATN atn = f.createATN(); - - DOTGenerator dot = new DOTGenerator(g); - System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule(ruleName).index])); - - Rule r = g.getRule(ruleName); - ATNState startState = atn.ruleToStartState[r.index]; - ATNPrinter serializer = new ATNPrinter(g, startState); - String result = serializer.asString(); - - //System.out.print(result); - assertEquals(expecting, result); - } - - public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException { - int lp = templates.indexOf('('); - String name = templates.substring(0, lp); - STGroup group = new STGroupString(templates); - ST st = group.getInstanceOf(name); - st.add(actionName, action); - String grammar = st.render(); - ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(grammar, equeue); - if ( g.ast!=null && !g.ast.hasErrors ) { - SemanticPipeline sem = new SemanticPipeline(g); - sem.process(); - - ATNFactory factory = new ParserATNFactory(g); - if ( g.isLexer() ) factory = new LexerATNFactory((LexerGrammar)g); - g.atn = factory.createATN(); - - CodeGenerator gen = new CodeGenerator(g); - ST outputFileST = gen.generateParser(); - String output = outputFileST.render(); - //System.out.println(output); - String b = "#" + actionName + "#"; - int start = output.indexOf(b); - String e = "#end-" + actionName + "#"; - int end = output.indexOf(e); - String snippet = output.substring(start+b.length(),end); - assertEquals(expected, snippet); - } - if ( equeue.size()>0 ) { - System.err.println(equeue.toString()); - } - } - - protected void checkGrammarSemanticsError(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception - { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if ( equeue.size()!=1 ) { - System.err.println(equeue); - } - } - - protected void checkGrammarSemanticsWarning(ErrorQueue equeue, - GrammarSemanticsMessage expectedMessage) - throws Exception - { - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.warnings.size(); i++) { - ANTLRMessage m = equeue.warnings.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertNotNull("no error; "+expectedMessage.getErrorType()+" expected", foundMsg); - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs())); - if ( equeue.size()!=1 ) { - System.err.println(equeue); - } - } - - protected void checkError(ErrorQueue equeue, - ANTLRMessage expectedMessage) - throws Exception - { - //System.out.println("errors="+equeue); - ANTLRMessage foundMsg = null; - for (int i = 0; i < equeue.errors.size(); i++) { - ANTLRMessage m = equeue.errors.get(i); - if (m.getErrorType()==expectedMessage.getErrorType() ) { - foundMsg = m; - } - } - assertTrue("no error; "+expectedMessage.getErrorType()+" expected", !equeue.errors.isEmpty()); - assertTrue("too many errors; "+equeue.errors, equeue.errors.size()<=1); - assertNotNull("couldn't find expected error: "+expectedMessage.getErrorType(), foundMsg); - /* - assertTrue("error is not a GrammarSemanticsMessage", - foundMsg instanceof GrammarSemanticsMessage); - */ - assertArrayEquals(expectedMessage.getArgs(), foundMsg.getArgs()); - } - - public static class FilteringTokenStream extends CommonTokenStream { - public FilteringTokenStream(TokenSource src) { super(src); } - Set hide = new HashSet(); - @Override - protected boolean sync(int i) { - if (!super.sync(i)) { - return false; - } - - Token t = get(i); - if ( hide.contains(t.getType()) ) { - ((WritableToken)t).setChannel(Token.HIDDEN_CHANNEL); - } - - return true; - } - public void setTokenTypeChannel(int ttype, int channel) { - hide.add(ttype); - } - } - - protected void mkdir(String dir) { - File f = new File(dir); - f.mkdirs(); - } - protected abstract void writeParserTestFile(String parserName, String lexerName, String listenerName, @@ -756,219 +246,13 @@ protected abstract void writeParserTestFile(String parserName, protected abstract void writeLexerTestFile(String lexerName, boolean showDFA); - public void writeRecognizer(String parserName, String lexerName, - String listenerName, String visitorName, - String parserStartRuleName, boolean debug, boolean trace) { - if ( parserName==null ) { - writeLexerTestFile(lexerName, debug); - } - else { - writeParserTestFile(parserName, - lexerName, - listenerName, - visitorName, - parserStartRuleName, - debug, - trace); - } - } - - - protected void eraseFiles(final String filesEndingWith) { - File tmpdirF = new File(tmpdir); - String[] files = tmpdirF.list(); - for(int i = 0; files!=null && i < files.length; i++) { - if ( files[i].endsWith(filesEndingWith) ) { - new File(tmpdir+"/"+files[i]).delete(); - } - } - } - - protected void eraseFiles(File dir) { - String[] files = dir.list(); - for(int i = 0; files!=null && i < files.length; i++) { - new File(dir,files[i]).delete(); - } - } - - @Override - public void eraseTempDir() { - if(shouldEraseTempDir()) { - File tmpdirF = new File(tmpdir); - if ( tmpdirF.exists() ) { - eraseFiles(tmpdirF); - tmpdirF.delete(); - } - } - } - - private boolean shouldEraseTempDir() { - if(tmpdir==null) - return false; - String propName = getPropertyPrefix() + "-erase-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) - return Boolean.getBoolean(prop); - else - return true; - } protected void eraseTempPyCache() { - File tmpdirF = new File(tmpdir+"/__pycache__"); + File tmpdirF = new File(getTempTestDir() + "/__pycache__"); if ( tmpdirF.exists() ) { - eraseFiles(tmpdirF); + eraseFilesInDir(tmpdirF); tmpdirF.delete(); } } - public String getFirstLineOfException() { - if ( this.stderrDuringParse ==null ) { - return null; - } - String[] lines = this.stderrDuringParse.split("\n"); - String prefix="Exception in thread \"main\" "; - return lines[0].substring(prefix.length(),lines[0].length()); - } - - /** - * When looking at a result set that consists of a Map/HashTable - * we cannot rely on the output order, as the hashing algorithm or other aspects - * of the implementation may be different on differnt JDKs or platforms. Hence - * we take the Map, convert the keys to a List, sort them and Stringify the Map, which is a - * bit of a hack, but guarantees that we get the same order on all systems. We assume that - * the keys are strings. - * - * @param m The Map that contains keys we wish to return in sorted order - * @return A string that represents all the keys in sorted order. - */ - public String sortMapToString(Map m) { - // Pass in crap, and get nothing back - // - if (m == null) { - return null; - } - - System.out.println("Map toString looks like: " + m.toString()); - - // Sort the keys in the Map - // - TreeMap nset = new TreeMap(m); - - System.out.println("Tree map looks like: " + nset.toString()); - return nset.toString(); - } - - public List realElements(List elements) { - return elements.subList(Token.MIN_USER_TOKEN_TYPE, elements.size()); - } - - public void assertNotNullOrEmpty(String message, String text) { - assertNotNull(message, text); - assertFalse(message, text.isEmpty()); - } - - public void assertNotNullOrEmpty(String text) { - assertNotNull(text); - assertFalse(text.isEmpty()); - } - - public static class IntTokenStream implements TokenStream { - IntegerList types; - int p=0; - public IntTokenStream(IntegerList types) { this.types = types; } - - @Override - public void consume() { p++; } - - @Override - public int LA(int i) { return LT(i).getType(); } - - @Override - public int mark() { - return index(); - } - - @Override - public int index() { return p; } - - @Override - public void release(int marker) { - seek(marker); - } - - @Override - public void seek(int index) { - p = index; - } - - @Override - public int size() { - return types.size(); - } - - @Override - public String getSourceName() { - return null; - } - - @Override - public Token LT(int i) { - CommonToken t; - int rawIndex = p + i - 1; - if ( rawIndex>=types.size() ) t = new CommonToken(Token.EOF); - else t = new CommonToken(types.get(rawIndex)); - t.setTokenIndex(rawIndex); - return t; - } - - @Override - public Token get(int i) { - return new org.antlr.v4.runtime.CommonToken(types.get(i)); - } - - @Override - public TokenSource getTokenSource() { - return null; - } - - @Override - public String getText() { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Interval interval) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(RuleContext ctx) { - throw new UnsupportedOperationException("can't give strings"); - } - - @Override - public String getText(Token start, Token stop) { - throw new UnsupportedOperationException("can't give strings"); - } - } - - /** Sort a list */ - public > List sort(List data) { - List dup = new ArrayList(); - dup.addAll(data); - Collections.sort(dup); - return dup; - } - - /** Return map sorted by key */ - public ,V> LinkedHashMap sort(Map data) { - LinkedHashMap dup = new LinkedHashMap(); - List keys = new ArrayList(); - keys.addAll(data.keySet()); - Collections.sort(keys); - for (K k : keys) { - dup.put(k, data.get(k)); - } - return dup; - } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/BasePython2Test.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/BasePython2Test.java index ced2665c20..7167a290ee 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/BasePython2Test.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python2/BasePython2Test.java @@ -47,7 +47,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { : "") + "\n" + "if __name__ == '__main__':\n" + " main(sys.argv)\n" + "\n"); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.py", outputFileST.render()); + writeFile(getTempDirPath(), "Test.py", outputFileST.render()); } @Override @@ -105,6 +105,6 @@ protected void writeParserTestFile(String parserName, String lexerName, outputFileST.add("listenerName", listenerName); outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.py", outputFileST.render()); + writeFile(getTempDirPath(), "Test.py", outputFileST.render()); } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/BasePython3Test.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/BasePython3Test.java index 0c0bc814a6..834d99174f 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/BasePython3Test.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python3/BasePython3Test.java @@ -44,7 +44,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) { : "") + "\n" + "if __name__ == '__main__':\n" + " main(sys.argv)\n" + "\n"); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "Test.py", outputFileST.render()); + writeFile(getTempDirPath(), "Test.py", outputFileST.render()); } @Override @@ -102,6 +102,6 @@ protected void writeParserTestFile(String parserName, String lexerName, outputFileST.add("listenerName", listenerName); outputFileST.add("visitorName", visitorName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "Test.py", outputFileST.render()); + writeFile(getTempDirPath(), "Test.py", outputFileST.render()); } } diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java index 2efd08a644..d142bb5d1e 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java @@ -7,13 +7,7 @@ package org.antlr.v4.test.runtime.swift; import org.antlr.v4.runtime.misc.Pair; -import org.antlr.v4.test.runtime.ErrorQueue; -import org.antlr.v4.test.runtime.RuntimeTestDescriptor; -import org.antlr.v4.test.runtime.RuntimeTestSupport; -import org.antlr.v4.test.runtime.StreamVacuum; -import org.junit.rules.TestRule; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; +import org.antlr.v4.test.runtime.*; import org.stringtemplate.v4.ST; import java.io.BufferedReader; @@ -24,11 +18,11 @@ import java.util.*; import static org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString; -import static org.antlr.v4.test.runtime.BaseRuntimeTest.mkdir; +import static org.antlr.v4.test.runtime.RuntimeTestUtils.mkdir; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; import static org.junit.Assert.assertTrue; -public class BaseSwiftTest implements RuntimeTestSupport { +public class BaseSwiftTest extends BaseRuntimeTestSupport implements RuntimeTestSupport { private static final boolean USE_ARCH_ARM64 = false; private static final boolean VERBOSE = false; @@ -81,91 +75,16 @@ public void run() { }); } - public String tmpdir = null; - - /** - * If error during parser execution, store stderr here; can't return - * stdout and stderr. This doesn't trap errors from running antlr. - */ - private String stderrDuringParse; - - /** - * Errors found while running antlr - */ - private StringBuilder antlrToolErrors; + @Override + protected String getPropertyPrefix() { + return "antrl4-swift"; + } /** * Source files used in each small swift project. */ private final Set sourceFiles = new HashSet<>(); - @org.junit.Rule - public final TestRule testWatcher = new TestWatcher() { - - @Override - protected void succeeded(Description description) { - // remove tmpdir if no error. - eraseTempDir(); - } - - }; - - @Override - public void testSetUp() throws Exception { - // new output dir for each test - String propName = "antlr-swift-test-dir"; - String prop = System.getProperty(propName); - if (prop != null && prop.length() > 0) { - tmpdir = prop; - } - else { - String classSimpleName = getClass().getSimpleName(); - String threadName = Thread.currentThread().getName(); - String childPath = String.format("%s-%s-%s", classSimpleName, threadName, System.currentTimeMillis()); - tmpdir = new File(System.getProperty("java.io.tmpdir"), childPath).getAbsolutePath(); - } - antlrToolErrors = new StringBuilder(); - } - - @Override - public void testTearDown() throws Exception { - } - - @Override - public void beforeTest(RuntimeTestDescriptor descriptor) { - System.out.println(descriptor.getTestName()); - } - - @Override - public void afterTest(RuntimeTestDescriptor descriptor) { - } - - @Override - public void eraseTempDir() { - } - - @Override - public String getTmpDir() { - return tmpdir; - } - - @Override - public String getStdout() { - return null; - } - - @Override - public String getParseErrors() { - return stderrDuringParse; - } - - @Override - public String getANTLRToolErrors() { - if (antlrToolErrors.length() == 0) { - return null; - } - return antlrToolErrors.toString(); - } @Override public String execLexer(String grammarFileName, String grammarStr, String lexerName, String input, boolean showDFA) { @@ -173,12 +92,12 @@ public String execLexer(String grammarFileName, String grammarStr, String lexerN grammarStr, null, lexerName); - writeFile(tmpdir, "input", input); + writeFile(getTempDirPath(), "input", input); writeLexerTestFile(lexerName, showDFA); addSourceFiles("main.swift"); String projectName = "testcase-" + System.currentTimeMillis(); - String projectDir = getTmpDir() + "/" + projectName; + String projectDir = new File(getTempTestDir(), projectName).getAbsolutePath(); try { buildProject(projectDir, projectName); return execTest(projectDir, projectName); @@ -196,7 +115,7 @@ public String execParser(String grammarFileName, String grammarStr, String parse parserName, lexerName, "-visitor"); - writeFile(getTmpDir(), "input", input); + writeFile(getTempDirPath(), "input", input); return execParser(parserName, lexerName, startRuleName, @@ -207,7 +126,7 @@ private String execTest(String projectDir, String projectName) { try { Pair output = runProcess(projectDir, "./.build/debug/" + projectName, "input"); if (output.b.length() > 0) { - stderrDuringParse = output.b; + setParseErrors(output.b); } String stdout = output.a; return stdout.length() > 0 ? stdout : null; @@ -227,10 +146,10 @@ private void buildProject(String projectDir, String projectName) throws IOExcept mkdir(projectDir); fastFailRunProcess(projectDir, SWIFT_CMD, "package", "init", "--type", "executable"); for (String sourceFile: sourceFiles) { - String absPath = getTmpDir() + "/" + sourceFile; - fastFailRunProcess(getTmpDir(), "mv", "-f", absPath, projectDir + "/Sources/" + projectName); + String absPath = new File(getTempTestDir(), sourceFile).getAbsolutePath(); + fastFailRunProcess(getTempDirPath(), "mv", "-f", absPath, projectDir + "/Sources/" + projectName); } - fastFailRunProcess(getTmpDir(), "mv", "-f", "input", projectDir); + fastFailRunProcess(getTempDirPath(), "mv", "-f", "input", projectDir); String dylibPath = ANTLR_RUNTIME_PATH + "/.build/debug/"; // System.err.println(dylibPath); Pair buildResult = runProcess(projectDir, SWIFT_CMD, "build", @@ -356,7 +275,7 @@ private String execParser(String parserName, addSourceFiles("main.swift"); String projectName = "testcase-" + System.currentTimeMillis(); - String projectDir = getTmpDir() + "/" + projectName; + String projectDir = new File(getTempTestDir(), projectName).getAbsolutePath(); try { buildProject(projectDir, projectName); return execTest(projectDir, projectName); @@ -421,7 +340,7 @@ private void writeParserTestFile(String parserName, outputFileST.add("parserName", parserName); outputFileST.add("lexerName", lexerName); outputFileST.add("parserStartRuleName", parserStartRuleName); - writeFile(tmpdir, "main.swift", outputFileST.render()); + writeFile(getTempDirPath(), "main.swift", outputFileST.render()); } private void writeLexerTestFile(String lexerName, boolean showDFA) { @@ -443,7 +362,7 @@ private void writeLexerTestFile(String lexerName, boolean showDFA) { (showDFA ? "print(lex.getInterpreter().getDFA(Lexer.DEFAULT_MODE).toLexerString(), terminator: \"\" )\n" : "")); outputFileST.add("lexerName", lexerName); - writeFile(tmpdir, "main.swift", outputFileST.render()); + writeFile(getTempDirPath(), "main.swift", outputFileST.render()); } /** @@ -454,7 +373,7 @@ private void generateParser(String grammarFileName, String parserName, String lexerName, String... extraOptions) { - ErrorQueue equeue = antlrOnString(getTmpDir(), "Swift", grammarFileName, grammarStr, false, extraOptions); + ErrorQueue equeue = antlrOnString(getTempDirPath(), "Swift", grammarFileName, grammarStr, false, extraOptions); assertTrue(equeue.errors.isEmpty()); // System.out.println(getTmpDir()); @@ -480,4 +399,5 @@ private void generateParser(String grammarFileName, } addSourceFiles(files.toArray(new String[0])); } + } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/BaseJavaToolTest.java b/tool-testsuite/test/org/antlr/v4/test/tool/BaseJavaToolTest.java index 6c2dd465f8..f3d0f7177c 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/BaseJavaToolTest.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/BaseJavaToolTest.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertEquals; public class BaseJavaToolTest extends BaseJavaTest { + public void testErrors(String[] pairs, boolean printTree) { for (int i = 0; i < pairs.length; i+=2) { String grammarStr = pairs[i]; @@ -23,10 +24,10 @@ public void testErrors(String[] pairs, boolean printTree) { String[] lines = grammarStr.split("\n"); String fileName = getFilenameFromFirstLineOfGrammar(lines[0]); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, null, fileName, grammarStr, false); // use default language target in case test overrides + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), null, fileName, grammarStr, false); // use default language target in case test overrides String actual = equeue.toString(true); - actual = actual.replace(tmpdir + File.separator, ""); + actual = actual.replace(getTempDirPath() + File.separator, ""); // System.err.println(actual); String msg = grammarStr; msg = msg.replace("\n","\\n"); diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNInterpreter.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNInterpreter.java index 201463ed45..758d565182 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNInterpreter.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNInterpreter.java @@ -9,12 +9,14 @@ import org.antlr.v4.automata.ParserATNFactory; import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.NoViableAltException; +import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.BlockStartState; import org.antlr.v4.runtime.atn.LexerATNSimulator; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntegerList; +import org.antlr.v4.test.runtime.MockIntTokenStream; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; @@ -22,6 +24,7 @@ import org.junit.Before; import org.junit.Test; +import static org.antlr.v4.test.runtime.RuntimeTestUtils.getTokenTypesViaATN; import static org.junit.Assert.assertEquals; // NOTICE: TOKENS IN LEXER, PARSER MUST BE SAME OR TOKEN TYPE MISMATCH @@ -373,7 +376,7 @@ public void checkMatchedAlt(LexerGrammar lg, final Grammar g, ParserATNFactory f = new ParserATNFactory(g); ATN atn = f.createATN(); - IntTokenStream input = new IntTokenStream(types); + TokenStream input = new MockIntTokenStream(types); // System.out.println("input="+input.types); ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input); ATNState startState = atn.ruleToStartState[g.getRule("a").index]; diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNLexerInterpreter.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNLexerInterpreter.java index 0d06a031b0..4cd34deec6 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNLexerInterpreter.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNLexerInterpreter.java @@ -11,6 +11,7 @@ import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.misc.Utils; +import org.antlr.v4.test.runtime.RuntimeTestUtils; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.LexerGrammar; import org.junit.Before; @@ -386,7 +387,7 @@ protected void checkLexerMatches(LexerGrammar lg, String inputString, String exp DOTGenerator dot = new DOTGenerator(lg); // System.out.println(dot.getDOT(startState, true)); - List tokenTypes = getTokenTypes(lg, atn, input); + List tokenTypes = RuntimeTestUtils.getTokenTypes(lg, atn, input); String result = Utils.join(tokenTypes.iterator(), ", "); // System.out.println(tokenTypes); diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNParserPrediction.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNParserPrediction.java index 9e58fe9d7e..3fb0dc9e61 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestATNParserPrediction.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestATNParserPrediction.java @@ -17,6 +17,7 @@ import org.antlr.v4.runtime.atn.PredictionContextCache; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.IntegerList; +import org.antlr.v4.test.runtime.MockIntTokenStream; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LeftRecursiveRule; @@ -27,6 +28,7 @@ import java.util.Arrays; +import static org.antlr.v4.test.runtime.RuntimeTestUtils.getTokenTypesViaATN; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -526,7 +528,7 @@ public void checkPredictedAlt(LexerGrammar lg, Grammar g, int decision, // Check ATN prediction // ParserATNSimulator interp = new ParserATNSimulator(atn); - TokenStream input = new IntTokenStream(types); + TokenStream input = new MockIntTokenStream(types); ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input); int alt = interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY); @@ -559,7 +561,7 @@ public void checkDFAConstruction(LexerGrammar lg, Grammar g, int decision, // Check DFA IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp); // System.out.println(types); - TokenStream input = new IntTokenStream(types); + TokenStream input = new MockIntTokenStream(types); try { interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY); } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestCompositeGrammars.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestCompositeGrammars.java index 03368785be..330ca7fa77 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestCompositeGrammars.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestCompositeGrammars.java @@ -8,6 +8,7 @@ import org.antlr.v4.test.runtime.BaseRuntimeTest; import org.antlr.v4.test.runtime.ErrorQueue; +import org.antlr.v4.test.runtime.RuntimeTestUtils; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.ErrorType; import org.antlr.v4.tool.Grammar; @@ -18,6 +19,7 @@ import java.io.File; import static org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile; +import static org.antlr.v4.test.runtime.RuntimeTestUtils.sort; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -34,9 +36,9 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - String subdir = tmpdir + "/sub"; - BaseRuntimeTest.mkdir(subdir); + RuntimeTestUtils.mkdir(getTempDirPath()); + String subdir = getTempDirPath() + PATH_SEP + "sub"; + RuntimeTestUtils.mkdir(subdir); writeFile(subdir, "S.g4", slave); String master = "grammar M;\n" + @@ -44,65 +46,65 @@ public void testSetUp() throws Exception { "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "M.g4", master); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", subdir); + writeFile(getTempDirPath(), "M.g4", master); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", subdir); assertEquals(0, equeue.size()); } // Test for https://github.com/antlr/antlr4/issues/1317 @Test public void testImportSelfLoop() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "grammar M;\n" + "import M;\n" + "s : 'a' ;\n"; - writeFile(tmpdir, "M.g4", master); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + writeFile(getTempDirPath(), "M.g4", master); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.size()); } @Test public void testImportIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + "import S;\n" + "A : 'a';\n" + "B : 'b';\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "C : 'c';\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } @Test public void testImportModesIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + "import S;\n" + "A : 'a' -> pushMode(X);\n" + "B : 'b';\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } - + @Test public void testImportChannelsIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + @@ -110,19 +112,19 @@ public void testSetUp() throws Exception { "channels {CH_A, CH_B}\n" + "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "C : 'c';\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } - + @Test public void testImportMixedChannelsIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + @@ -130,20 +132,20 @@ public void testSetUp() throws Exception { "channels {CH_A, CH_B}\n" + "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "channels {CH_C}\n" + "C : 'c' -> channel(CH_C);\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } @Test public void testImportClashingChannelsIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + @@ -152,20 +154,20 @@ public void testSetUp() throws Exception { "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n" + "C : 'C' -> channel(CH_C);\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "channels {CH_C}\n" + "C : 'c' -> channel(CH_C);\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); - } - + } + @Test public void testMergeModesIntoLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + @@ -173,43 +175,43 @@ public void testSetUp() throws Exception { "A : 'a' -> pushMode(X);\n" + "mode X;\n" + "B : 'b';\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } - + @Test public void testEmptyModesInLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "lexer grammar M;\n" + "import S;\n" + "A : 'a';\n" + - "C : 'e';\n" + + "C : 'e';\n" + "B : 'b';\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(0, equeue.errors.size()); } - + @Test public void testCombinedGrammarImportsModalLexerGrammar() throws Exception { - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); String master = "grammar M;\n" + @@ -217,16 +219,16 @@ public void testSetUp() throws Exception { "A : 'a';\n" + "B : 'b';\n" + "r : A B;\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); - String slave = + String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n"; - writeFile(tmpdir, "S.g4", slave); + writeFile(getTempDirPath(), "S.g4", slave); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); assertEquals(1, equeue.errors.size()); ANTLRMessage msg = equeue.errors.get(0); assertEquals(ErrorType.MODE_NOT_IN_LEXER, msg.getErrorType()); @@ -234,7 +236,7 @@ public void testSetUp() throws Exception { assertEquals(3, msg.line); assertEquals(5, msg.charPosition); assertEquals("M.g4", new File(msg.fileName).getName()); - } + } @Test public void testDelegatesSeeSameTokenType() throws Exception { String slaveS = @@ -246,9 +248,9 @@ public void testSetUp() throws Exception { "tokens { C, B, A } // reverse order\n"+ "y : A ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slaveS); - writeFile(tmpdir, "T.g4", slaveT); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slaveS); + writeFile(getTempDirPath(), "T.g4", slaveT); String master = "// The lexer will create rules to match letters a, b, c.\n"+ @@ -268,9 +270,9 @@ public void testSetUp() throws Exception { "A : 'a' ;\n"+ "C : 'c' ;\n"+ "WS : (' '|'\\n') -> skip ;\n"; - writeFile(tmpdir, "M.g4", master); + writeFile(getTempDirPath(), "M.g4", master); ErrorQueue equeue = new ErrorQueue(); - Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); + Grammar g = new Grammar(getTempDirPath()+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, B=1, A=2, C=3, WS=4}"; String expectedStringLiteralToTypeMap = "{'a'=2, 'b'=1, 'c'=3}"; String expectedTypeToTokenList = "[B, A, C, WS]"; @@ -284,13 +286,13 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : 'a' | c;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave); String master = "grammar M;\n" + "import S;\n"; - writeFile(tmpdir, "M.g4", master); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir); + writeFile(getTempDirPath(), "M.g4", master); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath()); ANTLRMessage msg = equeue.errors.get(0); assertEquals(ErrorType.UNDEFINED_RULE_REF, msg.getErrorType()); assertEquals("c", msg.getArgs()[0]); @@ -303,9 +305,9 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - String outdir = tmpdir + "/out"; - BaseRuntimeTest.mkdir(outdir); + RuntimeTestUtils.mkdir(getTempDirPath()); + String outdir = getTempDirPath() + "/out"; + RuntimeTestUtils.mkdir(outdir); writeFile(outdir, "S.g4", slave); String master = "grammar M;\n" + @@ -313,8 +315,8 @@ public void testSetUp() throws Exception { "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "M.g4", master); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir); + writeFile(getTempDirPath(), "M.g4", master); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-o", outdir); assertEquals(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, equeue.errors.get(0).getErrorType()); } @@ -322,9 +324,9 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - String subdir = tmpdir + "/sub"; - BaseRuntimeTest.mkdir(subdir); + RuntimeTestUtils.mkdir(getTempDirPath()); + String subdir = getTempDirPath() + "/sub"; + RuntimeTestUtils.mkdir(subdir); writeFile(subdir, "S.g4", slave); String master = "grammar M;\n" + @@ -332,10 +334,10 @@ public void testSetUp() throws Exception { "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "M.g4", master); - String outdir = tmpdir + "/out"; - BaseRuntimeTest.mkdir(outdir); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir, "-lib", subdir); + writeFile(getTempDirPath(), "M.g4", master); + String outdir = getTempDirPath() + "/out"; + RuntimeTestUtils.mkdir(outdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-o", outdir, "-lib", subdir); assertEquals(0, equeue.size()); } @@ -343,26 +345,26 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - String subdir = tmpdir + "/sub"; - BaseRuntimeTest.mkdir(subdir); + RuntimeTestUtils.mkdir(getTempDirPath()); + String subdir = getTempDirPath() + "/sub"; + RuntimeTestUtils.mkdir(subdir); writeFile(subdir, "S.g4", slave); String parser = "parser grammar MParser;\n" + "import S;\n" + "options {tokenVocab=MLexer;}\n" + "s : a ;\n"; - writeFile(tmpdir, "MParser.g4", parser); + writeFile(getTempDirPath(), "MParser.g4", parser); String lexer = "lexer grammar MLexer;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "MLexer.g4", lexer); - String outdir = tmpdir + "/out"; - BaseRuntimeTest.mkdir(outdir); - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MLexer.g4", false, "-o", outdir); + writeFile(getTempDirPath(), "MLexer.g4", lexer); + String outdir = getTempDirPath() + "/out"; + RuntimeTestUtils.mkdir(outdir); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "MLexer.g4", false, "-o", outdir); assertEquals(0, equeue.size()); - equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MParser.g4", false, "-o", outdir, "-lib", subdir); + equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "MParser.g4", false, "-o", outdir, "-lib", subdir); assertEquals(0, equeue.size()); } @@ -373,16 +375,16 @@ public void testSetUp() throws Exception { "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "M.g4", master); - Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); + writeFile(getTempDirPath(), "M.g4", master); + Grammar g = new Grammar(getTempDirPath()+"/M.g4", master, equeue); Object expectedArg = "S"; ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE; @@ -399,16 +401,16 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "options {toke\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n" ; - writeFile(tmpdir, "M.g4", master); - /*Grammar g =*/ new Grammar(tmpdir+"/M.g4", master, equeue); + writeFile(getTempDirPath(), "M.g4", master); + /*Grammar g =*/ new Grammar(getTempDirPath()+"/M.g4", master, equeue); assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType()); } @@ -419,21 +421,21 @@ public void testSetUp() throws Exception { String slave = "parser grammar T;\n" + "a : T ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "T.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "T.g4", slave); String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave2); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave2); String master = "grammar M;\n" + "import S;\n" + "a : M ;\n" ; - writeFile(tmpdir, "M.g4", master); - Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); + writeFile(getTempDirPath(), "M.g4", master); + Grammar g = new Grammar(getTempDirPath()+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, M=1}"; // S and T aren't imported; overridden String expectedStringLiteralToTypeMap = "{}"; @@ -459,43 +461,43 @@ public void testSetUp() throws Exception { "parser grammar T;\n" + "tokens{T}\n" + "x : T ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "T.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "T.g4", slave); slave = "parser grammar S;\n" + "import T;\n" + "tokens{S}\n" + "y : S ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave); slave = "parser grammar C;\n" + "tokens{C}\n" + "i : C ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "C.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "C.g4", slave); slave = "parser grammar B;\n" + "tokens{B}\n" + "j : B ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "B.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "B.g4", slave); slave = "parser grammar A;\n" + "import B,C;\n" + "tokens{A}\n" + "k : A ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "A.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "A.g4", slave); String master = "grammar M;\n" + "import S,A;\n" + "tokens{M}\n" + "a : M ;\n" ; - writeFile(tmpdir, "M.g4", master); - Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); + writeFile(getTempDirPath(), "M.g4", master); + Grammar g = new Grammar(getTempDirPath()+"/M.g4", master, equeue); assertEquals("[]", equeue.errors.toString()); assertEquals("[]", equeue.warnings.toString()); @@ -520,21 +522,21 @@ public void testSetUp() throws Exception { String slave = "parser grammar T;\n" + "x : T ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "T.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "T.g4", slave); String slave2 = "parser grammar S;\n" + // A, B, C token type order "import T;\n" + "a : S ;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave2); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave2); String master = "grammar M;\n" + "import S;\n" + "a : M x ;\n" ; // x MUST BE VISIBLE TO M - writeFile(tmpdir, "M.g4", master); - Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue); + writeFile(getTempDirPath(), "M.g4", master); + Grammar g = new Grammar(getTempDirPath()+"/M.g4", master, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, M=1, T=2}"; String expectedStringLiteralToTypeMap = "{}"; @@ -558,30 +560,30 @@ public void testSetUp() throws Exception { "T2: '2';\n" + "T3: '3';\n" + "T4: '4';\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "L.g4", gstr); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "L.g4", gstr); gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "G1.g4", gstr); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "G1.g4", gstr); gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "G2.g4", gstr); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "G2.g4", gstr); String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n" ; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "G3.g4", G3str); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "G3.g4", G3str); - Grammar g = new Grammar(tmpdir+"/G3.g4", G3str, equeue); + Grammar g = new Grammar(getTempDirPath()+"/G3.g4", G3str, equeue); String expectedTokenIDToTypeMap = "{EOF=-1, T4=1, T3=2}"; String expectedStringLiteralToTypeMap = "{}"; @@ -605,8 +607,8 @@ public void testSetUp() throws Exception { String slave = "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "S.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "S.g4", slave); String master = "grammar M;\n" + "import S;\n" + @@ -614,7 +616,7 @@ public void testSetUp() throws Exception { "s : a ;\n" + "B : 'b' ;" + // defines B from inherited token space "WS : (' '|'\\n') -> skip ;\n" ; - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", master, false); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", master, false); int expecting = 0; // should be ok assertEquals(expecting, equeue.errors.size()); } @@ -633,12 +635,12 @@ public void testImportLargeGrammar() throws Exception { "grammar NewJava;\n" + "import Java;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "Java.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "Java.g4", slave); String found = execParser("NewJava.g4", master, "NewJavaParser", "NewJavaLexer", null, null, "compilationUnit", "package Foo;", debug); assertEquals(null, found); - assertNull(stderrDuringParse); + assertNull(getParseErrors()); } /** @@ -661,11 +663,11 @@ public void testImportLeftRecursiveGrammar() throws Exception { "import Java;\n" + "s : e ;\n"; - BaseRuntimeTest.mkdir(tmpdir); - writeFile(tmpdir, "Java.g4", slave); + RuntimeTestUtils.mkdir(getTempDirPath()); + writeFile(getTempDirPath(), "Java.g4", slave); String found = execParser("T.g4", master, "TParser", "TLexer", null, null, "s", "a=b", debug); assertEquals(null, found); - assertNull(stderrDuringParse); + assertNull(getParseErrors()); } } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestDollarParser.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestDollarParser.java index 6103e44237..91efcabe11 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestDollarParser.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestDollarParser.java @@ -28,7 +28,7 @@ public void testSimpleCall() throws Exception { String found = execParser("T.g4", grammar, "TParser", "TLexer", null, null, "a", "x", true); assertTrue(found.indexOf(this.getClass().getSimpleName())>=0); - assertNull(this.stderrDuringParse); + assertNull(getParseErrors()); } } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestParserExec.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestParserExec.java index 14005c3c08..0d0265d826 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestParserExec.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestParserExec.java @@ -75,7 +75,7 @@ public void testSetUp() throws Exception { "s1-INT->s2\n" + "s2-EOF->:s3=>1\n"; // Must point at accept state assertEquals(expecting, result); - assertNull(this.stderrDuringParse); + assertNull(getParseErrors()); } /** @@ -88,7 +88,7 @@ public void testSetUp() throws Exception { String grammar = load("Psl.g4", "UTF-8"); String found = execParser("Psl.g4", grammar, "PslParser", "PslLexer", null, null, "floating_constant", " . 234", false); assertEquals(null, found); - assertEquals("line 1:6 rule floating_constant DEC:A floating-point constant cannot have internal white space\n", stderrDuringParse); + assertEquals("line 1:6 rule floating_constant DEC:A floating-point constant cannot have internal white space\n", getParseErrors()); } /** @@ -129,7 +129,7 @@ public void testSetUp() throws Exception { String found = execParser("ModeTagsParser.g4", parserGrammar, "ModeTagsParser", "ModeTagsLexer", null, null, "file", "", false); assertEquals(null, found); - assertNull(stderrDuringParse); + assertNull(getParseErrors()); } /** @@ -158,6 +158,6 @@ public void testSetUp() throws Exception { String found = execParser("Data.g4", grammar, "DataParser", "DataLexer", null, null, "file", input, false); assertEquals("6\n", found); - assertNull(stderrDuringParse); + assertNull(getParseErrors()); } } diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestParserProfiler.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestParserProfiler.java index a5cd58a747..fe89349e0c 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestParserProfiler.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestParserProfiler.java @@ -223,7 +223,7 @@ public void testSetUp() throws Exception { " {decision=1, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, " + "SLL_ATNTransitions=3, SLL_DFATransitions=3, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}]\n"; assertEquals(expecting, found); - assertEquals(null, stderrDuringParse); + assertEquals(null, getParseErrors()); } public DecisionInfo[] interpAndGetDecisionInfo( diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestPerformance.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestPerformance.java index b5bd60662b..823927a0d1 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestPerformance.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestPerformance.java @@ -42,6 +42,7 @@ import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.test.runtime.BaseRuntimeTest; import org.antlr.v4.test.runtime.ErrorQueue; +import org.antlr.v4.test.runtime.RuntimeTestUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -701,20 +702,20 @@ public static String getOptionsDescription(String topPackage) { builder.append(", Grammar=").append(USE_LR_GRAMMAR ? "LR" : "Standard"); builder.append(", ForceAtn=").append(FORCE_ATN); - builder.append(newline); + builder.append(NEW_LINE); builder.append("Op=Lex").append(RUN_PARSER ? "+Parse" : " only"); builder.append(", Strategy=").append(BAIL_ON_ERROR ? BailErrorStrategy.class.getSimpleName() : DefaultErrorStrategy.class.getSimpleName()); builder.append(", BuildParseTree=").append(BUILD_PARSE_TREES); builder.append(", WalkBlankListener=").append(BLANK_LISTENER); - builder.append(newline); + builder.append(NEW_LINE); builder.append("Lexer=").append(REUSE_LEXER ? "setInputStream" : "newInstance"); builder.append(", Parser=").append(REUSE_PARSER ? "setInputStream" : "newInstance"); builder.append(", AfterPass=").append(CLEAR_DFA ? "newInstance" : "setInputStream"); - builder.append(newline); + builder.append(NEW_LINE); return builder.toString(); } @@ -1135,7 +1136,7 @@ private static void updateChecksum(MurmurHashChecksum checksum, Token token) { protected ParserFactory getParserFactory(String lexerName, String parserName, String listenerName, final String entryPoint) { try { - ClassLoader loader = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader()); + ClassLoader loader = new URLClassLoader(new URL[] { getTempTestDir().toURI().toURL() }, ClassLoader.getSystemClassLoader()); final Class lexerClass = loader.loadClass(lexerName).asSubclass(Lexer.class); final Class parserClass = loader.loadClass(parserName).asSubclass(Parser.class); final Class listenerClass = loader.loadClass(listenerName).asSubclass(ParseTreeListener.class); @@ -1952,7 +1953,7 @@ public void testExponentialInclude() { "\n" + "rule_%d_%d : EOF;\n"; - BaseRuntimeTest.mkdir(tmpdir); + RuntimeTestUtils.mkdir(getTempDirPath()); long startTime = System.nanoTime(); @@ -1960,14 +1961,14 @@ public void testExponentialInclude() { for (int level = 0; level < levels; level++) { String leafPrefix = level == levels - 1 ? "//" : ""; String grammar1 = String.format(grammarFormat, level, 1, leafPrefix, level + 1, level + 1, level, 1); - writeFile(tmpdir, "Level_" + level + "_1.g4", grammar1); + writeFile(getTempDirPath(), "Level_" + level + "_1.g4", grammar1); if (level > 0) { String grammar2 = String.format(grammarFormat, level, 2, leafPrefix, level + 1, level + 1, level, 1); - writeFile(tmpdir, "Level_" + level + "_2.g4", grammar2); + writeFile(getTempDirPath(), "Level_" + level + "_2.g4", grammar2); } } - ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "Level_0_1.g4", false); + ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "Level_0_1.g4", false); Assert.assertTrue(equeue.errors.isEmpty()); long endTime = System.nanoTime();