Track ending lines/columns; fix end locations in Mozilla AST.

This commit is contained in:
Ingvar Stepanyan 2014-08-08 14:15:43 +03:00 committed by Richard van Velzen
parent 6b23cbc852
commit ae5366a31d
3 changed files with 28 additions and 30 deletions

View File

@ -84,7 +84,7 @@ function DEFNODE(type, props, methods, base) {
return ctor; 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); }, null);
var AST_Node = DEFNODE("Node", "start end", { var AST_Node = DEFNODE("Node", "start end", {

View File

@ -370,25 +370,27 @@
/* -----[ tools ]----- */ /* -----[ tools ]----- */
function my_start_token(moznode) { function my_start_token(moznode) {
var loc = moznode.loc; var loc = moznode.loc, start = loc && loc.start;
var range = moznode.range; var range = moznode.range;
return new AST_Token({ return new AST_Token({
file : loc && loc.source, file : loc && loc.source,
line : loc && loc.start.line, line : start && start.line,
col : loc && loc.start.column, col : start && start.column,
pos : range ? range[0] : moznode.start, pos : range ? range[0] : moznode.start,
endpos : range ? range[0] : moznode.start endpos : range ? range[0] : moznode.start
}); });
}; };
function my_end_token(moznode) { function my_end_token(moznode) {
var loc = moznode.loc; var loc = moznode.loc, end = loc && loc.end;
var range = moznode.range; var range = moznode.range;
return new AST_Token({ return new AST_Token({
file : loc && loc.source, file : loc && loc.source,
line : loc && loc.end.line, line : end && end.line,
col : loc && loc.end.column, col : end && end.column,
pos : range ? range[1] : moznode.end, pos : range ? range[1] : moznode.end,
endline : end && end.line,
endcol : end && end.column,
endpos : range ? range[1] : moznode.end endpos : range ? range[1] : moznode.end
}); });
}; };
@ -465,23 +467,16 @@
return ast; return ast;
}; };
function moz_sub_loc(token) { function set_moz_loc(mynode, moznode, myparent) {
return token.line ? {
line: token.line,
column: token.col
} : null;
};
function set_moz_loc(mynode, moznode) {
var start = mynode.start; var start = mynode.start;
var end = mynode.end; var end = mynode.end;
if (start.pos != null && end.pos != null) { if (start.pos != null && end.endpos != null) {
moznode.range = [start.pos, end.pos]; moznode.range = [start.pos, end.endpos];
} }
if (start.line) { if (start.line) {
moznode.loc = { moznode.loc = {
start: moz_sub_loc(start), start: {line: start.line, column: start.col},
end: moz_sub_loc(end) end: end.endline ? {line: end.endline, column: end.endcol} : null
}; };
if (start.file) { if (start.file) {
moznode.loc.source = start.file; moznode.loc.source = start.file;

View File

@ -272,6 +272,8 @@ function tokenizer($TEXT, filename, html5_comments) {
line : S.tokline, line : S.tokline,
col : S.tokcol, col : S.tokcol,
pos : S.tokpos, pos : S.tokpos,
endline : S.line,
endcol : S.col,
endpos : S.pos, endpos : S.pos,
nlb : S.newline_before, nlb : S.newline_before,
file : filename file : filename
@ -397,6 +399,7 @@ function tokenizer($TEXT, filename, html5_comments) {
ret = S.text.substring(S.pos, i); ret = S.text.substring(S.pos, i);
S.pos = i; S.pos = i;
} }
S.col = S.tokcol + (S.pos - S.tokpos);
S.comments_before.push(token(type, ret, true)); S.comments_before.push(token(type, ret, true));
S.regex_allowed = regex_allowed; S.regex_allowed = regex_allowed;
return next_token(); return next_token();