forked from jasder/antlr
add unit tests for -o and -lib; improve BaseTest: refactor writeFile to Utils. -o options and others not accepted by antlr().
This commit is contained in:
parent
31aa7bf5c9
commit
2e352e2697
|
@ -33,13 +33,12 @@ package org.antlr.v4.runtime.misc;
|
||||||
import java.awt.Window;
|
import java.awt.Window;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.Writer;
|
import java.io.OutputStreamWriter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -98,13 +97,25 @@ public class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeFile(String fileName, String content) throws IOException {
|
public static void writeFile(String fileName, String content) throws IOException {
|
||||||
FileWriter fw = new FileWriter(fileName);
|
writeFile(fileName, content, null);
|
||||||
Writer w = new BufferedWriter(fw);
|
}
|
||||||
|
|
||||||
|
public static void writeFile(String fileName, String content, String encoding) throws IOException {
|
||||||
|
File f = new File(fileName);
|
||||||
|
FileOutputStream fos = new FileOutputStream(f);
|
||||||
|
OutputStreamWriter osw;
|
||||||
|
if (encoding != null) {
|
||||||
|
osw = new OutputStreamWriter(fos, encoding);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
osw = new OutputStreamWriter(fos);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
w.write(content);
|
osw.write(content);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
w.close();
|
osw.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,7 @@ import org.antlr.v4.runtime.misc.Interval;
|
||||||
import org.antlr.v4.runtime.misc.NotNull;
|
import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
import org.antlr.v4.runtime.misc.Pair;
|
import org.antlr.v4.runtime.misc.Pair;
|
||||||
|
import org.antlr.v4.runtime.misc.Utils;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import org.antlr.v4.semantics.SemanticPipeline;
|
import org.antlr.v4.semantics.SemanticPipeline;
|
||||||
import org.antlr.v4.tool.ANTLRMessage;
|
import org.antlr.v4.tool.ANTLRMessage;
|
||||||
|
@ -81,13 +82,10 @@ import javax.tools.JavaFileObject;
|
||||||
import javax.tools.StandardJavaFileManager;
|
import javax.tools.StandardJavaFileManager;
|
||||||
import javax.tools.ToolProvider;
|
import javax.tools.ToolProvider;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.io.PipedInputStream;
|
import java.io.PipedInputStream;
|
||||||
import java.io.PipedOutputStream;
|
import java.io.PipedOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
@ -442,19 +440,21 @@ public abstract class BaseTest {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return true if all is ok, no errors */
|
protected ErrorQueue antlr(String grammarFileName, boolean defaultListener, String... extraOptions) {
|
||||||
protected ErrorQueue antlr(String fileName, String grammarFileName, String grammarStr, boolean defaultListener, String... extraOptions) {
|
|
||||||
System.out.println("dir "+tmpdir);
|
|
||||||
mkdir(tmpdir);
|
|
||||||
writeFile(tmpdir, fileName, grammarStr);
|
|
||||||
final List<String> options = new ArrayList<String>();
|
final List<String> options = new ArrayList<String>();
|
||||||
Collections.addAll(options, extraOptions);
|
Collections.addAll(options, extraOptions);
|
||||||
|
if ( !options.contains("-o") ) {
|
||||||
options.add("-o");
|
options.add("-o");
|
||||||
options.add(tmpdir);
|
options.add(tmpdir);
|
||||||
|
}
|
||||||
|
if ( !options.contains("-lib") ) {
|
||||||
options.add("-lib");
|
options.add("-lib");
|
||||||
options.add(tmpdir);
|
options.add(tmpdir);
|
||||||
|
}
|
||||||
|
if ( !options.contains("-encoding") ) {
|
||||||
options.add("-encoding");
|
options.add("-encoding");
|
||||||
options.add("UTF-8");
|
options.add("UTF-8");
|
||||||
|
}
|
||||||
options.add(new File(tmpdir,grammarFileName).toString());
|
options.add(new File(tmpdir,grammarFileName).toString());
|
||||||
|
|
||||||
final String[] optionsA = new String[options.size()];
|
final String[] optionsA = new String[options.size()];
|
||||||
|
@ -474,7 +474,12 @@ public abstract class BaseTest {
|
||||||
System.err.println(msg);
|
System.err.println(msg);
|
||||||
}
|
}
|
||||||
System.out.println("!!!\ngrammar:");
|
System.out.println("!!!\ngrammar:");
|
||||||
System.out.println(grammarStr);
|
try {
|
||||||
|
System.out.println(new String(Utils.readFile(tmpdir+"/"+grammarFileName)));
|
||||||
|
}
|
||||||
|
catch (IOException ioe) {
|
||||||
|
System.err.println(ioe.toString());
|
||||||
|
}
|
||||||
System.out.println("###");
|
System.out.println("###");
|
||||||
}
|
}
|
||||||
if ( !defaultListener && !equeue.warnings.isEmpty() ) {
|
if ( !defaultListener && !equeue.warnings.isEmpty() ) {
|
||||||
|
@ -488,6 +493,13 @@ public abstract class BaseTest {
|
||||||
return equeue;
|
return equeue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected ErrorQueue antlr(String grammarFileName, String grammarStr, boolean defaultListener, String... extraOptions) {
|
||||||
|
System.out.println("dir "+tmpdir);
|
||||||
|
mkdir(tmpdir);
|
||||||
|
writeFile(tmpdir, grammarFileName, grammarStr);
|
||||||
|
return antlr(grammarFileName, defaultListener, extraOptions);
|
||||||
|
}
|
||||||
|
|
||||||
protected String execLexer(String grammarFileName,
|
protected String execLexer(String grammarFileName,
|
||||||
String grammarStr,
|
String grammarStr,
|
||||||
String lexerName,
|
String lexerName,
|
||||||
|
@ -632,7 +644,7 @@ public abstract class BaseTest {
|
||||||
String... extraOptions)
|
String... extraOptions)
|
||||||
{
|
{
|
||||||
ErrorQueue equeue =
|
ErrorQueue equeue =
|
||||||
antlr(grammarFileName, grammarFileName, grammarStr, defaultListener, extraOptions);
|
antlr(grammarFileName, grammarStr, defaultListener, extraOptions);
|
||||||
if (!equeue.errors.isEmpty()) {
|
if (!equeue.errors.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -789,7 +801,7 @@ public abstract class BaseTest {
|
||||||
|
|
||||||
String[] lines = input.split("\n");
|
String[] lines = input.split("\n");
|
||||||
String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
|
String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
|
||||||
ErrorQueue equeue = antlr(fileName, fileName, input, false);
|
ErrorQueue equeue = antlr(fileName, input, false);
|
||||||
|
|
||||||
String actual = equeue.toString(true);
|
String actual = equeue.toString(true);
|
||||||
actual = actual.replace(tmpdir + File.separator, "");
|
actual = actual.replace(tmpdir + File.separator, "");
|
||||||
|
@ -1027,13 +1039,7 @@ public abstract class BaseTest {
|
||||||
|
|
||||||
public static void writeFile(String dir, String fileName, String content) {
|
public static void writeFile(String dir, String fileName, String content) {
|
||||||
try {
|
try {
|
||||||
File f = new File(dir, fileName);
|
Utils.writeFile(dir+"/"+fileName, content, "UTF-8");
|
||||||
FileOutputStream outputStream = new FileOutputStream(f);
|
|
||||||
OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8");
|
|
||||||
BufferedWriter bw = new BufferedWriter(w);
|
|
||||||
bw.write(content);
|
|
||||||
bw.close();
|
|
||||||
w.close();
|
|
||||||
}
|
}
|
||||||
catch (IOException ioe) {
|
catch (IOException ioe) {
|
||||||
System.err.println("can't write file");
|
System.err.println("can't write file");
|
||||||
|
|
|
@ -35,11 +35,98 @@ import org.antlr.v4.tool.Grammar;
|
||||||
import org.antlr.v4.tool.GrammarSemanticsMessage;
|
import org.antlr.v4.tool.GrammarSemanticsMessage;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
public class TestCompositeGrammars extends BaseTest {
|
public class TestCompositeGrammars extends BaseTest {
|
||||||
protected boolean debug = false;
|
protected boolean debug = false;
|
||||||
|
|
||||||
|
@Test public void testImportFileLocationInSubdir() throws Exception {
|
||||||
|
String slave =
|
||||||
|
"parser grammar S;\n" +
|
||||||
|
"a : B {System.out.println(\"S.a\");} ;\n";
|
||||||
|
mkdir(tmpdir);
|
||||||
|
String subdir = tmpdir + "/sub";
|
||||||
|
mkdir(subdir);
|
||||||
|
writeFile(subdir, "S.g4", slave);
|
||||||
|
String master =
|
||||||
|
"grammar M;\n" +
|
||||||
|
"import S;\n" +
|
||||||
|
"s : a ;\n" +
|
||||||
|
"B : 'b' ;" + // defines B from inherited token space
|
||||||
|
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||||
|
writeFile(tmpdir, "M.g4", master);
|
||||||
|
ErrorQueue equeue = antlr("M.g4", false, "-lib", subdir);
|
||||||
|
assertEquals(equeue.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testImportFileNotSearchedForInOutputDir() throws Exception {
|
||||||
|
String slave =
|
||||||
|
"parser grammar S;\n" +
|
||||||
|
"a : B {System.out.println(\"S.a\");} ;\n";
|
||||||
|
mkdir(tmpdir);
|
||||||
|
String outdir = tmpdir + "/out";
|
||||||
|
mkdir(outdir);
|
||||||
|
writeFile(outdir, "S.g4", slave);
|
||||||
|
String master =
|
||||||
|
"grammar M;\n" +
|
||||||
|
"import S;\n" +
|
||||||
|
"s : a ;\n" +
|
||||||
|
"B : 'b' ;" + // defines B from inherited token space
|
||||||
|
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||||
|
writeFile(tmpdir, "M.g4", master);
|
||||||
|
ErrorQueue equeue = antlr("M.g4", false, "-o", outdir);
|
||||||
|
assertEquals(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, equeue.errors.get(0).getErrorType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testOutputDirShouldNotEffectImports() throws Exception {
|
||||||
|
String slave =
|
||||||
|
"parser grammar S;\n" +
|
||||||
|
"a : B {System.out.println(\"S.a\");} ;\n";
|
||||||
|
mkdir(tmpdir);
|
||||||
|
String subdir = tmpdir + "/sub";
|
||||||
|
mkdir(subdir);
|
||||||
|
writeFile(subdir, "S.g4", slave);
|
||||||
|
String master =
|
||||||
|
"grammar M;\n" +
|
||||||
|
"import S;\n" +
|
||||||
|
"s : a ;\n" +
|
||||||
|
"B : 'b' ;" + // defines B from inherited token space
|
||||||
|
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||||
|
writeFile(tmpdir, "M.g4", master);
|
||||||
|
String outdir = tmpdir + "/out";
|
||||||
|
mkdir(outdir);
|
||||||
|
ErrorQueue equeue = antlr("M.g4", false, "-o", outdir, "-lib", subdir);
|
||||||
|
assertEquals(0, equeue.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testTokensFileInOutputDirAndImportFileInSubdir() throws Exception {
|
||||||
|
String slave =
|
||||||
|
"parser grammar S;\n" +
|
||||||
|
"a : B {System.out.println(\"S.a\");} ;\n";
|
||||||
|
mkdir(tmpdir);
|
||||||
|
String subdir = tmpdir + "/sub";
|
||||||
|
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);
|
||||||
|
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";
|
||||||
|
mkdir(outdir);
|
||||||
|
ErrorQueue equeue = antlr("MLexer.g4", false, "-o", outdir);
|
||||||
|
assertEquals(0, equeue.size());
|
||||||
|
equeue = antlr("MParser.g4", false, "-o", outdir, "-lib", subdir);
|
||||||
|
assertEquals(0, equeue.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void testDelegatorInvokesDelegateRule() throws Exception {
|
@Test public void testDelegatorInvokesDelegateRule() throws Exception {
|
||||||
String slave =
|
String slave =
|
||||||
"parser grammar S;\n" +
|
"parser grammar S;\n" +
|
||||||
|
@ -641,7 +728,7 @@ public class TestCompositeGrammars extends BaseTest {
|
||||||
"s : a ;\n" +
|
"s : a ;\n" +
|
||||||
"B : 'b' ;" + // defines B from inherited token space
|
"B : 'b' ;" + // defines B from inherited token space
|
||||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||||
ErrorQueue equeue = antlr("M.g4", "M.g4", master, false);
|
ErrorQueue equeue = antlr("M.g4", master, false);
|
||||||
int expecting = 0; // should be ok
|
int expecting = 0; // should be ok
|
||||||
assertEquals(expecting, equeue.errors.size());
|
assertEquals(expecting, equeue.errors.size());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue