Obtaining position data (Line, Column) while parsing #369
-
Hi, I am building an interpreter and would like to store where my expressions/statements were parsed from on the source code to my AST nodes. Now internally, the parser input must implement the Question is how can I hook into that to obtain line and column data within a rule? Currently only the Does a more advanced |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The built-in conversion from offset to line/col for error reporting is O(n), so you wouldn't want to use it for every AST node. Usually you'd store the byte offsets or similar in the AST and convert to line and column only when reporting errors or for use in protocols like LSP. A data structure like codemap may be useful for this, which also includes line number mapping using binary search into an array of line start offsets. |
Beta Was this translation helpful? Give feedback.
The built-in conversion from offset to line/col for error reporting is O(n), so you wouldn't want to use it for every AST node. Usually you'd store the byte offsets or similar in the AST and convert to line and column only when reporting errors or for use in protocols like LSP. A data structure like codemap may be useful for this, which also includes line number mapping using binary search into an array of line start offsets.