pt_parse_peg(3tcl)
β¨ pt_parse_peg β the PEG parser bestie β¨
okay so this is the pt_parse_peg page, straight out of the 3tcl manual, and it lives in the Parser Tools family. it's giving "grammar parser, but make it a class". let's go π
πΈ NAME πΈ
pt_parse_peg β Parser Tools PEG Parser. that's the whole name, periodt.
π SYNOPSIS π
package require Tcl 8.5 9
package require pt::parse::peg 1
pt::parse::peg ?objectName?
objectName destroy
objectName parse chan
objectName parset text
look at that little lineup. so cute. so powerful. π«Ά
π¦ DESCRIPTION π¦
okay bestie, real talk: are you lost? do you have trouble understanding this document? no shame, it happens. in that case please read the overview provided by the Introduction to Parser Tools. that document is literally the entrypoint to the whole system this package is a part of. iykyk.
so what does this package do? it provides a class whose instances are parsers for parsing expression grammars in textual form. yes. a whole class. a whole parser. iconic behavior. π
π CLASS API π
pt::parse::peg ?objectName?β the class command, and it constructs parser instances, aka objects. the result of the command is the fully-qualified name of the instance command. so you always know exactly who you just birthed into existence.
if no objectName is specified, the class will generate and use an automatic name. if the objectName was specified, but is not fully qualified, the command will be created in the current namespace. no cap.
π INSTANCES API π
all parser instances provide at least the methods shown below. like, at minimum. the bare minimum. slay.
objectName destroyβ this method destroys the parser instance, releasing all claimed memory and other resources, and deleting the instance command. it's a whole farewell arc π.
the result of the command is the empty string. nothing. void. the silence after the goodbye.
objectName parse chanβ this method runs the parser using the contents of chan as input, starting at the current location in the channel, until parsing is not possible anymore. that's either because parsing has completed, or because it ran into a syntax error. two endings, both valid.
note here that the Parser Tools are based on Tcl 8.5+. in other words, the chan argument is not restricted to files, sockets, etc. we have the full power of reflected channels available. the whole buffet. π
it should also be noted that the parser pulls the characters from the input stream as it needs them. so if a parser created by this package has to be operated in a push aka event-driven manner, it will be necessary to go to Tcl 8.6+ and use the coroutine::auto to wrap it into a coroutine where read is properly changed for push-operation. okay? okay. noted. π
upon successful completion the command returns an abstract syntax tree as its result. this AST is in the form specified in the section AST serialization format. as a plain nested Tcl-list it can then be processed with any Tcl commands the user likes β doing transformations, semantic checks, etc. you do you, bestie.
to help in this, the package pt::ast provides a set of convenience commands for:
- validation of the tree's basic structure
- printing it for debugging
- walking it either from the bottom up, or top down
when encountering a syntax error, the command will throw an error instead. π this error will be a 4-element Tcl-list, containing, in the order listed below:
- the string
pt::rde, identifying it as parser runtime error. - the location of the parse error, as character offset from the beginning of the parsed input.
- the location of parse error, now as a 2-element list containing line-number and column in the line.
- a set of atomic parsing expressions indicating (encoding) the characters and/or nonterminal symbols the parser expected to see at the location of the parse error, but did not get. for the specification of atomic parsing expressions please see the section PE serialization format.
so yeah. four little guys telling you exactly what went wrong. we love a detailed error. π
objectName parset textβ this method runs the parser using the string in text as input. in all other ways it behaves like the methodparse, shown above. same energy, different input source. π«
π§ BUGS, IDEAS, FEEDBACK π§
this document, and the package it describes, will undoubtedly contain bugs and other problems. not me being honest about it π. 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 the package and/or the documentation. we want the glow-up. β¨
when proposing code changes, please provide unified diffs, i.e. the output of diff -u. that's the vibe.
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. it's right there. you got this. π«Ά
π 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
(that's a lot of keywords and honestly every single one of them ate π₯)
β CATEGORY β
Parsing and Grammars
π COPYRIGHT π
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
okay that's the whole page, bestie. this one came out of tcllib version 1.1, and yeah, the page you just read is literally named pt_parse_peg(3tcl). no cap. go parse something gorgeous. πβ¨π¦
π¦ 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