File: me_tcl.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (521 lines) | stat: -rw-r--r-- 10,975 bytes parent folder | download | duplicates (9)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# -*- tcl -*-
# ### ### ### ######### ######### #########
## Package description

## Implementation of the ME virtual machine as a singleton, tied to
## Tcl for control flow and stack handling (except the AST stack).

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

# ### ### ### ######### ######### #########
## Implementation

namespace eval ::grammar::me::tcl {
    namespace export \
	init lc tok sv tokens ast \
	astall ctok nc next ord \
	\
	isv_clear              ict_advance        inc_save    \
	isv_terminal           ict_match_token    inc_restore \
	isv_nonterminal_leaf   ict_match_tokrange icl_get     \
	isv_nonterminal_range  ict_match_tokclass icl_rewind  \
	isv_nonterminal_reduce iok_ok      \
	ier_clear              iok_fail    \
	ier_get                iok_negate  \
	ier_expected           ias_push    \
	ier_nonterminal        ias_mark    \
	ier_merge              ias_pop2mark

    variable ok
}

# ### ### ### ######### ######### #########
## Implementation, API. Ensemble command.

proc ::grammar::me::tcl {cmd args} {
    # Dispatcher for the ensemble command.
    variable tcl::cmds
    return [uplevel 1 [linsert $args 0 $cmds($cmd)]]
}

namespace eval grammar::me::tcl {
    variable cmds

    # Mapping from cmd names to procedures for quick dispatch. The
    # objects will shimmer into resolved command references.

    array set cmds {
	init   ::grammar::me::tcl::init
	lc     ::grammar::me::tcl::lc
	tok    ::grammar::me::tcl::tok
	sv     ::grammar::me::tcl::sv
	tokens ::grammar::me::tcl::tokens
	ast    ::grammar::me::tcl::ast
	astall ::grammar::me::tcl::astall
	ctok   ::grammar::me::tcl::ctok
	nc     ::grammar::me::tcl::nc
	next   ::grammar::me::tcl::next
	ord    ::grammar::me::tcl::ord
    }
}

# ### ### ### ######### ######### #########
## API Implementation.

proc ::grammar::me::tcl::init {nxcmd {tokmap {}}} {
    variable next  $nxcmd
    variable as    {}
    variable ok    0
    variable error {}
    variable sv    {}
    variable loc  -1
    variable ct    {}
    variable tc    {}
    variable nc
    variable tokOrd
    variable tokUseOrd 0

    array unset nc     *
    array unset tokOrd *

    if {[llength $tokmap]} {
	if {[llength $tokmap] % 2 == 1} {
	    return -code error \
		    "Bad token order map, not a dictionary"
	}
	array set tokOrd $tokmap
	set tokUseOrd 1
    }
    return
}

proc ::grammar::me::tcl::lc {pos} {
    variable tc
    return [lrange [lindex $tc $pos] 2 3]
}

proc ::grammar::me::tcl::tok {from {to {}}} {
    variable tc
    if {$to == {}} {set to $from}
    return [lrange $tc $from $to]
}

proc ::grammar::me::tcl::tokens {} {
    variable tc
    return [llength $tc]
}

proc ::grammar::me::tcl::sv {} {
    variable sv
    return  $sv
}

proc ::grammar::me::tcl::ast {} {
    variable as
    return [lindex $as end]
}

proc ::grammar::me::tcl::astall {} {
    variable as
    return $as
}

proc ::grammar::me::tcl::ctok {} {
    variable ct
    return  $ct
}

proc ::grammar::me::tcl::nc {} {
    variable nc
    return [array get nc]
}

proc ::grammar::me::tcl::next {} {
    variable next
    return  $next
}

proc ::grammar::me::tcl::ord {} {
    variable tokOrd
    return [array get tokOrd]
}

# ### ### ### ######### ######### #########
## Terminal matching

proc ::grammar::me::tcl::ict_advance {msg} {
    # Inlined: Getch, Expected, ClearErrors

    variable ok
    variable error
    # ------------------------
    variable tc
    variable loc
    variable ct
    # ------------------------
    variable next
    # ------------------------

    # Satisfy from input cache if possible.
    incr loc
    if {$loc < [llength $tc]} {
	set ct [lindex $tc $loc 0]
	set ok 1
	set error {}
	return
    }

    # Actually read from the input, and remember
    # the information.

    # Read from buffer, and remember.
    # Note: loc is the instance variable.
    # This implicitly increments the location!

    set tokdata [uplevel \#0 $next]
    if {![llength $tokdata]} {
	set ok 0
	set error [list $loc [list $msg]]
	return
    } elseif {[llength $tokdata] != 4} {
	return -code error "Bad callback result, expected 4 elements"
    }

    lappend tc $tokdata
    set ct [lindex $tokdata 0]
    set ok    1
    set error {}
    return
}

proc ::grammar::me::tcl::ict_match_token {tok msg} {
    variable ct
    variable ok

    set ok [expr {$tok eq $ct}]

    OkFail $msg
    return
}

proc ::grammar::me::tcl::ict_match_tokrange {toks toke msg} {
    variable ct
    variable ok
    variable tokUseOrd
    variable tokOrd

    if {$tokUseOrd} {
	set ord $tokOrd($ct)
	set ok [expr {
	    ($toks <= $ord) &&
	    ($ord <= $toke)
	}] ; # {}
    } else {
	set ok [expr {
	    ([string compare $toks   $ct] <= 0) &&
	    ([string compare $ct   $toke] <= 0)
	}] ; # {}
    }

    OkFail $msg
    return
}

proc ::grammar::me::tcl::ict_match_tokclass {code msg} {
    variable ct
    variable ok

    set ok [string is $code -strict $ct]

    OkFail $msg
    return
}

proc ::grammar::me::tcl::OkFail {msg} {
    variable ok
    variable error
    variable loc

    # Inlined: Expected, Unget, ClearErrors

    if {!$ok} {
	set error [list $loc [list $msg]]
	incr loc -1
    } else {
	set error {}
    }
    return
}

# ### ### ### ######### ######### #########
## Nonterminal cache

proc ::grammar::me::tcl::inc_restore {symbol} {
    variable loc
    variable nc
    variable ok
    variable error
    variable sv

    # Satisfy from cache if possible.
    if {[info exists nc($loc,$symbol)]} {
	foreach {go ok error sv} $nc($loc,$symbol) break

	# Go forward, as the nonterminal matches (or not).
	set loc $go
	return 1
    }
    return 0
}

proc ::grammar::me::tcl::inc_save {symbol at} {
    variable loc
    variable nc
    variable ok
    variable error
    variable sv

    if 0 {
	if {[info exists nc($at,$symbol)]} {
	    return -code error "Cannot overwrite\
		    existing data @ ($at, $symbol)"
	}
    }

    # FIXME - end location should be argument.

    # Store not only the value, but also how far
    # the match went (if it was a match).

    set nc($at,$symbol) [list $loc $ok $error $sv]
    return
}

# ### ### ### ######### ######### #########
## Unconditional matching.

proc ::grammar::me::tcl::iok_ok {} {
    variable ok 1
    return
}

proc ::grammar::me::tcl::iok_fail {} {
    variable ok 0
    return
}

proc ::grammar::me::tcl::iok_negate {} {
    variable ok
    set ok [expr {!$ok}]
    return
}

# ### ### ### ######### ######### #########
## Basic input handling and tracking

proc ::grammar::me::tcl::icl_get {} {
    variable loc
    return  $loc
}

proc ::grammar::me::tcl::icl_rewind {oldloc} {
    variable loc

    if 0 {
	if {($oldloc < -1) || ($oldloc > $loc)} {
	    return -code error "Bad location \"$oldloc\" (vs $loc)"
	}
    }
    set loc $oldloc
    return
}

# ### ### ### ######### ######### #########
## Error handling.

proc ::grammar::me::tcl::ier_get {} {
    variable error
    return  $error
}

proc ::grammar::me::tcl::ier_clear {} {
    variable error {}
    return
}

proc ::grammar::me::tcl::ier_nonterminal {msg pos} {
    # Inlined: Errors, Expected.

    variable error

    if {[llength $error]} {
	foreach {l m} $error break
	incr pos
	if {$l == $pos} {
	    set error [list $l [list $msg]]
	}
    }
}

proc ::grammar::me::tcl::ier_merge {new} {
    variable error

    # We have either old or new error data, keep it.

    if {![llength $error]} {set error $new ; return}
    if {![llength $new]}   {return}

    # If one of the errors is further on in the input choose that as
    # the information to propagate.

    foreach {loe msgse} $error break
    foreach {lon msgsn} $new   break

    if {$lon > $loe} {set error $new ; return}
    if {$loe > $lon} {return}

    # Equal locations, merge the message lists.

    foreach m $msgsn {lappend msgse $m}
    set error [list $loe [lsort -uniq $msgse]]
    return
}

# ### ### ### ######### ######### #########
## Operations for the construction of the
## abstract syntax tree (AST).

proc ::grammar::me::tcl::isv_clear {} {
    variable sv {}
    return
}

proc ::grammar::me::tcl::isv_terminal {} {
    variable loc
    variable sv
    variable as

    set sv [list {} $loc $loc]
    lappend as $sv
    return
}

proc ::grammar::me::tcl::isv_nonterminal_leaf {nt pos} {
    # Inlined clear, reduce, and optimized.
    variable ok
    variable loc
    variable sv {}

    # Clear ; if {$ok} {Reduce $nt}

    if {$ok} {
	incr pos
	set sv [list $nt $pos $loc]
    }
    return
}

proc ::grammar::me::tcl::isv_nonterminal_range {nt pos} {
    variable ok
    variable loc
    variable sv {}

    if {$ok} {
	# TerminalString $pos
	# Get all characters after 'pos' to current location as terminal data.

	incr pos
	set sv [list $nt $pos $loc [list {} $pos $loc]]

	#set sv [linsert $sv 0 $nt] ;#Reduce $nt
    }
    return
}

proc ::grammar::me::tcl::isv_nonterminal_reduce {nt pos {mrk 0}} {
    variable ok
    variable as
    variable loc
    variable sv {}

    if {$ok} {
	incr pos
	set sv [lrange $as $mrk end]         ;#SaveToMark $mrk
	set sv [linsert $sv 0 $nt $pos $loc] ;#Reduce $nt
    }
    return
}

# ### ### ### ######### ######### #########
## AST stack handling

proc ::grammar::me::tcl::ias_push {} {
    variable as
    variable sv
    lappend as $sv
    return
}

proc ::grammar::me::tcl::ias_mark {} {
    variable as
    return [llength $as]
}

proc ::grammar::me::tcl::ias_pop2mark {mark} {
    variable as
    if {[llength $as] <= $mark} return
    incr mark -1
    set as [lrange $as 0 $mark]
    return
}

# ### ### ### ######### ######### #########
## Data structures.

namespace eval ::grammar::me::tcl {
    # ### ### ### ######### ######### #########
    ## Public State of MVM (Matching Virtual Machine)

    variable ok   0  ; # Boolean: Ok/Fail of last match operation.

    # ### ### ### ######### ######### #########
    ## Internal state.

    variable ct   {}  ; # Current token.
    variable loc  0   ; # Location of 'ct' as offset in input.

    variable error {} ; # Error data for last match.
    #                 ; # == List (loc, list of strings)
    #                 ; # or empty list
    variable sv   {}  ; # Semantic value for last match.

    # ### ### ### ######### ######### #########
    ## Data structures for AST construction

    variable as {} ; # Stack of values for AST

    # ### ### ### ######### ######### #########
    ## Memo data structures for tokens and match results.

    variable tc {}
    variable nc ; array set nc {}

    # ### ### ### ######### ######### #########
    ## Input buffer, location of next character to read.
    ## ASSERT (loc <= cloc)

    variable next   ; # Callback to get next character.

    # Token ordering for range checks. Optional

    variable tokOrd ; array set tokOrd {}
    variable tokUseOrd 0

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

# ### ### ### ######### ######### #########
## Package Management

package provide grammar::me::tcl 0.1