use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class TokenVocabParser method load.
/** Load a vocab file {@code <vocabName>.tokens} and return mapping. */
public Map<String, Integer> load() {
Map<String, Integer> tokens = new LinkedHashMap<String, Integer>();
int maxTokenType = -1;
File fullFile = getImportedVocabFile();
FileInputStream fis = null;
BufferedReader br = null;
Tool tool = g.tool;
String vocabName = g.getOptionString("tokenVocab");
try {
Pattern tokenDefPattern = Pattern.compile("([^\n]+?)[ \\t]*?=[ \\t]*?([0-9]+)");
fis = new FileInputStream(fullFile);
InputStreamReader isr;
if (tool.grammarEncoding != null) {
isr = new InputStreamReader(fis, tool.grammarEncoding);
} else {
isr = new InputStreamReader(fis);
}
br = new BufferedReader(isr);
String tokenDef = br.readLine();
int lineNum = 1;
while (tokenDef != null) {
Matcher matcher = tokenDefPattern.matcher(tokenDef);
if (matcher.find()) {
String tokenID = matcher.group(1);
String tokenTypeS = matcher.group(2);
int tokenType;
try {
tokenType = Integer.valueOf(tokenTypeS);
} catch (NumberFormatException nfe) {
tool.errMgr.toolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token type: " + tokenTypeS, lineNum);
tokenType = Token.INVALID_TOKEN_TYPE;
}
tool.log("grammar", "import " + tokenID + "=" + tokenType);
tokens.put(tokenID, tokenType);
maxTokenType = Math.max(maxTokenType, tokenType);
lineNum++;
} else {
if (tokenDef.length() > 0) {
// ignore blank lines
tool.errMgr.toolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token def: " + tokenDef, lineNum);
}
}
tokenDef = br.readLine();
}
} catch (FileNotFoundException fnfe) {
GrammarAST inTree = g.ast.getOptionAST("tokenVocab");
String inTreeValue = inTree.getToken().getText();
if (vocabName.equals(inTreeValue)) {
tool.errMgr.grammarError(ErrorType.CANNOT_FIND_TOKENS_FILE_REFD_IN_GRAMMAR, g.fileName, inTree.getToken(), fullFile);
} else {
// must be from -D option on cmd-line not token in tree
tool.errMgr.toolError(ErrorType.CANNOT_FIND_TOKENS_FILE_GIVEN_ON_CMDLINE, fullFile, g.name);
}
} catch (Exception e) {
tool.errMgr.toolError(ErrorType.ERROR_READING_TOKENS_FILE, e, fullFile, e.getMessage());
} finally {
try {
if (br != null)
br.close();
} catch (IOException ioe) {
tool.errMgr.toolError(ErrorType.ERROR_READING_TOKENS_FILE, ioe, fullFile, ioe.getMessage());
}
}
return tokens;
}
use of org.antlr.v4.runtime.tree.Tree in project Alpha by alpha-asp.
the class Main method parseVisit.
public static ParsedProgram parseVisit(ANTLRInputStream is) throws IOException {
/*
// In order to require less memory: use unbuffered streams and avoid constructing a full parse tree.
ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is));
lexer.setTokenFactory(new CommonTokenFactory(true));
final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer));
parser.setBuildParseTree(false);
*/
CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(is));
final ASPCore2Parser parser = new ASPCore2Parser(tokens);
// Try SLL parsing mode (faster but may terminate incorrectly).
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
final CustomErrorListener errorListener = new CustomErrorListener(is.getSourceName());
ASPCore2Parser.ProgramContext programContext;
try {
// Parse program
programContext = parser.program();
} catch (ParseCancellationException e) {
// retry with LL parser and DefaultErrorStrategy printing errors to console.
if (e.getCause() instanceof RecognitionException) {
tokens.reset();
parser.addErrorListener(errorListener);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
// Re-run parse.
programContext = parser.program();
} else {
throw e;
}
}
// is attempted) and the user will only see the first error encountered.
if (errorListener.getRecognitionException() != null) {
throw errorListener.getRecognitionException();
}
// Construct internal program representation.
ParsedTreeVisitor visitor = new ParsedTreeVisitor();
return (ParsedProgram) visitor.visitProgram(programContext);
}
use of org.antlr.v4.runtime.tree.Tree in project aic-praise by aic-sri-international.
the class HOGMParserWrapper method parse.
//
// PRIVATE
//
private Expression parse(String string, Parser.ErrorListener errorListener, ParseTreeRetriever parseTreeRetriever) throws RecognitionException, UnableToParseAllTheInputError, HOGModelException {
Expression result = null;
AntlrErrorListener antlrErrorListener = new AntlrErrorListener(errorListener);
ANTLRInputStream input = new ANTLRInputStream(string);
HOGMLexer lexer = new HOGMLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
HOGMParser parser = new HOGMParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener(antlrErrorListener);
parser.addErrorListener(antlrErrorListener);
ParseTree tree = parseTreeRetriever.retrieve(parser);
boolean eofReached = parser.getInputStream().LA(1) == Recognizer.EOF;
if (!antlrErrorListener.errorsDetected) {
if (!eofReached) {
throw new UnableToParseAllTheInputError();
} else {
lexer.removeErrorListeners();
parser.removeErrorListeners();
HOGModelVisitor hogmModelVisitor = new HOGModelVisitor();
result = hogmModelVisitor.visit(tree);
}
}
return result;
}
use of org.antlr.v4.runtime.tree.Tree in project lucene-solr by apache.
the class JavascriptCompiler method getAntlrParseTree.
/**
* Parses the sourceText into an ANTLR 4 parse tree
*
* @return The ANTLR parse tree
* @throws ParseException on failure to parse
*/
private ParseTree getAntlrParseTree() throws ParseException {
final ANTLRInputStream antlrInputStream = new ANTLRInputStream(sourceText);
final JavascriptErrorHandlingLexer javascriptLexer = new JavascriptErrorHandlingLexer(antlrInputStream);
javascriptLexer.removeErrorListeners();
final JavascriptParser javascriptParser = new JavascriptParser(new CommonTokenStream(javascriptLexer));
javascriptParser.removeErrorListeners();
javascriptParser.setErrorHandler(new JavascriptParserErrorStrategy());
return javascriptParser.compile();
}
use of org.antlr.v4.runtime.tree.Tree in project hive by apache.
the class Exec method init.
/**
* Initialize PL/HQL
*/
Integer init(String[] args) throws Exception {
if (!parseArguments(args)) {
return 1;
}
// specify the default log4j2 properties file.
System.setProperty("log4j.configurationFile", "hive-log4j2.properties");
conf = new Conf();
conf.init();
conn = new Conn(this);
meta = new Meta(this);
initOptions();
expr = new Expression(this);
select = new Select(this);
stmt = new Stmt(this);
converter = new Converter(this);
function = new Function(this);
new FunctionDatetime(this).register(function);
new FunctionMisc(this).register(function);
new FunctionString(this).register(function);
new FunctionOra(this).register(function);
addVariable(new Var(ERRORCODE, Var.Type.BIGINT, 0L));
addVariable(new Var(SQLCODE, Var.Type.BIGINT, 0L));
addVariable(new Var(SQLSTATE, Var.Type.STRING, "00000"));
addVariable(new Var(HOSTCODE, Var.Type.BIGINT, 0L));
for (Map.Entry<String, String> v : arguments.getVars().entrySet()) {
addVariable(new Var(v.getKey(), Var.Type.STRING, v.getValue()));
}
InputStream input = null;
if (execString != null) {
input = new ByteArrayInputStream(execString.getBytes("UTF-8"));
} else {
input = new FileInputStream(execFile);
}
HplsqlLexer lexer = new HplsqlLexer(new ANTLRInputStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexer);
HplsqlParser parser = new HplsqlParser(tokens);
tree = parser.program();
if (trace) {
System.err.println("Configuration file: " + conf.getLocation());
System.err.println("Parser tree: " + tree.toStringTree(parser));
}
includeRcFile();
return 0;
}
Aggregations