Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# actually wrote jsparse.
Chris Double <chris.double@double.co.nz>

# just this random guy who came along and packaged it for node.
Peter Burns <rictic@gmail.com>

120 changes: 61 additions & 59 deletions es3.js → examples/es3.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (C) 2007 Chris Double.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
//
// 2. 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 ``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 THE
Expand All @@ -22,16 +22,18 @@
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

//This has not been updated to use jsparse.*

// Forward Declarations
var SourceElement =
var SourceElement =
function(input) { return SourceElement(input); }
var AssignmentExpression =
var AssignmentExpression =
function(input) { return AssignmentExpression(input); }
var Expression =
var Expression =
function(input) { return Expression(input); }
var Statement =
var Statement =
function(input) { return Statement(input); }
var LeftHandSideExpression =
var LeftHandSideExpression =
function(input) { return LeftHandSideExpression(input); }

var Whitespace = choice("\t", " ");
Expand All @@ -48,7 +50,7 @@ var BooleanLiteral = choice("true", "false");
var Zero = action("0", function(ast) { return 0; });
var DecimalDigit = action(range("0", "9"), function(ast) { return parseInt(ast); });
var NonZeroDigit = action(range("1", "9"), function(ast) { return parseInt(ast); });
var DecimalDigits = repeat1(DecimalDigit);
var DecimalDigits = repeat1(DecimalDigit);
var DecimalIntegerLiteral = choice(Zero, sequence(NonZeroDigit, optional(DecimalDigits)));
var SignedInteger = choice(DecimalDigits, sequence("+", DecimalDigits), sequence("-", DecimalDigits));
var ExponentIndicator = choice("e", "E");
Expand Down Expand Up @@ -77,9 +79,9 @@ var DoubleStringCharacters = repeat1(DoubleStringCharacter);
var StringLiteral = choice(sequence("\"", optional(DoubleStringCharacters), "\""),
sequence("'", optional(SingleStringCharacters), "'"));

var Literal = choice(NullLiteral, BooleanLiteral, NumericLiteral, StringLiteral);
var Literal = choice(NullLiteral, BooleanLiteral, NumericLiteral, StringLiteral);

var Keyword =
var Keyword =
choice("break",
"case",
"catch",
Expand Down Expand Up @@ -112,24 +114,24 @@ var HexDigit = choice(range("0", "9"), range("a", "f"), range("A", "F"));
var IdentifierLetter = choice(range("a", "z"), range("A", "Z"));
var IdentifierStart = choice(IdentifierLetter, "$", "_");
var IdentifierPart = choice(IdentifierStart, range("0-9"));
var IdentifierName =
var IdentifierName =
action(sequence(IdentifierStart, join_action(repeat0(IdentifierPart), "")),
function(ast) {
return ast[0].concat(ast[1]);
function(ast) {
return ast[0].concat(ast[1]);
});
var Identifier = butnot(IdentifierName, ReservedWord);

var StatementList = repeat1(Statement);
var Block = wsequence("{", optional(StatementList), "}");
var Initialiser = wsequence("=", AssignmentExpression);
var VariableDeclaration = wsequence(Identifier, optional(Initialiser));
var VariableDeclarationList = wlist(VariableDeclaration, ",");
var VariableStatement =
var VariableDeclarationList = wlist(VariableDeclaration, ",");
var VariableStatement =
wsequence("var", VariableDeclarationList);

var EmptyStatement = token(";");

var IfStatement =
var IfStatement =
choice(wsequence("if", "(", Expression, ")", Statement, "else", Statement),
wsequence("if", "(", Expression, ")", Statement));

Expand Down Expand Up @@ -161,15 +163,15 @@ var ThrowStatement = wsequence("throw", Expression, ";");

var Catch = wsequence("catch", "(", Identifier, ")", Block);
var Finally = wsequence("finally", Block);
var TryStatement =
var TryStatement =
choice(wsequence("try", Block, Catch),
wsequence("try", Block, Finally),
wsequence("try", Block, Catch, Finally));

var ExpressionStatement =
var ExpressionStatement =
choice(sequence(choice("{", "function"), nothing_p),
Expression);
var Statement =
var Statement =
choice(Block,
VariableStatement,
EmptyStatement,
Expand All @@ -185,46 +187,46 @@ var Statement =
ThrowStatement,
TryStatement);

var FunctionDeclaration =
var FunctionDeclaration =
function(input) { return FunctionDeclaration(input); }

var FunctionBody = repeat0(SourceElement);
var FormalParameterList = wlist(Identifier, ",");
var FunctionExpression =
var FormalParameterList = wlist(Identifier, ",");
var FunctionExpression =
wsequence("function", optional(Identifier), "(", optional(FormalParameterList), ")", "{", FunctionBody, "}");

var FunctionDeclaration =
var FunctionDeclaration =
wsequence("function", Identifier, "(", optional(FormalParameterList), ")", "{", FunctionBody, "}");


var PrimaryExpression =
var PrimaryExpression =
function(input) { return PrimaryExpression(input); }

var ArgumentList = list(AssignmentExpression, ",");
var Arguments =
var ArgumentList = list(AssignmentExpression, ",");
var Arguments =
choice(wsequence("(", ")"),
wsequence("(", ArgumentList, ")"));

var MemberExpression = function(input) { return MemberExpression(input); }
var MemberExpression = function(input) { return MemberExpression(input); }
var MemberExpression =
left_factor_action(sequence(choice(wsequence("new", MemberExpression, Arguments),
PrimaryExpression,
FunctionExpression),
repeat0(choice(wsequence("[", Expression, "]"),
wsequence(".", Identifier)))));

var NewExpression =
var NewExpression =
choice(MemberExpression,
wsequence("new", NewExpression));
var CallExpression =
var CallExpression =
left_factor_action(wsequence(wsequence(MemberExpression, Arguments),
repeat0(choice(Arguments,
wsequence("[", Expression, "]"),
wsequence(".", Identifier)))));

var LeftHandSideExpression = choice(CallExpression, NewExpression);

var AssignmentOperator =
var AssignmentOperator =
choice("=",
"*=",
"/=",
Expand All @@ -238,29 +240,29 @@ var AssignmentOperator =
"^=",
"|=");

var LogicalORExpression =
var LogicalORExpression =
function(input) { return LogicalORExpression(input); }
var LogicalANDExpression =
var LogicalANDExpression =
function(input) { return LogicalANDExpression(input); }
var BitwiseORExpression =
var BitwiseORExpression =
function(input) { return BitwiseORExpression(input); }
var BitwiseXORExpression =
var BitwiseXORExpression =
function(input) { return BitwiseXORExpression(input); }
var BitwiseANDExpression =
var BitwiseANDExpression =
function(input) { return BitwiseANDExpression(input); }
var EqualityExpression =
var EqualityExpression =
function(input) { return EqualityExpression(input); }
var RelationalExpression =
var RelationalExpression =
function(input) { return RelationalExpression(input); }
var ShiftExpression =
var ShiftExpression =
function(input) { return ShiftExpression(input); }
var AdditiveExpression =
var AdditiveExpression =
function(input) { return AdditiveExpression(input); }
var MultiplicativeExpression =
var MultiplicativeExpression =
function(input) { return MultiplicativeExpression(input); }
var UnaryExpression =
var UnaryExpression =
function(input) { return UnaryExpression(input); }
var PostfixExpression =
var PostfixExpression =
function(input) { return PostfixExpression(input); }

var PostfixExpression =
Expand Down Expand Up @@ -290,8 +292,8 @@ var AdditiveExpression =
wsequence(MultiplicativeExpression,
repeat0(choice(wsequence("+", MultiplicativeExpression),
wsequence("-", MultiplicativeExpression))));
var ShiftExpression =

var ShiftExpression =
wsequence(AdditiveExpression,
repeat0(choice(wsequence("<<", AdditiveExpression),
wsequence(">>", AdditiveExpression),
Expand All @@ -306,49 +308,49 @@ var RelationalExpression =
wsequence("instanceof", ShiftExpression))));

var EqualityExpression =
wsequence(RelationalExpression,
wsequence(RelationalExpression,
repeat0(choice(wsequence("==", RelationalExpression),
wsequence("!==", RelationalExpression),
wsequence("===", RelationalExpression),
wsequence("!==", RelationalExpression))));

var BitwiseANDExpression =
var BitwiseANDExpression =
wsequence(EqualityExpression, repeat0(wsequence("&", EqualityExpression)));
var BitwiseXORExpression =
var BitwiseXORExpression =
wsequence(BitwiseANDExpression, repeat0(wsequence("^", BitwiseANDExpression)));
var BitwiseORExpression =
var BitwiseORExpression =
wsequence(BitwiseXORExpression, repeat0(wsequence("|", BitwiseXORExpression)));
var LogicalANDExpression =
var LogicalANDExpression =
wsequence(BitwiseORExpression, repeat0(wsequence("&&", BitwiseORExpression)));

var LogicalORExpression =
var LogicalORExpression =
wsequence(LogicalANDExpression, repeat0(wsequence("||", LogicalANDExpression)));

var ConditionalExpression =
var ConditionalExpression =
choice(LogicalORExpression,
wsequence(LogicalORExpression, "?", AssignmentExpression, ":", AssignmentExpression));

var AssignmentExpression =
var AssignmentExpression =
choice(wsequence(LeftHandSideExpression, AssignmentOperator, AssignmentExpression),
ConditionalExpression);

var Expression = list(AssignmentExpression, ",");

var Elision = repeat1(",");
var Elision = repeat1(",");
var ElementList = list(wsequence(optional(Elision), AssignmentExpression), ",");
var ArrayLiteral =
var ArrayLiteral =
choice(wsequence("[", optional(Elision), "]"),
wsequence("[", ElementList, "]"),
wsequence("[", ElementList, optional(Elision), "]"));

var PropertyName = choice(Identifier, StringLiteral, NumericLiteral);
var PropertyNameAndValueList =
list(wsequence(PropertyName, ":", AssignmentExpression), ",");
var ObjectLiteral =
var ObjectLiteral =
choice(wsequence("{", "}"),
wsequence("{", PropertyNameAndValueList, "}"));

var PrimaryExpression =
var PrimaryExpression =
choice("this",
wsequence("(", Expression, ")"),
Identifier,
Expand Down
19 changes: 11 additions & 8 deletions es3_tests.js → examples/es3_tests.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (C) 2007 Chris Double.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
//
// 2. 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 ``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 THE
Expand All @@ -21,6 +21,9 @@
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

//This has NOT been updated to use jsparse.*

load("jsparse.js");
load("es3.js");
load("tests.js");
Expand All @@ -36,7 +39,7 @@ function LineTerminatorTest() {
assertTrue("LineTerminator failed to parse newline", LineTerminator(ps("\n")));
assertFalse("LineTerminator parsed incorrect data", LineTerminator(ps("abcd")));
}

function SingleLineCommentTest() {
assertTrue("SingleLineComment failed to parse comment with no space", SingleLineComment(ps("//foo\n")));
assertTrue("SingleLineComment failed to parse comment with space", SingleLineComment(ps("// foo\n")));
Expand Down Expand Up @@ -86,7 +89,7 @@ function NonZeroDigitTest() {
function IdentifierTest() {
assertFullyParsed("Identifier", "abcd");
assertFalse("Identifier('while')", Identifier(ps('while')));
assertTrue("Identifier('abcd').ast=='abcd'", Identifier(ps('abcd')).ast=='abcd');
assertTrue("Identifier('abcd').ast=='abcd'", Identifier(ps('abcd')).ast=='abcd');
}

function DecimalDigitsTest() {
Expand Down Expand Up @@ -167,7 +170,7 @@ function FunctionDeclarationTest() {
assertFullyParsed("FunctionBody", "return 123;");
assertFullyParsed("FunctionBody", "return function() { };");
}

function allTests() {
WhitespaceTest();
LineTerminatorTest();
Expand All @@ -189,4 +192,4 @@ function allTests() {
FunctionDeclarationTest();
}

time(function() { runTests(allTests); });
time(function() { runTests(allTests); });
Loading