summaryrefslogtreecommitdiffstats
path: root/tcllib/support/devel/sak/validate/syntax.tcl
blob: 24e06d2a9506105c3ae484090066b8b636d9d989 (plain)
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# -*- tcl -*-
# (C) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
##
# ###

package require  sak::animate
package require  sak::feedback
package require  sak::color

getpackage textutil::repeat textutil/repeat.tcl
getpackage doctools         doctools/doctools.tcl

namespace eval ::sak::validate::syntax {
    namespace import ::textutil::repeat::blank
    namespace import ::sak::color::*
    namespace import ::sak::feedback::!
    namespace import ::sak::feedback::>>
    namespace import ::sak::feedback::+=
    namespace import ::sak::feedback::=
    namespace import ::sak::feedback::=|
    namespace import ::sak::feedback::log
    namespace import ::sak::feedback::summary
    rename summary sum
}

# ###

proc ::sak::validate::syntax {modules mode stem tclv} {
    syntax::run $modules $mode $stem $tclv
    syntax::summary
    return
}

proc ::sak::validate::syntax::run {modules mode stem tclv} {
    sak::feedback::init $mode $stem
    sak::feedback::first log  "\[ Syntax \] ======================================================"
    sak::feedback::first unc  "\[ Syntax \] ======================================================"
    sak::feedback::first fail "\[ Syntax \] ======================================================"
    sak::feedback::first warn "\[ Syntax \] ======================================================"
    sak::feedback::first miss "\[ Syntax \] ======================================================"
    sak::feedback::first none "\[ Syntax \] ======================================================"

    # Preprocessing of module names to allow better formatting of the
    # progress output, i.e. vertically aligned columns

    # Per module we can distinguish the following levels of
    # syntactic completeness and validity.

    # Rule completeness
    # - No package has pcx rules
    # - Some, but not all packages have pcx rules
    # - All packages have pcx rules
    #
    # Validity. Not of the pcx rules, but of the files in the
    # packages.
    # - Package has errors and warnings
    # - Package has errors, but no warnings.
    # - Package has no errors, but warnings.
    # - Package has neither errors nor warnings.

    # Progress report per module: Modules and packages it is working on.
    # Summary at module level:
    # - Number of packages, number of packages with pcx rules
    # - Number of errors, number of warnings.

    # Full log:
    # - Lists packages without pcx rules.
    # - Lists packages with errors/warnings.
    # - Lists the exact errors/warnings per package, and location.

    # Global preparation: Pull information about all packages and the
    # modules they belong to.

    Setup
    Count $modules
    MapPackages

    InitCounters
    foreach m $modules {
	# Skip tcllibc shared library, not a module.
	if {[string equal $m tcllibc]} continue

	InitModuleCounters
	!
	log "@@ Module $m"
	Head $m

	# Per module: Find all syntax definition (pcx) files inside
	# and process them. Further find all the Tcl files and process
	# them as well. We get errors, warnings, and determine the
	# package(s) they may belong to.

	# Per package: Have they pcx files claiming them? After that,
	# are pcx files left over (i.e. without a package)?

	ProcessAllPCX     $m
	ProcessPackages   $m
	ProcessUnclaimed
	ProcessTclSources $m $tclv
	ModuleSummary
    }

    Shutdown
    return
}

proc ::sak::validate::syntax::summary {} {
    Summary
    return
}

# ###

proc ::sak::validate::syntax::ProcessAllPCX {m} {
    !claims
    foreach f [glob -nocomplain [file join [At $m] *.pcx]] {
	ProcessOnePCX $f
    }
    return
}

proc ::sak::validate::syntax::ProcessOnePCX {f} {
    =file $f

    if {[catch {
	Scan [get_input $f]
    } msg]} {
	+e $msg
    } else {
        +claim $msg
    }

    return
}

proc ::sak::validate::syntax::ProcessPackages {m} {
    !used
    if {![HasPackages $m]} return

    foreach p [ThePackages $m] {
	+pkg $p
	if {[claimants $p]} {
	    +pcx $p
	} else {
	    nopcx $p
	}
    }
    return
}

proc ::sak::validate::syntax::ProcessUnclaimed {} {
    variable claims
    if {![array size claims]} return
    foreach p [lsort -dict [array names claims]] {
	foreach fx $claims($p) { +u $fx }
    }
    return
}

proc ::sak::validate::syntax::ProcessTclSources {m tclv} {
    variable tclchecker
    if {![llength $tclchecker]} return

    foreach t [modtclfiles $m] {
	# Ignore TeX files.
	if {[string equal [file extension $t] .tex]} continue

	=file $t
	set cmd [Command $t $tclv]
	if {[catch {Close [Process [open |$cmd r+]]} msg]} {
	    if {[string match {*child process exited abnormally*} $msg]} continue
	    +e $msg
	}
    }
    return
}

###

proc ::sak::validate::syntax::Setup {} {
    variable ip [interp create]

    # Make it mostly empty (We keep the 'set' command).

    foreach n [interp eval $ip [list ::namespace children ::]] {
	if {[string equal $n ::tcl]} continue
	interp eval $ip [list namespace delete $n]
    }
    foreach c [interp eval $ip [list ::info commands]] {
	if {[string equal $c set]}       continue
	if {[string equal $c if]}        continue
	if {[string equal $c rename]}    continue
	if {[string equal $c namespace]} continue
	interp eval $ip [list ::rename $c {}]
    }

    if {![package vsatisfies [package present Tcl] 8.6]} {
	interp eval $ip [list ::namespace delete ::tcl]
    }
    interp eval $ip [list ::rename namespace {}]
    interp eval $ip [list ::rename rename    {}]

    foreach m {
	pcx::register unknown
    } {
	interp alias $ip $m {} ::sak::validate::syntax::PCX/[string map {:: _} $m] $ip
    }
    return
}

proc ::sak::validate::syntax::Shutdown {} {
    variable ip
    interp delete $ip
    return
}

proc ::sak::validate::syntax::Scan {data} {
    variable ip
    variable pcxpackage
    while {1} {
	if {[catch {
	    $ip eval $data
	} msg]} {
	    if {[string match {can't read "*": no such variable} $msg]} {
		regexp  {can't read "(.*)": no such variable} $msg -> var
		log "@@ + variable \"$var\""
		$ip eval [list set $var {}]
		continue
	    }
	    return -code error $msg
	}
	break
    }
    return $pcxpackage
}

proc ::sak::validate::syntax::PCX/pcx_register {ip pkg} {
    variable pcxpackage $pkg
    return
}

proc ::sak::validate::syntax::PCX/unknown {ip args} {
    return 0
}

###

proc ::sak::validate::syntax::Process {pipe} {
    variable current
    set dst log
    while {1} {
	if {[eof  $pipe]} break
	if {[gets $pipe line] < 0} break

	set tline [string trim $line]
	if {[string equal $tline ""]} continue

	if {[string match scanning:* $tline]} {
	    log $line
	    continue
	}
	if {[string match checking:* $tline]} {
	    log $line
	    continue
	}
	if {[regexp {^([^:]*):(\d+) \(([^)]*)\) (.*)$} $tline -> path at code detail]} {
	    = "$current $at $code"
	    set dst code,$code
	    if {[IsError $code]} {
		+e $line
	    } else {
		+w $line
	    }
	}
	log $line $dst
    }
    return $pipe
}

proc ::sak::validate::syntax::IsError {code} {
    variable codetype
    variable codec
    if {[info exists codec($code)]} {
	return $codec($code)
    }

    foreach {p t} $codetype {
	if {![string match $p $code]} continue
	set codec($code) $t
	return $t
    }

    # We assume that codetype contains a default * pattern as the last
    # entry, capturing all unknown codes.
    +e INTERNAL
    exit
}

proc ::sak::validate::syntax::Command {t tclv} {
    # Unix. Construction of the pipe to run the tclchecker against a
    # single tcl file.

    set     cmd [Driver $tclv]
    lappend cmd $t

    #lappend cmd >@ stdout 2>@ stderr
    #puts <<$cmd>>

    return $cmd
}

proc ::sak::validate::syntax::Close {pipe} {
    close $pipe
    return
}

proc ::sak::validate::syntax::Driver {tclv} {
    variable tclchecker
    set cmd $tclchecker

    if {$tclv ne {}} { lappend cmd -use Tcl-$tclv }

    # Make all syntax definition files we may have available to the
    # checker for higher accuracy of its output.
    foreach m [modules] { lappend cmd -pcx [At $m] }

    # Memoize
    proc ::sak::validate::syntax::Driver {tclv} [list return $cmd]
    return $cmd
}

###

proc ::sak::validate::syntax::=file {f} {
    variable current [file tail $f]
    = "$current ..."
    return
}

###

proc ::sak::validate::syntax::!claims {} {
    variable    claims
    array unset claims *
    return
}

proc ::sak::validate::syntax::+claim {pkg} {
    variable current
    variable claims
    lappend  claims($pkg) $current
    return
}

proc ::sak::validate::syntax::claimants {pkg} {
    variable claims
    expr { [info exists claims($pkg)] && [llength $claims($pkg)] }
}


###

proc ::sak::validate::syntax::!used {} {
    variable    used
    array unset used *
    return
}

proc ::sak::validate::syntax::+use {pkg} {
    variable used
    variable claims
    foreach fx $claims($pkg) { set used($fx) . }
    unset claims($pkg)
    return
}

###

proc ::sak::validate::syntax::MapPackages {} {
    variable    pkg
    array unset pkg *

    !
    += Package
    foreach {pname pdata} [ipackages] {
	= "$pname ..."
	foreach {pver pmodule} $pdata break
	lappend pkg($pmodule) $pname
    }
    !
    =| {Packages mapped ...}
    return
}

proc ::sak::validate::syntax::HasPackages {m} {
    variable pkg
    expr { [info exists pkg($m)] && [llength $pkg($m)] }
}

proc ::sak::validate::syntax::ThePackages {m} {
    variable pkg
    return [lsort -dict $pkg($m)]
}

###

proc ::sak::validate::syntax::+pkg {pkg} {
    variable mtotal ; incr mtotal
    variable total  ; incr total
    return
}

proc ::sak::validate::syntax::+pcx {pkg} {
    variable mhavepcx ; incr mhavepcx
    variable havepcx  ; incr havepcx
    = "$pkg Ok"
    +use $pkg
    return
}

proc ::sak::validate::syntax::nopcx {pkg} {
    = "$pkg Bad"
    log "@@ WARN  No syntax definition: $pkg"
    return
}

###

proc ::sak::validate::syntax::+w {msg} {
    variable mwarnings ; incr mwarnings
    variable warnings  ; incr warnings
    variable current
    foreach {a b c} [split $msg \n] break
    log "@@ WARN  $current: [Trim $a] [Trim $b] [Trim $c]"
    return
}

proc ::sak::validate::syntax::+e {msg} {
    variable merrors ; incr merrors
    variable errors  ; incr errors
    variable current
    log "@@ ERROR $current $msg"
    return
}

proc ::sak::validate::syntax::+u {f} {
    variable used
    if {[info exists used($f)]} return
    variable munclaimed ; incr munclaimed
    variable unclaimed  ; incr unclaimed
    set used($f) .
    log "@@ WARN  Unclaimed syntax definition file: $f"
    return
}

###

proc ::sak::validate::syntax::Count {modules} {
    variable maxml 0
    !
    foreach m [linsert $modules 0 Module] {
	= "M $m"
	set l [string length $m]
	if {$l > $maxml} {set maxml $l}
    }
    =| "Validate syntax (code, and API definitions) ..."
    return
}

proc ::sak::validate::syntax::Head {m} {
    variable maxml
    += ${m}[blank [expr {$maxml - [string length $m]}]]
    return
}

###

proc ::sak::validate::syntax::InitModuleCounters {} {
    variable mtotal     0
    variable mhavepcx   0
    variable munclaimed 0
    variable merrors    0
    variable mwarnings  0
    return
}

proc ::sak::validate::syntax::ModuleSummary {} {
    variable mtotal
    variable mhavepcx
    variable munclaimed
    variable merrors
    variable mwarnings
    variable tclchecker

    set complete [F $mhavepcx]/[F $mtotal]
    set not      "! [F [expr {$mtotal - $mhavepcx}]]"
    set err      "E [F $merrors]"
    set warn     "W [F $mwarnings]"
    set unc      "U [F $munclaimed]"

    if {$munclaimed} {
	set unc [=cya $unc]
	>> unc
    }
    if {!$mhavepcx && $mtotal} {
	set complete [=red $complete]
	set not      [=red $not]
	>> none
    } elseif {$mhavepcx < $mtotal} {
	set complete [=yel $complete]
	set not      [=yel $not]
	>> miss
    }
    if {[llength $tclchecker]} {
	if {$merrors} {
	    set err  " [=red $err]"
	    set warn " [=yel $warn]"
	    >> fail
	} elseif {$mwarnings} {
	    set err " $err"
	    set warn " [=yel $warn]"
	    >> warn
	} else {
	    set err  " $err"
	    set warn " $warn"
	}
    } else {
	set err  ""
	set warn ""
    }

    =| "~~ $complete $not $unc$err$warn"
    return
}

###

proc ::sak::validate::syntax::InitCounters {} {
    variable total     0
    variable havepcx   0
    variable unclaimed 0
    variable errors    0
    variable warnings  0
    return
}

proc ::sak::validate::syntax::Summary {} {
    variable total
    variable havepcx
    variable unclaimed
    variable errors
    variable warnings
    variable tclchecker

    set tot   [F $total]
    set doc   [F $havepcx]
    set udc   [F [expr {$total - $havepcx}]]

    set unc   [F $unclaimed]
    set per   [format %6.2f [expr {$havepcx*100./$total}]]
    set uper  [format %6.2f [expr {($total - $havepcx)*100./$total}]]
    set err   [F $errors]
    set wrn   [F $warnings]

    if {$errors}    { set err [=red $err] }
    if {$warnings}  { set wrn [=yel $wrn] }
    if {$unclaimed} { set unc [=cya $unc] }

    if {!$havepcx && $total} {
	set doc [=red $doc]
	set udc [=red $udc]
    } elseif {$havepcx < $total} {
	set doc [=yel $doc]
	set udc [=yel $udc]
    }

    if {[llength $tclchecker]} {
	set sfx " ($tclchecker)"
    } else {
	set sfx " ([=cya {No tclchecker available}])"
    }

    sum ""
    sum "Syntax statistics$sfx"
    sum "#Packages:     $tot"
    sum "#Syntax def:   $doc (${per}%)"
    sum "#No syntax:    $udc (${uper}%)"
    sum "#Unclaimed:    $unc"
    if {[llength $tclchecker]} {
	sum "#Errors:       $err"
	sum "#Warnings:     $wrn"
    }
    return
}

###

proc ::sak::validate::syntax::F {n} { format %6d $n }

proc ::sak::validate::syntax::Trim {text} {
    regsub {^[^:]*:} $text {} text
    return [string trim $text]
}

###

proc ::sak::validate::syntax::At {m} {
    global distribution
    return [file join $distribution modules $m]
}

# ###

namespace eval ::sak::validate::syntax {
    # Max length of module names and patchlevel information.
    variable maxml 0

    # Counters across all modules
    variable total     0 ; # Number of packages overall.
    variable havepcx   0 ; # Number of packages with syntax definition (pcx) files.
    variable unclaimed 0 ; # Number of PCX files not claimed by a specific package.
    variable errors    0 ; # Number of errors found in all code.
    variable warnings  0 ; # Number of warnings found in all code.

    # Same counters, per module.
    variable mtotal     0
    variable mhavepcx   0
    variable munclaimed 0
    variable merrors    0
    variable mwarnings  0

    # Name of currently processed syntax definition or code file
    variable current ""

    # Map from packages to files claiming to define the syntax of their API.
    variable  claims
    array set claims {}

    # Set of files taken by packages, as array
    variable  used
    array set used {}

    # Map from modules to packages contained in them
    variable  pkg
    array set pkg {}

    # Transient storage used while collecting packages per syntax definition.
    variable pcxpackage {}
    variable ip         {}

    # Location of the tclchecker used to perform syntactic validation.
    variable tclchecker [auto_execok tclchecker]

    # Patterns for separation of errors from warnings
    variable codetype {
	warn*        0
	nonPort*     0
	pkgUnchecked 0
	pkgVConflict 0
	*            1
    }
    variable codec ; array  set codec {}
}

##
# ###

package provide sak::validate::syntax 1.0