| 1 |
% |
| 2 |
% \iffalse |
| 3 |
%<*driver> |
| 4 |
\documentclass{tclldoc} |
| 5 |
|
| 6 |
\newcommand{\ParseTcl}{Parse\Tcllogo} |
| 7 |
\providecommand{\href}[2]{#2} |
| 8 |
|
| 9 |
\begin{document} |
| 10 |
\DocInput{parsetcl.dtx} |
| 11 |
\end{document} |
| 12 |
%</driver> |
| 13 |
% \fi |
| 14 |
% |
| 15 |
% |
| 16 |
% \title{\ParseTcl\ --- a parser for \Tcllogo\ code} |
| 17 |
% \author{Lars Hellstr\"om} |
| 18 |
% |
| 19 |
% \maketitle |
| 20 |
% |
| 21 |
% |
| 22 |
% \begin{abstract} |
| 23 |
% \ParseTcl\ is a \Tcllogo\ package for parsing \Tcllogo\ code. |
| 24 |
% It is written completely in \Tcllogo, and make heavy use of some |
| 25 |
% new additions in \Tcllogo\,8.4. |
| 26 |
% \end{abstract} |
| 27 |
% |
| 28 |
% \begin{tcl} |
| 29 |
%<*pkg> |
| 30 |
namespace eval parsetcl {} |
| 31 |
package require Tcl 8.4 |
| 32 |
package provide parsetcl 0.1 |
| 33 |
% \end{tcl} |
| 34 |
% \setnamespace{parsetcl} |
| 35 |
% |
| 36 |
% \tableofcontents |
| 37 |
% |
| 38 |
% \section{Parser results} |
| 39 |
% |
| 40 |
% The values returned by the parser are conceptually rooted trees, but |
| 41 |
% technically the return values are lists. The general format of a |
| 42 |
% subtree is |
| 43 |
% \begin{quote} |
| 44 |
% \word{type} \word{interval} \word{text} \word{subtree}\regstar |
| 45 |
% \end{quote} |
| 46 |
% where there is one \word{subtree} for each component that remains if |
| 47 |
% the root of the subtree under consideration is deleted. The |
| 48 |
% \word{type} specifies what kind of construction the subtree |
| 49 |
% corresponds to. The \word{interval} is a list with the structure |
| 50 |
% \begin{quote} |
| 51 |
% \word{first} \word{last} |
| 52 |
% \end{quote} |
| 53 |
% where \word{first} and \word{last} are integers; the indices into |
| 54 |
% the input string of the first and last respectively character that |
| 55 |
% corresponds to this part of the parser tree. The \word{text} is, |
| 56 |
% depending on the \word{type}, either empty or the explicit string |
| 57 |
% that corresponds to this subtree. |
| 58 |
% |
| 59 |
% The \word{type}s are generally two-character strings, where the first |
| 60 |
% letter specifies a major category and the second letter an exact type |
| 61 |
% within that category, but this system is not enforced by any code. |
| 62 |
% \begin{description} |
| 63 |
% \item[Null types] |
| 64 |
% The \emph{null} types all begin with an |N|. These correspond to |
| 65 |
% information which isn't part of the script proper, but still is |
| 66 |
% useful to include in the parse result. It is generally possible |
| 67 |
% to add or remove subtrees of this type without changing the |
| 68 |
% meaning of the reconstruction of the script. |
| 69 |
% \begin{enumerate} |
| 70 |
% \item[\texttt{Nc}] \describestring[subtree type]{Nc} |
| 71 |
% This type is for comments, and it normally appears only in |
| 72 |
% places where one would expect a command subtree. The |
| 73 |
% \word{interval} includes the |#| beginning the comment, but not |
| 74 |
% the newline ending it. The \word{text} is the text in the |
| 75 |
% comment (initial |#| not included). |
| 76 |
% \item[\texttt{Ne}] \describestring[subtree type]{Ne} |
| 77 |
% This type is for syntax errors; rather than stopping at every |
| 78 |
% error, the parser routines will try to recover as best they can, |
| 79 |
% and an error item will be inserted in the parser tree. This |
| 80 |
% type of item generally appears as a \word{subtree} of whatever |
| 81 |
% was being parsed when the error was detected. The \word{text} is |
| 82 |
% a human-readable error message. The \word{interval} specifies |
| 83 |
% the erroneous characters; if the error is that some character(s) |
| 84 |
% is missing then the end of the interval is the index of the |
| 85 |
% character before that which is missing, and the start of the |
| 86 |
% interval is the character after that which is missing. |
| 87 |
% \item[\texttt{Np}] \describestring[subtree type]{Np} |
| 88 |
% This type is merely a placeholder; both the \word{interval} and |
| 89 |
% the \word{text} are empty strings. It is used when there was |
| 90 |
% nothing to return, but call syntax requires that a subtree is |
| 91 |
% returned. If some call returns an item of this type then it |
| 92 |
% usually means that its caller also has reached the end of |
| 93 |
% whatever that was parsing. |
| 94 |
% \end{enumerate} |
| 95 |
% |
| 96 |
% \item[Literate types] |
| 97 |
% The literate, or verbatim, types all begin with an |L|. Words (of |
| 98 |
% a command) where no variable or command substitution will happen |
| 99 |
% usually belong in this category. There are three types of |
| 100 |
% literates: \describestring[subtree type]{Lb}|Lb|s are delimited by |
| 101 |
% braces and \describestring[subtree type]{Lq}|Lq|s are delimited by |
| 102 |
% quotes, whereas \describestring[subtree type]{Lr}|Lr|s have no |
| 103 |
% particular delimiters. In each case, the \word{text} is the string |
| 104 |
% that this interval of the input gets parsed into. |
| 105 |
% |
| 106 |
% Backslash--newline substitution is silently performed, but other |
| 107 |
% types of substitution will result in that the item is considered |
| 108 |
% to consist of one or more parts, each of which will be |
| 109 |
% represented as a separate \word{subtree}. The delimiting quotes |
| 110 |
% of an |Lq| is never included in the \word{interval} of any |
| 111 |
% \word{subtree}. An |Lb| never has any \word{subtree}s due to |
| 112 |
% substitution, since it can only be subject to backslash--newline |
| 113 |
% substitution, but it may have |Ne| subtrees. |
| 114 |
% |
| 115 |
% \item[Merge types] |
| 116 |
% The merge types all begin with an |M|. They are similar to the |
| 117 |
% literate types, but are used for intervals of the input for which |
| 118 |
% the \emph{text} cannot be completely determined. |
| 119 |
% \describestring[subtree type]{Mq}|Mq|s are delimited by quotes, |
| 120 |
% whereas \describestring[subtree type]{Mr}|Mr|s have no particular |
| 121 |
% delimiters. The \word{text} is set to an empty string. There is |
| 122 |
% always at least one \word{subtree}. |
| 123 |
% |
| 124 |
% \item[Substitution types] |
| 125 |
% The substitution types all begin with an |S|. As the name |
| 126 |
% indicates, they are used for intervals of the input that will be |
| 127 |
% substituted by a \Tcllogo\ parser. |
| 128 |
% \begin{enumerate} |
| 129 |
% \item[\texttt{Sb}] \describestring[subtree type]{Sb} |
| 130 |
% This type is for backslash substitution. The \word{text} is |
| 131 |
% the character produced by this substitution. There is always |
| 132 |
% an |Lr| subtree for the text following the initial backslash. |
| 133 |
% \item[\texttt{Sv}] \describestring[subtree type]{Sv} |
| 134 |
% This type is for scalar variable substitution. The |
| 135 |
% \word{text} is empty, and there is one \word{subtree} for the |
| 136 |
% name of the variable being substituted. The type of this |
| 137 |
% subtree is either |Lr| or |Lb|, depending on whether the name |
| 138 |
% is surrounded by braces or not. |
| 139 |
% \item[\texttt{Sa}] \describestring[subtree type]{Sa} |
| 140 |
% This type is for array variable substitution. The |
| 141 |
% \word{text} is empty, and there are two \word{subtree}s: one |
| 142 |
% for the name of the array (always an |Lr|) and one for the |
| 143 |
% index into the array (either an |Lr| or an |Mr|). |
| 144 |
% \item[\texttt{Sc}] \describestring[subtree type]{Sc} |
| 145 |
% This type is for command substitution. The \word{text} is |
| 146 |
% empty, and there is one \word{subtree} for each command in |
| 147 |
% the script. |
| 148 |
% \end{enumerate} |
| 149 |
% |
| 150 |
% \item[The command type] |
| 151 |
% The \describestring[subtree type]{Cd}|Cd| type is for complete |
| 152 |
% commands. The \word{text} is empty, and each word in the command |
| 153 |
% has its own \word{subtree} (naturally, these are in the order of |
| 154 |
% the words in the command). |
| 155 |
% |
| 156 |
% \item[Root types] |
| 157 |
% The root types are used for the root in a parser tree, but they |
| 158 |
% may also appear as proper subtrees if the parser is invoked |
| 159 |
% recursively. This is for example what one does with the body |
| 160 |
% argument of the |while| command: technically it is a string just |
| 161 |
% like any other string, but it makes most sense to parse that |
| 162 |
% string as a script and replace the original literate item by the |
| 163 |
% entire tree generated by parsing this string. |
| 164 |
% |
| 165 |
% The root types all begin with an |R|. They are |
| 166 |
% \begin{enumerate} |
| 167 |
% \item[\texttt{Rs}] \describestring[subtree type]{Rs} |
| 168 |
% This type is for script parsing. The \word{text} is usually |
| 169 |
% empty, and each \word{subtree} is a command of the script. |
| 170 |
% \item[\texttt{Rx}] \describestring[subtree type]{Rx} |
| 171 |
% This type is for |expr| expression parsing. |
| 172 |
% \end{enumerate} |
| 173 |
% |
| 174 |
% \end{description} |
| 175 |
% |
| 176 |
% \subsection{Some examples} |
| 177 |
% |
| 178 |
% The script |
| 179 |
% \begin{quote} |
| 180 |
% |set a "b\nc"| |
| 181 |
% \end{quote} |
| 182 |
% will be parsed as the list |
| 183 |
%\begin{verbatim} |
| 184 |
% Rs {0 11} {} \ |
| 185 |
% {Cd {0 11} {} |
| 186 |
% {Lr {0 2} set} |
| 187 |
% {Lr {4 4} a} |
| 188 |
% {Lq {6 11} b\nc |
| 189 |
% {Lr {7 7} b} |
| 190 |
% {Sb {8 9} \n {Lr {9 9} n}} |
| 191 |
% {Lr {10 10} c} |
| 192 |
% } |
| 193 |
% } |
| 194 |
%\end{verbatim} |
| 195 |
% NB: This is not the canonical string representation, but one that was |
| 196 |
% chosen for clarity of exposition. The |parsetcl::format_tree| |
| 197 |
% procedure can be used to reformat a parser tree with indentation as |
| 198 |
% shown above. |
| 199 |
% |
| 200 |
% |
| 201 |
% \section{Basic script parsing} |
| 202 |
% |
| 203 |
% Basic script parsing makes a purely syntactic parsing of a script. It |
| 204 |
% does not make any assumptions about the meaning of the commands used |
| 205 |
% in the script, and hence it will not try to parse as scripts any |
| 206 |
% argument of a commands (not even the byte-compiled commands). |
| 207 |
% |
| 208 |
% The parsing relies on a set of procedures which parses the next item |
| 209 |
% of a particular sort, and returns the corresponding parse subtree. |
| 210 |
% These procedures can call each other recursively in complicated ways, |
| 211 |
% following the actual structure of the script that is being parsed. |
| 212 |
% The general syntax for calling such a procedure is |
| 213 |
% \begin{quote} |
| 214 |
% \word{proc name} \word{string} \word{index-var-name} |
| 215 |
% \word{extra}\regstar |
| 216 |
% \end{quote} |
| 217 |
% where \word{string} is the string from which something should be |
| 218 |
% parsed and \word{index-var-name} is the name of a variable in the |
| 219 |
% local context of the caller which holds the index of the first |
| 220 |
% character that hasn't been parsed yet (or possibly the last character |
| 221 |
% that has been parsed, it may depend on what is most convenient for |
| 222 |
% that particular procedure). There are two reasons for using this |
| 223 |
% set-up. One is that the calling procedure usually needs to continue |
| 224 |
% parsing the string after the part that it made a recursive call to |
| 225 |
% have parsed, and for that purpose this variable provides a convenient |
| 226 |
% method of returning the value. The other reason is one of efficiency: |
| 227 |
% If the script as a whole is passed as an argument to all procedures |
| 228 |
% that are involved in the parsing process, as opposed to passing only |
| 229 |
% the part that hasn't been parsed yet, then the need to copy the data |
| 230 |
% decreases drastically. |
| 231 |
% |
| 232 |
% |
| 233 |
% \subsection{Commands and scripts} |
| 234 |
% |
| 235 |
% \begin{proc}{flush_whitespace} |
| 236 |
% The |flush_whitespace| procedure advances the index past a |
| 237 |
% whitespace sequence. The syntax is |
| 238 |
% \begin{quote} |
| 239 |
% |parsetcl::flush_whitespace| \word{script} \word{index-var-name} |
| 240 |
% \word{cmdsep} |
| 241 |
% \end{quote} |
| 242 |
% where \word{cmdsep} is |1| if command separators (newlines and |
| 243 |
% semicolons) should be flushed as well, but |0| if they should be |
| 244 |
% treated separately. The return value is the number of characters |
| 245 |
% that were flushed. |
| 246 |
% \begin{tcl} |
| 247 |
proc parsetcl::flush_whitespace {script index_var cmdsep} { |
| 248 |
upvar 1 $index_var index |
| 249 |
if {[ |
| 250 |
if {$cmdsep} then { |
| 251 |
regexp -start $index -- {\A([ \t-\r;]|\\\n)+} $script match |
| 252 |
} else { |
| 253 |
regexp -start $index -- {\A([ \t\v\f\r]|\\\n)+} $script match |
| 254 |
} |
| 255 |
]} then { |
| 256 |
incr index [string length $match] |
| 257 |
return [string length $match] |
| 258 |
} else { |
| 259 |
return 0 |
| 260 |
} |
| 261 |
} |
| 262 |
% \end{tcl} |
| 263 |
% \end{proc} |
| 264 |
% |
| 265 |
% \begin{proc}{parse_command} |
| 266 |
% The |parse_command| procedure parses the next command, returning a |
| 267 |
% |Cd|, |Nc|, or |Np| item to its caller. The syntax is |
| 268 |
% \begin{quote} |
| 269 |
% |parsetcl::parse_command| \word{script} \word{index-var-name} |
| 270 |
% \word{nested} |
| 271 |
% \end{quote} |
| 272 |
% The \word{nested} argument is |1| if the context is that of command |
| 273 |
% substitution (in which case |]| acts as command terminator), and |0| |
| 274 |
% otherwise. Upon return, the index variable generally points to the |
| 275 |
% character that terminates the command. |
| 276 |
% |
| 277 |
% The procedure skips past any whitespace or command separators in front |
| 278 |
% of the actual command. |
| 279 |
% \begin{tcl} |
| 280 |
proc parsetcl::parse_command {script index_var nested} { |
| 281 |
upvar 1 $index_var index |
| 282 |
% \end{tcl} |
| 283 |
% The first step is to scan past the whitespace in front of the |
| 284 |
% command. |
| 285 |
% \begin{tcl} |
| 286 |
flush_whitespace $script index 1 |
| 287 |
% \end{tcl} |
| 288 |
% Then there are two ``not a command'' cases to take care of: there |
| 289 |
% might be a comment or the command sequence might have ended. In the |
| 290 |
% former case, the extent of the comment is determined and a |
| 291 |
% corresponding |Nc| item is returned. Backslash--newline |
| 292 |
% substitution adds a bit complexity to the problem of finding the end |
| 293 |
% of the comment, since an escaped newline may be part of the comment, |
| 294 |
% although a backslash is only escaping if it isn't itself escaped. |
| 295 |
% \begin{tcl} |
| 296 |
switch -- "[string index $script $index]$nested" {#0} - {#1} { |
| 297 |
regexp -start $index -indices -- {\A#([^\n\\]|\\.)*(\\$)?}\ |
| 298 |
$script interval |
| 299 |
incr index |
| 300 |
regsub -all -- {\\\n[ \t]*}\ |
| 301 |
[string range $script $index [lindex $interval 1]]\ |
| 302 |
{ } text |
| 303 |
set index [expr {[lindex $interval 1] + 1}] |
| 304 |
return [list Nc $interval $text] |
| 305 |
} 0 - 1 - \]1 { |
| 306 |
% \end{tcl} |
| 307 |
% In the latter case, an |Np| item is returned. It happens if |
| 308 |
% |$index| is past the last character in the string, but also if |
| 309 |
% |$index| is at a right bracket and the command is \word{nested}. |
| 310 |
% \begin{tcl} |
| 311 |
return [list Np "" ""] |
| 312 |
} |
| 313 |
% \end{tcl} |
| 314 |
% But if we get this far, then there is a command to parse. One only |
| 315 |
% has to parse all the words. |
| 316 |
% \begin{tcl} |
| 317 |
set res [list Cd [list $index ""] ""] |
| 318 |
set next [parse_word $script index $nested] |
| 319 |
while {[lindex $next 0] ne "Np"} { |
| 320 |
lappend res $next |
| 321 |
set next [parse_word $script index $nested] |
| 322 |
} |
| 323 |
lset res 1 1 [lindex $res end 1 1] |
| 324 |
return $res |
| 325 |
} |
| 326 |
% \end{tcl} |
| 327 |
% \end{proc} |
| 328 |
% |
| 329 |
% \begin{proc}{basic_parse_script} |
| 330 |
% The |basic_parse_script| procedure parses a script (that is to say, a |
| 331 |
% sequence of commands) and returns the corresponding |Rs| item. |
| 332 |
% The syntax is |
| 333 |
% \begin{quote} |
| 334 |
% |parsetcl::basic_parse_script| \word{script} |
| 335 |
% \end{quote} |
| 336 |
% \begin{tcl} |
| 337 |
proc parsetcl::basic_parse_script {script} { |
| 338 |
set index 0 |
| 339 |
set res [list Rs [list $index ""] ""] |
| 340 |
while {[lindex [set next [parse_command $script index 0]] 0] ne "Np"} { |
| 341 |
lappend res $next |
| 342 |
} |
| 343 |
incr index -1 |
| 344 |
lset res 1 1 $index |
| 345 |
return $res |
| 346 |
} |
| 347 |
% \end{tcl} |
| 348 |
% \end{proc} |
| 349 |
% |
| 350 |
% |
| 351 |
% \subsection{Parsing words} |
| 352 |
% |
| 353 |
% \begin{proc}{parse_word} |
| 354 |
% The |parse_word| procedure parses the next command word, returning |
| 355 |
% an |Lb|, |Lq|, |Lr|, |Mq|, |Mr|, or |Np| item to its caller. The |
| 356 |
% syntax is |
| 357 |
% \begin{quote} |
| 358 |
% |parsetcl::parse_word| \word{script} \word{index-var-name} |
| 359 |
% \word{nested} |
| 360 |
% \end{quote} |
| 361 |
% The \word{nested} argument is |1| if the context is that of command |
| 362 |
% substitution (in which case |]| acts as command terminator), and |0| |
| 363 |
% otherwise. It is assumed that the index variable points to the first |
| 364 |
% character of the word when this procedure is called. Upon return, |
| 365 |
% the index variable points to the first none-whitespace character |
| 366 |
% (or command separator) after the word that was parsed. |
| 367 |
% |
| 368 |
% Most of the actual work is done by the helper procedures |
| 369 |
% |parse_raw_word|, |parse_quoted_word|, and |parse_braced_word|. |
| 370 |
% \begin{tcl} |
| 371 |
proc parsetcl::parse_word {script index_var nested} { |
| 372 |
upvar 1 $index_var index |
| 373 |
switch -- [string index $script $index] \{ { |
| 374 |
parse_braced_word $script index $nested |
| 375 |
} \" { |
| 376 |
parse_quoted_word $script index $nested |
| 377 |
} "" - \; - \n { |
| 378 |
list Np "" "" |
| 379 |
} \] { |
| 380 |
if {$nested} then { |
| 381 |
list Np "" "" |
| 382 |
} else { |
| 383 |
parse_raw_word $script index $nested |
| 384 |
} |
| 385 |
} default { |
| 386 |
parse_raw_word $script index $nested |
| 387 |
} |
| 388 |
} |
| 389 |
% \end{tcl} |
| 390 |
% \end{proc} |
| 391 |
% |
| 392 |
% \begin{proc}{parse_braced_word} |
| 393 |
% The |parse_braced_word| procedure parses a brace-delimited word and |
| 394 |
% returns the corresponding |Lb| item to its caller. The syntax is |
| 395 |
% \begin{quote} |
| 396 |
% |parsetcl::parse_braced_word| \word{script} \word{index-var-name} |
| 397 |
% \word{nested} |
| 398 |
% \end{quote} |
| 399 |
% where the arguments and call conventions are as for |parse_word|. |
| 400 |
% In case the braced word is followed by some non-whitespace |
| 401 |
% characters, then this will be interpreted as though a space was |
| 402 |
% missing after the brace. A corresponding |Ne| subtree will be |
| 403 |
% attached to the |Lb| item returned, and the index variable will be |
| 404 |
% pointed to that none-whitespace character after the brace. |
| 405 |
% \begin{tcl} |
| 406 |
proc parsetcl::parse_braced_word {script index_var nested} { |
| 407 |
upvar 1 $index_var index |
| 408 |
set res [list Lb [list $index ""]] |
| 409 |
set depth 1 |
| 410 |
set text "" |
| 411 |
incr index |
| 412 |
while {$depth>0} { |
| 413 |
% \end{tcl} |
| 414 |
% Each iteration of this loop takes care of one non-trivial character |
| 415 |
% combination and whatever piece of trivial material that preceeds |
| 416 |
% it. Braces which are not escaped obviously constitute non-trivial |
| 417 |
% material (because they change the nesting depth), but escaped |
| 418 |
% newlines are also non-trivial (because they are subject to |
| 419 |
% substitution). |
| 420 |
% \begin{tcl} |
| 421 |
regexp -start $index -- {\A([^{}\\]|\\[^\n])*} $script match |
| 422 |
append text $match |
| 423 |
incr index [string length $match] |
| 424 |
switch -- [string index $script $index] \{ { |
| 425 |
incr depth |
| 426 |
incr index |
| 427 |
} \} { |
| 428 |
incr depth -1 |
| 429 |
incr index |
| 430 |
} \\ { |
| 431 |
if {[regexp -start $index -- {\A\\\n[ \t]*} $script match]}\ |
| 432 |
then { |
| 433 |
incr index [string length $match] |
| 434 |
append text { } |
| 435 |
} else { |
| 436 |
% \end{tcl} |
| 437 |
% In this (rather curious) case, the \word{script} ended with an |
| 438 |
% escaping backslash, but there is nothing to escape. This is |
| 439 |
% interpreted as just the backslash. However, there will be an error |
| 440 |
% since the braces apparently haven't been properly balanced. |
| 441 |
% \begin{tcl} |
| 442 |
append text \\ |
| 443 |
break |
| 444 |
} |
| 445 |
} "" { |
| 446 |
break |
| 447 |
} |
| 448 |
} |
| 449 |
if {$depth>0} then { |
| 450 |
lset res 1 1 $index |
| 451 |
lappend res $text [list Ne [list "" $index] {missing close-brace}] |
| 452 |
lset res 3 1 0 [incr index] |
| 453 |
return $res |
| 454 |
} |
| 455 |
lset res 1 1 [expr {$index - 1}] |
| 456 |
lappend res $text |
| 457 |
% \end{tcl} |
| 458 |
% What remains now is to check that there aren't any stray characters |
| 459 |
% following the close-brace. If there is whitespace to flush then |
| 460 |
% everything is alright. Things are also alright if the next character |
| 461 |
% is a command terminator or we're at the end of the string. |
| 462 |
% \begin{tcl} |
| 463 |
if {[flush_whitespace $script index 0]} then {return $res} |
| 464 |
switch -- [string index $script $index] \n - \; - {} { |
| 465 |
return $res |
| 466 |
} \] { |
| 467 |
if {$nested} then {return $res} |
| 468 |
} |
| 469 |
% \end{tcl} |
| 470 |
% But if that is not the case then there is an error, and an |Ne| item |
| 471 |
% should be appended to |res|. The exact appearence of this item does |
| 472 |
% however depend on how the parser attempts to recover from the error, |
| 473 |
% and that is not immediately obvious. The \Tcllogo\ parser makes no |
| 474 |
% attempt to recover---and can therefore report the error as ``extra |
| 475 |
% characters after close-brace''---but the best way to recover rather |
| 476 |
% seems to be to assume that a space is missing. That is why the |
| 477 |
% following error message is non-standard. |
| 478 |
% \begin{tcl} |
| 479 |
lappend res [list Ne [list $index [expr {$index - 1}]]\ |
| 480 |
{missing space after close-brace}] |
| 481 |
return $res |
| 482 |
} |
| 483 |
% \end{tcl} |
| 484 |
% \end{proc} |
| 485 |
% |
| 486 |
% \begin{proc}{parse_quoted_word} |
| 487 |
% The |parse_quoted_word| procedure parses a quote-delimited word and |
| 488 |
% returns the corresponding |Lq| or |Mq| item to its caller. The |
| 489 |
% syntax is |
| 490 |
% \begin{quote} |
| 491 |
% |parsetcl::parse_quoted_word| \word{script} \word{index-var-name} |
| 492 |
% \word{nested} |
| 493 |
% \end{quote} |
| 494 |
% where the arguments and call conventions are as for |parse_word|. |
| 495 |
% In case the quoted word is followed by some non-whitespace |
| 496 |
% characters, then this will be interpreted as though a space was |
| 497 |
% missing after the last quote. A corresponding |Ne| subtree will |
| 498 |
% be be put last in the |Lq| or |Mq| item returned, and the index |
| 499 |
% variable will be pointed to that none-whitespace character after |
| 500 |
% the quote. |
| 501 |
% \begin{tcl} |
| 502 |
proc parsetcl::parse_quoted_word {script index_var nested} { |
| 503 |
upvar 1 $index_var index |
| 504 |
set res [list Lq [list $index ""] ""] |
| 505 |
set text "" |
| 506 |
incr index |
| 507 |
while {1} { |
| 508 |
% \end{tcl} |
| 509 |
% Each iteration of this loop adds one subtree to |res| and the |
| 510 |
% corresponding literate material to |text|. If some substitution |
| 511 |
% happens which does not produce literate material then the |Lq| |
| 512 |
% in |res| is changed to an |Mq|. |
| 513 |
% \begin{tcl} |
| 514 |
switch -- [string index $script $index] \\ { |
| 515 |
lappend res [parse_backslash $script index] |
| 516 |
append text [lindex $res end 2] |
| 517 |
} \$ { |
| 518 |
lappend res [parse_dollar $script index] |
| 519 |
lset res 0 Mq |
| 520 |
} \[ { |
| 521 |
lappend res [parse_bracket $script index] |
| 522 |
lset res 0 Mq |
| 523 |
} \" { |
| 524 |
incr index |
| 525 |
break |
| 526 |
} "" { |
| 527 |
lappend res [list Ne [list $index [expr {$index - 1}]]\ |
| 528 |
{missing close-quote}] |
| 529 |
break |
| 530 |
} default { |
| 531 |
regexp -start $index -- {[^\\$\["]*} $script match |
| 532 |
set t $index |
| 533 |
incr index [string length $match] |
| 534 |
lappend res [list Lr [list $t [expr {$index - 1}]] $match] |
| 535 |
append text $match |
| 536 |
} |
| 537 |
} |
| 538 |
lset res 1 1 [expr {$index - 1}] |
| 539 |
if {[lindex $res 0] eq "Lq"} then { |
| 540 |
lset res 2 $text |
| 541 |
% \end{tcl} |
| 542 |
% An |Lq| item that has precisely one subtree that furthermore is of |
| 543 |
% type |Lr| can do without that subtree, since all the interesting |
| 544 |
% information is in the |text|. |
| 545 |
% \begin{tcl} |
| 546 |
if {[llength $res] == 4 && [lindex $res 3 0] eq "Lr"} then { |
| 547 |
set res [lrange $res 0 2] |
| 548 |
} |
| 549 |
} |
| 550 |
% \end{tcl} |
| 551 |
% What remains now is to check that there aren't any stray characters |
| 552 |
% following the close-quote. If there is whitespace to flush then |
| 553 |
% everything is alright. Things are also alright if the next character |
| 554 |
% is a command terminator or we're at the end of the string. |
| 555 |
% \begin{tcl} |
| 556 |
if {[flush_whitespace $script index 0]} then {return $res} |
| 557 |
switch -- [string index $script $index] \n - \; - {} { |
| 558 |
return $res |
| 559 |
} \] { |
| 560 |
if {$nested} then {return $res} |
| 561 |
} |
| 562 |
% \end{tcl} |
| 563 |
% But if that is not the case then there is an error, and an |Ne| item |
| 564 |
% should be appended to |res|. As with characters after close-quotes, |
| 565 |
% it is assumed that a space is missing. |
| 566 |
% \begin{tcl} |
| 567 |
lappend res [list Ne [list $index [expr {$index - 1}]]\ |
| 568 |
{missing space after close-quote}] |
| 569 |
return $res |
| 570 |
} |
| 571 |
% \end{tcl} |
| 572 |
% \end{proc} |
| 573 |
% |
| 574 |
% \begin{proc}{parse_raw_word} |
| 575 |
% The |parse_raw_word| procedure parses a whitespace-delimited word |
| 576 |
% of a command and returns the corresponding |Lr| or |Mr| item to its |
| 577 |
% caller. The syntax is |
| 578 |
% \begin{quote} |
| 579 |
% |parsetcl::parse_raw_word| \word{script} \word{index-var-name} |
| 580 |
% \word{nested} |
| 581 |
% \end{quote} |
| 582 |
% where the arguments and call conventions are as for |parse_word|. |
| 583 |
% \begin{tcl} |
| 584 |
proc parsetcl::parse_raw_word {script index_var nested} { |
| 585 |
upvar 1 $index_var index |
| 586 |
set res [list] |
| 587 |
set type Lr |
| 588 |
set interval [list $index] |
| 589 |
set text "" |
| 590 |
while {1} { |
| 591 |
% \end{tcl} |
| 592 |
% Each iteration of this loop adds one subtree to |res| and the |
| 593 |
% corresponding literate material to |text|. If some substitution |
| 594 |
% happens which does not produce literate material then |type| is set |
| 595 |
% to an |Mr|. |
| 596 |
% \begin{tcl} |
| 597 |
switch -- [string index $script $index] \\ { |
| 598 |
if {[string index $script [expr {$index+1}]] eq "\n"} then { |
| 599 |
break |
| 600 |
} |
| 601 |
lappend res [parse_backslash $script index] |
| 602 |
append text [lindex $res end 2] |
| 603 |
continue |
| 604 |
} \$ { |
| 605 |
lappend res [parse_dollar $script index] |
| 606 |
set type Mr |
| 607 |
continue |
| 608 |
} \[ { |
| 609 |
lappend res [parse_bracket $script index] |
| 610 |
set type Mr |
| 611 |
continue |
| 612 |
} \t - \n - \v - \f - \r - " " - \; - "" { |
| 613 |
break |
| 614 |
} |
| 615 |
if {$nested} then { |
| 616 |
if {![ |
| 617 |
regexp -start $index -- {\A[^\\$\[\]\t-\r ;]+} $script match |
| 618 |
]} then {break} |
| 619 |
} else { |
| 620 |
regexp -start $index -- {\A[^\\$\[\t-\r ;]+} $script match |
| 621 |
} |
| 622 |
set t $index |
| 623 |
incr index [string length $match] |
| 624 |
lappend res [list Lr [list $t [expr {$index - 1}]] $match] |
| 625 |
append text $match |
| 626 |
} |
| 627 |
% \end{tcl} |
| 628 |
% In case there is only a single element in |res| then that will be |
| 629 |
% the result. |
| 630 |
% \begin{tcl} |
| 631 |
if {[llength $res]==1} then { |
| 632 |
set res [lindex $res 0] |
| 633 |
} else { |
| 634 |
lappend interval [expr {$index - 1}] |
| 635 |
if {$type ne "Lr"} then {set text ""} |
| 636 |
set res [linsert $res 0 $type $interval $text] |
| 637 |
} |
| 638 |
flush_whitespace $script index 0 |
| 639 |
return $res |
| 640 |
} |
| 641 |
% \end{tcl} |
| 642 |
% \end{proc} |
| 643 |
% |
| 644 |
% |
| 645 |
% \subsection{Parsing substitutions} |
| 646 |
% |
| 647 |
% \begin{proc}{parse_backslash} |
| 648 |
% The |parse_backslash| procedure parses a backslash sequence and |
| 649 |
% returns the corresponding |Sb| item. The syntax is |
| 650 |
% \begin{quote} |
| 651 |
% |parsetcl::parse_backslash| \word{script} \word{index-var-name} |
| 652 |
% \end{quote} |
| 653 |
% and it is assumed that the index character in \word{script} is the |
| 654 |
% initial backslash in the sequence. Upon return, the index character |
| 655 |
% is the first character after the backslash sequence. |
| 656 |
% \begin{tcl} |
| 657 |
proc parsetcl::parse_backslash {script index_var} { |
| 658 |
upvar 1 $index_var index |
| 659 |
set start $index |
| 660 |
incr index |
| 661 |
set ch [string index $script $index] |
| 662 |
set res [list Lr [list $index $index] $ch] |
| 663 |
switch -- $ch a { |
| 664 |
set res [list Sb [list $start $index] \a $res] |
| 665 |
} b { |
| 666 |
set res [list Sb [list $start $index] \b $res] |
| 667 |
} f { |
| 668 |
set res [list Sb [list $start $index] \f $res] |
| 669 |
} n { |
| 670 |
set res [list Sb [list $start $index] \n $res] |
| 671 |
} r { |
| 672 |
set res [list Sb [list $start $index] \r $res] |
| 673 |
} t { |
| 674 |
set res [list Sb [list $start $index] \t $res] |
| 675 |
} v { |
| 676 |
set res [list Sb [list $start $index] \v $res] |
| 677 |
} x { |
| 678 |
if {[regexp -start [expr {$index + 1}] -- {\A[0-9A-Fa-f]+}\ |
| 679 |
$script match]} then { |
| 680 |
scan [string range $match end-1 end] %x code |
| 681 |
incr index [string length $match] |
| 682 |
lset res 1 1 $index |
| 683 |
lset res 2 "x$match" |
| 684 |
set res [list Sb [list $start $index]\ |
| 685 |
[format %c $code] $res] |
| 686 |
} else { |
| 687 |
set res [list Sb [list $start $index] x $res] |
| 688 |
} |
| 689 |
} u { |
| 690 |
if {[regexp -start [expr {$index + 1}] -- {\A[0-9A-Fa-f]{1,4}}\ |
| 691 |
$script match]} then { |
| 692 |
scan $match %x code |
| 693 |
incr index [string length $match] |
| 694 |
lset res 1 1 $index |
| 695 |
lset res 2 "u$match" |
| 696 |
set res [list Sb [list $start $index]\ |
| 697 |
[format %c $code] $res] |
| 698 |
} else { |
| 699 |
set res [list Sb [list $start $index] u $res] |
| 700 |
} |
| 701 |
} \n { |
| 702 |
regexp -start [expr {$index + 1}] -- {\A[ \t]*} $script match |
| 703 |
incr index [string length $match] |
| 704 |
lset res 1 1 $index |
| 705 |
lset res 2 "\n$match" |
| 706 |
set res [list Sb [list $start $index] " " $res] |
| 707 |
} "" { |
| 708 |
return [list Sb [list $start $start] \\] |
| 709 |
} default { |
| 710 |
if {[regexp -start $index -- {\A[0-7]{1,3}} $script match]} then { |
| 711 |
scan $match %o code |
| 712 |
incr index [expr {[string length $match]-1}] |
| 713 |
lset res 1 1 $index |
| 714 |
lset res 2 $match |
| 715 |
set res [list Sb [list $start $index] [format %c $code] $res] |
| 716 |
} else { |
| 717 |
set res [list Sb [list $start $index] $ch $res] |
| 718 |
} |
| 719 |
|
| 720 |
} |
| 721 |
incr index |
| 722 |
return $res |
| 723 |
} |
| 724 |
% \end{tcl} |
| 725 |
% \end{proc} |
| 726 |
% |
| 727 |
% \begin{proc}{parse_bracket} |
| 728 |
% The |parse_bracket| procedure parses one pair of command |
| 729 |
% substitution brackets and returns the corresponding |Sc| item. |
| 730 |
% The syntax is |
| 731 |
% \begin{quote} |
| 732 |
% |parsetcl::parse_bracket| \word{script} \word{index-var-name} |
| 733 |
% \end{quote} |
| 734 |
% and it is assumed that the index character in \word{script} is the |
| 735 |
% initial bracket in the sequence. Upon return, the index character |
| 736 |
% is the first character after the close-bracket. |
| 737 |
% \begin{tcl} |
| 738 |
proc parsetcl::parse_bracket {script index_var} { |
| 739 |
upvar 1 $index_var index |
| 740 |
set res [list Sc [list $index ""] ""] |
| 741 |
incr index |
| 742 |
while {[lindex [set next [parse_command $script index 1]] 0] ne "Np"} { |
| 743 |
lappend res $next |
| 744 |
} |
| 745 |
if {[string index $script $index] eq "\]"} then { |
| 746 |
lset res 1 1 $index |
| 747 |
incr index |
| 748 |
return $res |
| 749 |
} else { |
| 750 |
lappend res [list Ne [list $index [expr {$index-1}]]\ |
| 751 |
{missing close-bracket}] |
| 752 |
lset res 1 1 [expr {$index-1}] |
| 753 |
return $res |
| 754 |
} |
| 755 |
} |
| 756 |
% \end{tcl} |
| 757 |
% \end{proc} |
| 758 |
% |
| 759 |
% \begin{variable}{varname_RE} |
| 760 |
% The |varname_RE| is the regular expression used with |$| |
| 761 |
% substitution for grabbing the name of the variable. |
| 762 |
% \begin{tcl} |
| 763 |
set parsetcl::varname_RE {\A(\w|::)+} |
| 764 |
% \end{tcl} |
| 765 |
% The reason for factoring this out into a variable is that |
| 766 |
% \Tcllogo\ isn't using the natural (Unicode) definition of an |
| 767 |
% alphanumeric character here, but relies on a locale-dependent |
| 768 |
% C~library function to perform this test. They generally agree on |
| 769 |
% ASCII characters, but outside that one probably cannot rely on that |
| 770 |
% the test produces sensible results. Hence if a script needs to be |
| 771 |
% parsed where some non-Unicode behaviour of this library function is |
| 772 |
% significant, then you might need to change the above regexp. |
| 773 |
% |
| 774 |
% There is a \Tcllogo\ bug report~\cite{Letter-bug} on the matter, |
| 775 |
% and hopefully there will be no need to ever change this regexp |
| 776 |
% once that has been resolved. |
| 777 |
% \end{variable} |
| 778 |
% |
| 779 |
% \begin{proc}{parse_dollar} |
| 780 |
% The |parse_dollar| procedure parses one |$|-sequence and returns |
| 781 |
% the corresponding |Sv|, |Sa|, or |Lr| item. The syntax is |
| 782 |
% \begin{quote} |
| 783 |
% |parsetcl::parse_dollar| \word{script} \word{index-var-name} |
| 784 |
% \end{quote} |
| 785 |
% and it is assumed that the index character in \word{script} is the |
| 786 |
% initial |$| in the sequence. Upon return, the index character |
| 787 |
% is the first character after the parsed sequence. |
| 788 |
% \begin{tcl} |
| 789 |
proc parsetcl::parse_dollar {script index_var} { |
| 790 |
upvar 1 $index_var index |
| 791 |
set res [list "" [list $index ""] ""] |
| 792 |
incr index |
| 793 |
% \end{tcl} |
| 794 |
% If the first character after the |$| is a left brace, then the |
| 795 |
% variable is scalar and its name is terminated by the next right |
| 796 |
% brace. Note that braces do not nest in this case. |
| 797 |
% \begin{tcl} |
| 798 |
if {[string index $script $index] eq "\{"} then { |
| 799 |
lset res 0 Sv |
| 800 |
set end [string first \} $script $index] |
| 801 |
if {$end<0} then { |
| 802 |
set end [expr {[string length $script] - 1}] |
| 803 |
lappend res [list Lb [list $index $end]\ |
| 804 |
[string range $script [expr {$index + 1}] end]]\ |
| 805 |
[list Ne [list [expr {$end+1}] $end]\ |
| 806 |
{missing close-brace for variable name}] |
| 807 |
} else { |
| 808 |
lappend res [list Lb [list $index $end]\ |
| 809 |
[string range $script [expr {$index + 1}] [expr {$end-1}]]] |
| 810 |
} |
| 811 |
lset res 1 1 $end |
| 812 |
set index [expr {$end + 1}] |
| 813 |
return $res |
| 814 |
} |
| 815 |
% \end{tcl} |
| 816 |
% Otherwise see if there is something which can be interpreted as an |
| 817 |
% alphanumeric variable name. First treat the case when there isn't; |
| 818 |
% in that case the |$| is just a literate |$| and it is returned as an |
| 819 |
% |Lr| iterm. Then treat the case that the variable is scalar. |
| 820 |
% \begin{tcl} |
| 821 |
variable varname_RE |
| 822 |
if {![regexp -start $index -- $varname_RE $script match]} then { |
| 823 |
if {[string index $script $index] eq "("} then { |
| 824 |
set match "" |
| 825 |
} else { |
| 826 |
return [list Lr [list [lindex $res 1 0] [lindex $res 1 0]] \$] |
| 827 |
} |
| 828 |
} |
| 829 |
set t $index |
| 830 |
incr index [string length $match] |
| 831 |
lappend res [list Lr [list $t [expr {$index-1}]] $match] |
| 832 |
if {[string index $script $index] ne "("} then { |
| 833 |
lset res 0 Sv |
| 834 |
lset res 1 1 [lindex $res 3 1 1] |
| 835 |
return $res |
| 836 |
} |
| 837 |
% \end{tcl} |
| 838 |
% What remains is to treat the case of an array variable. This is |
| 839 |
% very much like |parse_quoted_word|, but it is the right parenthesis |
| 840 |
% rather than |"| that acts as terminator. |
| 841 |
% \begin{tcl} |
| 842 |
lset res 0 Sa |
| 843 |
incr index |
| 844 |
set subres [list Lr [list $index ""] ""] |
| 845 |
lappend res "" |
| 846 |
set text "" |
| 847 |
while {1} { |
| 848 |
% \end{tcl} |
| 849 |
% Each iteration of this loop adds one subtree to |subres| and the |
| 850 |
% corresponding literate material to |text|. If some substitution |
| 851 |
% happens which does not produce literate material then the |Lr| |
| 852 |
% in |subres| is changed to an |Mr|. |
| 853 |
% \begin{tcl} |
| 854 |
switch -- [string index $script $index] \\ { |
| 855 |
lappend subres [parse_backslash $script index] |
| 856 |
append text [lindex $subres end 2] |
| 857 |
} \$ { |
| 858 |
lappend subres [parse_dollar $script index] |
| 859 |
lset subres 0 Mr |
| 860 |
} \[ { |
| 861 |
lappend subres [parse_bracket $script index] |
| 862 |
lset subres 0 Mr |
| 863 |
} ) { |
| 864 |
lset subres 1 1 [expr {$index - 1}] |
| 865 |
break |
| 866 |
} "" { |
| 867 |
lappend res\ |
| 868 |
[list Ne [list $index [incr index -1]] {missing )}] |
| 869 |
lset subres 1 1 $index |
| 870 |
break |
| 871 |
} default { |
| 872 |
regexp -start $index -- {[^\\$\[)]*} $script match |
| 873 |
set t $index |
| 874 |
incr index [string length $match] |
| 875 |
lappend subres [list Lr [list $t [expr {$index - 1}]] $match] |
| 876 |
append text $match |
| 877 |
} |
| 878 |
} |
| 879 |
if {[lindex $subres 0] eq "Lr"} then {lset subres 2 $text} |
| 880 |
if {[llength $subres] == 4} then {set subres [lindex $subres 3]} |
| 881 |
lset res 1 1 $index |
| 882 |
incr index |
| 883 |
lset res 4 $subres |
| 884 |
return $res |
| 885 |
} |
| 886 |
% \end{tcl} |
| 887 |
% \end{proc} |
| 888 |
% |
| 889 |
% |
| 890 |
% |
| 891 |
% \section{Some utility procdures} |
| 892 |
% |
| 893 |
% \subsection{Viewing parser trees} |
| 894 |
% |
| 895 |
% It is useful to have a surveyable presentation of parser trees. An |
| 896 |
% easy step towards this would be to ensure that nesting depth is |
| 897 |
% mirrored in the indentation. |
| 898 |
% |
| 899 |
% \begin{proc}{format_tree} |
| 900 |
% The |format_tree| procedure takes a parser tree, a base indentation |
| 901 |
% (string of whitespace), and an indentation step (string of whitespace) |
| 902 |
% as argument. It returns a string that is list-wise equivalent to |
| 903 |
% |[list]| of the original tree, but has indentation mirroring the |
| 904 |
% nesting depth. |
| 905 |
% \begin{tcl} |
| 906 |
proc parsetcl::format_tree {tree base step} { |
| 907 |
set res $base |
| 908 |
append res \{ [lrange $tree 0 1] { } |
| 909 |
% \end{tcl} |
| 910 |
% The following is a trick to make a list element string |
| 911 |
% representation for the \word{text} that does not contain any sort |
| 912 |
% of newline: making the element unbalanced with respect to braces |
| 913 |
% forces escape-quoting also for newlines. |
| 914 |
% \begin{tcl} |
| 915 |
if {[regexp {[\n\r]} [lindex $tree 2]]} then { |
| 916 |
append res [string range [list "[lindex $tree 2]\{"] 0 end-2] |
| 917 |
} else { |
| 918 |
append res [lrange $tree 2 2] |
| 919 |
} |
| 920 |
if {[llength $tree]<=3} then { |
| 921 |
append res \} |
| 922 |
return $res |
| 923 |
} elseif {[llength $tree] == 4 &&\ |
| 924 |
[string match {S[bv]} [lindex $tree 0]]} then { |
| 925 |
% \end{tcl} |
| 926 |
% This is a slight optimization: |Sb| and |Sv| trees look better on |
| 927 |
% one line than on three. |
| 928 |
% \begin{tcl} |
| 929 |
append res " " [format_tree [lindex $tree 3] "" ""] \} |
| 930 |
return $res |
| 931 |
} |
| 932 |
append res \n |
| 933 |
foreach subtree [lrange $tree 3 end] { |
| 934 |
append res [format_tree $subtree $base$step $step] \n |
| 935 |
} |
| 936 |
append res $base \} |
| 937 |
} |
| 938 |
% \end{tcl} |
| 939 |
% \end{proc} |
| 940 |
% |
| 941 |
% |
| 942 |
% \subsection{Offsetting intervals} |
| 943 |
% |
| 944 |
% \begin{proc}{offset_intervals} |
| 945 |
% This procedure modifies a \word{tree} by adding \word{offset} to |
| 946 |
% all endpoints of each interval in that tree, and returns the |
| 947 |
% modified tree. The syntax is |
| 948 |
% \begin{quote} |
| 949 |
% |parsetcl::offset_intervals| \word{tree} \word{offset} |
| 950 |
% \end{quote} |
| 951 |
% \begin{tcl} |
| 952 |
proc parsetcl::offset_intervals {tree offset} { |
| 953 |
set res [lrange $tree 0 2] |
| 954 |
foreach i {0 1} { |
| 955 |
lset res 1 $i [expr {[lindex $res 1 $i] + $offset}] |
| 956 |
} |
| 957 |
foreach subtree [lrange $tree 3 end] { |
| 958 |
lappend res [offset_intervals $subtree $offset] |
| 959 |
} |
| 960 |
return $res |
| 961 |
} |
| 962 |
% \end{tcl} |
| 963 |
% \end{proc} |
| 964 |
% |
| 965 |
% \begin{proc}{reparse_Lb_as_script} |
| 966 |
% The |reparse_Lb_as_script| procedure replaces an |Lb| node from an |
| 967 |
% existing parser tree with the |Rs| node produced by parsing the text |
| 968 |
% between the braces as a script. The syntax is |
| 969 |
% \begin{quote} |
| 970 |
% |parsetcl::reparse_Lb_as_script| \word{tree-var} \word{node-index} |
| 971 |
% \word{parsed string} |
| 972 |
% \end{quote} |
| 973 |
% where \word{tree-var} is the name in the caller's local context of |
| 974 |
% the variable in which the tree to substitute is stored and |
| 975 |
% \word{node-index} is the index list of the node to replace. The |
| 976 |
% procedure returns |2| if the node was an |Lb| node, |1| if is was |
| 977 |
% an |Lr| or |Lq| node, and |0| if the node was not of any of these |
| 978 |
% types. In the last case, the tree is not modified. In the case that |
| 979 |
% |1| is returned, the node has been substituted, but the intervals |
| 980 |
% may be slightly off since the string to parse was taken from the |
| 981 |
% \word{text} part of the substituted node. The normal case is that |2| |
| 982 |
% is returned, and in this case the intervals unambiguously refer to |
| 983 |
% positions in the \word{parsed string}. |
| 984 |
% \begin{tcl} |
| 985 |
proc parsetcl::reparse_Lb_as_script {tree_var index parsed} { |
| 986 |
upvar 1 $tree_var tree |
| 987 |
set node [lindex $tree $index] |
| 988 |
switch -- [lindex $node 0] Lb - Lr - Lq { |
| 989 |
set base [expr {[lindex $node 1 0] + 1}] |
| 990 |
if {[lindex $node 0] eq "Lb"} then { |
| 991 |
set script [string range $parsed $base\ |
| 992 |
[expr {[lindex $node 1 1] - 1}]] |
| 993 |
} else { |
| 994 |
set script [lindex $node 2] |
| 995 |
} |
| 996 |
lset tree $index\ |
| 997 |
[offset_intervals [basic_parse_script $script] $base] |
| 998 |
if {[lindex $node 0] eq "Lb"} then { |
| 999 |
return 2 |
| 1000 |
} else { |
| 1001 |
return 1 |
| 1002 |
} |
| 1003 |
} default { |
| 1004 |
return 0 |
| 1005 |
} |
| 1006 |
} |
| 1007 |
% \end{tcl} |
| 1008 |
% \end{proc} |
| 1009 |
|
| 1010 |
|
| 1011 |
% |
| 1012 |
% |
| 1013 |
% \subsection{Traversing parser trees} |
| 1014 |
% |
| 1015 |
% \begin{proc}{walk_tree} |
| 1016 |
% The |walk_tree| procedure has the syntax |
| 1017 |
% \begin{quote} |
| 1018 |
% |parsetcl::walk_tree| \word{tree-var} \word{index-var} |
| 1019 |
% \begin{regblock}[\regplus]\word{type-pattern} |
| 1020 |
% \word{body}\end{regblock} |
| 1021 |
% \end{quote} |
| 1022 |
% It walks through each node in the tree stored in the |
| 1023 |
% \word{tree-var} in the order that they appear in the string, i.e., |
| 1024 |
% first the root of a tree, then it walks through each subtree in |
| 1025 |
% sequence. When visiting a node, the type of the node is |
| 1026 |
% regexp-matched against the \word{type-pattern}s, and the first |
| 1027 |
% matching \word{body} is evaluated (entirely using |switch -regexp|) |
| 1028 |
% in the local context of the caller. While walking, the procedure is |
| 1029 |
% updating the \word{index-var} variable so that it always contains |
| 1030 |
% the index list specifying the current node in the tree. |
| 1031 |
% |
| 1032 |
% It is OK to modify the tree while walking through it, provided that |
| 1033 |
% the index list of the current node remains valid throughout. It is |
| 1034 |
% particular possible to change the contents of the current node (add |
| 1035 |
% or remove children) without messing things up. |
| 1036 |
% |
| 1037 |
% There is no particular return value from this procedure. |
| 1038 |
% \begin{tcl} |
| 1039 |
proc parsetcl::walk_tree {tree_var index_var args} { |
| 1040 |
upvar 1 $tree_var tree $index_var idxL |
| 1041 |
set idxL [list] |
| 1042 |
set i 0 |
| 1043 |
while {$i>=0} { |
| 1044 |
if {$i==0} then { |
| 1045 |
uplevel 1 [list switch -regexp --\ |
| 1046 |
[lindex [lindex $tree $idxL] 0] $args] |
| 1047 |
set i 3 |
| 1048 |
} elseif {$i < [llength [lindex $tree $idxL]]} then { |
| 1049 |
lappend idxL $i |
| 1050 |
set i 0 |
| 1051 |
} elseif {[llength $idxL]} then { |
| 1052 |
set i [lindex $idxL end] |
| 1053 |
set idxL [lrange $idxL 0 end-1] |
| 1054 |
incr i |
| 1055 |
} else { |
| 1056 |
set i -1 |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
% \end{tcl} |
| 1061 |
% \end{proc} |
| 1062 |
% |
| 1063 |
% \begin{proc}{simple_parse_script} |
| 1064 |
% The |simple_parse_script| procedure is similar to |
| 1065 |
% |basic_parse_script|, but it tries to recognise some common control |
| 1066 |
% structures (|if|, |for|, etc.) and reparses those arguments that |
| 1067 |
% (for standard command definitions) are scripts. The syntax is |
| 1068 |
% \begin{quote} |
| 1069 |
% |parsetcl::simple_parse_script| \word{script} |
| 1070 |
% \end{quote} |
| 1071 |
% and it returns the parsed script. |
| 1072 |
% \begin{tcl} |
| 1073 |
proc parsetcl::simple_parse_script {script} { |
| 1074 |
set tree [parsetcl::basic_parse_script $script] |
| 1075 |
walk_tree tree indices Cd { |
| 1076 |
switch -- [lindex [lindex $tree $indices] 3 2] if { |
| 1077 |
for {set i 3} {$i < [llength [lindex $tree $indices]]}\ |
| 1078 |
{incr i} { |
| 1079 |
switch -- [lindex [lindex $tree $indices] $i 2]\ |
| 1080 |
if - elseif { |
| 1081 |
incr i; continue |
| 1082 |
} then - else { |
| 1083 |
incr i |
| 1084 |
} |
| 1085 |
parsetcl::reparse_Lb_as_script tree\ |
| 1086 |
[linsert $indices end $i] $script |
| 1087 |
} |
| 1088 |
} while { |
| 1089 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 5]\ |
| 1090 |
$script |
| 1091 |
} for { |
| 1092 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 4]\ |
| 1093 |
$script |
| 1094 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 6]\ |
| 1095 |
$script |
| 1096 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 7]\ |
| 1097 |
$script |
| 1098 |
} foreach { |
| 1099 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end end]\ |
| 1100 |
$script |
| 1101 |
} catch { |
| 1102 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 4]\ |
| 1103 |
$script |
| 1104 |
} proc { |
| 1105 |
parsetcl::reparse_Lb_as_script tree [linsert $indices end 6]\ |
| 1106 |
$script |
| 1107 |
} |
| 1108 |
} |
| 1109 |
return $tree |
| 1110 |
} |
| 1111 |
% \end{tcl} |
| 1112 |
% \end{proc} |
| 1113 |
% |
| 1114 |
% \section{Parsing expressions} |
| 1115 |
% |
| 1116 |
% This hasn't been implemented yet. |
| 1117 |
% |
| 1118 |
% |
| 1119 |
% \section{Advanced script parsing} |
| 1120 |
% |
| 1121 |
% Most of the meaning of a \Tcllogo\ script depends on its commands |
| 1122 |
% rather than being given by the syntax---there is e.g. nothing in the |
| 1123 |
% general syntax that makes the last argument of a |foreach| command |
| 1124 |
% more likely to contain a script than any of the other arguments---and |
| 1125 |
% therefore any higher level parsing of \Tcllogo\ scripts must employ a |
| 1126 |
% table of commands |
| 1127 |
% |
| 1128 |
% This hasn't been implemented yet. |
| 1129 |
% |
| 1130 |
% |
| 1131 |
% \section{Script reconstruction} |
| 1132 |
% |
| 1133 |
% This hasn't been implemented yet, but the parser trees contain |
| 1134 |
% the necessary information. Applications of script reconstruction |
| 1135 |
% includes writing a |proc|-like command that inlines code in the body |
| 1136 |
% argument before the procedure is created. |
| 1137 |
% |
| 1138 |
% |
| 1139 |
% \begin{thebibliography}{9} |
| 1140 |
% \bibitem{Letter-bug} |
| 1141 |
% Kevin B. Kenny and Jeffrey Hobbs: |
| 1142 |
% \textit{Dollar-substitution and non-Latin-1}, |
| 1143 |
% \Tcllogo~project bug \#408568 (2001, still open April~2003); |
| 1144 |
% \href{https://sourceforge.net/tracker/^^A |
| 1145 |
% ?func=detail&aid=408568&group_id=10894&atid=110894}^^A |
| 1146 |
% {\textsc{https}:/\slash \texttt{sourceforge.net}\slash |
| 1147 |
% \texttt{tracker}\slash |
| 1148 |
% \texttt{?func=detail\&}\penalty\exhyphenpenalty |
| 1149 |
% \texttt{aid=408568\&}\penalty\exhyphenpenalty |
| 1150 |
% \texttt{group\_id=10894\&}\penalty\exhyphenpenalty |
| 1151 |
% \texttt{atid=110894}.} |
| 1152 |
% |
| 1153 |
% \end{thebibliography} |
| 1154 |
% |
| 1155 |
\endinput |