File: common

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 (426 lines) | stat: -rw-r--r-- 12,179 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
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
# -*- tcl -*-
# Code common to the various control files.
#
# Copyright (c) 2009-2014,2024 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.

# -------------------------------------------------------------------------

# Similar to TestFiles in devtools/testutilities.tcl, but not
# identical.  Here we do not expect source'able test suites, but data
# files, organized in sections under a main directory.

proc TestFilesProcess {maindir section inset outset -> nv lv iv dv ev script {optionalok 0}} {
    upvar 1 $nv n $lv label $dv data $ev expected $iv inputfile

    set pattern $maindir/$section/$inset/*

    set files [TestFilesGlob $pattern]
    if {![llength $files]} {
	if {$optionalok} return
	return -code error "No files matching \"$pattern\""
    }
    foreach src $files {
	if {[string match *README* $src]} continue
	if {[file isdirectory      $src]} continue

	set srcname  [file tail $src]
	set exp      [localPath $maindir]/$section/$outset/$srcname
	set data     [fileutil::cat -translation binary -encoding utf-8 $src]
	set expected [string trim [fileutil::cat -translation binary -encoding utf-8 $exp]]
	set expected [string map [list \
				      @sak   @sak \
				      @line  @line \
				      {@ %d} {@ %d} \
				      {@ %p} {@ %p} \
				      @ $::tcltest::testsDirectory] $expected]

	regexp -- {^([0-9]+)}    $srcname -> n
	regsub -all -- {^[0-9]+} $srcname {} label

	scan $n %d n
	set label [string trim [string map {_ { }} $label]]
	set inputfile $src

	uplevel 1 $script
    }
    return
}


proc TestFilesProcessIn {maindir section inset -> nv lv iv dv script} {
    upvar 1 $nv n $lv label $dv data $iv inputfile

    set pattern $maindir/$section/$inset/*

    set files [TestFilesGlob $pattern]
    if {![llength $files]} {
	return -code error "No files matching \"$pattern\""
    }
    foreach src $files {
	if {[string match *README* $src]} continue
	if {[file isdirectory      $src]} continue

	set srcname  [file tail $src]
	set data     [fileutil::cat -translation binary -encoding utf-8 $src]

	regexp -- {^([0-9]+)}    $srcname -> n
	regsub -all -- {^[0-9]+} $srcname {} label

	scan $n %d n
	set label [string trim [string map {_ { }} $label]]
	set inputfile $src

	uplevel 1 $script
    }
    return
}

# -------------------------------------------------------------------------

proc setup_plugins {} {
    global env

    array_unset env LANG*
    array_unset env LC_*
    set env(LANG) C ; # Usually default if nothing is set, OS X requires this.

    set paths [join [list \
			 [tcllibPath grammar_peg] \
			 [tcllibPath struct] \
			 [tcllibPath json] \
			 [tcllibPath textutil] \
			] \
		   [expr {$::tcl_platform(platform) eq "windows" ? ";" : ":"}]]

    # Initialize the paths an import plugin manager should use when
    # searching for an import plugin used by the code under test, and
    # also provide the paths enabling the import plugins to find their
    # supporting packages as well.

    set env(GRAMMAR_PEG_IMPORT_PLUGINS) $paths

    # Initialize the paths an export plugin manager should use when
    # searching for an export plugin used by the code under test, and
    # also provide the paths enabling the export plugins to find their
    # supporting packages as well.

    set env(GRAMMAR_PEG_EXPORT_PLUGINS) $paths

    return
}

# -------------------------------------------------------------------------

proc stripcomments {text} {
    set pattern {[[:space:]]*\[comment[[:space:]][[:space:]]*\{[^\}]*\}[[:space:]]*\][[:space:]]*}
    regsub -all -- $pattern $text {} text
    return $text
}

proc striphtmlcomments {text {n {}}} {
    set pattern {<!--.*?-->}
    if {$n eq {}} {
	regsub -all -- $pattern $text {} text
    } else {
	while {$n} {
	    regsub -- $pattern $text {} text
	    incr n -1
	}
    }
    return $text
}

proc stripmanmacros {text} {
    return [string map [list \n[pt::nroff::man_macros::contents] {}] $text]
}

proc stripnroffcomments {text {n {}}} {
#    return $text
    set pattern "'\\\\\"\[^\n\]*\n"
    if {$n eq {}} {
	regsub -all -- $pattern $text {} text
    } else {
	while {$n} {
	    regsub -- $pattern $text {} text
	    incr n -1
	}
    }
    return $text
}

# -------------------------------------------------------------------------

# Validate a serialization against the tree it
# was generated from.

proc validate_serial {t serial {rootname {}}} {
    if {$rootname == {}} {
	set rootname [$t rootname]
    }

    # List length is multiple of 3
    if {[llength $serial] % 3} {
	return serial/wrong#elements
    }

    # Scan through list and built a number helper
    # structures (arrays).

    array set a  {}
    array set p  {}
    array set ch {}
    foreach {node parent attr} $serial {
	# Node has to exist in tree
	if {![$t exists $node]} {
	    return node/$node/unknown
	}
	if {![info exists ch($node)]} {set ch($node) {}}
	# Parent reference has to be empty or
	# integer, == 0 %3, >=0, < length serial
	if {$parent != {}} {
	    if {![string is integer -strict $parent]} {
		return node/$node/parent/no-integer/$parent
	    }
	    if {$parent % 3} {
		return node/$node/parent/not-triple/$parent
	    }
	    if {$parent < 0} {
		return node/$node/parent/out-of-bounds/$parent
	    }
	    if {$parent >= [llength $serial]} {
		return node/$node/parent/out-of-bounds/$parent
	    }
	    # Resolve parent index into node name, has to match
	    set parentnode [lindex $serial $parent]
	    if {![$t exists $parentnode]} {
		return node/$node/parent/unknown/$parent/$parentnode
	    }
	    if {![string equal [$t parent $node] $parentnode]} {
		return node/$node/parent/mismatch/$parent/$parentnode/[$t parent $node]
	    }
	    lappend ch($parentnode) $node
	} else {
	    set p($node) {}
	}
	# Attr list has to be of even length.
	if {[llength $attr] % 2} {
	    return attr/$node/wrong#elements
	}
	# Attr have to exist and match in all respects
	if {![string equal \
		[dictsort $attr] \
		[dictsort [$t getall $node]]]} {
	    return attr/$node/mismatch
	}
    }
    # Second pass, check that the children information is encoded
    # correctly. Reconstructed data has to match originals.

    foreach {node parent attr} $serial {
	if {![string equal $ch($node) [$t children $node]]} {
	    return node/$node/children/mismatch
	}
    }

    # Reverse check
    # - List of nodes from the 'rootname' and check
    #   that it and all its children are present
    #   in the structure.

    set ::FOO {}
    $t walk $rootname n {walker $n}

    foreach n $::FOO {
	if {![info exists ch($n)]} {
	    return node/$n/mismatch/reachable/missing
	}
    }
    if {[llength $::FOO] != [llength $serial]/3} {
	return structure/mismatch/#nodes/multiples
    }
    if {[llength $::FOO] != [array size ch]} {
	return structure/mismatch/#nodes/multiples/ii
    }
    return ok
}

# Callbacks for tree walking.
# Remember the node in a global variable.

proc walker {node} {
    lappend ::FOO $node
}

proc match_tree {ta tb} {
    match_node $ta [$ta rootname] $tb [$tb rootname]
    return
}

proc match_node {ta a tb b} {
    if {[dictsort [$ta getall $a]] ne [dictsort [$tb getall $b]]} {
	return -code error "$ta/$a at $tb/$b, attribute mismatch (([dictsort [$ta getall $a]]) ne ([dictsort [$tb getall $b]]))"
    }
    if {[llength [$ta children $a]] != [llength [$tb children $b]]} {
	return -code error "$ta/$a at $tb/$b, children mismatch"
    }
    foreach ca [$ta children $a] cb [$tb children $b] {
	match_node $ta $ca $tb $cb
    }
    return
}

# -------------------------------------------------------------------------
## Dynamically create a parser for a PE grammar stored in a string.
## Different types:
## - critcl    -- Run through critcl tool for compilation at test time.
## - oo
## - container -- interpreter loaded from a container
## - snit

proc show-p {name} { set ver [package provide $name] ; puts "#%% $name $ver = [package ifneeded $name $ver]" }

proc make-parser {format glabel grammar} {
    global pcounter
    if {![info exist pcounter]} { set pcounter 0 }

    set debug 0
    set keep  0

    # should be preloaded by test suite.
    if {[catch {
	package present pt::pgen
    }]} {
	error "pt::pgen package required and not loaded. Please fix your testsuite."
    }

    # Options per format.
    # container :        -name
    # critcl    : -class -name
    # oo        : -class -name
    # snit      : -class -name

    set gc GC[incr pcounter]
    lappend cmd pt::pgen peg $grammar $format -name G
    if {$format ne "container"} {
	lappend cmd -class $gc
    }

    if {$debug} {
	puts "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CMD $cmd"
	puts "#%% [info nameofexecutable] [info patchlevel]"
	show-p critcl
	show-p pt::pgen
	show-p pt::cparam::configuration::critcl
    }
    
    try {
	set code [eval $cmd]
    } trap {PT RDE SYNTAX} {e o} {
	error [pt::util error2readable $e $grammar]
    }

    # debugging generator output
    if {$debug} {
	set   k [expr {$keep ? [open $gc$format w] : "stdout"}]
	puts $k "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $format ($glabel)"
	puts $k $code
	puts $k "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $format ($glabel)"
	if {$keep} { close $k }
    }

    # Now do format-specific post-processing of the generated code to
    # get a proper parser object.

    switch -exact -- $format {
	container {
	    # should be preloaded by test suite.
	    if {[catch {
		package present pt::peg::interp
	    }]} {
		error "pt::peg::interp package required and not loaded. Please fix your testsuite."
	    }

	    # Instantiate container class (transient).
	    eval $code
	    # Instantiate container (transient).
	    set c [G %AUTO%]

	    # Instantiate PEG interpreter, and configure with grammar in container.
	    set p [pt::peg::interp %AUTO%]
	    $p use $c

	    # Clean up the transient pieces (container class and instance).
	    $c destroy
	    G destroy
	}
	snit {
	    # Instantiate the parser class.
	    # ATTENTION: We chop the last 2 lines of the code first,
	    # unwanted "package provide" and "return" commands.
	    eval [join [lrange [split $code \n] 0 end-2] \n]

	    # Instantiate a parser based on the class.
	    set p [$gc %AUTO%]
	    # Note: Cannot destroy class now, would destroy instance as well.
	}
	oo {
	    # Instantiate the parser class.
	    # ATTENTION: We chop the last 2 lines of the code first,
	    # unwanted "package provide" and "return" commands.
	    eval [join [lrange [split $code \n] 0 end-2] \n]

	    # Instantiate a parser based on the class.
	    set p [$gc new]
	    # Note: Cannot destroy class now, would destroy instance as well.
	}
	critcl {
	    # Instantiate the parser class.
	    #
	    # ATTENTION: We first remove the last line of the code, it
	    # contains an unwanted "return" command.
	    #
	    # ATTENTION: We muck with [info script] to distinguish the
	    # multiple parsers going through this file and procedure
	    # from each other. Without doing this they would all map
	    # to the same file and critcl bailing on us for code
	    # redefinition after a compile & link for that file.

	    set here [info script]
	    info script $gc

	    eval [join [lrange [split $code \n] 0 end-1] \n]

	    # Above invoked critcl's collection of the C fragments.
	    # We have made sure (in "pt_pgen.test" and
	    # "tests/pt_pgen.tests") that the critcl package is
	    # available (we use it in run&compile mode).

	    # Hidden in the execution of the command instantiating the
	    # parser is the compilation, link and load of the C
	    # pieces, via $auto_index() and [unknown].

	    if {$debug} {
		parray ::auto_index GC*
	    }
	    
	    set p [${gc}::${gc}_critcl]

	    info script $here
	}
    }

    # Provide parser instance.
    if {$debug} {
	puts "P = ($p)"
	puts "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $format ($glabel)"
	puts "** [join [info loaded] "\n** "]"
	puts "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% $format ($glabel)"
	puts %%
	puts %%%
    }
    return $p
}

# -------------------------------------------------------------------------
return