Add location data to tokens generated by the rewriter.

This commit is contained in:
Jason Walton 2012-11-19 11:34:09 -05:00
parent bb94e02fad
commit 12625cc00c
2 changed files with 47 additions and 1 deletions

View File

@ -18,6 +18,7 @@
this.tagPostfixConditionals();
this.addImplicitBraces();
this.addImplicitParentheses();
this.addLocationDataToGeneratedTokens();
return this.tokens;
};
@ -221,6 +222,31 @@
});
};
Rewriter.prototype.addLocationDataToGeneratedTokens = function() {
return this.scanTokens(function(token, i, tokens) {
var prevToken;
if (token.generated && !token.locationData) {
if (i > 0) {
prevToken = tokens[i - 1];
token.locationData = {
first_line: prevToken.locationData.last_line,
first_column: prevToken.locationData.last_column,
last_line: prevToken.locationData.last_line,
last_column: prevToken.locationData.last_column
};
} else {
token.locationData = {
first_line: 0,
first_column: 0,
last_line: 0,
last_column: 0
};
}
}
return 1;
});
};
Rewriter.prototype.addImplicitIndentation = function() {
var action, condition, indent, outdent, starter;
starter = indent = outdent = null;

View File

@ -26,6 +26,7 @@ class exports.Rewriter
@tagPostfixConditionals()
@addImplicitBraces()
@addImplicitParentheses()
@addLocationDataToGeneratedTokens()
@tokens
# Rewrite the token stream, looking one token ahead and behind.
@ -113,7 +114,7 @@ class exports.Rewriter
[tag] = token
sameLine = no if tag in LINEBREAKS
return (
(tag in ['TERMINATOR', 'OUTDENT'] or
(tag in ['TERMINATOR', 'OUTDENT'] or
(tag in IMPLICIT_END and sameLine and not (i - startIndex is 1))) and
((!startsLine and @tag(i - 1) isnt ',') or
not (two?[0] is ':' or one?[0] is '@' and three?[0] is ':'))) or
@ -190,6 +191,25 @@ class exports.Rewriter
prev[0] = 'FUNC_EXIST' if prev[0] is '?'
2
# Add location data to all tokens generated by the rewriter.
addLocationDataToGeneratedTokens: ->
@scanTokens (token, i, tokens) ->
if token.generated and not token.locationData
if i > 0
prevToken = tokens[i-1]
token.locationData =
first_line: prevToken.locationData.last_line
first_column: prevToken.locationData.last_column
last_line: prevToken.locationData.last_line
last_column: prevToken.locationData.last_column
else
token.locationData =
first_line: 0
first_column: 0
last_line: 0
last_column: 0
return 1
# Because our grammar is LALR(1), it can't handle some single-line
# expressions that lack ending delimiters. The **Rewriter** adds the implicit
# blocks, so it doesn't need to. ')' can close a single-line block,