aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIngvar Stepanyan <me@rreverser.com>2014-08-08 14:15:43 +0300
committerRichard van Velzen <rvanvelzen@experty.com>2015-01-06 11:32:41 +0100
commitae5366a31de7d65964400ffc7b2f05132e2538b6 (patch)
tree73f51541f3bbc2d0ef3bce84771c10c1d26e0c5d
parent6b23cbc8522bdc6d28e1abb44eb2d1d6eb6b697a (diff)
downloadtracifyjs-ae5366a31de7d65964400ffc7b2f05132e2538b6.tar.gz
tracifyjs-ae5366a31de7d65964400ffc7b2f05132e2538b6.zip
Track ending lines/columns; fix end locations in Mozilla AST.
-rw-r--r--lib/ast.js2
-rw-r--r--lib/mozilla-ast.js37
-rw-r--r--lib/parse.js19
3 files changed, 28 insertions, 30 deletions
diff --git a/lib/ast.js b/lib/ast.js
index 2eb8cc86..5aa1be30 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -84,7 +84,7 @@ function DEFNODE(type, props, methods, base) {
return ctor;
};
-var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before file", {
+var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos nlb comments_before file", {
}, null);
var AST_Node = DEFNODE("Node", "start end", {
diff --git a/lib/mozilla-ast.js b/lib/mozilla-ast.js
index 602ef0e6..5056ffed 100644
--- a/lib/mozilla-ast.js
+++ b/lib/mozilla-ast.js
@@ -370,26 +370,28 @@
/* -----[ tools ]----- */
function my_start_token(moznode) {
- var loc = moznode.loc;
+ var loc = moznode.loc, start = loc && loc.start;
var range = moznode.range;
return new AST_Token({
file : loc && loc.source,
- line : loc && loc.start.line,
- col : loc && loc.start.column,
+ line : start && start.line,
+ col : start && start.column,
pos : range ? range[0] : moznode.start,
endpos : range ? range[0] : moznode.start
});
};
function my_end_token(moznode) {
- var loc = moznode.loc;
+ var loc = moznode.loc, end = loc && loc.end;
var range = moznode.range;
return new AST_Token({
- file : loc && loc.source,
- line : loc && loc.end.line,
- col : loc && loc.end.column,
- pos : range ? range[1] : moznode.end,
- endpos : range ? range[1] : moznode.end
+ file : loc && loc.source,
+ line : end && end.line,
+ col : end && end.column,
+ pos : range ? range[1] : moznode.end,
+ endline : end && end.line,
+ endcol : end && end.column,
+ endpos : range ? range[1] : moznode.end
});
};
@@ -465,23 +467,16 @@
return ast;
};
- function moz_sub_loc(token) {
- return token.line ? {
- line: token.line,
- column: token.col
- } : null;
- };
-
- function set_moz_loc(mynode, moznode) {
+ function set_moz_loc(mynode, moznode, myparent) {
var start = mynode.start;
var end = mynode.end;
- if (start.pos != null && end.pos != null) {
- moznode.range = [start.pos, end.pos];
+ if (start.pos != null && end.endpos != null) {
+ moznode.range = [start.pos, end.endpos];
}
if (start.line) {
moznode.loc = {
- start: moz_sub_loc(start),
- end: moz_sub_loc(end)
+ start: {line: start.line, column: start.col},
+ end: end.endline ? {line: end.endline, column: end.endcol} : null
};
if (start.file) {
moznode.loc.source = start.file;
diff --git a/lib/parse.js b/lib/parse.js
index c3f0822c..f463526f 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -267,14 +267,16 @@ function tokenizer($TEXT, filename, html5_comments) {
(type == "punc" && PUNC_BEFORE_EXPRESSION(value)));
prev_was_dot = (type == "punc" && value == ".");
var ret = {
- type : type,
- value : value,
- line : S.tokline,
- col : S.tokcol,
- pos : S.tokpos,
- endpos : S.pos,
- nlb : S.newline_before,
- file : filename
+ type : type,
+ value : value,
+ line : S.tokline,
+ col : S.tokcol,
+ pos : S.tokpos,
+ endline : S.line,
+ endcol : S.col,
+ endpos : S.pos,
+ nlb : S.newline_before,
+ file : filename
};
if (!is_comment) {
ret.comments_before = S.comments_before;
@@ -397,6 +399,7 @@ function tokenizer($TEXT, filename, html5_comments) {
ret = S.text.substring(S.pos, i);
S.pos = i;
}
+ S.col = S.tokcol + (S.pos - S.tokpos);
S.comments_before.push(token(type, ret, true));
S.regex_allowed = regex_allowed;
return next_token();