Giter Club home page Giter Club logo

esprima-dotnet's Introduction

๐Ÿ“ฃ Important notices
If you are upgrading from an older version, please note that version 3 ships with numerous breaking changes to the public API because virtually all areas of the library have been revised.
Documentation of the previous major version is available here.

Build NuGet MyGet

Esprima .NET (BSD license) is a .NET port of the esprima.org project. It is a standard-compliant ECMAScript parser (also popularly known as JavaScript).

Features

API

Esprima can be used to perform lexical analysis (tokenization) or syntactic analysis (parsing) of a JavaScript program.

A simple C# example:

var parser = new JavaScriptParser();
var program = parser.ParseScript("const answer = 42");

You can control the behavior of the parser by initializing and passing a ParserOptions to the parser's constructor. (For the available options, see the XML comments of the ParserOptions class.)

Instead of ParseScript, you may use ParseModule or ParseExpression to make the parser treat the input as an ES6 module or as a plain JavaScript expression respectively.

In case the input is syntactically correct, each of these methods returns the root node of the resulting abstract syntax tree (AST), which you can freely analyze or transform. The library provides the AstVisitor and AstRewriter visitor classes to help you with such tasks.

When the input contains a severe syntax error, a ParserException is thrown. By catching it you can get details about the error. There are syntax errors though which can be tolerated by the parser. Such errors are ignored by default. You can record them by setting ParserOptions.ErrorHandler to an instance of CollectingErrorHandler. Alternatively, you can set ParserOptions.Tolerant to false to make the parser throw exceptions also in the case of tolerable syntax errors.

The library is able to write the AST (except for comments) back to JavaScript code:

var code = program.ToJavaScriptString(format: true);

It is also possible to serialize the AST into a JSON representation:

var astJson = program.ToJsonString(indent: "    ");

Considering the example above this call will return the following JSON:

{
    "type": "Program",
    "body": [
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "Identifier",
                        "name": "answer"
                    },
                    "init": {
                        "type": "Literal",
                        "value": 42,
                        "raw": "42"
                    }
                }
            ],
            "kind": "const"
        }
    ],
    "sourceType": "script",
    "strict": false
}

Benchmarks

Here is a list of common JavaScript libraries and the time it takes to parse them, compared to the time it took for the same script using the original Esprima in Chrome.

Script Size Esprima .NET Esprima (Chrome)
underscore-1.5.2 43 KB 2.4 ms 3.1 ms
backbone-1.1.0 60 KB 2.9 ms 3.5 ms
mootools-1.4.5 163 KB 18.7 ms 16.2 ms
jquery-1.9.1 271 KB 22.8 ms 19.0 ms
yui-3.12.0 341 KB 17.2 ms 16.2 ms
jquery.mobile-1.4.2 456 K 43.3 ms 46.9 ms
angular-1.2.5 721 KB 29.3 ms 37.2 ms

esprima-dotnet's People

Contributors

lahma avatar sebastienros avatar adams85 avatar atifaziz avatar jogibear9988 avatar xicy avatar kurtgokhan avatar laurent-yin avatar sam-lord avatar dependabot[bot] avatar charlieeriksen avatar ltcmelo avatar viceice avatar pluethi1 avatar rohan-warang avatar jarlob avatar maximburyak avatar tschombe avatar

Watchers

James Cloos avatar

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.