Skip to content
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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

yav
Copy link
Collaborator

@yav yav commented Feb 12, 2025

This is the first step of solving #823.

We introduce a new module Cerb_location.position, which defines a type replacing Lexing.position as the base type to use for positions. Values of the new position type keep the Lexing.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.

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.
@yav
Copy link
Collaborator Author

yav commented Feb 12, 2025

@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
to use to inject code in the pre-processed file.

yav added 4 commits February 12, 2025 15:39
* 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.
Copy link
Collaborator

@kmemarian kmemarian left a 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 *)
Copy link
Collaborator

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))
Copy link
Collaborator

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.

Copy link
Collaborator

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants