Module starlark::codemap [−][src]
A data structure for tracking source positions in language implementations
The CodeMap
tracks all source files and maps positions within them to linear indexes as if all
source files were concatenated. This allows a source position to be represented by a small
32-bit Pos
indexing into the CodeMap
, under the assumption that the total amount of parsed
source code will not exceed 4GiB. The CodeMap
can look up the source file, line, and column
of a Pos
or Span
, as well as provide source code snippets for error reporting.
Example
use starlark::codemap::CodeMap; let mut codemap = CodeMap::new("test.rs".to_owned(), "fn test(){\n println!(\"Hello\");\n}\n".to_owned()); let file = codemap.get_file(); let string_literal_span = file.span.subspan(24, 31); let location = codemap.look_up_span(string_literal_span); assert_eq!(location.file.name(), "test.rs"); assert_eq!(location.begin.line, 1); assert_eq!(location.begin.column, 13); assert_eq!(location.end.line, 1); assert_eq!(location.end.column, 20);
Structs
CodeMap | A data structure recording a source code file for position lookup. |
File | A |
LineCol | A line and column. |
Loc | A file, and a line and column within it. |
Pos | A small, |
Span | A range of text within a CodeMap. |
SpanLoc | A file, and a line and column range within it. |
Spanned | Associate a Span with a value of arbitrary type (e.g. an AST node). |