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

@@ -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