Expand description
A data structure for tracking source positions in language implementations, inspired by the CodeMap type in rustc’s libsyntax (dead link).
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 codemap2::{CodeMap, FileData, DefaultFileData};
let mut codemap = CodeMap::new();
let file = codemap.add_file(DefaultFileData::new("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);
Re-exports
pub use super::*;
Structs
- A wrapper around a
Box<str>
that meets the requirements forFileData::Source
andFileData::Name
. This type is used inDefaultFileData
because - A data structure recording source code files for position lookup.
- A default implementation of
FileData
that contains - A
CodeMap
’s record of a source file. - A line and column.
- A file, and a line and column within it.
- A small,
Copy
, value representing a position in aCodeMap
’s file. - A range of text within a CodeMap.
- A file, and a line and column range within it.
- Associate a Span with a value of arbitrary type (e.g. an AST node).
Traits
- A trait that represents file data