string::token(3tcl) | Text and string utilities | string::token(3tcl) |
string::token - Regex based iterative lexing
package require Tcl 8.5
package require string::token ?1?
package require fileutil
::string token text lex string
::string token file lex path
::string token chomp lex startvar string resultvar
This package provides commands for regular expression based lexing (tokenization) of strings.
The complete set of procedures is described below.
The result of the command is a list of tokens, where each token is a 3-element list of label, start- and end-index in the string.
The command will throw an error if it is not able to tokenize the whole string.
If the file is too large for this mode of operation a command directly based on ::string token chomp below will be necessary.
The command takes an ordered dictionary lex mapping regular expressions to labels, a variable startvar which indicates where to start lexing in the input string, and a result variable resultvar to extend.
The result of the command is a tri-state numeric code indicating one of
If a token was recognized (status 1) the command will update the index in startvar to point to the first character of the string past the recognized token, and it will further extend the resultvar with a 3-element list containing the label associated with the regular expression of the token, and the start- and end-character-indices of the token in string.
Neither startvar nor resultvar will be updated if no token is recognized at all.
Note that the regular expressions are applied (tested) in the order they are specified in lex, and the first matching pattern stops the process. Because of this it is recommended to specify the patterns to lex with from the most specific to the most general.
Further note that all regex patterns are implicitly prefixed with the constraint escape A to ensure that a match starts exactly at the character index found in startvar.
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category textutil of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
lexing, regex, string, tokenization
Text processing
1 | tcllib |