Nasty.AST.Node (Nasty v0.3.0)

View Source

Base types and utilities for AST nodes.

All AST nodes include position information for error reporting and editor integration.

Summary

Types

Byte offset in source text (0-indexed).

Language identifier (ISO 639-1 codes).

Line and column position in source text (1-indexed).

Span representing a range in source text.

Functions

Extracts text slice from source using span byte offsets.

Creates a span from NimbleParsec position tracking.

Merges two spans into a single span covering both ranges.

Types

byte_offset()

@type byte_offset() :: non_neg_integer()

Byte offset in source text (0-indexed).

language()

@type language() :: atom()

Language identifier (ISO 639-1 codes).

Examples: :en (English), :es (Spanish), :ca (Catalan)

position()

@type position() :: {line :: pos_integer(), column :: pos_integer()}

Line and column position in source text (1-indexed).

span()

@type span() :: %{
  start_pos: position(),
  end_pos: position(),
  start_offset: byte_offset(),
  end_offset: byte_offset()
}

Span representing a range in source text.

Includes both line/column positions (for editors) and byte offsets (for efficient slicing).

Functions

extract_text(source, map)

@spec extract_text(String.t(), span()) :: String.t()

Extracts text slice from source using span byte offsets.

Examples

iex> span = Nasty.AST.Node.make_span({1, 0}, 0, {1, 5}, 5)
iex> Nasty.AST.Node.extract_text("Hello world", span)
"Hello"

make_span(start_pos, start_offset, end_pos, end_offset)

@spec make_span(position(), byte_offset(), position(), byte_offset()) :: span()

Creates a span from NimbleParsec position tracking.

NimbleParsec provides byte offsets and line/column tuples.

Examples

iex> Nasty.AST.Node.make_span({1, 0}, 0, {1, 5}, 5)
%{
  start_pos: {1, 0},
  end_pos: {1, 5},
  start_offset: 0,
  end_offset: 5
}

merge_spans(span1, span2)

@spec merge_spans(span(), span()) :: span()

Merges two spans into a single span covering both ranges.

Examples

iex> span1 = Nasty.AST.Node.make_span({1, 0}, 0, {1, 5}, 5)
iex> span2 = Nasty.AST.Node.make_span({1, 6}, 6, {1, 11}, 11)
iex> Nasty.AST.Node.merge_spans(span1, span2)
%{
  start_pos: {1, 0},
  end_pos: {1, 11},
  start_offset: 0,
  end_offset: 11
}