Git Product home page Git Product logo

ecmascript-parser's Introduction

ECMAScript parser   Build Status

An ANTLR4 grammar for ECMAScript based on the Standard ECMA-262 5.1 Edition from June 2011.

Install

To install this library, do the following:

git clone https://github.com/bkiers/ecmascript-parser
cd ecmascript-parser
mvn clean install

Example

Let's say you would like to extract all regex literals from the following script:

// comment /./m

var x = a / b / m; // division, no regex!

/.*?\/\n\\(?!$)/.match('some text')

/*
another
/no pattern!/
comment
*/

var z = /a/m

If you look at the parser rule for a JS literal:

literal
 : ( NullLiteral
   | BooleanLiteral
   | StringLiteral
   | RegularExpressionLiteral
   )
 | numericLiteral
 ;

you see that it can potentially match a RegularExpressionLiteral.

We can now retrieve all regex literals by attaching a tree listener to the parser and upon entering the literal rule, we simply check if RegularExpressionLiteral() is not null, in which case we print the contents of this token:

String script = "// comment /./m                   \n" +
        "                                          \n" +
        "var x = a / b / m; // division, no regex! \n" +
        "                                          \n" +
        "/.*?\\/\\n\\\\(?!$)/.match('some text')   \n" +
        "                                          \n" +
        "/*                                        \n" +
        "another                                   \n" +
        "/no pattern!/                             \n" +
        "comment                                   \n" +
        "*/                                        \n" +
        "                                          \n" +
        "var z = /a/m                              \n";

// Create the parser.
ECMAScriptParser parser = new Builder.Parser(script).build();

// Walk the parse tree and listen when the `literal` is being entered.
ParseTreeWalker.DEFAULT.walk(new ECMAScriptBaseListener(){
    @Override
    public void enterLiteral(@NotNull ECMAScriptParser.LiteralContext ctx) {
        if (ctx.RegularExpressionLiteral() != null) {
            System.out.println("regex: " + ctx.RegularExpressionLiteral().getText());
        }
    }
}, parser.program());

Running the code above will print the following:

regex: /.*?\/\n\\(?!$)/
regex: /a/m

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.