1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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 + ")";
}
};
action = action.replace(/LOCDATA\(([0-9]*)\)/g, addLocationDataFn('$1'));
action = action.replace(/LOCDATA\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2'));
action = action.replace(/LOC\(([0-9]*)\)/g, addLocationDataFn('$1'));
action = action.replace(/LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2'));
return [patternString, "$$ = " + (addLocationDataFn(1, patternCount)) + "(" + action + ");", options];
};
@ -96,10 +96,10 @@
o('ObjAssignable', function() {
return Value.wrap($1);
}), 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 :\
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')
],
ObjAssignable: [o('Identifier'), o('AlphaNumeric'), o('ThisProperty')],
@ -189,7 +189,7 @@
}), o('?. Identifier', function() {
return new Access($2, 'soak');
}), 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() {
return new Access(new Literal('prototype'));
}), o('Index')
@ -281,7 +281,7 @@
],
ThisProperty: [
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: [
@ -348,7 +348,7 @@
o('CATCH Identifier Block', function() {
return [$2, $3];
}), o('CATCH Object Block', function() {
return [LOCDATA(2)(Value.wrap($2)), $3];
return [LOC(2)(Value.wrap($2)), $3];
})
],
Throw: [
@ -385,18 +385,18 @@
o('WhileSource Block', function() {
return $1.addBody($2);
}), o('Statement WhileSource', function() {
return $2.addBody(LOCDATA(1)(Block.wrap([$1])));
return $2.addBody(LOC(1)(Block.wrap([$1])));
}), o('Expression WhileSource', function() {
return $2.addBody(LOCDATA(1)(Block.wrap([$1])));
return $2.addBody(LOC(1)(Block.wrap([$1])));
}), o('Loop', function() {
return $1;
})
],
Loop: [
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() {
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: [
@ -411,7 +411,7 @@
ForBody: [
o('FOR Range', function() {
return {
source: LOCDATA(2)(Value.wrap($2))
source: LOC(2)(Value.wrap($2))
};
}), o('ForStart ForSource', function() {
$2.own = $1.own;
@ -520,12 +520,12 @@
o('IfBlock'), o('IfBlock ELSE Block', function() {
return $1.addElse($3);
}), 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,
statement: true
});
}), 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,
statement: true
});

View file

@ -1,4 +1,4 @@
/* parser generated by jison 0.4.2 */
/* Jison generated parser */
var parser = (function(){
var parser = {trace: function trace() { },
yy: {},
@ -591,16 +591,19 @@ return new Parser;
if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
exports.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) {
if (!args[1]) {
console.log('Usage: '+args[0]+' FILE');
process.exit(1);
if (!args[1])
throw new Error('Usage: '+args[0]+' FILE');
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);
};
}
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
"yy.addLocationDataFn(@#{first}, @#{last})"
action = action.replace /LOCDATA\(([0-9]*)\)/g, addLocationDataFn('$1')
action = action.replace /LOCDATA\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2')
action = action.replace /LOC\(([0-9]*)\)/g, addLocationDataFn('$1')
action = action.replace /LOC\(([0-9]*),\s*([0-9]*)\)/g, addLocationDataFn('$1', '$2')
[patternString, "$$ = #{addLocationDataFn(1, patternCount)}(#{action});", options]
@ -160,9 +160,9 @@ grammar =
# the ordinary **Assign** is that these allow numbers and strings as keys.
AssignObj: [
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 :
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'
]
@ -264,7 +264,7 @@ grammar =
Accessor: [
o '. Identifier', -> new Access $2
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 'Index'
]
@ -336,7 +336,7 @@ grammar =
# A reference to a property on *this*.
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.
@ -400,7 +400,7 @@ grammar =
# A catch clause names its error and runs a block of code.
Catch: [
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.
@ -429,14 +429,14 @@ grammar =
# or postfix, with a single expression. There is no do..while.
While: [
o 'WhileSource Block', -> $1.addBody $2
o 'Statement WhileSource', -> $2.addBody LOCDATA(1) Block.wrap([$1])
o 'Expression WhileSource', -> $2.addBody LOCDATA(1) Block.wrap([$1])
o 'Statement WhileSource', -> $2.addBody LOC(1) Block.wrap([$1])
o 'Expression WhileSource', -> $2.addBody LOC(1) Block.wrap([$1])
o 'Loop', -> $1
]
Loop: [
o 'LOOP Block', -> new While(LOCDATA(1) new Literal 'true').addBody $2
o 'LOOP Expression', -> new While(LOCDATA(1) new Literal 'true').addBody LOCDATA(2) Block.wrap [$2]
o 'LOOP Block', -> new While(LOC(1) new Literal 'true').addBody $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.
@ -449,7 +449,7 @@ grammar =
]
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
]
@ -519,8 +519,8 @@ grammar =
If: [
o 'IfBlock'
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 'Expression 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, LOC(1)(Block.wrap [$1]), type: $2, statement: true
]
# 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
[input, id, colon] = match
# Preserve lenght of id for location data
# Preserve length of id for location data
idLength = id.length
poppedToken = undefined