string::token(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

string::token(n) πŸ’…βœ¨

okay bestie, welcome to string::token from the tcllib text and string utilities corner of the world πŸŽ€ this one's the regex-based iterative lexing girlie and she is lowkey iconic.


✨ NAME ✨

string::token β€” Regex based iterative lexing πŸ¦‹


🌸 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

πŸ’– DESCRIPTION πŸ’–

so basically this package hands you commands for regular expression based lexing (aka tokenization) of strings. no cap.

the complete set of procedures is described below, and yes we're doing all of them, every single one, periodt. πŸ‘‘


🧚 ::string token text lex string

This command takes an ordered dictionary lex mapping regular expressions to labels, and tokenizes the string according to this dictionary. 🫢

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. 😭 (she does not do half-finished work, it's giving perfectionist)

πŸ¦‹ ::string token file lex path

This command is a convenience wrapper around ::string token text above, and fileutil::cat, enabling the easy tokenization of whole files. 🌈

Note that this command loads the file wholly into memory before starting to process it. πŸ’€

If the file is too large for this mode of operation a command directly based on ::string token chomp below will be necessary. πŸ™ˆ


πŸ‘‘ ::string token chomp lex startvar string resultvar

This command is the work horse underlying ::string token text above. 🐎 It is exposed to enable users to write their own lexers, which, for example may apply different lexing dictionaries according to some internal state, etc. main character energy fr fr.

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:

  • 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 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. πŸ’… (order matters bestie, don't come for me)
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. ⭐

🫢 BUGS, IDEAS, FEEDBACK 🫢

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 at http://core.tcl.tk/tcllib/reportlist. Please also report any ideas for enhancements you may have for either package and/or documentation. we love feedback, it's giving community. 🌸

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. πŸ’…


⭐ KEYWORDS ⭐

lexing, regex, string, tokenization


πŸŽ€ CATEGORY πŸŽ€

Text processing


okay that's the whole page bestie, you ate that. this one's from tcllib, version 1.1, and that's the string::token(n) moment β€” ily, go tokenize something πŸ’œβœ¨πŸ¦‹

πŸ“¦ where this page comes from