Skip to main content

Crate daml_syntax

Crate daml_syntax 

Source
Expand description

Shared parsed-source surface for Daml tools.

daml-parser stays the low-level lexer/layout/parser implementation. This crate owns the source-facing facts tools need around that parser: diagnostics, line/UTF-16 mapping, tokens, trivia, laid-out tokens, and conversion from parser byte spans to text-size ranges.

§Public dependencies

This crate intentionally exposes types from daml-parser and the text-size crate in its public API. Downstream SemVer expectations include compatible major versions of those dependencies when their types appear in function signatures or re-exports:

Coordinate newtypes such as LineNumber, ByteColumn, and CharColumn are 1-based and reject zero via LineNumber::try_new or TryFrom<usize>; 0-based offsets use ByteOffset and Utf16Offset. Use usize::from(coordinate) for explicit raw extraction. UTF-16 column lookup returns CoordinateRangeError for line or column coordinates outside a source, and UTF-16 ranges use Utf16Range so JavaScript string slices cannot be mistaken for byte ranges.

use daml_syntax::{parser_span_to_text_range, SourceFile};

let source = "module M where\nfoo : Int\nfoo = 1\n";
let file = SourceFile::parse(source);

assert_eq!(file.module().name, "M");
assert!(file.diagnostics().is_empty());
assert!(!file.tokens().is_empty());
assert!(!file.laid_out_tokens().is_empty());

let header_range = parser_span_to_text_range(source, file.module().header);
assert_eq!(usize::from(header_range.start()), 0);
assert_eq!(header_range, file.parser_span_to_text_range(file.module().header));

Fallible coordinate and span conversion reject invalid inputs instead of clamping or panicking:

use daml_parser::ast::Span;
use daml_syntax::{ByteColumn, LineIndex, LineNumber, SourceFile};

let source = "module M where\n";
let file = SourceFile::parse(source);
assert!(
    file.try_parser_span_to_text_range(Span::from_usize(999, 1000))
        .is_err()
);

let index = LineIndex::new(source);
assert!(index.utf16_col(LineNumber::new(99), ByteColumn::new(1)).is_err());
assert!(LineNumber::try_new(0).is_none());

Re-exports§

pub use coordinate::ByteColumn;
pub use coordinate::ByteLineCol;
pub use coordinate::ByteOffset;
pub use coordinate::CharColumn;
pub use coordinate::CharLineCol;
pub use coordinate::InvalidOneBasedCoordinate;
pub use coordinate::LineNumber;
pub use coordinate::Utf16Offset;
pub use coordinate::Utf16Range;

Modules§

coordinate
Domain-specific source coordinates.

Structs§

CoordinateRangeError
Error returned when a source coordinate is outside a LineIndex.
Diagnostic
A parser or lexer diagnostic anchored in source text.
LineIndex
Precomputed line, character, and UTF-16 offset tables for a source string.
ParserSpanToTextRangeError
Error returned when a parser span cannot be converted to a TextRange.
SourceFile
Parsed Daml module with diagnostics, line index, and lazy token access.
SourceTokens
Lexer output for a source string without running the full module parser.
TextRange
A range in text, represented as a pair of TextSize.
TextSize
A measure of text length. Also, equivalently, an index into text.

Enums§

CoordinateRangeErrorKind
Failure kind when resolving a line and byte column in a LineIndex.
DiagnosticEndColumn
End-column shape for a diagnostic span.
ParserSpanToTextRangeErrorKind
Failure kind when converting a parser span to a TextRange.

Functions§

parser_span_to_text_range
Convert a parser span into a text-size byte range for an arbitrary source string.
try_parser_span_to_text_range
Try to convert a parser span into a text-size byte range.