File: pt_peg_to_container.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (345 lines) | stat: -rw-r--r-- 8,572 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# peg_to_container.tcl --
#
#	Conversion from PEG to CONTAINER (Tcl code).
#
# Copyright (c) 2009 Andreas Kupries <andreas_kupries@sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# 
# RCS: @(#) $Id: pt_peg_to_container.tcl,v 1.1 2010/03/26 05:07:24 andreas_kupries Exp $

# This package takes the canonical serialization of a parsing
# expression grammar and produces text in CONTAINER format, a form
# of Tcl code which defines a snit::type whose instances store the
# converted grammar.

# ### ### ### ######### ######### #########
## Requisites

package require Tcl 8.5 9
package require pt::peg ; # Verification that the input is
				     # proper.
package require pt::pe             ; # Conversion of expressions.
package require text::write        ; # Text generation support
package require char               ; # Character quoting needed for
				     # the Tcl code to be correct.

# ### ### ### ######### ######### #########
##

namespace eval ::pt::peg::to::container {
    namespace export \
	reset configure convert

    namespace ensemble create
}

# ### ### ### ######### ######### #########
## API.

proc ::pt::peg::to::container::reset {} {
    variable template @code@
    variable mode     bulk
    variable name     a_pe_grammar
    variable file     unknown
    variable user     unknown
    return
}

proc ::pt::peg::to::container::configure {args} {
    variable template
    variable mode
    variable name
    variable file
    variable user

    if {[llength $args] == 0} {
	return [list \
		    -file     $file \
		    -mode     $mode \
		    -name     $name \
		    -template $template \
		    -user     $user]
    } elseif {[llength $args] == 1} {
	lassign $args option
	set variable [string range $option 1 end]
	if {[info exists $variable]} {
	    return [set $variable]
	} else {
	    return -code error "Expected one of -file, -mode, -name, -template, or -user, got \"$option\""
	}
    } elseif {[llength $args] % 2 == 0} {
	foreach {option value} $args {
	    set variable [string range $option 1 end]
	    if {![info exists $variable]} {
		return -code error "Expected one of -file, -mode, -name, -template, or -user, got \"$option\""
	    }
	}
	foreach {option value} $args {
	    set variable [string range $option 1 end]
	    switch -exact -- $variable {
		mode {
		    if {$value ni {bulk incremental}} {
			return -code error "Expected bulk, or incremental, got \"$value\""
		    }
		}
		template {
		    if {$value eq {}} {
			return -code error "Expected template, got the empty string"
		    }
		}
		name -
		file -
		user { }
	    }
	    set $variable $value
	}
    } else {
	return -code error {wrong#args, expected option value ...}
    }
}

proc ::pt::peg::to::container::convert {serial} {
    variable user
    variable file
    variable name
    variable mode
    variable template

    ::pt::peg verify-as-canonical $serial

    # TODO :: Reformat expressions for line-length (wrapping)
    # TODO :: Reformat 'add' bulk symbols for line-length (wrapping).
    # TODO :: Generate a read-only container.

    # Unpack the serialization, known as canonical.
    array set peg $serial
    array set peg $peg(pt::grammar::peg)
    unset     peg(pt::grammar::peg)

    # Determine the field size for nonterminal symbol names.
    set smax [text::write maxlen [dict keys $peg(rules)]]

    # Assemble the output, various pieces.
    text::write reset
    StartExpression $peg(start)
    Rules           $peg(rules) $smax
    Type

    # At last retrieve the fully assembled code and integrate with the
    # chosen template.
    return [string map \
		[list \
		     @user@   $user \
		     @format@ CONTAINER \
		     @file@   $file \
		     @name@   $name \
		     @mode@   $mode \
		     @code@   [text::write get]] $template]

    # ### ### ### ######### ######### #########
}

# ### ### ### ######### ######### #########
## Internals

proc ::pt::peg::to::container::StartExpression {startexpression} {
    text::write clear

    text::write field   install myg using pt::peg::container {${selfns}::G}
    text::write /line

    text::write field   {$myg} start [Expression $startexpression]
    text::write /line

    text::write indent 4
    text::write store START
    return
}

proc ::pt::peg::to::container::Rules {rules smax} {
    variable mode
    text::write clear
    if {[llength $rules]} {
	text::write /line
	switch -exact -- $mode {
	    bulk        { BulkLoading        $rules $smax }
	    incremental { IncrementalLoading $rules $smax }
	}

	text::write field  return
	text::write /line

	text::write indent 4
    }
    text::write store RULES
    return
}

proc ::pt::peg::to::container::BulkLoading {rules smax} {
    # 2 phases. First reshuffle the input into bulk
    # dictionaries, then write them.

    foreach {symbol def} $rules {
	lassign $def _ is _ mode
	lappend symbols $symbol
	lappend modes   $symbol $mode
	lappend rhs     $symbol $is
    }

    text::write clear
    foreach {symbol mode} $modes {
	text::write fieldl $smax $symbol
	text::write field        $mode
	text::write /line
    }
    text::write indent 4
    text::write store MODES

    text::write clear
    foreach {symbol is} $rhs {
	text::write fieldl $smax $symbol
	text::write field        [Expression $is]
	text::write /line
    }
    text::write indent 4
    text::write store RULES

    # note - allow line wrapping, max length of line?
    text::write clear
    text::write field {$myg} {add  } {*}$symbols
    text::write /line

    text::write field {$myg} modes \{
    text::write /line

    text::write recall MODES

    text::write field \}
    text::write /line

    text::write field {$myg} rules \{
    text::write /line

    text::write recall RULES

    text::write field \}
    text::write /line
    return
}

proc ::pt::peg::to::container::IncrementalLoading {rules smax} {
    foreach {symbol def} $rules {
	lassign $def _ is _ mode

	text::write field        {$myg}
	text::write fieldl 5     add
	text::write fieldl $smax $symbol
	text::write /line

	text::write field        {$myg}
	text::write fieldl 5     mode
	text::write fieldl $smax $symbol
	text::write field        $mode
	text::write /line

	text::write field        {$myg}
	text::write fieldl 5     rule
	text::write fieldl $smax $symbol
	text::write field        [Expression $is]
	text::write /line

	text::write /line
    }
    return
}

proc ::pt::peg::to::container::TypeBody {} {
    text::write clear

    text::write field constructor "{}" \{
    text::write /line

    text::write recall START
    text::write recall RULES

    text::write field \}
    text::write /line

    text::write /line

    text::write field component myg
    text::write /line

    text::write field delegate method * to myg
    text::write /line

    text::write indent 4
    text::write store BODY
    return
}

proc ::pt::peg::to::container::Type {} {
    variable name

    TypeBody

    text::write clear

    text::write field snit::type $name \{
    text::write /line

    text::write recall BODY

    text::write field \}
    text::write /line
    return
}

proc ::pt::peg::to::container::Expression {pe} {
    return [list [pt::pe bottomup \
		      [namespace current]::Convert \
		      $pe]]
}

proc ::pt::peg::to::container::Convert {pe operator arguments} {
    if {$operator eq "t"} {
	return "$operator [char quote tcl [lindex $arguments 0]]"
    } elseif {$operator eq ".."} {
	lassign $arguments ca ce
	return "$operator [char quote tcl $ca] [char quote tcl $ce]"
    } else {
	return $pe
    }
    return -code error {INTERNAL ERROR}
}

# ### ### ### ######### ######### #########
## Configuration

namespace eval ::pt::peg::to::container {

    variable template @code@       ; # A string. Specifies how to
				     # embed the generated code into a
				     # larger frame- work (the
				     # template).
    variable mode     bulk         ; # enum (bulk,
				     # incremental). Chooses between
				     # code for bulk or incrementally
				     # loading of the grammar into its
				     # container.
    variable name     a_pe_grammar ; # String. Name of the grammar.
    variable file     unknown      ; # String. Name of the file or
				     # other entity the grammar came
				     # from.
    variable user     unknown      ; # String. Name of the user on
				     # which behalf the conversion has
				     # been invoked.
}

# ### ### ### ######### ######### #########
## Ready

package provide pt::peg::to::container 1.1
return