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§
- BoxStr
- A wrapper around a
Box<str>that meets the requirements forFileData::SourceandFileData::Name. This type is used inDefaultFileDatabecause - CodeMap
- A data structure recording source code files for position lookup.
- Default
File Data - A default implementation of
FileDatathat contains - File
- A
CodeMap’s record of a source file. - LineCol
- A line and column.
- Loc
- A file, and a line and column within it.
- Pos
- A small,
Copy, value representing a position in aCodeMap’s file. - 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).
Traits§
- File
Data - A trait that represents file data