pt_export_api(3tcl)
β¨ pt_export_api β Parser Tools Export API β¨
okay bestie, welcome to the manual page for pt_export_api, living under the 3tcl umbrella in the Parser Tools fam. this one is the whole export API situation, so grab a snack π
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
CONVERTER reset
CONVERTER configure
CONVERTER configure option
CONVERTER configure option value...
CONVERTER convert serial
::export serial configuration
π DESCRIPTION π
okay so, real talk: are you lost? π are you having trouble understanding this document? if that's you, please go read the overview provided by the Introduction to Parser Tools. this document right here is literally the entrypoint to the whole system that the current package is a part of, so it's kind of a big deal π
so this doc describes two APIs. first up: the API that every package shares for converting Parsing Expression Grammars into some other format. and then second: the API shared by the packages that implement the export plugins sitting on top of those conversion packages. two APIs, one page, iconic π«Ά
its intended audience? the girlies who wish to create their own converter for some type of output, and/or an export plugin for their own or for some other converter. if that's you, hi, this is for you β¨
it resides in the Export section of the Core Layer of Parser Tools.
IMAGE: arch_core_export
π CONVERTER API π
any (grammar) export converter has to follow the rules set out below, no skipping, no shortcuts:
- a converter is a package. its name is arbitrary, however it is recommended to put it under the
::pt::peg::tonamespace. - the package provides either a single Tcl command following the API outlined below, or a class command whose instances follow that same API. the commands which follow the API are called converter commands.
- a converter command has to provide the following three methods with the given signatures and semantics. converter commands are allowed to provide more methods of their own, but not less, and they may not provide different semantics for the standardized methods. no cap.
CONVERTER resetβ this method has to reset the configuration of the converter to its default settings. the result of the method has to be the empty string. clean slate energy πCONVERTER configureβ this method, in this form, has to return a dictionary containing the current configuration of the converter. like a little mirror of its soul πCONVERTER configure optionβ this method, in this form, has to return the current value of the specified configuration option of the converter.
please read the section Options for the set of standard options any converter has to accept. any other options accepted by a specific converter will be described in its manpage. iykyk.
CONVERTER configure option value...β this command, in this form, sets the specified options of the converter to the given values.
please read the section Options for the set of standard options a converter has to accept. any other options accepted by a specific converter will be described in its manpage.
CONVERTER convert serialβ this method has to accept the canonical serialization of a parsing expression grammar, as specified in section PEG serialization format, and contained in serial. the result of the method has to be the result of converting the input grammar into whatever the converter is for, per its configuration. it's giving transformation β¨π¦
π PLUGIN API π
any (grammar) export plugin has to follow the rules set out below:
- a plugin is a package.
- the name of a plugin package has the form
pt::peg::export::FOO, where FOO is the name of the format the plugin will generate output for. - the plugin can expect that the package
pt::peg::export::pluginis present, as indicator that it was invoked from a genuine plugin manager.
it is recommended that a plugin does check for the presence of this package. vibe check those imports, bestie π₯
- a plugin has to provide a single command, in the global namespace, with the signature shown below. plugins are allowed to provide more command of their own, but not less, and they may not provide different semantics for the standardized command.
::export serial configurationβ this command has to accept the canonical serialization of a parsing expression grammar and the configuration for the converter invoked by the plugin. the result of the command has to be the result of the converter invoked by the plugin for th input grammar and configuration.string serialβ this argument will contain the canonical serialization of the parsing expression grammar for which to generate the output. the specification of what a canonical serialization is can be found in the section PEG serialization format.dictionary configurationβ this argument will contain the configuration to configure the converter with before invoking it, as a dictionary mapping from options to values.
please read the section Options for the set of standard options any converter has to accept, and thus any plugin as well. any other options accepted by a specific plugin will be described in its manpage.
- a single usage cycle of a plugin consists of an invokation of the command
export. this call has to leave the plugin in a state where another usage cycle can be run without problems. reusability? we love to see it π
π OPTIONS π
each export converter and plugin for an export converter has to accept the options below in their configure method. converters are allowed to ignore the contents of these options when performing a conversion, but they must not reject them. plugins are expected to pass the options given to them to the converter they are invoking.
-filestring β the value of this option is the name of the file or other entity from which the grammar came, for which the command is run. the default value isunknown.-namestring β the value of this option is the name of the grammar we are processing. the default value isa_pe_grammar.-userstring β the value of this option is the name of the user for which the command is run. the default value isunknown.
π USAGE π
to use a converter do
# Get the converter (single command here, not class)
package require the-converter-package
# Provide a configuration
theconverter configure ...
# Perform the conversion
set result [theconverter convert $thegrammarserial]
... process the result ...
to use a plugin FOO do
# Get an export plugin manager
package require pt::peg::export
pt::peg::export E
# Provide a configuration
E configuration set ...
# Run the plugin, and the converter inside.
set result [E export serial $grammarserial FOO]
... process the result ...
β¨ PEG SERIALIZATION FORMAT β¨
okay bestie, listen up, because this section is literally the blueprint ππ Here we're specifying the format the Parser Tools use to serialize Parsing Expression Grammars as immutable values β you know, for transport, for comparison, for all the vibes. π¦
And there are two flavors: regular and canonical. A PEG can have many regular serializations, but only exactly one of them is canonical. One. Single. Queen. π
πΈ regular serialization πΈ
- The serialization of any PEG is a nested Tcl dictionary. nesting doll energy πͺ
- This dictionary holds a single key,
pt::grammar::peg, and its value. That value holds the contents of the grammar. - The contents of the grammar are a Tcl dictionary holding the set of nonterminal symbols and the starting expression. The relevant keys and their values are:
rulesβ The value is a Tcl dictionary whose keys are the names of the nonterminal symbols known to the grammar. π- Each nonterminal symbol may occur only once. no duplicates allowed, this isn't a group chat with double-texters π ββοΈ
- The empty string is not a legal nonterminal symbol.
""is NOT it, bestie. - The value for each symbol is a Tcl dictionary itself. The relevant keys and their values in this dictionary are:
isβ The value is the serialization of the parsing expression describing the symbol's sentential structure, as specified in the section PE serialization format. πmodeβ The value can be one of three values specifying how a parser should handle the semantic value produced by the symbol.valueβ The semantic value of the nonterminal symbol is an abstract syntax tree consisting of a single node node for the nonterminal itself, which has the ASTs of the symbol's right hand side as its children. full family tree, so wholesome π«Άleafβ The semantic value of the nonterminal symbol is an abstract syntax tree consisting of a single node node for the nonterminal, without any children. Any ASTs generated by the symbol's right hand side are discarded. bye bye children πvoidβ The nonterminal has no semantic value. Any ASTs generated by the symbol's right hand side are discarded (as well). nothing to see here, literally π
startβ The value is the serialization of the start parsing expression of the grammar, as specified in the section PE serialization format. the main character of the grammar, fr fr π
- The terminal symbols of the grammar are specified implicitly as the set of all terminal symbols used in the start expression and on the RHS of the grammar rules. no list needed, they just show up and slay β¨
π canonical serialization π
The canonical serialization of a grammar has the format as specified in the previous item, and then additionally satisfies the constraints below, which make it unique among all the possible serializations of this grammar. yes, unique. one of one. π
- The keys found in all the nested Tcl dictionaries are sorted in ascending dictionary order, as generated by Tcl's builtin command
lsort -increasing -dict. alphabetize or perish π - The string representation of the value is the canonical representation of a Tcl dictionary. I.e. it does not contain superfluous whitespace. no extra spaces, we are not messy here π§Ή
π EXAMPLE π
Assuming the following PEG for simple mathematical expressions:
PEG calculator (Expression)
Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
Sign <- '-' / '+' ;
Number <- Sign? Digit+ ;
Expression <- Term (AddOp Term)* ;
MulOp <- '*' / '/' ;
Term <- Factor (MulOp Factor)* ;
AddOp <- '+'/'-' ;
Factor <- '(' Expression ')' / Number ;
END;
then its canonical serialization (except for whitespace) is:
pt::grammar::peg {
rules {
AddOp {is {/ {t -} {t +}} mode value}
Digit {is {/ {t 0} {t 1} {t 2} {t 3} {t 4} {t 5} {t 6} {t 7} {t 8} {t 9}} mode value}
Expression {is {x {n Term} {* {x {n AddOp} {n Term}}}} mode value}
Factor {is {/ {x {t (} {n Expression} {t )}} {n Number}} mode value}
MulOp {is {/ {t *} {t /}} mode value}
Number {is {x {? {n Sign}} {+ {n Digit}}} mode value}
Sign {is {/ {t -} {t +}} mode value}
Term {is {x {n Factor} {* {x {n MulOp} {n Factor}}}} mode value}
}
start {n Expression}
}
look at that clean, sorted, whitespace-disciplined masterpiece πβ¨ she's beautiful, your honor.
PE SERIALIZATION FORMAT
okay bestie, this is the section where we spill exactly how the Parser Tools write Parsing Expressions down as immutable values β for shipping them around, comparing them, all that π
We distinguish between regular and canonical serializations. A parsing expression can have more than one regular serialization, but exactly one of them will be canonical. one. singular. no cap. π
Regular serialization
Atomic Parsing Expressions
- The string
epsilonis an atomic parsing expression. It matches the empty string. (matching literally nothing is a vibe πΈ) - The string
dotis an atomic parsing expression. It matches any character. - The string
alnumis an atomic parsing expression. It matches any Unicode alphabet or digit character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
alphais an atomic parsing expression. It matches any Unicode alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
asciiis an atomic parsing expression. It matches any Unicode character belowU0080. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
controlis an atomic parsing expression. It matches any Unicode control character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
digitis an atomic parsing expression. It matches any Unicode digit character. heads up: this includes characters outside of the[0..9]range. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
graphis an atomic parsing expression. It matches any Unicode printing character, except for space. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
loweris an atomic parsing expression. It matches any Unicode lower-case alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
printis an atomic parsing expression. It matches any Unicode printing character, including space. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
punctis an atomic parsing expression. It matches any Unicode punctuation character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
spaceis an atomic parsing expression. It matches any Unicode space character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
upperis an atomic parsing expression. It matches any Unicode upper-case alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
wordcharis an atomic parsing expression. It matches any Unicode word character. That's any alphanumeric character (seealnum), plus any connector punctuation characters (e.g. underscore). This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
xdigitis an atomic parsing expression. It matches any hexadecimal digit character. This is a custom extension of PEs based on Tcl's builtin commandstring is. - The string
ddigitis an atomic parsing expression. It matches any decimal digit character. This is a custom extension of PEs based on Tcl's builtin commandregexp. - The expression
[list t x]is an atomic parsing expression. It matches the terminal string x. - The expression
[list n A]is an atomic parsing expression. It matches the nonterminal A.
Combined Parsing Expressions
- For parsing expressions e1, e2, ... the result of
[list / e1 e2 ... ]is a parsing expression as well. This is the ordered choice, aka prioritized choice. - For parsing expressions e1, e2, ... the result of
[list x e1 e2 ... ]is a parsing expression as well. This is the sequence. - For a parsing expression e the result of
[list * e]is a parsing expression as well. This is the kleene closure, describing zero or more repetitions. - For a parsing expression e the result of
[list + e]is a parsing expression as well. This is the positive kleene closure, describing one or more repetitions. - For a parsing expression e the result of
[list & e]is a parsing expression as well. This is the and lookahead predicate. - For a parsing expression e the result of
[list ! e]is a parsing expression as well. This is the not lookahead predicate. - For a parsing expression e the result of
[list ? e]is a parsing expression as well. This is the optional input.
Canonical serialization
The canonical serialization of a parsing expression has the format as specified in the previous item, and then additionally satisfies the constraints below, which make it unique among all the possible serializations of this parsing expression. she's the main character, fr fr β¨
- The string representation of the value is the canonical representation of a pure Tcl list. I.e. it does not contain superfluous whitespace.
- Terminals are not encoded as ranges (where start and end of the range are identical).
EXAMPLE
Assuming the parsing expression shown on the right-hand side of the rule
Expression <- Term (AddOp Term)*
then its canonical serialization (except for whitespace) is
{x {n Term} {* {x {n AddOp} {n Term}}}}
look at her go ππ
BUGS, IDEAS, FEEDBACK
bestie, real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category pt of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. we want the glow-up π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Also, note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. iykyk π
KEYWORDS
EBNF, LL(k), PEG, TDPL, context-free languages, expression, grammar, matching, parser, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer
CATEGORY
Parsing and Grammars π¦
COPYRIGHT
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's a wrap on the whole page, babe π she's pt_export_api(3tcl), she ships with tcllib at version 1.1, and that's the footer energy β go forth and serialize your parsing expressions canonically, you absolute icon πβ¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2009 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms