string::token(3tcl)
π string::token β bestie edition π
okay so like. this is a Tcl manual page, string::token(3tcl), living in the Text and string utilities section. it's giving "regex based iterative lexing" and honestly? iconic. let me explain everything, no cap, nothing skipped. π
β¨ NAME β¨
string::token β regex based iterative lexing. that's it. that's the whole vibe. π
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
package require string::token ?1.1?
package require fileutil
::string token text lex string
::string token file lex path
::string token chomp lex startvar string resultvar
like yes bestie, package require Tcl 8.5 9 β she needs Tcl 8.5, and 9 is fine too, she's flexible π. and package require string::token ?1.1? β the ?1.1? is optional, she's chill like that. and don't forget package require fileutil, she borrows from her friend. π«Ά
π¦ DESCRIPTION π¦
so basically this package hands you commands for regular expression based lexing β aka tokenization β of strings. she takes a big ol' string and chops it up into tokens. slay.
the complete set of procedures is described below. every single one. no skips, periodt. π
π ::string token text lex string
okay so this one takes an ordered dictionary lex that maps regular expressions to labels, and tokenizes the string according to that dictionary. dictionary says jump, the string says how high. π
the result of the command is a list of tokens, where each token is a 3-element list of:
- the
label - the
start-index in the string - the
end-index in the string
β οΈ the command will throw an error if it is not able to tokenize the whole string. she does not do half-finished work, bestie. it's all or nothing. π
π ::string token file lex path
this one is a convenience wrapper around ::string token text above, plus fileutil::cat, so you can tokenize whole files without crying. π
β οΈ note that this command loads the file wholly into memory before it even starts processing. so if your file is a monster, this is not the move.
if the file is too large for this mode of operation, you'll need a command directly based on ::string token chomp below. okay? okay. π«Ά
π ::string token chomp lex startvar string resultvar
this is the work horse behind ::string token text above. the main character, fr fr. she's exposed to the public so you can write your own lexers β ones that, for example, apply different lexing dictionaries according to some internal state. very custom, very you. β¨
the command takes:
- an ordered dictionary
lexmapping regular expressions to labels - a variable
startvar, which indicates where to start lexing in the input string - a result variable
resultvar, to extend
the result of the command is a tri-state numeric code indicating one of:
0β No token found. π1β Token found. π₯2β End of string reached. π
note that recognition of a token from lex is started at the character index in startvar.
if a token was recognized (status 1), the command will:
- update the index in
startvarto point to the first character of the string past the recognized token, and - further extend
resultvarwith a 3-element list containing:- the
labelassociated with the regular expression of the token, - the
start-character-index of the token instring, - the
end-character-index of the token instring.
- the
neither startvar nor resultvar will be updated if no token is recognized at all. like, zero edits, she simply did not see it. π
π‘ 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. order matters bestie, always has. π
π‘ further note that all regex patterns are implicitly prefixed with the constraint escapeAto ensure that a match starts exactly at the character index found instartvar. no wandering off. she starts where you told her to start. π
π BUGS, IDEAS, FEEDBACK π
ngl, this document, and the package it describes, will undoubtedly contain bugs and other problems. it's giving humbling, but honestly that's every software ever. π
please report such in the category textutil of the Tcllib Trackers at:
http://core.tcl.tk/tcllib/reportlist
please also report any ideas for enhancements you may have for either the package and/or the documentation. we want them. we love them. π«Ά
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. iykyk. π
π KEYWORDS π
lexing, regex, string, tokenization
πΈ CATEGORY πΈ
Text processing
and that's the whole page bestie!! this little cutie is from tcllib, version 1.1, manual page string::token(3tcl). go forth and tokenize responsibly, mwah πβ¨π¦π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms