Search in sources :

Example 6 with Lexer

use of org.antlr.v4.runtime.Lexer in project antlr4 by antlr.

the class BaseBrowserTest method writeLexerTestFile.

protected void writeLexerTestFile(String lexerName, boolean showDFA) {
    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" + "			" + lexerName + " = null;\r\n" + "\r\n" + "			loadLexer = function() {\r\n" + "				try {\r\n" + "					antlr4 = require('antlr4/index');\r\n" + "					" + lexerName + " = require('./parser/" + lexerName + "');\r\n" + "				} catch (ex) {\r\n" + "					document.getElementById('errors').value = ex.toString();\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" + "\r\n" + "			test = function() {\r\n" + "				document.getElementById('output').value = ''\r\n" + "				var input = document.getElementById('input').value;\r\n" + "			var chars = new antlr4.InputStream(input, true);\r\n" + "    			var lexer = new " + lexerName + "." + lexerName + "(chars);\r\n" + "				lexer._listeners = [new listener()];\r\n" + "    			var stream = new antlr4.CommonTokenStream(lexer);\r\n" + "    			stream.fill();\r\n" + "    			for(var i=0; i<stream.tokens.length; i++) {\r\n" + "					document.getElementById('output').value += stream.tokens[i].toString() + '\\r\\n';\r\n" + "    			}\n" + (showDFA ? "    			document.getElementById('output').value += lexer._interp.decisionToDFA[antlr4.Lexer.DEFAULT_MODE].toLexerString();\r\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='loadLexer()'>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);
}
Also used : STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 7 with Lexer

use of org.antlr.v4.runtime.Lexer in project siddhi by wso2.

the class SiddhiCompiler method parseFunctionDefinition.

public static FunctionDefinition parseFunctionDefinition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.definition_function_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (FunctionDefinition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FunctionDefinition(org.wso2.siddhi.query.api.definition.FunctionDefinition) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 8 with Lexer

use of org.antlr.v4.runtime.Lexer in project siddhi by wso2.

the class SiddhiCompiler method parseTimeConstantDefinition.

public static TimeConstant parseTimeConstantDefinition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.time_value();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (TimeConstant) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) TimeConstant(org.wso2.siddhi.query.api.expression.constant.TimeConstant)

Example 9 with Lexer

use of org.antlr.v4.runtime.Lexer in project siddhi by wso2.

the class SiddhiCompiler method parseStoreQuery.

public static StoreQuery parseStoreQuery(String storeQuery) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(storeQuery);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.store_query_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (StoreQuery) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 10 with Lexer

use of org.antlr.v4.runtime.Lexer in project SSM by Intel-bigdata.

the class TestSmartRuleParser method parseAndExecuteRule.

private void parseAndExecuteRule(String rule) throws Exception {
    System.out.println("--> " + rule);
    InputStream input = new ByteArrayInputStream(rule.getBytes());
    ANTLRInputStream antlrInput = new ANTLRInputStream(input);
    SmartRuleLexer lexer = new SmartRuleLexer(antlrInput);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SmartRuleParser parser = new SmartRuleParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new SSMRuleErrorListener());
    ParseTree tree = parser.ssmrule();
    System.out.println("Parser tree: " + tree.toStringTree(parser));
    System.out.println("Total number of errors: " + parseErrors.size());
    SmartRuleVisitTranslator visitor = new SmartRuleVisitTranslator();
    visitor.visit(tree);
    System.out.println("\nQuery:");
    TranslateResult result = visitor.generateSql();
    int index = 1;
    for (String sql : result.getSqlStatements()) {
        System.out.println("" + index + ". " + sql);
        index++;
    }
    if (parseErrors.size() > 0) {
        throw new IOException("Error while parse rule");
    }
    ExecutionContext ctx = new ExecutionContext();
    ctx.setProperty(ExecutionContext.RULE_ID, 2016);
    DBAdapter dbAdapter = new DBAdapter(TestDBUtil.getTestDBInstance());
    RuleQueryExecutor qe = new RuleQueryExecutor(null, ctx, result, dbAdapter);
    List<String> paths = qe.executeFileRuleQuery();
    index = 1;
    System.out.println("\nFiles:");
    for (String path : paths) {
        System.out.println("" + index + ". " + path);
        index++;
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SmartRuleParser(org.smartdata.server.rule.parser.SmartRuleParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmartRuleLexer(org.smartdata.server.rule.parser.SmartRuleLexer) SmartRuleVisitTranslator(org.smartdata.server.rule.parser.SmartRuleVisitTranslator) IOException(java.io.IOException) DBAdapter(org.smartdata.server.metastore.DBAdapter) ExecutionContext(org.smartdata.server.metastore.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslateResult(org.smartdata.server.rule.parser.TranslateResult) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

Test (org.junit.Test)427 LexerGrammar (org.antlr.v4.tool.LexerGrammar)407 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)279 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)143 Grammar (org.antlr.v4.tool.Grammar)125 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)108 CharStream (org.antlr.v4.runtime.CharStream)103 ParseTree (org.antlr.v4.runtime.tree.ParseTree)91 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)56 IOException (java.io.IOException)45 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 Token (org.antlr.v4.runtime.Token)41 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)39 ArrayList (java.util.ArrayList)37 RecognitionException (org.antlr.v4.runtime.RecognitionException)26 StringReader (java.io.StringReader)23 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)23 TokenStream (org.antlr.v4.runtime.TokenStream)23 Lexer (org.antlr.v4.runtime.Lexer)22