Module macro.Getter
Getter class.
Used to get values from the token stream. The first
argument get
of a macro substitution function is of this type.
M.define ('\\',function(get,put)
local args, body = get:names('('), get:list()
return put:keyword 'function' '(' : names(args) ')' :
keyword 'return' : list(body) : space() : keyword 'end'
end)
The second argument put
is a TokenList
object.
Functions
next () | get the next non-whitespace token. |
list (tok, endt, delim, endtoken) | get a delimited list of token lists. |
name (tok) | get the next identifier token. |
number (tok) | get the next number token. |
names (tok, endt, delim) | get a delimited list of names. |
string (tok) | get the next string token. |
expecting (type, value, tok) | assert that the next token has the given type. |
peek (k, dont_skip) | peek ahead or before in the token stream. |
peek2 () | peek ahead two tokens. |
patch (idx, text) | patch the token stream at the end. |
placeholder (put) | put out a placeholder for later patching. |
Functions
- next ()
-
get the next non-whitespace token.
Returns:
- token type
- token value
- list (tok, endt, delim, endtoken)
-
get a delimited list of token lists.
Typically used for grabbing argument lists like ('hello',a+1,fred(c,d)); will count parens
so that the delimiter (usually a comma) is ignored inside sub-expressions. You must have
already read the start token of the list, e.g. open parentheses. It will eat the end token
and return the list of TLs, plus the end token. Based on similar code in Penlight's
pl.lexer
module.Parameters:
tok
: the token streamendt
: the end token (default ')')delim
: the delimiter (default ',')endtoken
:
Returns:
- list of token lists
- end token in form {type,value}
- name (tok)
-
get the next identifier token.
(will be an error if the token has wrong type)
Parameters:
tok
:
Returns:
-
name
- number (tok)
-
get the next number token.
(will be an error if the token has wrong type)
Parameters:
tok
:
Returns:
-
converted number
- names (tok, endt, delim)
-
get a delimited list of names.
works like list.
Parameters:
tok
: the token streamendt
: the end token (default ')')delim
: the delimiter (default ',')
see also:
- string (tok)
-
get the next string token.
(will be an error if the token has wrong type)
Parameters:
tok
:
Returns:
-
string value (without quotes)
- expecting (type, value, tok)
-
assert that the next token has the given type.
This will throw an
error if the next non-whitespace token does not match.
Parameters:
type
: a token type ('iden','string',etc)value
: a token value (optional)tok
:
Usage:
get:expecting '('
get:expecting ('iden','bonzo')
- peek (k, dont_skip)
-
peek ahead or before in the token stream.
Parameters:
k
: positive delta for looking ahead, negative for looking behind.dont_skip
: true if you want to check for whitespace
Returns:
- the token type
- the token value
- the token offset
- peek2 ()
-
peek ahead two tokens.
Returns:
- first token type
- first token value
- second token type
- second token value
- patch (idx, text)
-
patch the token stream at the end.
Parameters:
idx
: index in output tabletext
: to replace value at that index
- placeholder (put)
-
put out a placeholder for later patching.
Parameters:
put
: a putter object
Returns:
-
an index into the output table