use of org.antlr.runtime.tree.CommonTreeNodeStream in project SQLWindowing by hbutani.
the class ParseUtils method parseSelect.
public static SelectSpec parseSelect(String selectExprStr) throws WindowingException {
Windowing2Lexer lexer;
CommonTokenStream tokens;
Windowing2Parser parser = null;
CommonTree t;
CommonTreeNodeStream nodes;
QSpecBuilder2 qSpecBldr = null;
String err;
try {
lexer = new Windowing2Lexer(new ANTLRStringStream(selectExprStr));
tokens = new CommonTokenStream(lexer);
parser = new Windowing2Parser(tokens);
parser.setTreeAdaptor(TranslateUtils.adaptor);
t = (CommonTree) parser.select().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);
SelectSpec selectSpec = qSpecBldr.select();
err = qSpecBldr.getWindowingParseErrors();
if (err != null) {
throw new WindowingException(err);
}
return selectSpec;
} 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 otertool by wuntee.
the class SmaliWorkshop method buildDex.
private static void buildDex(File smaliSourceDirectory, File destination) throws RecognitionException, SmaliSyntaxException, SmaliDexException, IOException {
DexFile dexFile = new DexFile();
// Load files into set
logger.debug("Loading smali files");
LinkedHashSet<File> filesToProcess = new LinkedHashSet<File>();
getSmaliFilesInDir(smaliSourceDirectory, filesToProcess);
// Process each file
logger.debug("Processing files");
for (File file : filesToProcess) {
logger.debug("Processing: " + file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
LexerErrorInterface lexer = new smaliFlexLexer(reader);
((smaliFlexLexer) lexer).setSourceFile(file);
CommonTokenStream tokens = new CommonTokenStream((TokenSource) lexer);
smaliParser parser = new smaliParser(tokens);
smaliParser.smali_file_return result = parser.smali_file();
// Errors
if (parser.getNumberOfSyntaxErrors() > 0) {
throw new SmaliSyntaxException(file, parser.getNumberOfSyntaxErrors());
}
if (lexer.getNumberOfSyntaxErrors() > 0) {
throw new SmaliSyntaxException(file, lexer.getNumberOfSyntaxErrors());
}
CommonTree t = (CommonTree) result.getTree();
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
dexGen.dexFile = dexFile;
dexGen.smali_file();
if (dexGen.getNumberOfSyntaxErrors() > 0) {
throw new SmaliDexException(file, dexGen.getNumberOfSyntaxErrors());
}
}
// Calculate signatures and write file
logger.debug("Writing dex file.");
dexFile.place();
ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
dexFile.writeTo(out);
byte[] bytes = out.toByteArray();
DexFile.calcSignature(bytes);
DexFile.calcChecksum(bytes);
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
fileOutputStream.close();
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project drools by kiegroup.
the class DSLTokenizedMappingFile method buildFileMappingWalker.
private DSLMapWalker buildFileMappingWalker(final List<ParserError> errors, CharStream stream) throws RecognitionException {
DSLMapLexer lexer = new DSLMapLexer(stream);
CommonTokenStream tokens = new CommonTokenStream();
tokens.setTokenSource(lexer);
DSLMapParser parser = new DSLMapParser(tokens);
DSLMapParser.mapping_file_return example = parser.mapping_file();
CommonTree tree = (CommonTree) example.getTree();
// logger.info(tree.toStringTree());
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
DSLMapWalker walker = new DSLMapWalker(nodes);
errors.addAll(lexer.getErrors());
errors.addAll(parser.getErrors());
return walker;
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project antlr4 by antlr.
the class GrammarTransformPipeline method reduceBlocksToSets.
public void reduceBlocksToSets(GrammarAST root) {
CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root);
GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
BlockSetTransformer transformer = new BlockSetTransformer(nodes, g);
transformer.setTreeAdaptor(adaptor);
transformer.downup(root);
}
use of org.antlr.runtime.tree.CommonTreeNodeStream in project antlr4 by antlr.
the class GrammarAST method toTokenString.
public String toTokenString() {
CharStream input = this.token.getInputStream();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this);
StringBuilder buf = new StringBuilder();
GrammarAST o = (GrammarAST) nodes.LT(1);
int type = adaptor.getType(o);
while (type != Token.EOF) {
buf.append(" ");
buf.append(o.getText());
nodes.consume();
o = (GrammarAST) nodes.LT(1);
type = adaptor.getType(o);
}
return buf.toString();
}
Aggregations