grammar

Types

Rule[N; T] = ref RuleObj[N, T]
Node[N; T] = ref object of RootObj
  case nodeKind*: NodeKind
  of nkNonTerminal:
      children*: seq[Node[N, T]]

  of nkTerminal:
      nil

  start*: int
  length*: int
  kind*: N
  source*: T

Procs

proc value[N, T](node: Node[N, T]): string
Returns the text referred to by the node
proc render[N, T](node: Node[N, T]): string
Returns the value of the node, prefixed by its kind
proc render[N, T](nodes: seq[Node[N, T]]): string
Returns a concatenation of the render of a sequence of nodes
proc parse[N: enum; T: string | seq](rule: Rule[N, T]; text: T; start = 0): seq[Node[N, T]]
Attempts to parse the suppled text using the given rule, starting at position start
proc literal[N, T, P](pattern: P; kind: N): Rule[N, T]
proc iliteral[N](pattern: string; kind: N): Rule[N, string]
proc any[N: enum](chars: string; kind: N): Rule[N, string]
proc any[N: enum; T: string | seq; S: enum](symbols: set[S] | seq[S]; kind: N): Rule[N, T]
proc ignore[N, T](rule: Rule[N, T]): Rule[N, T]
Ignores the contents of the rule supplied as an argument
proc eof[N, T](kind: N): Rule[N, T]
Matches the end of the text
proc combine[N, T](rule: Rule[N, T]; kind: N): Rule[N, T]

Creates a new rule that matches from the beginning to the end of the rule supplied as an argument

Warning: ignored sections of the text may be re-surfaced

proc build[N, T](rule: Rule[N, T]; kind: N): Rule[N, T]
Creates a non-terminal rule whose children are the matches of the rule supplied as an argument
proc fail[N, T](message: string; kind: N): Rule[N, T]
Fails the parse, printing an indicator as to where the failure happened to stdout
proc error[N, T](message: string; kind: N): Rule[N, T]
Fails the parse, assuming that the previous matched node is the one that caused the failure
proc dump[N, T](message: string; kind: N): Rule[N, T]
Prints a dump of the parse up until this point
proc warn[N, T](message: string; kind: N): Rule[N, T]
Fails the parse, assuming that the previous matched node is the one that caused the failure
proc `+`[N, T](left: Rule[N, T]; right: Rule[N, T]): Rule[N, T]
Matches if left matches and then right matches
proc `/`[N, T](left: Rule[N, T]; right: Rule[N, T]): Rule[N, T]
Matches if left or right matches
proc `?`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches rule if it matches, otherwise succeeds anyway
proc `+`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches at least one repetition of rule
proc `*`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches any number of repetitions of rule (including none)
proc `^`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches anything but rule (Note that this consumes - for zero-width lookahead see !)
proc `*`[N, T](repetitions: int; rule: Rule[N, T]): Rule[N, T]
Matches rule, repetition times
proc `&`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches, so long as rule would match at this point (i.e. zero-width lookahead)
proc `!`[N, T](rule: Rule[N, T]): Rule[N, T]
Matches, so long as rule would not match at this point (i.e. negative zero-width lookahead)
proc `/`[N, T](rule: Rule[N, T]): Rule[N, T]
proc `->`(rule: Rule; production: Rule)

Templates

template grammar[K; ](Kind, Text, Symbol: typedesc; default: K; code: untyped): void
template grammar[K; ](Kind: typedesc; default: K; code: untyped): void