-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track location in pre-processed file, as well references to source. #866
base: master
Are you sure you want to change the base?
Conversation
We do this by introducing a new module `Cerb_location.position`, which replaces `Lexing.position` as the base type to use for positions. Values of the new position type keepl the `Lexing.position` corresponding to the actual location in the preprocessed file, as well as the file name and line in the original source assocaited with the location.
@kmemarian @cp526 this PR contains the changes to the lexer and parser to keep track of both the position in the pre-processed file, and also the position in the original source. Please have a look and let me know what you think. At the moment the position in the actual file is not really used, but the next step would be to modify the injection code |
* Add a way to ask for cartesian location in pre-processed or user visible file * Add a way to save the pre-processed file, if needed * Add code paths to optionally inject using either cartesian coordinates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, all looks fine apart from the off-by-one difference in c_parser_driver.ml
(see inline comment).
Regarding the new global state in the lexer, it would indeed be better without it but I also don't see how we can do better for now.
let file pos = pos.source_file | ||
|
||
|
||
(* Column for this position, 0 based *) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should say 1 based
("char", `Int (p.pos_cnum - p.pos_bol)) | ||
[ ("file", `String (Cerb_position.file pos)); | ||
("line", `Int (Cerb_position.line pos)); | ||
("char", `Int (Cerb_position.column pos)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably not an issue, but note that since Cerb_position.column
is 1-based (which I think is reasonable) this diff is observable.
util/cerb_position.mli
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this all look good, modulo a few typos in the comments.
@@ -63,8 +64,9 @@ let diagnostic_get_tokens ~inside_cn loc string = | |||
let Lexing.{ pos_lnum; pos_bol; pos_cnum; _ } = lexbuf.lex_start_p in | |||
let (line, col) = | |||
(* the first line needs to have columns shifted by /*@ but the rest do not *) | |||
let col_off = if pos_lnum > 1 then 1 else start_pos.pos_cnum - start_pos.pos_bol + 1 in | |||
(pos_lnum + start_pos.pos_lnum, col_off + pos_cnum - pos_bol) in | |||
let col_off = if pos_lnum > 1 then 1 else Cerb_position.column start_pos + 1 in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the + 1
right? This makes it different from the previous version.
This is the first step of solving #823.
We introduce a new module
Cerb_location.position
, which defines a type replacingLexing.position
as the base type to use for positions. Values of the new position type keep theLexing.position
corresponding to the actual location in the preprocessed file, as well as the file name and line in the original source associated with the location.