Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Add a parseExpression public method (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromew authored and danez committed Jan 27, 2017
1 parent b6b4610 commit 898c4a7
Show file tree
Hide file tree
Showing 249 changed files with 7,484 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ export function parse(input, options) {
return new Parser(options, input).parse();
}

export function parseExpression(input, options) {
const parser = new Parser(options, input);
if (parser.options.strictMode) {
parser.state.strict = true;
}
return parser.getExpression();
}


export { tokTypes };
10 changes: 10 additions & 0 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ pp.checkPropClash = function (prop, propHash) {
}
};

// Convenience method to parse an Expression only
pp.getExpression = function() {
this.nextToken();
const expr = this.parseExpression();
if (!this.match(tt.eof)) {
this.unexpected();
}
return expr;
};

// ### Expression parsing

// These nest, from the most general expression type at the top to
Expand Down
21 changes: 21 additions & 0 deletions test/expressions/esprima/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x + y
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "BinaryExpression",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "x"
},
"name": "x"
},
"operator": "+",
"right": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "y"
},
"name": "y"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x - y
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "BinaryExpression",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "x"
},
"name": "x"
},
"operator": "-",
"right": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "y"
},
"name": "y"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict" + 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"type": "BinaryExpression",
"start": 0,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 17
}
},
"left": {
"type": "StringLiteral",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"extra": {
"rawValue": "use strict",
"raw": "\"use strict\""
},
"value": "use strict"
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start": 15,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 17
}
},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "AssignmentExpression",
"start": 0,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"operator": "=",
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "x"
},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start": 4,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 6
}
},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "AssignmentExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"operator": "=",
"left": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "eval"
},
"name": "eval"
},
"right": {
"type": "NumericLiteral",
"start": 7,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 9
}
},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arguments = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "AssignmentExpression",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"operator": "=",
"left": {
"type": "Identifier",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "arguments"
},
"name": "arguments"
},
"right": {
"type": "NumericLiteral",
"start": 12,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 14
}
},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x *= 42
Loading

0 comments on commit 898c4a7

Please sign in to comment.