regexpTestLib.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:8k
源码类别:

通讯编程

开发平台:

Visual C++

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