use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestVisitors method testShouldNotVisitTerminal.
/**
* This test verifies that {@link AbstractParseTreeVisitor#shouldVisitNextChild} is called before visiting the first
* child. It also verifies that {@link AbstractParseTreeVisitor#defaultResult} provides the default return value for
* visiting a tree.
*/
@Test
public void testShouldNotVisitTerminal() {
String input = "A";
VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
VisitorBasicParser.SContext context = parser.s();
Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {
@Override
public String visitTerminal(TerminalNode node) {
throw new RuntimeException("Should not be reachable");
}
@Override
protected String defaultResult() {
return "default result";
}
@Override
protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
return false;
}
};
String result = listener.visit(context);
String expected = "default result";
Assert.assertEquals(expected, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestVisitors method testShouldNotVisitEOF.
/**
* This test verifies that {@link AbstractParseTreeVisitor#visitChildren} does not call
* {@link ParseTreeVisitor#visit} after {@link AbstractParseTreeVisitor#shouldVisitNextChild} returns
* {@code false}.
*/
@Test
public void testShouldNotVisitEOF() {
String input = "A";
VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
VisitorBasicParser.SContext context = parser.s();
Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {
@Override
public String visitTerminal(TerminalNode node) {
return node.getSymbol().toString() + "\n";
}
@Override
protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
return currentResult == null || currentResult.isEmpty();
}
};
String result = listener.visit(context);
String expected = "[@0,0:0='A',<1>,1:0]\n";
Assert.assertEquals(expected, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class BaseBrowserTest method writeParserTestFile.
protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) {
String html = "<!DOCTYPE html>\r\n" + "<html>\r\n" + " <head>\r\n" + " <script src='lib/require.js'></script>\r\n" + " <script>\r\n" + " antlr4 = null;\r\n" + " listener = null;\r\n" + " TreeShapeListener = null;\r\n" + " " + lexerName + " = null;\r\n" + " " + parserName + " = null;\r\n" + " " + listenerName + " = null;\r\n" + " " + visitorName + " = null;\r\n" + " printer = function() {\r\n" + " this.println = function(s) { document.getElementById('output').value += s + '\\n'; }\r\n" + " this.print = function(s) { document.getElementById('output').value += s; }\r\n" + " return this;\r\n" + " };\r\n" + "\r\n" + " loadParser = function() {\r\n" + " try {\r\n" + " antlr4 = require('antlr4/index');\r\n" + " " + lexerName + " = require('./parser/" + lexerName + "');\n" + " " + parserName + " = require('./parser/" + parserName + "');\n" + " " + listenerName + " = require('./parser/" + listenerName + "');\n" + " " + visitorName + " = require('./parser/" + visitorName + "');\n" + " } catch (ex) {\r\n" + " document.getElementById('errors').value = ex.toString();\r\n" + " }\r\n" + "\r\n" + " listener = function() {\r\n" + " antlr4.error.ErrorListener.call(this);\r\n" + " return this;\r\n" + " }\r\n" + " listener.prototype = Object.create(antlr4.error.ErrorListener.prototype);\r\n" + " listener.prototype.constructor = listener;\r\n" + " listener.prototype.syntaxError = function(recognizer, offendingSymbol, line, column, msg, e) {\r\n" + " document.getElementById('errors').value += 'line ' + line + ':' + column + ' ' + msg + '\\r\\n';\r\n" + " };\r\n" + "\r\n" + " TreeShapeListener = function() {\r\n" + " antlr4.tree.ParseTreeListener.call(this);\r\n" + " return this;\r\n" + " };\r\n" + "\r\n" + " TreeShapeListener.prototype = Object.create(antlr4.tree.ParseTreeListener.prototype);\r\n" + " TreeShapeListener.prototype.constructor = TreeShapeListener;\r\n" + "\r\n" + " TreeShapeListener.prototype.enterEveryRule = function(ctx) {\r\n" + " for(var i=0;i<ctx.getChildCount; i++) {\r\n" + " var child = ctx.getChild(i);\r\n" + " var parent = child.parentCtx;\r\n" + " if(parent.getRuleContext() !== ctx || !(parent instanceof antlr4.tree.RuleNode)) {\r\n" + " throw 'Invalid parse tree shape detected.';\r\n" + " }\r\n" + " }\r\n" + " };\r\n" + " }\r\n" + "\r\n" + " test = function() {\r\n" + " document.getElementById('output').value = ''\r\n" + " var input = document.getElementById('input').value;\r\n" + " var stream = new antlr4.InputStream(input, true);\n" + " var lexer = new " + lexerName + "." + lexerName + "(stream);\n" + " lexer._listeners = [new listener()];\r\n" + " var tokens = new antlr4.CommonTokenStream(lexer);\n" + " var parser = new " + parserName + "." + parserName + "(tokens);\n" + " parser._listeners.push(new listener());\n" + (debug ? " parser._listeners.push(new antlr4.error.DiagnosticErrorListener());\n" : "") + " parser.buildParseTrees = true;\n" + " parser.printer = new printer();\n" + " var tree = parser." + parserStartRuleName + "();\n" + " antlr4.tree.ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\n" + " };\r\n" + "\r\n" + " </script>\r\n" + " </head>\r\n" + " <body>\r\n" + " <textarea id='input'></textarea><br>\r\n" + " <button id='load' type='button' onclick='loadParser()'>Load</button><br>\r\n" + " <button id='submit' type='button' onclick='test()'>Test</button><br>\r\n" + " <textarea id='output'></textarea><br>\r\n" + " <textarea id='errors'></textarea><br>\r\n" + " </body>\r\n" + "</html>\r\n";
writeFile(httpdir, "Test.html", html);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceThenInsertBeforeLastIndex.
@Test
public void testReplaceThenInsertBeforeLastIndex() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abc";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.replace(2, "x");
tokens.insertBefore(2, "y");
String result = tokens.getText();
String expecting = "abyx";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.CommonTokenStream in project antlr4 by antlr.
the class TestTokenStreamRewriter method testCombineInsertOnLeftWithReplace.
@Test
public void testCombineInsertOnLeftWithReplace() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abc";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.replace(0, 2, "foo");
tokens.insertBefore(0, "z");
stream.fill();
// combine with left edge of rewrite
String result = tokens.getText();
String expecting = "zfoo";
assertEquals(expecting, result);
}
Aggregations