Parsers

From MemCP
Revision as of 11:59, 28 August 2026 by Wikiservice (talk | contribs) (Refresh MemCP documentation: accuracy, operational guidance, performance profile and maintained API reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Parsers

Generated from MemCP commit c42e19eba on 27 August 2026. See Full SCM API documentation.

The Parsers module builds composable packrat parsers for structured text. Parser grammars combine atoms, regular expressions, sequences, alternatives, repetition, optional elements, captures, and semantic actions.

MemCP's SQL frontends are implemented with these facilities. Custom parsers should keep whitespace rules, recursion, error locations, and accepted ambiguity explicit because a permissive grammar can otherwise hide malformed input.

parser

creates a parser

Scm parsers work this way:
(parser syntax scmerresult) -> func

syntax can be one of:
(parser syntax scmerresult) will execute scmerresult after parsing syntax
(parser syntax scmerresult "skipper") will add a different whitespace skipper regex to the root parser
(define var syntax) valid inside (parser...), stores the result of syntax into var for use in scmerresult
"str" AtomParser
(atom "str" caseinsensitive skipws) AtomParser
(regex "asdf" caseinsensitive skipws) RegexParser
'(a b c) AndParser
(or a b c) OrParser
(* sub separator noMemo) KleeneParser
(+ sub separator) ManyParser
(? xyz) MaybeParser (if >1 AndParser)
(not mainparser parser1 parser2 parser3 ...) a parser that matches mainparser but not parser1...
(capture subparser) wraps a parser and returns (matched_text parsed_result)
$ EndParser
empty EmptyParser
symbol -> use other parser defined in env

for further details on packrat parsers, take a look at https://github.com/launix-de/go-packrat

Allowed number of parameters: 1–3

Parameters

  • syntax (any): syntax of the grammar (see docs)
  • generator (any): (optional) expressions to evaluate. All captured variables are available in the scope. (optional)
  • skipper (string): (optional) string that defines the skip mechanism for whitespaces as regexp (optional)

Returns

  • parser (func): parser produced from the grammar
    • Parameters
      • input (string): text to parse
    • Returns
      • result (any): value produced by the grammar generator