use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testCombinedGrammarImportsModalLexerGrammar.
@Test
public void testCombinedGrammarImportsModalLexerGrammar() throws Exception {
RuntimeTestUtils.mkdir(getTempDirPath());
String master = "grammar M;\n" + "import S;\n" + "A : 'a';\n" + "B : 'b';\n" + "r : A B;\n";
writeFile(getTempDirPath(), "M.g4", master);
String slave = "lexer grammar S;\n" + "D : 'd';\n" + "mode X;\n" + "C : 'c' -> popMode;\n";
writeFile(getTempDirPath(), "S.g4", slave);
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());
assertEquals("X", msg.getArgs()[0]);
assertEquals(3, msg.line);
assertEquals(5, msg.charPosition);
assertEquals("M.g4", new File(msg.fileName).getName());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testSyntaxErrorsInImportsNotThrownOut.
@Test
public void testSyntaxErrorsInImportsNotThrownOut() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave = "parser grammar S;\n" + "options {toke\n";
RuntimeTestUtils.mkdir(getTempDirPath());
writeFile(getTempDirPath(), "S.g4", slave);
String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
writeFile(getTempDirPath(), "M.g4", master);
/*Grammar g =*/
new Grammar(getTempDirPath() + "/M.g4", master, equeue);
assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class TestCompositeGrammars method testImportClashingChannelsIntoLexerGrammar.
@Test
public void testImportClashingChannelsIntoLexerGrammar() throws Exception {
RuntimeTestUtils.mkdir(getTempDirPath());
String master = "lexer grammar M;\n" + "import S;\n" + "channels {CH_A, CH_B, CH_C}\n" + "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n" + "C : 'C' -> channel(CH_C);\n";
writeFile(getTempDirPath(), "M.g4", master);
String slave = "lexer grammar S;\n" + "channels {CH_C}\n" + "C : 'c' -> channel(CH_C);\n";
writeFile(getTempDirPath(), "S.g4", slave);
ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
assertEquals(0, equeue.errors.size());
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseCSharpTest method rawGenerateRecognizer.
/**
* Return true if all is well
*/
protected boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
ErrorQueue equeue = antlrOnString(getTempDirPath(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions);
if (!equeue.errors.isEmpty()) {
return false;
}
List<String> files = new ArrayList<String>();
if (lexerName != null) {
files.add(lexerName + ".cs");
}
if (parserName != null) {
files.add(parserName + ".cs");
Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
String grammarName = grammarFileName.substring(0, grammarFileName.lastIndexOf('.'));
if (!optionsSet.contains("-no-listener")) {
files.add(grammarName + "Listener.cs");
files.add(grammarName + "BaseListener.cs");
}
if (optionsSet.contains("-visitor")) {
files.add(grammarName + "Visitor.cs");
files.add(grammarName + "BaseVisitor.cs");
}
}
addSourceFiles(files.toArray(new String[files.size()]));
return true;
}
use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.
the class BaseRuntimeTest method antlrOnString.
/**
* Run ANTLR on stuff in workdir and error queue back
*/
public static ErrorQueue antlrOnString(String workdir, String targetName, String grammarFileName, boolean defaultListener, String... extraOptions) {
final List<String> options = new ArrayList<>();
Collections.addAll(options, extraOptions);
if (targetName != null) {
options.add("-Dlanguage=" + targetName);
}
if (!options.contains("-o")) {
options.add("-o");
options.add(workdir);
}
if (!options.contains("-lib")) {
options.add("-lib");
options.add(workdir);
}
if (!options.contains("-encoding")) {
options.add("-encoding");
options.add("UTF-8");
}
options.add(new File(workdir, grammarFileName).toString());
final String[] optionsA = new String[options.size()];
options.toArray(optionsA);
Tool antlr = new Tool(optionsA);
ErrorQueue equeue = new ErrorQueue(antlr);
antlr.addListener(equeue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
synchronized (antlrLock) {
antlr.processGrammarsOnCommandLine();
}
List<String> errors = new ArrayList<>();
if (!defaultListener && !equeue.errors.isEmpty()) {
for (int i = 0; i < equeue.errors.size(); i++) {
ANTLRMessage msg = equeue.errors.get(i);
ST msgST = antlr.errMgr.getMessageTemplate(msg);
errors.add(msgST.render());
}
}
if (!defaultListener && !equeue.warnings.isEmpty()) {
for (int i = 0; i < equeue.warnings.size(); i++) {
ANTLRMessage msg = equeue.warnings.get(i);
// antlrToolErrors.append(msg); warnings are hushed
}
}
return equeue;
}
Aggregations