mtparser/helper
Shared construction and manipulation functions for the AST types: spans, lines, interpolated text, expressions, and the text/token utilities the statement parsers build on.
Values
pub fn all_but_last(items: List(a)) -> List(a)
pub fn arg_offset(
span: ast.Span,
full: String,
arg: String,
) -> Int
Absolute offset of arg (a trim_start’ed suffix of full whose
span is span).
pub fn comma_segments(arg: String) -> List(#(String, Int))
Comma-separated segments of arg, each trimmed (with its UTF-16 offset
within arg); empty segments are kept.
pub fn drop_token(s: String) -> String
pub fn first_word(s: String) -> String
Leading identifier chars [a-zA-Z0-9_-] of a string.
pub fn is_literal(text: List(ast.TextPart)) -> Bool
True if the text contains no variable references.
pub fn leading_ws(line: ast.Line) -> Int
pub fn split_lines(source: String) -> List(ast.Line)
Split source into lines.
\n separates lines; a trailing \r (CRLF files) stays in the text and
is handled as trailing whitespace by consumers. The empty region after a
final \n produces no line.
pub fn sub_span(
base: ast.Span,
body: String,
start: Int,
len: Int,
) -> ast.Span
Span of a range inside body, which starts at base.offset (with
base.line/base.column pointing at that position). Counts newlines so
the line/column are real even when the command spans lines.
pub fn take_till_any(
gs: List(String),
stops: List(String),
) -> List(String)
pub fn take_till_list(
gs: List(String),
stop: String,
) -> Result(#(List(String), List(String)), Nil)
Graphemes before the first stop; Error when stop never occurs.
pub fn take_token(s: String) -> String
The first whitespace-delimited token of a string.
pub fn take_while_list(
gs: List(String),
pred: fn(String) -> Bool,
) -> #(List(String), List(String))
pub fn to_raw_string(text: List(ast.TextPart)) -> String
Raw string form: variable references rendered with their $ prefix.
pub fn trim_start_list(gs: List(String)) -> #(List(String), Int)
pub fn utf16_size(s: String) -> Int
Length of a string in UTF-16 code units — the unit JS string indexing
(String.prototype.length, substring) and editors (VS Code/LSP) use
for offsets. Supplementary-plane codepoints count as 2. All span offsets
and lengths in this library are in these units.
pub fn variable_names(text: List(ast.TextPart)) -> List(String)
All variable names referenced in this text, in order.