Search in sources :

Example 1 with Parser

use of com.google.javascript.jscomp.parsing.parser.Parser in project closure-compiler by google.

the class ParserRunner method parse.

public static ParseResult parse(StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
    // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
    String sourceName = sourceFile.getName();
    try {
        SourceFile file = new SourceFile(sourceName, sourceString);
        boolean keepGoing = config.runMode() == Config.RunMode.KEEP_GOING;
        Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
        com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = newParserConfig(config);
        Parser p = new Parser(es6config, es6ErrorReporter, file);
        ProgramTree tree = p.parseProgram();
        Node root = null;
        List<Comment> comments = ImmutableList.of();
        FeatureSet features = p.getFeatures();
        if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
            IRFactory factory = IRFactory.transformTree(tree, sourceFile, config, errorReporter);
            root = factory.getResultNode();
            features = features.union(factory.getFeatures());
            root.putProp(Node.FEATURE_SET, features);
            if (config.jsDocParsingMode().shouldParseDescriptions()) {
                comments = p.getComments();
            }
        }
        return new ParseResult(root, comments, features, p.getSourceMapURL());
    } catch (Throwable t) {
        throw new RuntimeException("Exception parsing \"" + sourceName + "\"", t);
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) Node(com.google.javascript.rhino.Node) Parser(com.google.javascript.jscomp.parsing.parser.Parser) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile)

Example 2 with Parser

use of com.google.javascript.jscomp.parsing.parser.Parser in project closure-compiler by google.

the class ParserRunner method detectFeatures.

// TODO(sdh): this is less useful if we end up needing the node for library version detection
public static FeatureSet detectFeatures(String sourcePath, String sourceString) {
    SourceFile file = new SourceFile(sourcePath, sourceString);
    ErrorReporter reporter = IRFactory.NULL_REPORTER;
    com.google.javascript.jscomp.parsing.parser.Parser.Config config = newParserConfig(IRFactory.NULL_CONFIG);
    Parser p = new Parser(config, new Es6ErrorReporter(reporter, false), file);
    ProgramTree tree = p.parseProgram();
    StaticSourceFile simpleSourceFile = new SimpleSourceFile(sourcePath, false);
    return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).union(p.getFeatures());
}
Also used : ErrorReporter(com.google.javascript.rhino.ErrorReporter) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) Parser(com.google.javascript.jscomp.parsing.parser.Parser)

Aggregations

Parser (com.google.javascript.jscomp.parsing.parser.Parser)2 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)2 ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)2 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)2 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 ErrorReporter (com.google.javascript.rhino.ErrorReporter)1 Node (com.google.javascript.rhino.Node)1 SimpleSourceFile (com.google.javascript.rhino.SimpleSourceFile)1