Module pl.sip
Simple Input Patterns (SIP).
SIP patterns start with ‘$’, then a one-letter type, and then an optional variable in curly braces.
sip.match('$v=$q','name="dolly"',res)
==> res=={'name','dolly'}
sip.match('($q{first},$q{second})','("john","smith")',res)
==> res=={second='smith',first='john'}
‘'Type names’'
v identifier
i integer
f floating-point
q quoted string
([{< match up to closing bracket
See the Guide
Functions
create_pattern (spec, options) | convert a SIP pattern into the equivalent Lua string pattern. |
compile (spec, options) | convert a SIP pattern into a matching function. |
match (spec, line, res, options) | match a SIP pattern against a string. |
match_at_start (spec, line, res) | match a SIP pattern against the start of a string. |
fields (spec, f) | given a pattern and a file object, return an iterator over the results |
pattern (spec, fun) | register a match which will be used in the read function. |
read (f) | enter a loop which applies all registered matches to the input file. |
Functions
- create_pattern (spec, options)
-
convert a SIP pattern into the equivalent Lua string pattern.
Parameters:
spec
: a SIP patternoptions
: a table; only theat_start
field is currently meaningful and esures that the pattern is anchored at the start of the string.
Returns:
-
a Lua string pattern.
- compile (spec, options)
-
convert a SIP pattern into a matching function.
The returned function takes two arguments, the line and an empty table.
If the line matched the pattern, then this function return true
and the table is filled with field-value pairs.
Parameters:
spec
: a SIP patternoptions
: optional table; {anywhere=true} will stop pattern anchoring at start
Returns:
-
a function if successful, or nil,
- match (spec, line, res, options)
-
match a SIP pattern against a string.
Parameters:
spec
: a SIP patternline
: a stringres
: a table to receive valuesoptions
: (optional) option table
Returns:
-
true or false
- match_at_start (spec, line, res)
-
match a SIP pattern against the start of a string.
Parameters:
spec
: a SIP patternline
: a stringres
: a table to receive values
Returns:
-
true or false
- fields (spec, f)
-
given a pattern and a file object, return an iterator over the results
Parameters:
spec
: a SIP patternf
: a file – use standard input if not specified.
- pattern (spec, fun)
-
register a match which will be used in the read function.
Parameters:
spec
: a SIP patternfun
: a function to be called with the results of the match
see also:
- read (f)
-
enter a loop which applies all registered matches to the input file.
Parameters:
f
: a file object; if nil, then io.stdin is assumed.