global token
The token library provides means to intercept the input and deal with it at the Lua level. The library provides a basic scanner infrastructure that can be used to write macros that accept a wide range of arguments. This interface is on purpose kept general and as performance is quite ok. One can build additional parsers without too much overhead. It's up to macro package writers to see how they can benefit from this as the main principle behind LuaTeX is to provide a minimal set of tools and no solutions. The scanner functions are probably the most intriguing.
Reference:
- Source file of the
LuaTeX
manual: luatex-tex.tex#L2091-L2098
😱 Types incomplete or incorrect? 🙏 Please contribute!
methods
token.scan_keyword
@param
keyword
- An ASCII based keyword to scan for.
@return - True if the keyword could be gobbled up otherwise false.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Scan and gobble a given keyword.
As with the regular TeX keyword scanner this is case insensitive (and ASCII based).
Example:
\def\scanner{\directlua{
print(token.scan_keyword('keyword'))
}}
\scanner keyword % true
\scanner KEYWORD % true
\scanner not the keyword % false
Reference:
- Corresponding C source code: lnewtokenlib.c#L339-L353
token.scan_keyword_cs
@param
keyword
- A case sensitive and UTF-8 based keyword
@return - True if the case sensitive and UTF-8 based keyword could be gobbled up otherwise false.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Scan and gobble a given case sensitive and UTF-8 based keyword.
Example:
\def\scanner{\directlua{
print(token.scan_keyword_cs('Keyword'))
}}
\scanner Keyword % true
\scanner keyword % false
Reference:
- Corresponding C source code: lnewtokenlib.c#L355-L369
token.scan_int
Scan and gobble a given integer.
Example:
\def\scanner{\directlua{
print(token.scan_int())
}}
\scanner 1 % 1
\scanner 1.1 % 1 (Scans only 1 not 1.1)
\scanner -1 % -1
\scanner 1234567890 % 1234567890
\scanner string % Missing number, treated as zero
\scanner 12345678901 % Number to big
Reference:
- Corresponding C source code: lnewtokenlib.c#L391-L401
token.scan_real
Scan and gobble a floating point number that cannot have an exponent (1E10
is scanned as 1.0
).
Example:
\def\scan{\directlua{
print(token.scan_real())
}}
\scan 1E10 % 1.0 Does not scan “E10“
\scan 1 % 1.0
\scan 1.1 % 1.1
\scan .1 % 0.1
\scan - .1 % -0.1
\scan -1 % -1.0
\scan - 1 % -1.0
\scan 1234567890 % 1234567890.0
Reference:
- Corresponding C source code: lnewtokenlib.c#L530-L533
token.scan_float
Scan and gobble a floating point number that can be provided with an exponent (e. g. 1E10
).
Example:
\def\scan{\directlua{
print(token.scan_float())
}}
\scan 1E10 % 10000000000.0
\scan .1e-10 % 1e-11
\scan 1 % 1.0
\scan 1.1 % 1.1
\scan .1 % 0.1
\scan - .1 % -0.1
\scan -1 % -1.0
\scan - 1 % -1.0
Reference:
- Corresponding C source code: lnewtokenlib.c#L525-L528
token.scan_dimen
@param
inf
- inf values allowed
@param mu
- mu (math units) units required
Returns a number representing a dimension and or two numbers being the filler and order
Example:
Parameter inf
:
\directlua{token.scan_dimen(true)}1fi % 1
\directlua{token.scan_dimen(true)}1fil % 2
\directlua{token.scan_dimen(true)}1fill % 3
\directlua{token.scan_dimen(true)}1filll % 4
Parameter mu
:
\directlua{token.scan_dimen(false, true)}1mu % 65536
\directlua{token.scan_dimen(false, true)}1cm % Illegal unit of measure (mu inserted).
- Corresponding C source code: lnewtokenlib.c#L535-L557
token.scan_glue
function token.scan_glue(mu_units: boolean?) -> GlueSpecNode {
width = integer,
stretch = integer,
stretch_order = integer,
shrink = integer,
shrink_order = integer,
}
returns a glue spec node
Example:
\def\scan{\directlua{
local node = token.scan_glue()
print(node.width, node.stretch, node.stretch_order, node.shrink, node.shrink_order)
}}
\def\scanMu{\directlua{
local node = token.scan_glue(true)
print(node.width, node.stretch, node.stretch_order, node.shrink, node.shrink_order)
}}
\scan 1pt % 65536 0 0 0 0
\scan 1pt plus 2pt % 65536 131072 0 0 0
\scan 1pt minus 3pt % 65536 0 0 196608 0
\scan 1pt plus 2pt minus 3pt % 65536 131072 0 196608 0
\scan 1pt plus 2fi minus 3fi % 65536 131072 1 196608 1
\scan 1pt plus 2fil minus 3fil % 65536 131072 2 196608 2
\scan 1pt plus 2fill minus 3fill % 65536 131072 3 196608 3
\scan 1pt plus 2filll minus 3filll % 65536 131072 4 196608 4
\scan string % Missing number, treated as zero.
\scanMu 3mu % 196608 0 0 0 0
Reference:
- Corresponding C source code: lnewtokenlib.c#L559-L573
token.scan_toks
@param
definer
- macro_def
, \def
Scan a list of tokens delimited by balanced braces.
Example:
\directlua{
local t = token.scan_toks()
for id, tok in ipairs(t) do
print(id, tok, tok.command, tok.cmdname, tok.csname)
end
}{Some text}
Reference:
- Corresponding C source code: lnewtokenlib.c#L575-L602
😱 Types incomplete or incorrect? 🙏 Please contribute!-
token.scan_code
Return a character if its category is in the given bitset (representing catcodes)
Reference:
- Corresponding C source code: lnewtokenlib.c#L730-L756
😱 Types incomplete or incorrect? 🙏 Please contribute!
token.scan_string
@return - A string given between {
}
, as \macro
or as sequence of characters with catcode 11 or 12
😱 Types incomplete or incorrect? 🙏 Please contribute!
Scan and gobble a string.
The string scanner scans for something between curly braces and expands on the
way, or when it sees a control sequence it will return its meaning. Otherwise it
will scan characters with catcode letter
or other
.
Example:
\def\scan{\directlua{
print(token.scan_string())
}}
\def\bar{bar}
\def\foo{\bar}
\scan \foo % bar
\scan {\foo} % bar
\scan {A string} % A string
\scan A string % A
\scan Word1 Word2 % Word1
Reference:
- Corresponding C source code: lnewtokenlib.c#L604-L641
token.scan_argument
@param
expand
- When a braced argument is scanned, expansion can be prohibited by passing false
(default is true
)
Scan and gobble an argument.
This function is simular to token.scan_string
but also accepts a \cs
.
It expands the given argument. When a braced
argument is scanned, expansion can be prohibited by passing false
(default is true
). In case of a control sequence passing false
will result in a one-level expansion (the meaning of the macro).
Example:
\def\scan{\directlua{
print(token.scan_argument(true))
}}
\def\scanNoExpand{\directlua{
print(token.scan_argument(false))
}}
\def\foo{bar}
\scan \foo % bar
\scan { {\bf text} } % {\fam \bffam \tenbf text}
\scanNoExpand { {\bf text} } % {\bf text}
\scan c % c
\scan \bf % \fam \bffam \tenbf
Reference:
- Corresponding C source code: lnewtokenlib.c#L643-L702
token.scan_word
Return a sequence of characters with catcode 11
or 12
as a string.
Reference:
- Corresponding C source code: lnewtokenlib.c#L704-L728
😱 Types incomplete or incorrect? 🙏 Please contribute!
token.scan_csname
Return foo
after scanning \foo
.
Reference:
- Corresponding C source code: lnewtokenlib.c#L371-L389
😱 Types incomplete or incorrect? 🙏 Please contribute!
token.scan_list
Pick up a box specification and return a [h|v]list
node.
Reference:
- Corresponding C source code: lnewtokenlib.c#L1100-L1111
😱 Types incomplete or incorrect? 🙏 Please contribute!
token.get_next
function token.get_next() -> Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}
Scan and gobble the next token.
The different scanner functions of the token
library look for a
sequence of tokens. This function scans just the next token.
Reference:
- Source file of the
LuaTeX
manual: luatex-tex.tex#L2237-L2239 - Corresponding C source code: lnewtokenlib.c#L231-L239
token.scan_token
function token.scan_token() -> Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}
Use scan_token
if you want to
enforce expansion first you can.
Reference:
- Corresponding C source code: lnewtokenlib.c#L1090-L1098
token.expand
Trigger expansion of the next token in the input.
This can be quite unpredictable but when you call it you probably know enough about TeX not to be too worried about that. It basically is a call to the internal expand related function.
Reference:
- Corresponding C source code: lnewtokenlib.c#L764-L769
😱 Types incomplete or incorrect? 🙏 Please contribute!
token.get_command
function token.get_command(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> command integer
@return command
- A number representing the internal command number, for example 147
.
Return the internal command number.
Reference:
- Corresponding C source code: lnewtokenlib.c#L870-L880
@see Token.command
token.get_cmdname
function token.get_cmdname(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> cmdname TokenCommandName
@return cmdname
- The type of the command (for instance the catcode in case of a character or the classifier that determines the internal treatment, for example letter
.
Return the type of the command (for instance the catcode in case of a character or the classifier that determines the internal treatment, for example letter
.
Reference:
- Corresponding C source code: lnewtokenlib.c#L936-L943
@see Token.cmdname
token.get_csname
function token.get_csname(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> csname string?
@return csname
- The associated control sequence (if applicable), for example bigskip
.
Return the associated control sequence (if applicable), for example bigskip
.
Reference:
- Corresponding C source code: lnewtokenlib.c#L945-L959
@see Token.csname
token.get_id
function token.get_id(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> id integer
@return id
- The unique id of the token, for example 6876
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Return the unique id of the token.
Reference:
- Corresponding C source code: lnewtokenlib.c#L961-L966
token.get_tok
function token.get_tok(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> tok integer
@return tok
- The full token number as stored in TeX, for example 536883863
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Return the full token number as stored in TeX.
Reference:
- Corresponding C source code: lnewtokenlib.c#L968-L974
@see Token.tok
token.get_active
function token.get_active(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> active boolean
@return active
- A boolean indicating the active state of the token, for example true
.
Return a boolean indicating the active state of the token, for example true
.
Reference:
- Corresponding C source code: lnewtokenlib.c#L976-L991
@see Token.active
token.get_expandable
function token.get_expandable(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> expandable boolean
@return expandable
- A boolean indicating if the token (macro) is expandable, for example true
.
Return a boolean indicating if the token (macro) is expandable.
Reference:
- Corresponding C source code: lnewtokenlib.c#L993-L1004
@see Token.expandable
token.get_protected
function token.get_protected(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> protected boolean
@return protected
- A boolean indicating if the token (macro) is protected, for example false
.
Return a boolean indicating if the token (macro) is protected.
Reference:
- Corresponding C source code: lnewtokenlib.c#L1006-L1022
@see Token.protected
token.get_mode
function token.get_mode(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> mode integer
@return mode
- A number either representing a character or another entity, for example 1007
.
Return a number either representing a character or another entity.
Reference:
- Corresponding C source code: lnewtokenlib.c#L924-L934
@see Token.mode
token.get_index
function token.get_index(t: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}) -> index integer
@return index
- A number running from 0x0000 upto 0xFFFF indicating a TeX register index, for example 1007
.
Return a number running from 0x0000
upto 0xFFFF
indicating a TeX register index.
Reference:
- Corresponding C source code: lnewtokenlib.c#L882-L922
@see Token.index
token.get_macro
@param
name
- The name of the macro without the leading backslash.
@return - for example foo #1 bar
.
Get the content of a macro.
Reference:
- Corresponding C source code: lnewtokenlib.c#L1141-L1166
@see token.set_macro
token.get_meaning
@param
name
- The name of the macro without the leading backslash.
@return - for example ->foo #1 bar
.
😱 Types incomplete or incorrect? 🙏 Please contribute!
Get the meaning of a macro including the argument
specification (as usual in TeX separated by ->
).
Reference:
- Corresponding C source code: lnewtokenlib.c#L1121-L1139
token.commands
Ask for a list of commands.
Reference:
- Corresponding C source code: lnewtokenlib.c#L1373-L1382
token.command_id
@param
cmdname
- for example letter
@return __Reference
- :__
- Corresponding C source code: lnewtokenlib.c#L201-L213
Return the id of a token class.
token.create
function token.create(
chr: integer,
cmd: integer?
) -> Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}
Create a token.
Reference:
- Corresponding C source code: lnewtokenlib.c#L826-L845
@see token.new
token.new
function token.new(
chr: integer,
cmd: integer
) -> Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
}
A variant that ignores the current catcode table is:
Reference:
- Corresponding C source code: lnewtokenlib.c#L847-L854
@see token.create
token.is_defined
Example:
\def\foo{bar}
\directlua{
print(token.is_defined('foo')) % true
print(token.is_defined('nofoo')) % false
print(token.is_defined('bf')) % true
}
Reference:
- Corresponding C source code: lnewtokenlib.c#L807-L824
token.biggest_char
Example:
Reference:
- Corresponding C source code: lnewtokenlib.c#L185-L189
token.set_macro
Create a macro.
Example:
Reference:
- Source file of the
LuaTeX
manual: luatex-tex.tex#L2368-L2382 - Corresponding C source code: lnewtokenlib.c#L1218-L1342
token.set_macro
function token.set_macro(
catcodetable: integer,
csname: string,
content: string?,
global: "global"?
)
catcodetable
- A catcodetable identifier.
Create a macro.
Example:
Reference:
- Source file of the
LuaTeX
manual: luatex-tex.tex#L2368-L2382 - Corresponding C source code: lnewtokenlib.c#L1218-L1342
token.set_char
Do a chardef
at the
Lua end, where invalid assignments are silently ignored.
Example:
\directlua{
token.set_char('myT', 84)
token.set_char('mye', 101)
token.set_char('myX', 88)
}
\myT\mye\myX % TeX
Reference:
- Corresponding C source code: lnewtokenlib.c#L1344-L1371
token.set_lua
local index = 1 while t[index] do index = index + 1 end
t[index] = function(slot) print(slot) end token.set_lua('mycode', index, 'protected', 'global') }
\mycode
\bye
__Reference:__
* Corresponding C source code: [lnewtokenlib.c#L1168-L1216](https://gitlab.lisn.upsaclay.fr/texlive/luatex/-/blob/f52b099f3e01d53dc03b315e1909245c3d5418d3/source/texk/web2c/luatexdir/lua/lnewtokenlib.c#L1168-L1216)
@see lua.get_functions_table
### token.put_next
---
```lua
function token.put_next(...: Token {
command = integer,
cmdname = TokenCommandName,
csname = string?,
id = integer,
tok = integer,
active = boolean,
expandable = boolean,
protected = boolean,
mode = integer,
index = integer?,
})
Put the next token back in the input.
Example:
local t1 = token.get_next()
local t2 = token.get_next()
local t3 = token.get_next()
local t4 = token.get_next()
-- watch out, we flush in sequence
token.put_next { t1, t2 }
-- but this one gets pushed in front
token.put_next ( t3, t4 )
-- so when we get wxyz we put yzwx!
Reference:
- Source file of the
LuaTeX
manual: luatex-tex.tex#L2422-L2433 - Corresponding C source code: lnewtokenlib.c#L262-L337
token.is_token
Check if the given argument is a token.
Example:
\directlua{
local t = token.get_next()
print(token.is_token(t)) % true
print(token.is_token('text')) % false
print(token.is_token(true)) % false
}Token
Reference:
- Corresponding C source code: lnewtokenlib.c#L758-L762
@see token.type
token.type
Return the string token
if the given parameter is a token else nil
.
Example:
\directlua{
local t = token.get_next()
print(token.type(t)) % 'token'
print(token.type('text')) % nil
print(token.type(true)) % nil
}Token
Reference:
- Corresponding C source code: lnewtokenlib.c#L1080-L1088
@see token.is_token