pt::pe(3tcl)
π pt::pe β Parsing Expression Serialization π
okay bestie, welcome to pt::pe, a manual page from the Parser Tools collection, sitting pretty in the 3tcl section. this one's about serialization of parsing expressions, and honestly? it's giving foundational. π
β¨ NAME β¨
pt::pe β Parsing Expression Serialization. that's the whole name, that's the whole vibe. π
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
package require pt::pe ?1.0.2?
package require char
::pt::pe verify serial ?canonvar?
::pt::pe verify-as-canonical serial
::pt::pe canonicalize serial
::pt::pe print serial
::pt::pe bottomup cmdprefix pe
cmdprefix pe op arguments
::pt::pe topdown cmdprefix pe
::pt::pe equal seriala serialb
::pt::pe epsilon
::pt::pe dot
::pt::pe alnum
::pt::pe alpha
::pt::pe ascii
::pt::pe control
::pt::pe digit
::pt::pe graph
::pt::pe lower
::pt::pe print
::pt::pe punct
::pt::pe space
::pt::pe upper
::pt::pe wordchar
::pt::pe xdigit
::pt::pe ddigit
::pt::pe terminal t
::pt::pe range ta tb
::pt::pe nonterminal nt
::pt::pe choice pe...
::pt::pe sequence pe...
::pt::pe repeat0 pe
::pt::pe repeat1 pe
::pt::pe optional pe
::pt::pe ahead pe
::pt::pe notahead pe
π¦ DESCRIPTION π¦
are you lost?? π like, genuinely confused by this document? no worries babe, go read the overview in the Introduction to Parser Tools. that's the entrypoint to the entire system this package is a part of, and it'll catch you up. π«Ά
so. this package hands you the commands to work with the serializations of parsing expressions as managed by the Parser Tools β all specified over in the section PE serialization format. β¨
this is a supporting package in the Core Layer of Parser Tools, so it's lowkey the glue holding things together. π
IMAGE: arch_core_support
π API π
π ::pt::pe verify serial ?canonvar?
this one checks that whatever's inside serial is actually a valid serialization of a parsing expression. if it's not? boom, it throws an error. π if it is valid, the command just returns the empty string. no drama.
if you pass the argument canonvar, it gets interpreted as the name of a variable in the calling context. that variable gets written to if and only if serial is a valid regular serialization. its value will be a boolean, and here's the tea:
Trueβ the serialization isn't just valid, it's also canonical. slay.Falseβ valid, but non-canonical. still valid tho, don't be rude.
for the full spec on serializations, go check the section PE serialization format. π
π§ ::pt::pe verify-as-canonical serial
this one is stricter β it verifies that serial is a valid canonical serialization of a parsing expression, and throws an error if it isn't. π
result of the command is the empty string.
same deal: spec lives in the PE serialization format section. β¨
π ::pt::pe canonicalize serial
this command assumes the content of serial is a valid regular serialization of a parsing expression, and will throw an error if that's not the case. no cap.
assuming it's fine, it converts the input into the canonical serialization of that parsing expression and returns it as the result. and if the input was already canonical? it comes right back at you unchanged. iconic. π
again, for the spec on regular and canonical serializations, see PE serialization format. π
π¦ ::pt::pe print serial
this one assumes the argument serial holds a valid serialization of a parsing expression, and it returns a string containing that PE in a human readable form. so pretty. πΈ
heads up bestie: the exact format of that human-readable form is not specified and you cannot rely on it for parsing or any other machine-based activities. it's for your eyeballs only, not your parser's. π
for the serialization spec, see PE serialization format. π
π ::pt::pe bottomup cmdprefix pe
this walks the parsing expression pe from the bottom up to the root, invoking the command prefix cmdprefix for each partial expression. what that means in practice: the children of a parsing expression PE get handled before PE itself. so it's leaves first, then upward. π±β‘οΈπ³
the command prefix has this signature:
cmdprefix pe op arguments
so basically it gets called with:
- the parsing expression
pethat the walk is currently sitting at, - the
operator in thatpe, - and the operator's
arguments.
here's the fun part: the result returned by the command prefix replaces pe in the parsing expression it was a child of. so yes, you can transform the expression tree. π
which also means that for all inner parsing expressions, the contents of arguments are the results of the command prefix invoked for the children of that inner parsing expression. nesting, but make it cute. π
π ::pt::pe topdown cmdprefix pe
this one walks pe from the root down to the leaves, invoking cmdprefix for each partial expression. so here the children of a parsing expression PE are handled after PE. top first, then down. β¬οΈ
the command prefix has the same signature as for bottomup (scroll up, bestie βοΈ).
and the result returned by the command prefix is ignored. noted. π«‘
π«Ά ::pt::pe equal seriala serialb
tests the two parsing expressions seriala and serialb for structural equality. result is a boolean: true if the expressions are identical, false otherwise. simple as that. β¨
one caveat tho: string equality is usable only if we can assume the two parsing expressions are pure Tcl lists. don't forget it. π
π atomic parsing expressions β the little building blocks π
each of these constructs the atomic parsing expression for the thing named. one-liners, but they're all main characters in their own right. π
::pt::pe epsilonβ constructs the atomic parsing expression forepsilon.::pt::pe dotβ constructs the atomic parsing expression fordot.::pt::pe alnumβ constructs the atomic parsing expression foralnum.::pt::pe alphaβ constructs the atomic parsing expression foralpha.::pt::pe asciiβ constructs the atomic parsing expression forascii.::pt::pe controlβ constructs the atomic parsing expression forcontrol.::pt::pe digitβ constructs the atomic parsing expression fordigit.::pt::pe graphβ constructs the atomic parsing expression forgraph.::pt::pe lowerβ constructs the atomic parsing expression forlower.::pt::pe printβ constructs the atomic parsing expression forprint.::pt::pe punctβ constructs the atomic parsing expression forpunct.::pt::pe spaceβ constructs the atomic parsing expression forspace.::pt::pe upperβ constructs the atomic parsing expression forupper.::pt::pe wordcharβ constructs the atomic parsing expression forwordchar.::pt::pe xdigitβ constructs the atomic parsing expression forxdigit.::pt::pe ddigitβ constructs the atomic parsing expression forddigit.
π ::pt::pe terminal t
constructs the atomic parsing expression for the terminal symbol t. πΈ
π ::pt::pe range ta tb
constructs the atomic parsing expression for the range of terminal symbols ta ... tb. inclusive vibes. β¨
π§ ::pt::pe nonterminal nt
constructs the atomic parsing expression for the nonterminal symbol nt. π
π ::pt::pe choice pe...
constructs the parsing expression representing the ordered (or prioritized) choice between the argument parsing expressions. and important detail: the first argument has the highest priority. so order matters, bestie. π₯
π ::pt::pe sequence pe...
constructs the parsing expression representing the sequence of the argument parsing expressions. the first argument is the first element of the sequence. left to right, no notes. π
β ::pt::pe repeat0 pe
constructs the parsing expression representing the zero or more repetition of the argument parsing expression pe β also known as the kleene closure. zero is allowed. we love an inclusive queen. π
π₯ ::pt::pe repeat1 pe
constructs the parsing expression representing the one or more repetition of the argument parsing expression pe β also known as the positive kleene closure. at least one, no skipping. πͺ
π¦ ::pt::pe optional pe
constructs the parsing expression representing the optionality of the argument parsing expression pe. take it or leave it, literally. π
π ::pt::pe ahead pe
constructs the parsing expression representing the positive lookahead of the argument parsing expression pe. peeking respectfully. π
π ::pt::pe notahead pe
constructs the parsing expression representing the negative lookahead of the argument parsing expression pe. peeking, but with an agenda. π«
that's it for part 1, bestie β more is coming, so don't go anywhere. π
β¨ PE SERIALIZATION FORMAT β¨
okay bestie, gather round, because this is the whole spec for how the Parser Tools serialize Parsing Expressions β as immutable values, so you can ship them around for transport, comparison, all of that. π
And there's a plot twist: we split this into regular and canonical serializations. A parsing expression can have more than one regular serialization, but exactly one of them is canonical. only one. periodt. π
π Regular serialization π
πΈ Atomic Parsing Expressions πΈ
- The string
epsilonis an atomic parsing expression. It matches the empty string. β¨ - 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 below U0080. 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. Note that 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. This is any alphanumeric character (seealnum), and 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 is literally what makes it unique among all the possible serializations of this parsing expression. it's giving main character energy, 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 π
Say the parsing expression is the one sitting on the right-hand side of this rule:
Expression <- Term (AddOp Term)*
then its canonical serialization (except for whitespace, bestie) is:
{x {n Term} {* {x {n AddOp} {n Term}}}}
slay. π¦
π©· BUGS, IDEAS, FEEDBACK π©·
ngl, 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 at http://core.tcl.tk/tcllib/reportlist. And please also report any ideas for enhancements you may have for either the package and/or the documentation β we want them, truly. π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Also note, and this is a real one: attachments are strongly preferred over inlined patches. You can make an attachment 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 the whole page, bestie!! π this one shipped as tcllib version 1.0.2, and it's the pt::pe(3tcl) manual page (the date field on the footer is just vibing empty, so nothing to report there π). go forth and serialize, 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