Module rsast

Require: require("rsast")

Index

Function

set_call_limit()

Sets the maximum call limit for the parser state.

set_error_detail()

Sets whether information for more error details should be collected.

Class

Ast

A PEG parser

Node

Pair

A matching pair of rsast.Token and everything between them

Pairs

An iterator over rsast.Pair

Tree

Api reference

class rsast.Node: table
start: integer
stop: integer
rule: string
node_tag: string?
pairs: rsast.Tree?
class rsast.Tree: table
start: integer
stop: integer
pairs: rsast.Node[]
class rsast.Pair: userdata

A matching pair of rsast.Token and everything between them

start: fun(self): integer
stop: fun(self): integer
as_rule: fun(self): string
as_str: fun(self): string
as_node_tag: fun(self): string?
get_input: fun(self): string
line_col: fun(self): (integer, integer)
dump: fun(self): rsast.Node
pairs(self): rsast.Pairs?

Returns any inner nodes as an rsast.Pairs

If there are no inner nodes, returns nil

class rsast.Pairs

An iterator over rsast.Pair

as_str: fun(self): string
get_input: fun(self): string
is_empty: fun(self): boolean
dump: fun(self): rsast.Tree
peek(self): rsast.Pair?

Get the next pair without advancing the iterator.

Returns nil if the iterator is exhausted

next(self): rsast.Pair?

Get the next pair.

Returns nil if the iterator is exhausted

next_back(self): rsast.Pair?

Get the next pair from the end.

Returns nil if the iterator is exhausted

iter(self): fun(): rsast.Pair

Iterate over the pairs

reviter(self): fun(): rsast.Pair

Iterate over the pairs in reverse order

flatten(self): rsast.Pairs

Flatten nested nodes into a single iterator

find_tagged(self, tag: string): rsast.Pair[]

Searches through the top level nodes for all nodes marked with tag

If you would like to search through all nodes, including inner nodes, call rsast.Pairs:flatten() first.

class rsast.Ast: userdata

A PEG parser

staticmethod new(grammar: string): (ast?: rsast.Ast, errors?: string[])

Load a grammar from a string for parsing.

Returns rsast.Ast, nil if the grammar was loaded successfully, or nil, string[], where string[] is a list of errors encountered while loading the grammar.

Parameters:

grammar (string) – The grammar to load

Returns:
  • ast? (rsast.Ast) – A parser for the provided grammar, returns nil of it could not be loaded

  • errors? (string[]) – Errors encountered while loading the grammar, or nil if there were none.

set_error_formatter(
    self,
    rule_handler?: fun(rule: string): string,
    ws_handler: fun(text: string): boolean
)

Set callbacks to handle error formatting.

rule_handler is a callback which receives the name of a rule and returns a string to write in its place.

ws_handler is a callback which receives a text and returns a boolean as to whether the text should be considered whitespace

Parameters:
  • rule_handler? (fun(rule: string): string) – Formatter for rule names

  • ws_handler (fun(text: string): boolean) – Formatter for whitespace

validate(self, rule: string, input: string): (valid: boolean, errors?: string)

Validate in input against the grammar

Parameters:
  • rule (string) – The rule to parse

  • input (string) – The input to parse

Returns:
  • valid (boolean) – Whether the input was valid, and any errors if it was not.

  • errors? (string) – Any errors encountered when parsing

parse<R>(
    self,
    rule: string,
    input: string,
    callback: fun(pairs: rsast.Pairs): R...
): R...

Parse an input with the loaded grammar.

callback is called with an rsast.Pairs iterator as the first argument, and the return value of the function is returned from parse.

Parameters:
  • rule (string) – The rule to parse

  • input (string) – The input to parse

Returns:

_1 (R...) – Returns the result of callback

rsast.set_call_limit(limit?: integer)

Sets the maximum call limit for the parser state.

If set, the calls are tracked as a running total over all non-terminal rules that can nest closures (which are passed to transform the parser state).

This can help prevent stack overflows or excessive execution times in some grammars.

Passing limit as 0 or nil disables any call limit.

rsast.set_error_detail(enable: boolean)

Sets whether information for more error details should be collected.

This can have a performance impact, so it is disabled by default.