-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-wc.tcl
executable file
·120 lines (88 loc) · 1.9 KB
/
scan-wc.tcl
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
#! /bin/sh
# \
exec tclsh8.3 "$0" ${1+"$@"}
#
# simple scanner that counts lines, words and characters: wc equivalent
#
lappend auto_path [file join [file dirname [info script]] .. ..]
package require ylex
#
# initialize scanner generator
#
set scannergenerator [yeti::ylex #auto -name wc]
$scannergenerator code public {
variable lines 0
variable words 0
variable chars 0
}
$scannergenerator code reset {
set lines 0
set words 0
set chars 0
}
$scannergenerator add {[[:alnum:][:punct:][:graph:]]+} {
incr words
incr chars [string length $yytext]
}
$scannergenerator add {\n} {
incr lines
incr chars
}
$scannergenerator add {.} {
incr chars
}
#
# Generate new Scanner
#
set scannertext [$scannergenerator dump]
if {1} {
set f [open "scan-wc-app.tcl" w]
puts $f $scannertext
close $f
}
eval $scannertext
rename $scannergenerator ""
#
# Create instance of scanner
#
set scanner [wc #auto]
#
# process file from command line, or myself
#
if {[llength $argv] == 0} {
lappend argv [info script]
}
set totlines 0
set totwords 0
set totchars 0
foreach filename $argv {
if {[catch {set file [open $filename]} res]} {
puts "cannot open $filename: $res"
continue
}
if {[catch {set text [read $file]} res]} {
puts "cannot read from $filename: $res"
close $file
continue
}
close $file
#
# run the scanner on this file
#
$scanner start $text
$scanner run
set lines [$scanner cget -lines]
set words [$scanner cget -words]
set chars [$scanner cget -chars]
puts [format "%7d %7d %7d %s" $lines $words $chars $filename]
incr totlines $lines
incr totwords $words
incr totchars $chars
$scanner reset
}
if {[llength $argv] > 1} {
puts [format "%7d %7d %7d %s" $totlines $totwords $totchars total]
}
rename $scanner ""
#puts "tcl++ [package present tcl++]"
#puts "itcl [package present Itcl]"