grammar::peg::interp(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– grammar::peg::interp β€” bestie edition πŸ’–

okay so this is a page from the Grammar operations and usage section of the manual, and it's literally about grammar::peg::interp, the interpreter for parsing expression grammars. sit down, grab a boba, we're doing this πŸ§‹βœ¨


🌸 NAME 🌸

grammar::peg::interp β€” the interpreter for parsing expression grammars, no cap πŸ’…


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require grammar::mengine ?0.1?

package require grammar::peg::interp ?0.1.2?

::grammar::peg::interp::setup peg

::grammar::peg::interp::parse nextcmd errorvar astvar

πŸ¦‹ DESCRIPTION πŸ¦‹

so this package hands you the commands to do controlled matching of a character stream against a parsing expression grammar, plus it builds you an abstract syntax tree for the stream and for partials. it's giving "i match your grammar and i also journal about it" πŸ““

it's built on top of the virtual machine that the package grammar::me::tcl provides, and it directly interprets the parsing expression grammar you give it. like, the grammar is not pre-compiled, it's just used as is. no glow-up required, she's already that girl πŸ’β™€οΈ

the grammar it interprets gets taken from a container object that follows the interface specified by the package grammar::peg::container. and listen β€” only the relevant parts get copied into the state of this package. we're not doing extra, we're doing enough πŸŽ€

⚠️ okay this part matters, bestie: the package provides exactly one instance of the interpreter. so if you wanna interpret a second grammar, you have to either abort or complete the running interpretation first, or shove them into different Tcl interpreters. one at a time, queen, we're not polyamorous with grammars 😭

also of note (not me being thorough πŸ’…): the implementation assumes a pull-type handling of the input. in other words, the interpreter pulls characters from the input stream as it needs them. for usage in a push environment β€” i.e. where the environment pushes new characters as they come β€” we have to put the engine into its own thread. go little engine, go live your life in a thread 🧚


πŸ‘‘ THE INTERPRETER API πŸ‘‘

the package exports the following API:

  • ::grammar::peg::interp::setup peg β€” this command (re)initializes the interpreter. it returns the empty string. this one has to be invoked first, before any matching run. its argument peg is the handle of an object containing the parsing expression grammar to interpret. this grammar has to be valid, or an error will be thrown. no invalid grammars allowed on this campus 🚫✨
  • ::grammar::peg::interp::parse nextcmd errorvar astvar β€” this command interprets the loaded grammar and tries to match it against the stream of characters represented by the command prefix nextcmd. the command prefix nextcmd represents the input stream of characters, and the interpreter invokes it whenever a new character from the stream is required. the callback has to return either the empty list, or a list of 4 elements containing the token, its lexeme attribute, and its location as line number and column index β€” in that order. the empty list is the signal that the end of the input stream has been reached. the lexeme attribute gets stored in the terminal cache, but otherwise it's not used by the machine (she's just there for vibes, respectfully πŸ™ˆ).

the result of the command is a boolean value indicating whether the matching process was successful (true) or not (false). in the case of a match failure, error information will be stored into the variable referenced by errorvar. the variable referenced by astvar will always contain the generated abstract syntax tree β€” however, in the case of an error it'll be only partial and possibly malformed. not ideal, but she tried 😭

the abstract syntax tree is represented by a nested list, as described in section AST VALUES of document grammar::me_ast. go read that one too, it's the sequel 🎬


πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

this document, and the package it describes, will undoubtedly contain bugs and other problems. like, obviously. please report such in the category grammar_peg of the Tcllib Trackers at [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 tea β˜•

when proposing code changes, please provide unified diffs, i.e. the output of diff -u.

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. do it like that, bestie, it's the move 🫢


⭐ KEYWORDS ⭐

LL(k), TDPL, context-free languages, expression, grammar, matching, parsing, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer, virtual machine


πŸŽ€ CATEGORY πŸŽ€

Grammars and finite automata


Copyright (c) 2005-2011 Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay and for the cute sign-off: this whole thing lives in tcllib, version 0.1.2, and it's filed under grammar::peg::interp(n). that's the vibe check, we passed it, periodt πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from