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 drop_word(s: String) -> String
pub fn dummy_span() -> ast.Span

Placeholder span for tests or unknown positions.

pub fn first_word(s: String) -> String

Leading identifier chars [a-zA-Z0-9_-] of a string.

pub fn gs_len(gs: List(String)) -> Int

Total UTF-16 length of a list of graphemes.

pub fn is_alnum(g: String) -> Bool
pub fn is_digit(g: String) -> Bool
pub fn is_ident(g: String) -> Bool
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 line_span(line: ast.Line) -> ast.Span

Span of a line’s trimmed content.

pub fn merge_span(a: ast.Span, b: ast.Span) -> ast.Span

Merge two spans into one covering both.

pub fn nth(items: List(a), n: Int) -> Result(a, Nil)

The n-th element of a list.

pub fn prefix_len(s: String, n: Int) -> String

The first n UTF-16 units of a string.

pub fn span_to_string(span: ast.Span) -> String

“line L col C”

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.

pub fn ws_tokens(arg: String) -> List(#(String, Int))

Whitespace-separated tokens of arg, each with its UTF-16 offset within arg (which must be trim_start’ed — the first token starts at 0).

✨ Search Document