| 1 |
# regexpTestLib.tcl -- |
| 2 |
# |
| 3 |
# This file contains tcl procedures used by spencer2testregexp.tcl and |
| 4 |
# spencer2regexp.tcl, which are programs written to convert Henry |
| 5 |
# Spencer's test suite to tcl test files. |
| 6 |
# |
| 7 |
# Copyright (c) 1996 Sun Microsystems, Inc. |
| 8 |
|
| 9 |
proc readInputFile {} { |
| 10 |
global inFileName |
| 11 |
global lineArray |
| 12 |
|
| 13 |
set fileId [open $inFileName r] |
| 14 |
|
| 15 |
set i 0 |
| 16 |
while {[gets $fileId line] >= 0} { |
| 17 |
|
| 18 |
set len [string length $line] |
| 19 |
|
| 20 |
if {($len > 0) && ([string index $line [expr {$len - 1}]] eq "\\")} { |
| 21 |
if {[info exists lineArray(c$i)] == 0} { |
| 22 |
set lineArray(c$i) 1 |
| 23 |
} else { |
| 24 |
incr lineArray(c$i) |
| 25 |
} |
| 26 |
set line [string range $line 0 [expr {$len - 2}]] |
| 27 |
append lineArray($i) $line |
| 28 |
continue |
| 29 |
} |
| 30 |
if {[info exists lineArray(c$i)] == 0} { |
| 31 |
set lineArray(c$i) 1 |
| 32 |
} else { |
| 33 |
incr lineArray(c$i) |
| 34 |
} |
| 35 |
append lineArray($i) $line |
| 36 |
incr i |
| 37 |
} |
| 38 |
|
| 39 |
close $fileId |
| 40 |
return $i |
| 41 |
} |
| 42 |
|
| 43 |
# |
| 44 |
# strings with embedded @'s are truncated |
| 45 |
# unpreceeded @'s are replaced by {} |
| 46 |
# |
| 47 |
proc removeAts {ls} { |
| 48 |
set len [llength $ls] |
| 49 |
set newLs {} |
| 50 |
foreach item $ls { |
| 51 |
regsub @.* $item "" newItem |
| 52 |
lappend newLs $newItem |
| 53 |
} |
| 54 |
return $newLs |
| 55 |
} |
| 56 |
|
| 57 |
proc convertErrCode {code} { |
| 58 |
|
| 59 |
set errMsg "couldn't compile regular expression pattern:" |
| 60 |
|
| 61 |
if {[string compare $code "INVARG"] == 0} { |
| 62 |
return "$errMsg invalid argument to regex routine" |
| 63 |
} elseif {[string compare $code "BADRPT"] == 0} { |
| 64 |
return "$errMsg ?+* follows nothing" |
| 65 |
} elseif {[string compare $code "BADBR"] == 0} { |
| 66 |
return "$errMsg invalid repetition count(s)" |
| 67 |
} elseif {[string compare $code "BADOPT"] == 0} { |
| 68 |
return "$errMsg invalid embedded option" |
| 69 |
} elseif {[string compare $code "EPAREN"] == 0} { |
| 70 |
return "$errMsg unmatched ()" |
| 71 |
} elseif {[string compare $code "EBRACE"] == 0} { |
| 72 |
return "$errMsg unmatched {}" |
| 73 |
} elseif {[string compare $code "EBRACK"] == 0} { |
| 74 |
return "$errMsg unmatched \[\]" |
| 75 |
} elseif {[string compare $code "ERANGE"] == 0} { |
| 76 |
return "$errMsg invalid character range" |
| 77 |
} elseif {[string compare $code "ECTYPE"] == 0} { |
| 78 |
return "$errMsg invalid character class" |
| 79 |
} elseif {[string compare $code "ECOLLATE"] == 0} { |
| 80 |
return "$errMsg invalid collating element" |
| 81 |
} elseif {[string compare $code "EESCAPE"] == 0} { |
| 82 |
return "$errMsg invalid escape sequence" |
| 83 |
} elseif {[string compare $code "BADPAT"] == 0} { |
| 84 |
return "$errMsg invalid regular expression" |
| 85 |
} elseif {[string compare $code "ESUBREG"] == 0} { |
| 86 |
return "$errMsg invalid backreference number" |
| 87 |
} elseif {[string compare $code "IMPOSS"] == 0} { |
| 88 |
return "$errMsg can never match" |
| 89 |
} |
| 90 |
return "$errMsg $code" |
| 91 |
} |
| 92 |
|
| 93 |
proc writeOutputFile {numLines fcn} { |
| 94 |
global outFileName |
| 95 |
global lineArray |
| 96 |
|
| 97 |
# open output file and write file header info to it. |
| 98 |
|
| 99 |
set fileId [open $outFileName w] |
| 100 |
|
| 101 |
puts $fileId "# Commands covered: $fcn" |
| 102 |
puts $fileId "#" |
| 103 |
puts $fileId "# This Tcl-generated file contains tests for the $fcn tcl command." |
| 104 |
puts $fileId "# Sourcing this file into Tcl runs the tests and generates output for" |
| 105 |
puts $fileId "# errors. No output means no errors were found. Setting VERBOSE to" |
| 106 |
puts $fileId "# -1 will run tests that are known to fail." |
| 107 |
puts $fileId "#" |
| 108 |
puts $fileId "# Copyright (c) 1998 Sun Microsystems, Inc." |
| 109 |
puts $fileId "#" |
| 110 |
puts $fileId "# See the file \"license.terms\" for information on usage and redistribution" |
| 111 |
puts $fileId "# of this file, and for a DISCLAIMER OF ALL WARRANTIES." |
| 112 |
puts $fileId "#" |
| 113 |
puts $fileId "\# SCCS: \%Z\% \%M\% \%I\% \%E\% \%U\%" |
| 114 |
puts $fileId "\nproc print \{arg\} \{puts \$arg\}\n" |
| 115 |
puts $fileId "if \{\[string compare test \[info procs test\]\] == 1\} \{" |
| 116 |
puts $fileId " source defs ; set VERBOSE -1\n\}\n" |
| 117 |
puts $fileId "if \{\$VERBOSE != -1\} \{" |
| 118 |
puts $fileId " proc print \{arg\} \{\}\n\}\n" |
| 119 |
puts $fileId "#" |
| 120 |
puts $fileId "# The remainder of this file is Tcl tests that have been" |
| 121 |
puts $fileId "# converted from Henry Spencer's regexp test suite." |
| 122 |
puts $fileId "#\n" |
| 123 |
|
| 124 |
set lineNum 0 |
| 125 |
set srcLineNum 1 |
| 126 |
while {$lineNum < $numLines} { |
| 127 |
|
| 128 |
set currentLine $lineArray($lineNum) |
| 129 |
|
| 130 |
# copy comment string to output file and continue |
| 131 |
|
| 132 |
if {[string index $currentLine 0] eq "#"} { |
| 133 |
puts $fileId $currentLine |
| 134 |
incr srcLineNum $lineArray(c$lineNum) |
| 135 |
incr lineNum |
| 136 |
continue |
| 137 |
} |
| 138 |
|
| 139 |
set len [llength $currentLine] |
| 140 |
|
| 141 |
# copy empty string to output file and continue |
| 142 |
|
| 143 |
if {$len == 0} { |
| 144 |
puts $fileId "\n" |
| 145 |
incr srcLineNum $lineArray(c$lineNum) |
| 146 |
incr lineNum |
| 147 |
continue |
| 148 |
} |
| 149 |
if {($len < 3)} { |
| 150 |
puts "warning: test is too short --\n\t$currentLine" |
| 151 |
incr srcLineNum $lineArray(c$lineNum) |
| 152 |
incr lineNum |
| 153 |
continue |
| 154 |
} |
| 155 |
|
| 156 |
puts $fileId [convertTestLine $currentLine $len $lineNum $srcLineNum] |
| 157 |
|
| 158 |
incr srcLineNum $lineArray(c$lineNum) |
| 159 |
incr lineNum |
| 160 |
} |
| 161 |
|
| 162 |
close $fileId |
| 163 |
} |
| 164 |
|
| 165 |
proc convertTestLine {currentLine len lineNum srcLineNum} { |
| 166 |
|
| 167 |
regsub -all {(?b)\\} $currentLine {\\\\} currentLine |
| 168 |
set re [lindex $currentLine 0] |
| 169 |
set flags [lindex $currentLine 1] |
| 170 |
set str [lindex $currentLine 2] |
| 171 |
|
| 172 |
# based on flags, decide whether to skip the test |
| 173 |
|
| 174 |
if {[findSkipFlag $flags]} { |
| 175 |
regsub -all {\[|\]|\(|\)|\{|\}|\#} $currentLine {\&} line |
| 176 |
set msg "\# skipping char mapping test from line $srcLineNum\n" |
| 177 |
append msg "print \{... skip test from line $srcLineNum: $line\}" |
| 178 |
return $msg |
| 179 |
} |
| 180 |
|
| 181 |
# perform mapping if '=' flag exists |
| 182 |
|
| 183 |
set noBraces 0 |
| 184 |
if {[regexp {=|>} $flags] == 1} { |
| 185 |
regsub -all {_} $currentLine {\\ } currentLine |
| 186 |
regsub -all {A} $currentLine {\\007} currentLine |
| 187 |
regsub -all {B} $currentLine {\\b} currentLine |
| 188 |
regsub -all {E} $currentLine {\\033} currentLine |
| 189 |
regsub -all {F} $currentLine {\\f} currentLine |
| 190 |
regsub -all {N} $currentLine {\\n} currentLine |
| 191 |
|
| 192 |
# if and \r substitutions are made, do not wrap re, flags, |
| 193 |
# str, and result in braces |
| 194 |
|
| 195 |
set noBraces [regsub -all {R} $currentLine {\\\u000D} currentLine] |
| 196 |
regsub -all {T} $currentLine {\\t} currentLine |
| 197 |
regsub -all {V} $currentLine {\\v} currentLine |
| 198 |
if {[regexp {=} $flags] == 1} { |
| 199 |
set re [lindex $currentLine 0] |
| 200 |
} |
| 201 |
set str [lindex $currentLine 2] |
| 202 |
} |
| 203 |
set flags [removeFlags $flags] |
| 204 |
|
| 205 |
# find the test result |
| 206 |
|
| 207 |
set numVars [expr {$len - 3}] |
| 208 |
set vars {} |
| 209 |
set vals {} |
| 210 |
set result 0 |
| 211 |
set v 0 |
| 212 |
|
| 213 |
if {[regsub {\*} "$flags" "" newFlags] == 1} { |
| 214 |
# an error is expected |
| 215 |
|
| 216 |
if {[string compare $str "EMPTY"] == 0} { |
| 217 |
# empty regexp is not an error |
| 218 |
# skip this test |
| 219 |
|
| 220 |
return "\# skipping the empty-re test from line $srcLineNum\n" |
| 221 |
} |
| 222 |
set flags $newFlags |
| 223 |
set result "\{1 \{[convertErrCode $str]\}\}" |
| 224 |
} elseif {$numVars > 0} { |
| 225 |
# at least 1 match is made |
| 226 |
|
| 227 |
if {[regexp {s} $flags] == 1} { |
| 228 |
set result "\{0 1\}" |
| 229 |
} else { |
| 230 |
while {$v < $numVars} { |
| 231 |
append vars " var($v)" |
| 232 |
append vals " \$var($v)" |
| 233 |
incr v |
| 234 |
} |
| 235 |
set tmp [removeAts [lrange $currentLine 3 $len]] |
| 236 |
set result "\{0 \{1 $tmp\}\}" |
| 237 |
if {$noBraces} { |
| 238 |
set result "\[subst $result\]" |
| 239 |
} |
| 240 |
} |
| 241 |
} else { |
| 242 |
# no match is made |
| 243 |
|
| 244 |
set result "\{0 0\}" |
| 245 |
} |
| 246 |
|
| 247 |
# set up the test and write it to the output file |
| 248 |
|
| 249 |
set cmd [prepareCmd $flags $re $str $vars $noBraces] |
| 250 |
if {$cmd == -1} { |
| 251 |
return "\# skipping test with metasyntax from line $srcLineNum\n" |
| 252 |
} |
| 253 |
|
| 254 |
set test "test regexp-1.$srcLineNum \{converted from line $srcLineNum\} \{\n" |
| 255 |
append test "\tcatch {unset var}\n" |
| 256 |
append test "\tlist \[catch \{\n" |
| 257 |
append test "\t\tset match \[$cmd\]\n" |
| 258 |
append test "\t\tlist \$match $vals\n" |
| 259 |
append test "\t\} msg\] \$msg\n" |
| 260 |
append test "\} $result\n" |
| 261 |
return $test |
| 262 |
} |
| 263 |
|