[−][src]Crate codemap
A data structure for tracking source positions in language implementations, inspired by the CodeMap type in rustc's libsyntax.
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 codemap::CodeMap; let mut codemap = CodeMap::new(); let file = codemap.add_file("test.rs".to_string(), "fn test(){\n println!(\"Hello\");\n}\n".to_string()); 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 source code files 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). |