Merging in location data in the AST. Yee-hah.

This commit is contained in:
Jeremy Ashkenas 2013-02-25 17:51:05 +13:00
parent 5e498ca395
commit ac9d0e17e4
4 changed files with 40 additions and 37 deletions

View File

@ -24,8 +24,8 @@
return "yy.addLocationDataFn(@" + first + ", @" + last + ")"; return "yy.addLocationDataFn(@" + first + ", @" + last + ")";
} }
}; };
action = action.replace(/LOCDATA\(([0-9]*)\)/g, addLocationDataFn('$1')); action = action.replace(/LOC\(([0-9]*)\)/g, addLocationDataFn('$1'));
action = action.replace(/LOCDATA\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2')); action = action.replace(/LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2'));
return [patternString, "$$ = " + (addLocationDataFn(1, patternCount)) + "(" + action + ");", options]; return [patternString, "$$ = " + (addLocationDataFn(1, patternCount)) + "(" + action + ");", options];
}; };
@ -96,10 +96,10 @@
o('ObjAssignable', function() { o('ObjAssignable', function() {
return Value.wrap($1); return Value.wrap($1);
}), o('ObjAssignable : Expression', function() { }), o('ObjAssignable : Expression', function() {
return new Assign(LOCDATA(1)(Value.wrap($1)), $3, 'object'); return new Assign(LOC(1)(Value.wrap($1)), $3, 'object');
}), o('ObjAssignable :\ }), o('ObjAssignable :\
INDENT Expression OUTDENT', function() { INDENT Expression OUTDENT', function() {
return new Assign(LOCDATA(1)(Value.wrap($1)), $4, 'object'); return new Assign(LOC(1)(Value.wrap($1)), $4, 'object');
}), o('Comment') }), o('Comment')
], ],
ObjAssignable: [o('Identifier'), o('AlphaNumeric'), o('ThisProperty')], ObjAssignable: [o('Identifier'), o('AlphaNumeric'), o('ThisProperty')],
@ -189,7 +189,7 @@
}), o('?. Identifier', function() { }), o('?. Identifier', function() {
return new Access($2, 'soak'); return new Access($2, 'soak');
}), o(':: Identifier', function() { }), o(':: Identifier', function() {
return [LOCDATA(1)(new Access(new Literal('prototype'))), LOCDATA(2)(new Access($2))]; return [LOC(1)(new Access(new Literal('prototype'))), LOC(2)(new Access($2))];
}), o('::', function() { }), o('::', function() {
return new Access(new Literal('prototype')); return new Access(new Literal('prototype'));
}), o('Index') }), o('Index')
@ -281,7 +281,7 @@
], ],
ThisProperty: [ ThisProperty: [
o('@ Identifier', function() { o('@ Identifier', function() {
return Value.wrap(LOCDATA(1)(new Literal('this')), [LOCDATA(2)(new Access($2))], 'this'); return Value.wrap(LOC(1)(new Literal('this')), [LOC(2)(new Access($2))], 'this');
}) })
], ],
Array: [ Array: [
@ -348,7 +348,7 @@
o('CATCH Identifier Block', function() { o('CATCH Identifier Block', function() {
return [$2, $3]; return [$2, $3];
}), o('CATCH Object Block', function() { }), o('CATCH Object Block', function() {
return [LOCDATA(2)(Value.wrap($2)), $3]; return [LOC(2)(Value.wrap($2)), $3];
}) })
], ],
Throw: [ Throw: [
@ -385,18 +385,18 @@
o('WhileSource Block', function() { o('WhileSource Block', function() {
return $1.addBody($2); return $1.addBody($2);
}), o('Statement WhileSource', function() { }), o('Statement WhileSource', function() {
return $2.addBody(LOCDATA(1)(Block.wrap([$1]))); return $2.addBody(LOC(1)(Block.wrap([$1])));
}), o('Expression WhileSource', function() { }), o('Expression WhileSource', function() {
return $2.addBody(LOCDATA(1)(Block.wrap([$1]))); return $2.addBody(LOC(1)(Block.wrap([$1])));
}), o('Loop', function() { }), o('Loop', function() {
return $1; return $1;
}) })
], ],
Loop: [ Loop: [
o('LOOP Block', function() { o('LOOP Block', function() {
return new While(LOCDATA(1)(new Literal('true'))).addBody($2); return new While(LOC(1)(new Literal('true'))).addBody($2);
}), o('LOOP Expression', function() { }), o('LOOP Expression', function() {
return new While(LOCDATA(1)(new Literal('true'))).addBody(LOCDATA(2)(Block.wrap([$2]))); return new While(LOC(1)(new Literal('true'))).addBody(LOC(2)(Block.wrap([$2])));
}) })
], ],
For: [ For: [
@ -411,7 +411,7 @@
ForBody: [ ForBody: [
o('FOR Range', function() { o('FOR Range', function() {
return { return {
source: LOCDATA(2)(Value.wrap($2)) source: LOC(2)(Value.wrap($2))
}; };
}), o('ForStart ForSource', function() { }), o('ForStart ForSource', function() {
$2.own = $1.own; $2.own = $1.own;
@ -520,12 +520,12 @@
o('IfBlock'), o('IfBlock ELSE Block', function() { o('IfBlock'), o('IfBlock ELSE Block', function() {
return $1.addElse($3); return $1.addElse($3);
}), o('Statement POST_IF Expression', function() { }), o('Statement POST_IF Expression', function() {
return new If($3, LOCDATA(1)(Block.wrap([$1])), { return new If($3, LOC(1)(Block.wrap([$1])), {
type: $2, type: $2,
statement: true statement: true
}); });
}), o('Expression POST_IF Expression', function() { }), o('Expression POST_IF Expression', function() {
return new If($3, LOCDATA(1)(Block.wrap([$1])), { return new If($3, LOC(1)(Block.wrap([$1])), {
type: $2, type: $2,
statement: true statement: true
}); });

View File

@ -1,4 +1,4 @@
/* parser generated by jison 0.4.2 */ /* Jison generated parser */
var parser = (function(){ var parser = (function(){
var parser = {trace: function trace() { }, var parser = {trace: function trace() { },
yy: {}, yy: {},
@ -591,16 +591,19 @@ return new Parser;
if (typeof require !== 'undefined' && typeof exports !== 'undefined') { if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
exports.parser = parser; exports.parser = parser;
exports.Parser = parser.Parser; exports.Parser = parser.Parser;
exports.parse = function () { return parser.parse.apply(parser, arguments); }; exports.parse = function () { return parser.parse.apply(parser, arguments); }
exports.main = function commonjsMain(args) { exports.main = function commonjsMain(args) {
if (!args[1]) { if (!args[1])
console.log('Usage: '+args[0]+' FILE'); throw new Error('Usage: '+args[0]+' FILE');
process.exit(1); var source, cwd;
if (typeof process !== 'undefined') {
source = require('fs').readFileSync(require('path').resolve(args[1]), "utf8");
} else {
source = require("file").path(require("file").cwd()).join(args[1]).read({charset: "utf-8"});
} }
var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8");
return exports.parser.parse(source); return exports.parser.parse(source);
}; }
if (typeof module !== 'undefined' && require.main === module) { if (typeof module !== 'undefined' && require.main === module) {
exports.main(process.argv.slice(1)); exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
} }
} }

View File

@ -50,8 +50,8 @@ o = (patternString, action, options) ->
else else
"yy.addLocationDataFn(@#{first}, @#{last})" "yy.addLocationDataFn(@#{first}, @#{last})"
action = action.replace /LOCDATA\(([0-9]*)\)/g, addLocationDataFn('$1') action = action.replace /LOC\(([0-9]*)\)/g, addLocationDataFn('$1')
action = action.replace /LOCDATA\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2') action = action.replace /LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2')
[patternString, "$$ = #{addLocationDataFn(1, patternCount)}(#{action});", options] [patternString, "$$ = #{addLocationDataFn(1, patternCount)}(#{action});", options]
@ -160,9 +160,9 @@ grammar =
# the ordinary **Assign** is that these allow numbers and strings as keys. # the ordinary **Assign** is that these allow numbers and strings as keys.
AssignObj: [ AssignObj: [
o 'ObjAssignable', -> Value.wrap $1 o 'ObjAssignable', -> Value.wrap $1
o 'ObjAssignable : Expression', -> new Assign LOCDATA(1)(Value.wrap($1)), $3, 'object' o 'ObjAssignable : Expression', -> new Assign LOC(1)(Value.wrap($1)), $3, 'object'
o 'ObjAssignable : o 'ObjAssignable :
INDENT Expression OUTDENT', -> new Assign LOCDATA(1)(Value.wrap($1)), $4, 'object' INDENT Expression OUTDENT', -> new Assign LOC(1)(Value.wrap($1)), $4, 'object'
o 'Comment' o 'Comment'
] ]
@ -264,7 +264,7 @@ grammar =
Accessor: [ Accessor: [
o '. Identifier', -> new Access $2 o '. Identifier', -> new Access $2
o '?. Identifier', -> new Access $2, 'soak' o '?. Identifier', -> new Access $2, 'soak'
o ':: Identifier', -> [LOCDATA(1)(new Access new Literal 'prototype'), LOCDATA(2)(new Access $2)] o ':: Identifier', -> [LOC(1)(new Access new Literal 'prototype'), LOC(2)(new Access $2)]
o '::', -> new Access new Literal 'prototype' o '::', -> new Access new Literal 'prototype'
o 'Index' o 'Index'
] ]
@ -336,7 +336,7 @@ grammar =
# A reference to a property on *this*. # A reference to a property on *this*.
ThisProperty: [ ThisProperty: [
o '@ Identifier', -> Value.wrap LOCDATA(1)(new Literal('this')), [LOCDATA(2)(new Access($2))], 'this' o '@ Identifier', -> Value.wrap LOC(1)(new Literal('this')), [LOC(2)(new Access($2))], 'this'
] ]
# The array literal. # The array literal.
@ -400,7 +400,7 @@ grammar =
# A catch clause names its error and runs a block of code. # A catch clause names its error and runs a block of code.
Catch: [ Catch: [
o 'CATCH Identifier Block', -> [$2, $3] o 'CATCH Identifier Block', -> [$2, $3]
o 'CATCH Object Block', -> [LOCDATA(2)(Value.wrap($2)), $3] o 'CATCH Object Block', -> [LOC(2)(Value.wrap($2)), $3]
] ]
# Throw an exception object. # Throw an exception object.
@ -429,14 +429,14 @@ grammar =
# or postfix, with a single expression. There is no do..while. # or postfix, with a single expression. There is no do..while.
While: [ While: [
o 'WhileSource Block', -> $1.addBody $2 o 'WhileSource Block', -> $1.addBody $2
o 'Statement WhileSource', -> $2.addBody LOCDATA(1) Block.wrap([$1]) o 'Statement WhileSource', -> $2.addBody LOC(1) Block.wrap([$1])
o 'Expression WhileSource', -> $2.addBody LOCDATA(1) Block.wrap([$1]) o 'Expression WhileSource', -> $2.addBody LOC(1) Block.wrap([$1])
o 'Loop', -> $1 o 'Loop', -> $1
] ]
Loop: [ Loop: [
o 'LOOP Block', -> new While(LOCDATA(1) new Literal 'true').addBody $2 o 'LOOP Block', -> new While(LOC(1) new Literal 'true').addBody $2
o 'LOOP Expression', -> new While(LOCDATA(1) new Literal 'true').addBody LOCDATA(2) Block.wrap [$2] o 'LOOP Expression', -> new While(LOC(1) new Literal 'true').addBody LOC(2) Block.wrap [$2]
] ]
# Array, object, and range comprehensions, at the most generic level. # Array, object, and range comprehensions, at the most generic level.
@ -449,7 +449,7 @@ grammar =
] ]
ForBody: [ ForBody: [
o 'FOR Range', -> source: LOCDATA(2) Value.wrap($2) o 'FOR Range', -> source: LOC(2) Value.wrap($2)
o 'ForStart ForSource', -> $2.own = $1.own; $2.name = $1[0]; $2.index = $1[1]; $2 o 'ForStart ForSource', -> $2.own = $1.own; $2.name = $1[0]; $2.index = $1[1]; $2
] ]
@ -519,8 +519,8 @@ grammar =
If: [ If: [
o 'IfBlock' o 'IfBlock'
o 'IfBlock ELSE Block', -> $1.addElse $3 o 'IfBlock ELSE Block', -> $1.addElse $3
o 'Statement POST_IF Expression', -> new If $3, LOCDATA(1)(Block.wrap [$1]), type: $2, statement: true o 'Statement POST_IF Expression', -> new If $3, LOC(1)(Block.wrap [$1]), type: $2, statement: true
o 'Expression POST_IF Expression', -> new If $3, LOCDATA(1)(Block.wrap [$1]), type: $2, statement: true o 'Expression POST_IF Expression', -> new If $3, LOC(1)(Block.wrap [$1]), type: $2, statement: true
] ]
# Arithmetic and logical operators, working on one or more operands. # Arithmetic and logical operators, working on one or more operands.

View File

@ -104,7 +104,7 @@ exports.Lexer = class Lexer
return 0 unless match = IDENTIFIER.exec @chunk return 0 unless match = IDENTIFIER.exec @chunk
[input, id, colon] = match [input, id, colon] = match
# Preserve lenght of id for location data # Preserve length of id for location data
idLength = id.length idLength = id.length
poppedToken = undefined poppedToken = undefined