Skip to content

Commit 38c8b2f

Browse files
helixbassGeoffreyBooth
authored andcommitted
Root AST (#5137)
* root ast * updated grammar * preserve CoffeeScript.nodes() API * root ast methods * updates from code review * Style * Fix a few missing returns * Expand sourceType explanation * Simplify * Refactor Block.astProperties: use expression.astLocationData() to get location data, rather than extracting it from the whole AST object; move all the logic into one function, rather than spreading it out across several functions on the Block class that all appear to be internal * testing root location data * Fix location end data for root/File » Program AST node
1 parent 4392d26 commit 38c8b2f

12 files changed

+363
-92
lines changed

lib/coffeescript/coffeescript.js

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/grammar.js

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/lexer.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/nodes.js

Lines changed: 88 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/parser.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/coffeescript.coffee

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,18 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
9696

9797
nodes = parser.parse tokens
9898
# If all that was requested was a POJO representation of the nodes, e.g.
99-
# the abstract syntax tree (AST), we can stop now and just return that.
99+
# the abstract syntax tree (AST), we can stop now and just return that
100+
# (after fixing the location data for the root/`File`»`Program` node,
101+
# which might’ve gotten misaligned from the original source due to the
102+
# `clean` function in the lexer).
100103
if options.ast
101-
return nodes.ast options
104+
sourceCodeNumberOfLines = (code.match(/\r?\n/g) or '').length + 1
105+
sourceCodeLastLine = /.*$/.exec(code)[0] # `.*` matches all but line break characters.
106+
ast = nodes.ast options
107+
ast.end = ast.range[1] = ast.program.end = ast.program.range[1] = code.length
108+
ast.loc.end.line = ast.program.loc.end.line = sourceCodeNumberOfLines
109+
ast.loc.end.column = ast.program.loc.end.column = sourceCodeLastLine.length
110+
return ast
102111

103112
fragments = nodes.compileToFragments options
104113

@@ -181,10 +190,8 @@ exports.tokens = withPrettyErrors (code, options) ->
181190
# return the AST. You can then compile it by calling `.compile()` on the root,
182191
# or traverse it by using `.traverseChildren()` with a callback.
183192
exports.nodes = withPrettyErrors (source, options) ->
184-
if typeof source is 'string'
185-
parser.parse lexer.tokenize source, options
186-
else
187-
parser.parse source
193+
source = lexer.tokenize source, options if typeof source is 'string'
194+
parser.parse(source).body
188195

189196
# This file used to export these methods; leave stubs that throw warnings
190197
# instead. These methods have been moved into `index.coffee` to provide

src/grammar.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ grammar =
7373
# The **Root** is the top-level node in the syntax tree. Since we parse bottom-up,
7474
# all parsing must end here.
7575
Root: [
76-
o '', -> new Block
77-
o 'Body'
76+
o '', -> new Root new Block
77+
o 'Body', -> new Root $1
7878
]
7979

8080
# Any list of statements and expressions, separated by line breaks or semicolons.

src/lexer.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ exports.Lexer = class Lexer
973973
lastCharacter = if length > 0 then (length - 1) else 0
974974
[locationData.last_line, locationData.last_column, endOffset] =
975975
@getLineAndColumnFromChunk offsetInChunk + lastCharacter
976-
locationData.range[1] = endOffset + 1
976+
locationData.range[1] = if length > 0 then endOffset + 1 else endOffset
977977

978978
locationData
979979

0 commit comments

Comments
 (0)