Parsers: Difference between revisions

From MemCP
Jump to navigation Jump to search
(Created page with "= Parsers = The '''Parsers''' module provides parsing functionality for the SCM programming language. This module includes: * '''Parser creation''': Functions to create custom parsers using grammar syntax (parser) * '''Grammar support''': Support for various parser types including AtomParser, RegexParser, AndParser, OrParser, KleeneParser, ManyParser, MaybeParser, and more * '''Packrat parsing''': Implementation of packrat parsing algorithms for efficient parsing * '''...")
 
(Refresh MemCP documentation: accuracy, operational guidance, performance profile and maintained API reference)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<!-- Copyright (C) 2026 Carl-Philip Haensch -->
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
<span id="parsers"></span>
= Parsers =
= Parsers =


The '''Parsers''' module provides parsing functionality for the SCM programming language. This module includes:
<!-- Generated from MemCP c42e19eba on 2026-08-27; do not edit manually. -->
<div class="mw-message-box mw-message-box-notice">Generated from MemCP commit <code>c42e19eba</code> on 27 August 2026. See [[Full SCM API documentation]].</div>


* '''Parser creation''': Functions to create custom parsers using grammar syntax (parser)
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.
* '''Grammar support''': Support for various parser types including AtomParser, RegexParser, AndParser, OrParser, KleeneParser, ManyParser, MaybeParser, and more
* '''Packrat parsing''': Implementation of packrat parsing algorithms for efficient parsing
* '''Whitespace handling''': Configurable whitespace skipping mechanisms


These functions provide essential tools for creating custom parsers and processing structured text data in SCM programs.
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.
 
← Back to [[Full SCM API documentation]]


== parser ==
== parser ==
Line 16: Line 16:
creates a parser
creates a parser


SCM parsers work this way:
<pre>Scm parsers work this way:
<code>(parser syntax scmerresult) -> func</code>
(parser syntax scmerresult) -&gt; func


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


For further details on packrat parsers, take a look at https://github.com/launix-de/go-packrat
for further details on packrat parsers, take a look at https://github.com/launix-de/go-packrat</pre>
'''Allowed number of parameters:''' 1–3


'''Allowed number of parameters:''' 1–3
<span id="parameters"></span>
=== Parameters ===


'''Parameters:'''
* '''syntax''' (<code>any</code>): syntax of the grammar (see docs)
* '''syntax''' (<code>any</code>): syntax of the grammar (see docs)
* '''generator''' (<code>any</code>): (optional) expressions to evaluate. All captured variables are available in the scope.
* '''generator''' (<code>any</code>): (optional) expressions to evaluate. All captured variables are available in the scope. ''(optional)''
* '''skipper''' (<code>string</code>): (optional) string that defines the skip mechanism for whitespaces as regexp
* '''skipper''' (<code>string</code>): (optional) string that defines the skip mechanism for whitespaces as regexp ''(optional)''
 
<span id="returns"></span>
=== Returns ===


'''Returns:''' <code>func</code>
* '''parser''' (<code>func</code>): parser produced from the grammar
** '''Parameters'''
*** '''input''' (<code>string</code>): text to parse
** '''Returns'''
*** '''result''' (<code>any</code>): value produced by the grammar generator

Latest revision as of 11:59, 28 August 2026


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