Search in sources :

Example 6 with Parser

use of org.mozilla.javascript.Parser in project whole by wholeplatform.

the class RhinoTransformerVisitor method transform.

public static IEntity transform(IPersistenceProvider pp) throws Exception {
    CompilerEnvirons compilerEnvirons = CompilerEnvirons.ideEnvirons();
    compilerEnvirons.setLanguageVersion(Context.VERSION_1_8);
    Parser parser = new Parser(compilerEnvirons);
    AstRoot astRoot = parser.parse(new InputStreamReader(pp.getInputStream(), pp.getEncoding()), "http://whole.sourceforge.net/test.js", 0);
    ModelBuilderOperation mop = new ModelBuilderOperation();
    RhinoTransformerVisitor visitor = new RhinoTransformerVisitor(mop);
    astRoot.visit(visitor);
    return mop.wGetResult();
}
Also used : ModelBuilderOperation(org.whole.lang.builders.ModelBuilderOperation) InputStreamReader(java.io.InputStreamReader) CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) Parser(org.mozilla.javascript.Parser)

Example 7 with Parser

use of org.mozilla.javascript.Parser in project pmd by pmd.

the class EcmascriptParser method parseEcmascript.

protected AstRoot parseEcmascript(final String sourceCode, final List<ParseProblem> parseProblems) throws ParseException {
    final CompilerEnvirons compilerEnvirons = new CompilerEnvirons();
    compilerEnvirons.setRecordingComments(parserOptions.isRecordingComments());
    compilerEnvirons.setRecordingLocalJsDocComments(parserOptions.isRecordingLocalJsDocComments());
    compilerEnvirons.setLanguageVersion(parserOptions.getRhinoLanguageVersion().getVersion());
    // Scope's don't appear to get set right without this
    compilerEnvirons.setIdeMode(true);
    compilerEnvirons.setWarnTrailingComma(true);
    // see bug #1150 "EmptyExpression" for valid statements!
    compilerEnvirons.setReservedKeywordAsIdentifier(true);
    // TODO We should do something with Rhino errors...
    final ErrorCollector errorCollector = new ErrorCollector();
    final Parser parser = new Parser(compilerEnvirons, errorCollector);
    // TODO Fix hardcode
    final String sourceURI = "unknown";
    final int beginLineno = 1;
    AstRoot astRoot = parser.parse(sourceCode, sourceURI, beginLineno);
    parseProblems.addAll(errorCollector.getErrors());
    return astRoot;
}
Also used : CompilerEnvirons(org.mozilla.javascript.CompilerEnvirons) ErrorCollector(org.mozilla.javascript.ast.ErrorCollector) Parser(org.mozilla.javascript.Parser) AstRoot(org.mozilla.javascript.ast.AstRoot)

Example 8 with Parser

use of org.mozilla.javascript.Parser in project MtgDesktopCompanion by nicho92.

the class MTGSalvationDeckSniffer method parseColor.

private String parseColor(String string) {
    AstNode node = new Parser().parse(string, "", 1);
    node.visit(n -> {
        manajson = n.toSource();
        return false;
    });
    manajson = manajson.substring(manajson.indexOf("series") + "series: ".length(), manajson.length() - 8);
    JsonArray arr = new JsonParser().parse(manajson).getAsJsonArray();
    manajson = "";
    boolean hascolor = false;
    for (int i = 0; i < arr.size(); i++) {
        JsonObject obj = arr.get(i).getAsJsonObject();
        String c = ColorParser.parse(obj.get("name").getAsString());
        JsonArray tab = obj.get("data").getAsJsonArray();
        hascolor = false;
        for (int j = 0; j < tab.size(); j++) {
            if (tab.get(j).getAsInt() > 0)
                hascolor = true;
        }
        if (hascolor && !c.equals("{C}")) {
            manajson += c;
        }
    }
    return manajson;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) AstNode(org.mozilla.javascript.ast.AstNode) JsonParser(com.google.gson.JsonParser) ColorParser(org.magic.tools.ColorParser) Parser(org.mozilla.javascript.Parser) JsonParser(com.google.gson.JsonParser)

Aggregations

Parser (org.mozilla.javascript.Parser)8 CompilerEnvirons (org.mozilla.javascript.CompilerEnvirons)6 ErrorReporter (org.mozilla.javascript.ErrorReporter)2 NodeTransformer (org.mozilla.javascript.NodeTransformer)2 AstNode (org.mozilla.javascript.ast.AstNode)2 ScriptNode (org.mozilla.javascript.ast.ScriptNode)2 TestErrorReporter (org.mozilla.javascript.testing.TestErrorReporter)2 ToolErrorReporter (org.mozilla.javascript.tools.ToolErrorReporter)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TreeMap (java.util.TreeMap)1 Document (org.jsoup.nodes.Document)1 Element (org.jsoup.nodes.Element)1