use of org.antlr.v4.runtime.misc.ParseCancellationException in project claw-compiler by C2SM-RCM.
the class ClawLanguage method analyze.
/**
* Analyze a raw string input and match it with the CLAW language definition.
*
* @param rawPragma A raw pragma statement to be analyzed against the CLAW
* language.
* @param lineno Line number of the pragma statement.
* @param generator Accelerator directive generator.
* @param target Target that influences the code transformation.
* @return A ClawLanguage object with the corresponding extracted information.
* @throws IllegalDirectiveException If directive does not follow the CLAW
* language specification.
*/
private static ClawLanguage analyze(String rawPragma, int lineno, AcceleratorGenerator generator, Target target) throws IllegalDirectiveException {
// Remove additional claw keyword
rawPragma = nakenize(rawPragma);
// Discard the ignored code after the claw ignore directive
if (rawPragma.toLowerCase().contains(IGNORE)) {
rawPragma = rawPragma.substring(0, rawPragma.toLowerCase().indexOf(IGNORE) + IGNORE.length());
}
// Instantiate the lexer with the raw string input
ClawLexer lexer = new ClawLexer(CharStreams.fromString(rawPragma));
// Get a list of matched tokens
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Pass the tokens to the parser
ClawParser parser = new ClawParser(tokens);
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
try {
// Start the parser analysis from the "analyze" entry point
ClawParser.AnalyzeContext ctx = parser.analyze();
// Get the ClawLanguage object return by the parser after analysis.
ctx.l.setAcceleratorGenerator(generator);
ctx.l.setTarget(target);
return ctx.l;
} catch (ParseCancellationException pcex) {
if (pcex.getCause() instanceof InputMismatchException) {
InputMismatchException imex = (InputMismatchException) pcex.getCause();
throw new IllegalDirectiveException(getTokens(imex.getExpectedTokens(), parser), lineno, imex.getOffendingToken().getCharPositionInLine());
} else if (pcex.getCause() instanceof NoViableAltException) {
NoViableAltException nvex = (NoViableAltException) pcex.getCause();
throw new IllegalDirectiveException(nvex.getOffendingToken(), getTokens(nvex.getExpectedTokens(), parser), lineno, nvex.getOffendingToken().getCharPositionInLine());
}
throw new IllegalDirectiveException(rawPragma, "Unsupported construct", lineno, 0);
}
}
use of org.antlr.v4.runtime.misc.ParseCancellationException in project L42 by ElvisResearchGroup.
the class ReplState method add.
public ReplState add(String code) {
Expression.ClassB cbEmpty = new ClassB(Doc.empty(), new ast.Ast.InterfaceHeader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Position.noInfo);
try {
//parse
Expression.ClassB codeTmp = (ClassB) Parser.parse("Repl", "{" + code + "}");
//new original
ClassReuse newOriginal = this.originalL;
List<ast.Expression.ClassB.Member> newOriginalMs = newOriginal.getInner().getMs();
newOriginalMs.addAll(codeTmp.getMs());
newOriginal.withInner(newOriginal.getInner().withMs(newOriginalMs));
//new src to desugar
List<ClassB.Member> newMs = new ArrayList<>();
int nestedAdded = 0;
for (Member m : this.desugaredL.getMs()) {
if (!(m instanceof NestedClass)) {
continue;
}
NestedClass nc = (NestedClass) m;
newMs.add(new ClassB.NestedClass(Doc.empty(), nc.getName(), cbEmpty, nc.getP()));
nestedAdded += 1;
}
newMs.addAll(codeTmp.getMs());
codeTmp = codeTmp.withMs(newMs);
Expression code2 = Desugar.of(codeTmp);
ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
// TODO: will die after new reduction Refresh of position identities, it is used to generate correct Java code.
code3 = (ExpCore.ClassB) code3.accept(new CloneVisitor() {
@Override
public ExpCore visit(ExpCore.ClassB cb) {
Position p = cb.getP();
cb = cb.withP(new Position(p.getFile(), p.getLine1(), p.getPos1(), p.getLine2(), p.getPos2(), p.get_next()));
return super.visit(cb);
}
});
//integrate new desugared src with old desugared code
List<Member> resultMs = new ArrayList<>(this.desugaredL.getMs());
for (int i = nestedAdded; i < code3.getMs().size(); i++) {
resultMs.add(code3.getMs().get(i));
}
code3 = code3.withMs(resultMs);
//call the repl and return
ExpCore.ClassB result = ProgramReduction.allSteps(code3);
return new ReplState(this.originalS + "\n" + code, newOriginal, result);
} catch (ParseCancellationException parser) {
System.out.println(parser.getMessage());
return null;
} catch (ErrorMessage msg) {
ErrorFormatter.topFormatErrorMessage(msg);
return null;
}
}
use of org.antlr.v4.runtime.misc.ParseCancellationException in project L42 by ElvisResearchGroup.
the class Parser method parseCtx.
private static NudeEContext parseCtx(String s) {
LoggedPrintStream lpsErr = LoggedPrintStream.create(System.err);
System.setErr(lpsErr);
try {
NudeEContext ctxResult = getParser(s).nudeE();
if (lpsErr.buf.length() != 0) {
throw new ParseCancellationException("\n\n------------------------------------\nCurrent file: " + Parser.fileName + "\nParsing error:\n" + lpsErr.buf.toString());
}
return ctxResult;
} finally {
System.setErr(lpsErr.underlying);
}
}
use of org.antlr.v4.runtime.misc.ParseCancellationException in project presto by prestodb.
the class SqlParser method invokeParser.
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.reset();
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder().visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.misc.ParseCancellationException in project compiler by boalang.
the class BoaCompiler method main.
public static void main(final String[] args) throws IOException {
CommandLine cl = processCommandLineOptions(args);
if (cl == null)
return;
final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
// get the name of the generated class
final String className = getGeneratedClass(cl);
// get the filename of the jar we will be writing
final String jarName;
if (cl.hasOption('o'))
jarName = cl.getOptionValue('o');
else
jarName = className + ".jar";
// make the output directory
File outputRoot = null;
if (cl.hasOption("cd")) {
outputRoot = new File(cl.getOptionValue("cd"));
} else {
outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
}
final File outputSrcDir = new File(outputRoot, "boa");
if (!outputSrcDir.mkdirs())
throw new IOException("unable to mkdir " + outputSrcDir);
// find custom libs to load
final List<URL> libs = new ArrayList<URL>();
if (cl.hasOption('l'))
for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL());
final File outputFile = new File(outputSrcDir, className + ".java");
final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
final List<String> jobnames = new ArrayList<String>();
final List<String> jobs = new ArrayList<String>();
final List<Integer> seeds = new ArrayList<Integer>();
boolean isSimple = true;
final List<Program> visitorPrograms = new ArrayList<Program>();
SymbolTable.initialize(libs);
final int maxVisitors;
if (cl.hasOption('v'))
maxVisitors = Integer.parseInt(cl.getOptionValue('v'));
else
maxVisitors = Integer.MAX_VALUE;
for (int i = 0; i < inputFiles.size(); i++) {
final File f = inputFiles.get(i);
try {
final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath()));
// use the whole input string to seed the RNG
seeds.add(lexer._input.getText(new Interval(0, lexer._input.size())).hashCode());
lexer.removeErrorListeners();
lexer.addErrorListener(new LexerErrorListener());
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final BoaParser parser = new BoaParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
throw new ParseCancellationException(e);
}
});
final BoaErrorListener parserErrorListener = new ParserErrorListener();
final Start p = parse(tokens, parser, parserErrorListener);
if (cl.hasOption("ast"))
new ASTPrintingVisitor().start(p);
final String jobName = "" + i;
try {
if (!parserErrorListener.hasError) {
new TypeCheckingVisitor().start(p, new SymbolTable());
final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor();
simpleVisitor.start(p);
LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex"));
isSimple &= !simpleVisitor.isComplex();
new InheritedAttributeTransformer().start(p);
new LocalAggregationTransformer().start(p);
// also let jobs have own methods if visitor merging is disabled
if (!simpleVisitor.isComplex() || maxVisitors < 2 || inputFiles.size() == 1) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(jobName);
} else // if a job has visitors, fuse them all together into a single program
{
p.getProgram().jobName = jobName;
visitorPrograms.add(p.getProgram());
}
}
} catch (final TypeCheckException e) {
parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e);
}
} catch (final Exception e) {
System.err.print(f.getName() + ": compilation failed: ");
e.printStackTrace();
}
}
if (!visitorPrograms.isEmpty())
try {
for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(p.jobName);
}
} catch (final Exception e) {
System.err.println("error fusing visitors - falling back: " + e);
e.printStackTrace();
for (final Program p : visitorPrograms) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(p.jobName);
}
}
if (jobs.size() == 0)
throw new RuntimeException("no files compiled without error");
final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
st.add("name", className);
st.add("numreducers", inputFiles.size());
st.add("jobs", jobs);
st.add("jobnames", jobnames);
st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024);
st.add("seeds", seeds);
if (DefaultProperties.localDataPath != null) {
st.add("isLocal", true);
}
o.write(st.render().getBytes());
} finally {
o.close();
}
compileGeneratedSrc(cl, jarName, outputRoot, outputFile);
}
Aggregations