Skip to content

Commit

Permalink
Added 'this' variable support to the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jdudek committed Mar 22, 2012
1 parent e02bde4 commit 7995ebc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ast.js
Expand Up @@ -43,6 +43,7 @@ exports.BooleanLiteral = makeNodeConstructor("booleanLiteral", ["value"]);
exports.ObjectLiteral = makeNodeConstructor("objectLiteral", ["pairs"]);
exports.ArrayLiteral = makeNodeConstructor("arrayLiteral", ["items"]);
exports.FunctionLiteral = makeNodeConstructor("functionLiteral", ["args", "statements"]);
exports.ThisVariable = makeNodeConstructor("thisVariable", ["keyword"]);
exports.Variable = makeNodeConstructor("variable", ["identifier"]);
exports.Invocation = makeNodeConstructor("invocation", ["expression", "args"]);
exports.Refinement = makeNodeConstructor("refinement", ["expression", "key"]);
Expand Down
7 changes: 4 additions & 3 deletions src/parser.js
Expand Up @@ -443,9 +443,9 @@ var arrayLiteral = function (input) {
return p(input);
};

var variable = decorate(identifier, function (i) {
return AST.Variable(i);
});
var thisVariable = decorate(keyword("this"), AST.ThisVariable);

var variable = decorate(identifier, AST.Variable);

var functionLiteral = function (input) {
var args = parens(sepBy(symbol(","), identifier));
Expand Down Expand Up @@ -506,6 +506,7 @@ var expr = function (input) {
objectLiteral,
arrayLiteral,
functionLiteral,
thisVariable,
variable,
parens(expr)
]);
Expand Down
1 change: 1 addition & 0 deletions test/parser_test.js
Expand Up @@ -143,6 +143,7 @@ testParser("'abc'", { stringLiteral: "abc" }, parser.expr);
testParser('"abc"', { stringLiteral: "abc" }, parser.expr);
testParser("true", { booleanLiteral: true }, parser.expr);
testParser("false", { booleanLiteral: false }, parser.expr);
testParser("this", { thisVariable: "this" }, parser.expr);
testParser("{}", { objectLiteral: [] }, parser.expr);
testParser("{x: 2}", { objectLiteral: [["x", { numberLiteral: 2 }]] }, parser.expr);
testParser('{"x": 2}', { objectLiteral: [["x", { numberLiteral: 2 }]] }, parser.expr);
Expand Down

0 comments on commit 7995ebc

Please sign in to comment.