pt::param(n)
๐ฆ pt::param(n) โ Parser Tools ๐ฆ
okay bestie, welcome to pt::param(n), aka the PackRat Machine Specification. This is part 1, and we're about to vibe-check a whole virtual machine. No cap, every state and instruction is staying. ๐
โจ NAME โจ
pt::param โ PackRat Machine Specification
๐ธ SYNOPSIS ๐ธ
package require Tcl 8.5 9
๐ DESCRIPTION ๐
Are you lost? Is this doc giving you trouble? Then bestie, go read the overview in the Introduction to Parser Tools. This page is the entrypoint to the whole system this package lives in.
Welcome to the PackRat Machine (short: PARAM) โ a virtual machine built to support recursive descent parsers, especially packrat parsers. It's lowkey iconic: it caches and reuses partial results, caches the input it encounters, and can backtrack in both input and AST creation. The way it just does all that? Obsessed.
This document lays out the machine's architectural state and its instruction set.
๐ ARCHITECTURAL STATE ๐
Any PARAM implementation has to manage at least the following state:
Input (IN)
This is the channel we pull characters from when we need to process them.
The instructions in Input Handling are the ones that use and modify this part of the machine's state.
Current Character (CC)
This is the character from the input that's currently getting tested against all its possible alternatives.
The instructions in Character Processing are the ones that use and modify this part of the machine's state.
Current Location (CL)
This is where the current character sits in the input, measured as an offset from the start of the input. Character offsets start at 0.
The instructions in Character Processing, Location Handling, and Nonterminal Execution are the ones that use and modify this part of the machine's state.
Location Stack (LS)
A stack of input locations, saved so we can backtrack later.
The instructions in Character Processing, Location Handling, and Nonterminal Execution are the ones that use and modify this part of the machine's state.
Status (ST)
This is the result of the last attempt to test the input โ either success or failure.
The instructions in Status Control, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.
Semantic Value (SV)
The current semantic value. It's either empty, or it's a node for an AST built from the input.
The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.
AST Reduction Stack (ARS)
A stack of partial ASTs that get built while nonterminal symbols are being processed.
The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.
AST Stack (AS)
A stack of reduction stacks, saved so we can backtrack if we need to.
The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.
Error Status (ER)
What the machine currently knows about errors. It's either empty, or it's a pair: a location in the input plus the set of messages for that location.
Heads up: this part of the state can be set even when the last test of the current character succeeded. Like, the *-operator (matching a sub-expression zero or more times) in a PEG is always successful, even if it runs into a problem further in the input and has to backtrack. Those problems can't just be forgotten when parsing continues.
The instructions in Error Handling, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.
Error Stack (ES)
A stack of error stati, saved for backtracking. It lets the machine merge current and older error stati when it backtracks in choices after a failed match.
The instructions in Error Handling, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.
Nonterminal Cache (NC)
A cache of machine states, keyed by pairs of nonterminal symbol name and input location. Each pair (N, L) gets a 4-tuple: the values to use for CL, ST, SV, and ER after nonterminal N was parsed starting from location L. It's a performance boost for backtracking parsers, so they can skip expensive reparsing of complex nonterminal symbols if they've already been seen at a given location.
The key location is where the machine started trying to match the named nonterminal symbol, and the location inside the saved 4-tuple is where the machine ended up after the attempt finished โ doesn't matter if the attempt succeeded or not.
The instructions in Nonterminal Execution are the ones that use and modify this part of the machine's state.
Terminal Cache (TC)
A cache of characters read from IN, with their location in IN as a pair of line and column, keyed by the location in IN โ this time as a character offset from the beginning of IN. It's a performance aid for backtracking parsers: it avoids a possibly expensive reread of characters from IN, and it can even enable backtracking when IN isn't randomly seekable, i.e.
The instructions in Input Handling are the ones that use and modify this part of the machine's state.
๐ฅ INSTRUCTION SET ๐ฅ
Now that the machine's architectural state is on the table, we can talk about the instruction set that operates on it โ every PARAM realization has to implement these. There are 37 instructions, grouped roughly by the state they influence and/or query while they run.
๐ง INPUT HANDLING ๐ง
These instructions mostly touch IN, pulling characters into the machine for processing.
input_next msg
Reads the next character from IN โ that's the character after CL. If it works, that character becomes CC, CL moves forward by one, ES gets cleared, and the operation is marked as a success in ST.
If the next character isn't already known to TC, the operation might read it from IN. When that works, the new character gets stored in TC along with its location (line, column), and everything else behaves like above. Later reads from the same location โ which can happen because of backtracking โ will then be satisfied from TC instead of IN.
If we hit the end of IN instead, the operation is marked as failed in ST, CL stays exactly where it was, and the pair of CL and msg becomes the new ES.
๐ CHARACTER PROCESSING ๐
These instructions mostly access CC, testing it against character classes, ranges, and individual characters.
test_alnum
This instruction implements the special PE operator "alnum". It checks whether CC falls into the character class of the same name or not.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_alpha
This instruction implements the special PE operator "alpha". It checks whether CC falls into the character class of the same name or not.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_ascii
This instruction implements the special PE operator "ascii". It checks whether CC falls into the character class of the same name or not.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_char char
This instruction implements the character matching operator, i.e. it checks whether CC is char.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_ddigit
This instruction implements the special PE operator "ddigit". It checks whether CC falls into the character class of the same name or not.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_digit
This instruction implements the special PE operator "digit". It checks whether CC falls into the character class of the same name or not.
Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ that gets the machine ready for another parse attempt by a possible alternative.
test_graph
This instruction implements the special PE operator "graph". It checks whether CC falls into the character class of the same name or not.
okay so picking right back up where we left off โ that whole success/failure thing? it lands directly in ST, no detours. โจ and if it succeeds, ES gets cleared, cute. if it fails, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL gets rewound by one character โ which is the machine's way of going "okay bet, let's try again with a possible alternative." ๐
test_lower
this one implements the special PE operator lower โ it checks whether CC falls into the character class of the same name, or not. that's it, that's the vibe check. ๐
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐ธ
test_print
implements the special PE operator print, checking if CC falls into the character class of the same name, or not. printable bestie check, love that for us. ๐จ๏ธ
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. โจ
test_punct
implements the special PE operator punct โ is CC in that character class or not, that's the whole question. punctuation girlies rise ๐
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative.
test_range chars chare
this instruction implements the range matching operator, i.e. it checks if CC falls into the interval of characters spanned up by the two characters from chars to chare, both inclusive. so like, chars โค CC โค chare, no cap. ๐
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐ฅ
test_space
implements the special PE operator space, which checks if CC falls into the character class of the same name, or not. yes bestie, whitespace counts as a character class, iconic behaviour honestly. ๐ฆ
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐ธ
test_upper
implements the special PE operator upper, checking whether CC falls into the character class of the same name, or not. UPPERCASE ONLY, periodt. ๐
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐
test_wordchar
implements the special PE operator wordchar, which checks if CC falls into the character class of the same name, or not. word characters stay winning fr fr. ๐ซถ
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. โจ
test_xdigit
implements the special PE operator xdigit โ checks if CC falls into the character class of the same name, or not. hex digits, we love a base-16 moment. ๐
success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐
๐จ ERROR HANDLING ๐จ
the instructions in this section mainly access ER and ES. yeah, they're the drama-adjacent registers, we love them anyway. ๐
error_clear
this instruction clears ER. just wipes it, gone, bye. ๐ง
error_push
this instruction makes a copy of ER and pushes it on ES. a lil souvenir of the error state, kept safe on the stack. ๐
error_pop_merge
this instruction takes the topmost entry of ES and merges the error status it contains with ES, making the result the new ES.
the merge is governed by four rules, with the merge result:
- Empty if both states are empty.
- The non-empty state if only one of the two states is non-empty.
- The state with the larger location, if the two states specify different locations.
- The pair of the location shared by the two states, and the set-union of their messages for states at the same location.
like okay the merge is doing the most but it's also being extremely fair about it. โ๏ธ
error_nonterminal symbol
this is a guarded instruction. it does nothing if either ES is empty, or if the location in ES is not just past the last location saved in LS. otherwise it sets the pair of that location and the nonterminal symbol as the new ES. ๐
Note: in the above, "just past" means "that location plus one", or also "the location of the next character after that location". yes bestie, they said it twice to be sure, and honestly? valid.
๐ STATUS CONTROL ๐
the instructions in this section directly manipulate ST. no middlemen, no vibes, just straight up status edits. ๐
status_ok
this instruction sets ST to true, recording a success. we love a win. ๐
status_fail
this instruction sets ST to false, recording a failure. it happens, we move. ๐ญ
status_negate
this instruction negates ST, turning a failure into a success and vice versa. flip the narrative, main character energy. ๐
๐ LOCATION HANDLING ๐
the instructions in this section access CL and LS. yes, the position girlies. ๐ฆ
loc_push
this instruction makes a copy of CL and pushes it on LS. save point, bestie. ๐พ
loc_pop_discard
this instruction pops the last saved location from LS. and then it's justโฆ gone. no takebacks. ๐
loc_pop_rewind
this instruction pops the last saved location from LS and restores it as CL. full rewind, time travel era. โช
๐ธ NONTERMINAL EXECUTION ๐ธ
the instructions in this section access and manipulate NC. the cache. the memory. the archive. ๐
symbol_restore symbol
this instruction checks if NC contains data for the nonterminal symbol at CL, or not. the result of the instruction is a boolean flag, with True indicating that data was found in the cache. in that case the instruction has further updated the architectural state of the machine with the cached information, namely CL, ST, ER, and SV. ๐
the method with which the instruction's result is transformed into control flow is left undefined and the responsibility of the implementation. so like, the machine hands you the flag and goes "idk bestie, you figure out the jumps." ๐
symbol_save symbol
this instruction saves the current settings of CL, ST, ER, and SV in NC, using the pair of nonterminal symbol and the last location saved in LS as key. โจ
๐ VALUE CONSTRUCTION ๐
the instructions in this section manipulate SV. ๐
value_clear
this instruction clears SV. clean slate, fresh start, we love a reset. ๐ง
value_leaf symbol
this instruction constructs an AST node for symbol covering the range of IN from one character after the last location saved on LS to CL and stores it in SV. ... ๐ซ
value_reduce symbol
this instruction generally behaves like value_nonterminal_leaf, except that it takes all AST nodes on ARS, if any, and makes them the children of the new node, with the last node saved on ARS becoming the right-most / last child. note that ARS is not modified by this operation. ๐
๐ฆ AST CONSTRUCTION ๐ฆ
the instructions in this section manipulate ARS and AS. the tree-building department, truly the most glam. ๐ณ
ast_value_push
this instruction makes a copy of SV and pushes it on ARS. ๐
ast_push
this instruction pushes the current state of ARS on AS and then clears ARS. ๐ธ
ast_pop_rewind
this instruction pops the last entry saved on AS and restores it as the new state of ARS. โช
ast_pop_discard
this instruction pops the last entry saved on AS. and that's all it does. no restore, no rewind, just pop. ๐
๐ CONTROL FLOW ๐
normally this section would contain the specifications of the control flow instructions of the PARAM, i.e. (un)conditional jumps and the like. however, this part of the PARAM is intentionally left unspecified. ๐ซถ this allows the implementations to freely choose how to implement control flow. it's giving "we trust you, do your thing."
the implementation of this machine in Parser Tools, i.e. the package pt::rde, is not only coded in Tcl, but also relies on Tcl commands to provide it with control flow (instructions). ๐
โจ
๐ฎ INTERACTION OF THE INSTRUCTIONS WITH THE ARCHITECTURAL STATE ๐ฎ
okay bestie, this is the โจreceiptsโจ section โ the big ol' map of which instruction touches which piece of the architectural state. every row reads like this: the instruction on the left, then inputs -> outputs, and the empty spot before the arrow means it eats nothing and just spawns a value out of vibes. no cap, it's just the table but make it cute.
๐ณ ast โ the tree stack
ast_pop_discardโASโASast_pop_rewindโASโAS,ARSast_pushโARS,ASโASast_value_pushโSV,ARSโARS
๐ error handling
error_clearโ nothing in โERouterror_nonterminalsym โER,LSโERerror_pop_mergeโES,ERโERerror_pushโES,ERโES
๐ input
input_nextmsg โINโTC,CL,CC,ST,ER
๐ locations
loc_pop_discardโLSโLSloc_pop_rewindโLSโLS,CLloc_pushโCL,LSโLS
โญ status
status_failโ nothing in โSToutstatus_negateโSTโSTstatus_okโ nothing in โSTout
๐ง symbols
symbol_restoresym โNCโCL,ST,ER,SVsymbol_savesym โCL,ST,ER,SV,LSโNC
๐งช tests (the character class girlies)
all of these snack on CC and serve up ST, ER:
test_alnumโCCโST,ERtest_alphaโCCโST,ERtest_asciiโCCโST,ERtest_charchar โCCโST,ERtest_ddigitโCCโST,ERtest_digitโCCโST,ERtest_graphโCCโST,ERtest_lowerโCCโST,ERtest_printโCCโST,ERtest_punctโCCโST,ERtest_rangechars char โCCโST,ERtest_spaceโCCโST,ERtest_upperโCCโST,ERtest_wordcharโCCโST,ERtest_xdigitโCCโST,ER
๐ values
value_clearโ nothing in โSVoutvalue_leafsymbol โLS,CLโSVvalue_reducesymbol โARS,LS,CLโSV
๐ BUGS, IDEAS, FEEDBACK ๐
bestie. listen. this document, and the whole package it describes, is undoubtedly gonna have bugs and other problems. that's just how it is, no shame in it. so like, please report them โ in the category pt of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. and don't just report the bad stuff, okay?? if you have ideas for enhancements for either the package and/or the documentation, those go there too. we want them. ๐ซถ
when you propose code changes, please provide unified diffs, i.e. the output of diff -u.
also!! attachments are strongly preferred over inlined patches. you can attach stuff 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. it's giving "one extra click for a much happier maintainer" energy. do it for him. ๐
๐ 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, virtual machine
๐ท๏ธ CATEGORY ๐ท๏ธ
Parsing and Grammars
๐ COPYRIGHT ๐
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's a wrap on pt::param(n), bestie โ brought to you by tcllib, version 1.1. go forth and parse, you absolute icon. ๐๐โจ
๐ฆ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 ยท TCL
- fedora 44: tcllib 2.0-4.fc44 ยท TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- 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