Sammal v0.1.0 Sammal.Parser View Source

A simple recursive parser for a Scheme-ish language.

Link to this section Summary

Types

All the parse_* functions adhere to the following pattern: {AST, tokens} -> {:ok, {new AST, remaining tokens}} | {:error, error struct}

A form consists of a list of expressions

Functions

Parser entrypoint

Parse all parenthesis-enclosed expressions

Parse a single parenthesis-enclosed expression

Parse the next atom or expression

Parse up to (and including) the given lexeme

Link to this section Types

Link to this type cursor() View Source
cursor() :: {ast :: [form], remaining :: [Sammal.Expr]}

All the parse_* functions adhere to the following pattern: {AST, tokens} -> {:ok, {new AST, remaining tokens}} | {:error, error struct}

Link to this type form() View Source
form() :: [Sammal.Expr]

A form consists of a list of expressions.

Link to this section Functions

Link to this function parse(tokens) View Source
parse([Sammal.Expr]) :: {:ok, cursor} | {:error, Sammal.SammalError}

Parser entrypoint.

Example

iex> {:ok, tokens} = Sammal.Tokenizer.tokenize(“(define (x 10))”) iex> {:ok, {[[define, [_var, _val]]], []}} = Sammal.Parser.parse(tokens) iex> define %Sammal.Expr{ctx: “(define (x 10))”, lex: “define”, line: 0, row: 1, val: :define}

Link to this function parse_all_expressions(input) View Source
parse_all_expressions(cursor) ::
  {:ok, cursor} |
  {:error, Sammal.SammalError}

Parse all parenthesis-enclosed expressions.

Link to this function parse_expression(arg) View Source
parse_expression(cursor) ::
  {:ok, cursor} |
  {:error, Sammal.SammalError}

Parse a single parenthesis-enclosed expression.

Link to this function parse_next(input) View Source
parse_next(cursor) :: {:ok, cursor} | {:error, Sammal.SammalError}

Parse the next atom or expression.

Link to this function parse_until(input, until) View Source
parse_until(cursor, String.t) :: {:ok, cursor} | {:error, nil}

Parse up to (and including) the given lexeme.