use of org.antlr.runtime.tree.CommonTreeNodeStream in project SQLWindowing by hbutani.
the class ParseUtils method parse.
public static QuerySpec parse(String query) throws WindowingException {
Windowing2Lexer lexer;
CommonTokenStream tokens;
Windowing2Parser parser = null;
CommonTree t;
CommonTreeNodeStream nodes;
QSpecBuilder2 qSpecBldr = null;
String err;
try {
lexer = new Windowing2Lexer(new ANTLRStringStream(query));
tokens = new CommonTokenStream(lexer);
parser = new Windowing2Parser(tokens);
parser.setTreeAdaptor(TranslateUtils.adaptor);
t = (CommonTree) parser.query().getTree();
err = parser.getWindowingParseErrors();
if (err != null) {
throw new WindowingException(err);
}
} catch (WindowingException we) {
throw we;
} catch (Throwable te) {
err = parser.getWindowingParseErrors();
if (err != null) {
throw new WindowingException(err);
}
throw new WindowingException("Parse Error:" + te.toString(), te);
}
TranslateUtils.unescapeStringLiterals((ASTNode) t);
try {
nodes = new CommonTreeNodeStream(t);
nodes.setTokenStream(tokens);
qSpecBldr = new QSpecBuilder2(nodes);
qSpecBldr.query();
err = qSpecBldr.getWindowingParseErrors();
if (err != null) {
throw new WindowingException(err);
}
return qSpecBldr.getQuerySpec();
} catch (WindowingException we) {
throw we;
} catch (Throwable te) {
err = qSpecBldr.getWindowingParseErrors();
if (err != null) {
throw new WindowingException(err);
}
throw new WindowingException("Parse Error:" + te.toString(), te);
}
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project wso2-synapse by wso2.
the class StreamingXPATHCompiler method parse.
/**
* This will create the Custom XPATH Parser Components for a given XPATH. This will Use the Antlr Grammar XPATH1.0 for this process.
*
* @param source is the String for XPATH Expression
* @return A Custom XPATH Parser Component Chain
* @throws RecognitionException
*/
public static StreamingParser parse(String source) throws RecognitionException {
XPath1Lexer lexer = new XPath1Lexer();
lexer.setCharStream(new ANTLRStringStream(source));
CommonTokenStream tokens = new CommonTokenStream(lexer);
XPath1Parser parser = new XPath1Parser(tokens);
org.apache.synapse.util.streaming_xpath.compiler.XPath1Parser.xpath_return r = parser.xpath();
CommonTree t = (CommonTree) r.getTree();
CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
nodes.setTokenStream(tokens);
XPath1Walker walker = new XPath1Walker(nodes);
walker.xpath = source;
return walker.xpath();
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project antlr4 by tunnelvisionlabs.
the class ParserATNFactory method _createATN.
protected void _createATN(@NotNull Collection<Rule> rules) {
createRuleStartAndStopATNStates();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
for (Rule r : rules) {
// find rule's block
GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
ATNBuilder b = new ATNBuilder(nodes, this);
try {
setCurrentRuleName(r.name);
Handle h = b.ruleBlock(null);
rule(r.ast, r.name, h);
} catch (RecognitionException re) {
ErrorManager.fatalInternalError("bad grammar AST structure", re);
}
}
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project antlr4 by tunnelvisionlabs.
the class OutputModelController method buildNormalRuleFunction.
public void buildNormalRuleFunction(Rule r, RuleFunction function) {
CodeGenerator gen = delegate.getGenerator();
// TRIGGER factory functions for rule alts, elements
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
walker = new SourceGenTriggers(nodes, this);
try {
// walk AST of rule alts/elements
function.code = DefaultOutputModelFactory.list(walker.block(null, null));
function.hasLookaheadBlock = walker.hasLookaheadBlock;
} catch (org.antlr.runtime.RecognitionException e) {
e.printStackTrace(System.err);
}
function.ctxType = delegate.getTarget().getRuleFunctionContextStructName(function);
function.postamble = rulePostamble(function, r);
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project atlas by alibaba.
the class SmaliMod method assembleSmaliFile.
public static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, boolean verboseErrors, boolean printTokens) throws IOException, RecognitionException {
CommonTokenStream tokens;
LexerErrorInterface lexer;
InputStream is = new FileInputStream(smaliFile);
InputStreamReader reader = new InputStreamReader(is, "UTF-8");
lexer = new smaliFlexLexer(reader);
((smaliFlexLexer) lexer).setSourceFile(smaliFile);
tokens = new CommonTokenStream((TokenSource) lexer);
if (printTokens) {
tokens.getTokens();
for (int i = 0; i < tokens.size(); i++) {
Token token = tokens.get(i);
if (token.getChannel() == smaliParser.HIDDEN) {
continue;
}
System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
}
}
smaliParser parser = new smaliParser(tokens);
parser.setVerboseErrors(verboseErrors);
smaliParser.smali_file_return result = parser.smali_file();
if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
return false;
}
CommonTree t = (CommonTree) result.getTree();
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
dexGen.setVerboseErrors(verboseErrors);
dexGen.setDexBuilder(dexBuilder);
dexGen.smali_file();
return dexGen.getNumberOfSyntaxErrors() == 0;
}
Aggregations