talcparsec

parser
parsec
combinator
pratt
moon add kokic/talcparsec@0.1.1
Download zip
Author
Version
0.1.1
License
AGPL-3.0
Last updated
25 days ago
Downloads
7K
README

#
CParser

type CParser = Parser[Input, Char]

#
SParser

type SParser = Parser[Input, String]

#
StringParser

type StringParser[T] = Parser[Input, T]

Convenience alias for Parser[Input, T], used for string-input parsers.

#
Cursor

pub(open) trait Cursor {
fn cursor(Self) -> Int
fn position(Self) -> Position
fn is_at_eof(Self) -> Bool
fn same_cursor(Self, other : Self) -> Bool = _
}

Operations shared by parser input cursors. cursor must increase whenever an input element is consumed.

#
Input

pub(all) struct Input {
source : String
offset : Int
line : Int
column : Int
} derive(Eq,
Debug
)

Immutable character input cursor. Offsets are UTF-16 code unit offsets, matching MoonBit string indexing.
impl Cursor for Input

#
Input::advance

fn Input::advance(self : Input, ch : Char) -> Input

#
Input::advance_string

fn Input::advance_string(self : Input, text : String) -> Input

#
Input::is_eof

fn Input::is_eof(self : Input) -> Bool

#
Input::new

fn Input::new(source : String) -> Input

#
Input::next

fn Input::next(self : Input) -> (Char, Input)?

#
Input::peek

fn Input::peek(self : Input) -> Char?

#
Input::remaining

fn Input::remaining(self : Input) -> StringView

#
ParseError

pub(all) struct ParseError {
offset : Int
line : Int
column : Int
expected : Array[String]
message : String
committed : Bool
} derive(Eq,
Debug
)

Parse failure metadata. committed prevents alternatives from backtracking across input that has already been consumed.

#
ParseError::column

fn ParseError::column(self : ParseError) -> Int

#
ParseError::expected

fn ParseError::expected(self : ParseError) -> Array[String]

#
ParseError::is_committed

fn ParseError::is_committed(self : ParseError) -> Bool

#
ParseError::line

fn ParseError::line(self : ParseError) -> Int

#
ParseError::merge

fn ParseError::merge(self : ParseError, other : ParseError) -> ParseError

#
ParseError::message

fn[I : Cursor] ParseError::message(input : I, message : String) -> ParseError

#
ParseError::message_text

fn ParseError::message_text(self : ParseError) -> String

#
ParseError::new

fn[I : Cursor] ParseError::new(input : I, expected : String) -> ParseError

#
ParseError::offset

fn ParseError::offset(self : ParseError) -> Int

#
ParseError::reject_if_committed

fn[I, T] ParseError::reject_if_committed(self : ParseError, value : T, rest : I) -> Result[(T, I), ParseError]

#
ParseError::to_string

fn ParseError::to_string(self : ParseError) -> String

#
ParseError::with_commit

fn ParseError::with_commit(self : ParseError) -> ParseError

#
ParseError::without_commit

fn ParseError::without_commit(self : ParseError) -> ParseError

#
Parser

pub(all) struct Parser[I, T] {
parse_fn : (I) -> Result[(T, I), ParseError]
}

#
Parser::after

fn[I : Cursor, T, L] Parser::after(self : Parser[I, T], left : Parser[I, L]) -> Parser[I, T]

Runs left then self, returning self's result.

Parameters:

  • self : The parser whose result is returned.
  • left : The parser whose result is discarded.

Equivalent to left *> self.

#
Parser::attempt

fn[I, T] Parser::attempt(self : Parser[I, T]) -> Parser[I, T]

Strips the committed flag from any error.

Parameters:

  • self : The parser to wrap.

Enables backtracking across input consumed by self. Use sparingly — prefer grammars where alternatives are distinguishable without backtracking.

#
Parser::before

fn[I : Cursor, T, R] Parser::before(self : Parser[I, T], right : Parser[I, R]) -> Parser[I, T]

Runs self then right, returning self's result.

Parameters:

  • self : The parser whose result is returned.
  • right : The parser whose result is discarded.

Equivalent to self <* right.

#
Parser::between

fn[I : Cursor, T, L, R] Parser::between(self : Parser[I, T], left : Parser[I, L], right : Parser[I, R]) -> Parser[I, T]

Runs left, then self, then right, returning self's result.

Parameters:

  • self : The middle parser whose result is returned.
  • left : The opening parser; its result is discarded.
  • right : The closing parser; its result is discarded.

Equivalent to left.then(self).skip(right).

#
Parser::bind

fn[I : Cursor, T, U] Parser::bind(self : Parser[I, T], next : (T) -> Parser[I, U]) -> Parser[I, U]

Monadic bind. Runs self, passes the result to next.

Parameters:

  • self : The first parser to run.
  • next : A function that takes the result of self and returns the next parser to run.

If self consumed input, any error from next is marked committed, preventing backtracking across consumed input.

Returns a parser that runs self, then feeds its result to next.

#
Parser::label

fn[I : Cursor, T] Parser::label(self : Parser[I, T], expected : String) -> Parser[I, T]

Replaces the expected label in the error if self fails without having advanced the cursor.

Parameters:

  • self : The parser to label.
  • expected : The label to report on failure.

If input was consumed before failure, the original error is preserved (furthest position wins).

#
Parser::lexeme

fn[T] Parser::lexeme(self : Parser[Input, T]) -> Parser[Input, T]

Wraps self as a lexeme by skipping trailing whitespace.

Parameters:

  • self : The parser to wrap.

Only valid for Input-based parsers.

Returns a parser that runs self then skips whitespace.

#
Parser::many

fn[I : Cursor, T] Parser::many(self : Parser[I, T]) -> Parser[I, Array[T]]

Zero or more repetitions of self.

Parameters:

  • self : The parser to repeat.

Stops on non-committed failure. Rejects parsers that accept empty input (infinite loop guard).

Returns an array of accumulated results.

#
Parser::many_until

fn[I : Cursor, T, U] Parser::many_until(self : Parser[I, T], terminator : Parser[I, U]) -> Parser[I, Array[T]]

Keeps running self until terminator succeeds.

Parameters:

  • self : The body parser to repeat.
  • terminator : The terminator parser. It is consumed but its result is not included in the output.

If self fails with a non-committed error, accumulated values are returned. Committed errors from self propagate immediately.

Returns an array of results from self.

#
Parser::map

fn[I : Cursor, T, U] Parser::map(self : Parser[I, T], f : (T) -> U) -> Parser[I, U]

Transforms the result of a successful parse using f.

Parameters:

  • self : The parser whose result to transform.
  • f : The transformation function.

Returns a parser that parses the same input as self and applies f to the result on success.

#
Parser::new

fn[I, T] Parser::new(parse_fn : (I) -> Result[(T, I), ParseError]) -> Parser[I, T]

#
Parser::not_followed_by

fn[I : Cursor, T] Parser::not_followed_by(self : Parser[I, T], label : String) -> Parser[I, Unit]

Negative lookahead. Succeeds with () (consuming no input) when self fails.

Parameters:

  • self : The parser to check for absence.
  • label : The label for error messages when self succeeds.

When self succeeds, fails with an uncommitted error "unexpected {label}".

#
Parser::optional

fn[I, T] Parser::optional(self : Parser[I, T]) -> Parser[I, T?]

Runs self and returns Some(result) on success.

Parameters:

  • self : The parser to try.

Returns None on non-committed failure. Committed failures propagate immediately.

#
Parser::or

fn[I, T] Parser::or(self : Parser[I, T], other : Parser[I, T]) -> Parser[I, T]

Choice with commit-aware backtracking.

Parameters:

  • self : The first alternative.
  • other : The second alternative, tried if self fails without committing.

If self fails with committed error, other is NOT tried. Errors are merged by furthest position.

Returns the result of the first successful alternative.

#
Parser::parse

fn[I : Cursor, T] Parser::parse(self : Parser[I, T], input : I) -> Result[T, ParseError]

#
Parser::parse_partial

fn[I, T] Parser::parse_partial(self : Parser[I, T], input : I) -> Result[(T, I), ParseError]

#
Parser::run

fn[I, T] Parser::run(self : Parser[I, T], input : I) -> Result[(T, I), ParseError]

#
Parser::sep_by

fn[I : Cursor, T, S] Parser::sep_by(self : Parser[I, T], sep : Parser[I, S]) -> Parser[I, Array[T]]

Zero or more repetitions of self separated by sep.

Parameters:

  • self : The value parser.
  • sep : The separator parser; its result is discarded.

Returns empty array on no match.

#
Parser::sep_by1

fn[I : Cursor, T, S] Parser::sep_by1(self : Parser[I, T], separator : Parser[I, S]) -> Parser[I, Array[T]]

One or more repetitions of self separated by separator.

Parameters:

  • self : The value parser.
  • separator : The separator parser; its result is discarded.

Requires at least one match of self.

Returns an array of value results.

#
Parser::skip

fn[I : Cursor, T, U] Parser::skip(self : Parser[I, T], next : Parser[I, U]) -> Parser[I, T]

Runs self then next, returning self's result.

Parameters:

  • self : The first parser; its result is returned.
  • next : The second parser; its result is discarded.

Equivalent to self <* next.

#
Parser::some

fn[I : Cursor, T] Parser::some(self : Parser[I, T]) -> Parser[I, Array[T]]

One or more repetitions of self.

Parameters:

  • self : The parser to repeat. Must match at least once.

Implemented as self followed by self.many().

Returns an array of accumulated results.

#
Parser::string_map

fn[U] Parser::string_map(self : Parser[Input, Char], f : (String) -> U) -> Parser[Input, U]

Maps a char parser's result to a String before applying f.

Parameters:

  • self : A parser that yields a single Char.
  • f : A function from String to the desired result type.

Returns a parser that yields the result of f applied to the string representation of the matched character.

#
Parser::then

fn[I : Cursor, T, U] Parser::then(self : Parser[I, T], next : Parser[I, U]) -> Parser[I, U]

Runs self then next, discarding self's result.

Parameters:

  • self : The first parser; its result is discarded.
  • next : The second parser; its result is returned.

Equivalent to self >> next.

#
Position

pub(all) struct Position {
offset : Int
line : Int
column : Int
} derive(Eq,
Debug
)

A source position reported by parser diagnostics.

#
array_cons

fn[T] array_cons(tail : Array[T], head : T) -> Array[T]

#
ascii_digit

fn ascii_digit() -> Parser[Input, Char]

#
ascii_letter

fn ascii_letter() -> Parser[Input, Char]

#
between

fn[I : Cursor, T, L, R] between(left : Parser[I, L], parser : Parser[I, T], right : Parser[I, R]) -> Parser[I, T]

Standalone version of Parser::between.

Parameters:

  • left : The opening parser; its result is discarded.
  • parser : The middle parser whose result is returned.
  • right : The closing parser; its result is discarded.

#
char

fn char(expected : Char) -> Parser[Input, Char]

#
choice

fn[I : Cursor, T] choice(parsers : Array[Parser[I, T]]) -> Parser[I, T]

Tries each parser in the array in order on the same input.

Parameters:

  • parsers : The array of alternative parsers.

Returns the first success. Merges errors from all failures (furthest position wins). Aborts immediately on committed errors.

#
delay

fn[I, T] delay(thunk : () -> Parser[I, T]) -> Parser[I, T]

#
eof

fn[I : Cursor] eof() -> Parser[I, Unit]

#
fail

fn[I : Cursor, T] fail(expected : String) -> Parser[I, T]

#
lexeme

fn[T] lexeme(parser : Parser[Input, T]) -> Parser[Input, T]

Standalone version of Parser::lexeme.

Parameters:

  • parser : The parser to wrap.

Returns a parser that runs parser then skips trailing whitespace.

#
line_content

let line_content : Parser[Input, String]

#
many_chars

fn many_chars(parser : Parser[Input, Char]) -> Parser[Input, String]

Zero or more repetitions of a char parser, collecting results into a String.

Parameters:

  • parser : A char parser to repeat.

Unlike take_while, succeeds on zero matches (returns empty string). Stops on non-committed failure.

Returns a parser that yields the collected characters as a String.

#
many_chars1

fn many_chars1(parser : Parser[Input, Char]) -> Parser[Input, String]

One or more repetitions of a char parser, collecting results into a String.

Parameters:

  • parser : A char parser to repeat. Must match at least once.

Fails on zero matches. Returns a parser that yields the collected characters as a String.

#
newline

let newline : Parser[Input, Char]

#
none_of

fn none_of(chars : String) -> Parser[Input, Char]

#
not_newline

let not_newline : Parser[Input, Char]

#
one_of

fn one_of(chars : String) -> Parser[Input, Char]

#
pure

fn[I, T] pure(value : T) -> Parser[I, T]

#
rest_of_line

let rest_of_line : Parser[Input, String]

#
satisfy

fn satisfy(name : String, predicate : (Char) -> Bool) -> Parser[Input, Char]

#
satisfy_input

fn[I : Cursor, T] satisfy_input(next : (I) -> (T, I)?, name : String, predicate : (T) -> Bool) -> Parser[I, T]

#
space

let space : Parser[Input, Char]

#
spaces

fn spaces() -> Parser[Input, Array[Char]]

#
string

fn string(expected : String) -> Parser[Input, String]

#
symbol

fn symbol(text : String) -> Parser[Input, String]

Matches an exact string, then skips trailing whitespace.

Parameters:

  • text : The string to match.

Returns a parser that matches text followed by optional whitespace.

#
take_while

fn take_while(name : String, predicate : (Char) -> Bool) -> Parser[Input, String]

#
whitespace

fn whitespace() -> Parser[Input, Char]

#
with_line

fn with_line(String) -> Parser[Input, String]