1grammar::peg::interp(n) Grammar operations and usage grammar::peg::interp(n)
2
3
4
5______________________________________________________________________________
6
8 grammar::peg::interp - Interpreter for parsing expression grammars
9
11 package require Tcl 8.4
12
13 package require grammar::mengine ?0.1?
14
15 package require grammar::peg::interp ?0.1.1?
16
17 ::grammar::peg::interp::setup peg
18
19 ::grammar::peg::interp::parse nextcmd errorvar astvar
20
21______________________________________________________________________________
22
24 This package provides commands for the controlled matching of a charac‐
25 ter stream via a parsing expression grammar and the creation of an ab‐
26 stract syntax tree for the stream and partials.
27
28 It is built on top of the virtual machine provided by the package gram‐
29 mar::me::tcl and directly interprets the parsing expression grammar
30 given to it. In other words, the grammar is not pre-compiled but used
31 as is.
32
33 The grammar to be interpreted is taken from a container object follow‐
34 ing the interface specified by the package grammar::peg::container.
35 Only the relevant parts are copied into the state of this package.
36
37 It should be noted that the package provides exactly one instance of
38 the interpreter, and interpreting a second grammar requires the user to
39 either abort or complete a running interpretation, or to put them into
40 different Tcl interpreters.
41
42 Also of note is that the implementation assumes a pull-type handling of
43 the input. In other words, the interpreter pulls characters from the
44 input stream as it needs them. For usage in a push environment, i.e.
45 where the environment pushes new characters as they come we have to put
46 the engine into its own thread.
47
49 The package exports the following API
50
51 ::grammar::peg::interp::setup peg
52 This command (re)initializes the interpreter. It returns the
53 empty string. This command has to be invoked first, before any
54 matching run.
55
56 Its argument peg is the handle of an object containing the pars‐
57 ing expression grammar to interpret. This grammar has to be
58 valid, or an error will be thrown.
59
60 ::grammar::peg::interp::parse nextcmd errorvar astvar
61 This command interprets the loaded grammar and tries to match it
62 against the stream of characters represented by the command pre‐
63 fix nextcmd.
64
65 The command prefix nextcmd represents the input stream of char‐
66 acters and is invoked by the interpreter whenever the a new
67 character from the stream is required. The callback has to re‐
68 turn either the empty list, or a list of 4 elements containing
69 the token, its lexeme attribute, and its location as line number
70 and column index, in this order. The empty list is the signal
71 that the end of the input stream has been reached. The lexeme
72 attribute is stored in the terminal cache, but otherwise not
73 used by the machine.
74
75 The result of the command is a boolean value indicating whether
76 the matching process was successful (true), or not (false). In
77 the case of a match failure error information will be stored
78 into the variable referenced by errorvar. The variable refer‐
79 enced by astvar will always contain the generated abstract syn‐
80 tax tree, however in the case of an error it will be only par‐
81 tial and possibly malformed.
82
83 The abstract syntax tree is represented by a nested list, as de‐
84 scribed in section AST VALUES of document grammar::me_ast.
85
87 This document, and the package it describes, will undoubtedly contain
88 bugs and other problems. Please report such in the category gram‐
89 mar_peg of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist].
90 Please also report any ideas for enhancements you may have for either
91 package and/or documentation.
92
93 When proposing code changes, please provide unified diffs, i.e the out‐
94 put of diff -u.
95
96 Note further that attachments are strongly preferred over inlined
97 patches. Attachments can be made by going to the Edit form of the
98 ticket immediately after its creation, and then using the left-most
99 button in the secondary navigation bar.
100
102 LL(k), TDPL, context-free languages, expression, grammar, matching,
103 parsing, parsing expression, parsing expression grammar, push down au‐
104 tomaton, recursive descent, state, top-down parsing languages, trans‐
105 ducer, virtual machine
106
108 Grammars and finite automata
109
111 Copyright (c) 2005-2011 Andreas Kupries <andreas_kupries@users.sourceforge.net>
112
113
114
115
116tcllib 0.1.1 grammar::peg::interp(n)