Literal
Matches an ordered sequence of characters.
"content"
Where content
can be any sequence of characters except "
. Special characters can be written in escaped format, please read Escaped Characters for more details.
Example
kw-else = "else" | "Else" | "ELSE"
@pass 'else' kw-else
@pass 'Else' kw-else
@pass 'ELSE' kw-else
@fail 'eLse' kw-else
Reference
Matches an expression declared in the grammar.
name
Where name
must start with a letter (a-z
or A-Z
) and can be followed by any combination of letters, digits (0-9
), underscores (_
) or dashes (-
).
Example
expr = {1 token | group / " "}
token = {1`a..z`}
group = "(" expr ")"
@pass 'abc' expr
@pass 'abc xyz' expr
@pass 'abc (m n) xyz' expr
@fail 'a1' expr
Alternation
Tries to match the expressions and stops in the first one that matches, only one expression must match.
option1 | option2 | optionN
This expression has the lowest precedence; sequences and groups are evaluated before evaluate alternations.
Example
string = "\"" {!"\""} "\"" | "'" {!"\'"} "'"
@pass '\"abc\"' string
@pass '\'abs\'' string
@fail '\'abc\"' string
Sequence
Matches an expression followed by another and so on, all expressions must match.
expression1 expression2 expressionN
This expression has the middle precedence; only groups are evaluated before evaluate sequences.
Example
sum = {1`0..9`} "+" {1`0..9`}
@pass '0+1' sum
@pass '92+68' sum
@fail '1+a' sum
Group
This expression only helps to bump up the precedence of the contained expression. The contained expression is evaluated before surrounding expressions.
(expression)
Example
// any domain ending with .org or .net or localhost
domain = {1`a..z`} (".org" | ".net") | "localhost"
@pass "bakasoft.org" domain
@pass "google.net" domain
@pass "localhost" domain
@fail "localhost.com" domain
Optional
Everything between [
and ]
might not match and however this expression is considered as a correct matching.
[expression]
Example
domain = {1`a..z A..Z 0..9` / "."} [":" {1`0..9`}]
@pass "github.com" domain
@pass "bakasoft.org:8080" domain
@fail "localhost:port" domain
Repetition
Evertying between {
and }
can be repeated a determined number of times. Repetitions can receive an argument to indicate how many times it can be repeated.
{expression}
{expression / separator}
{min expression}
{min,max expression}
{count; expression}
Depending on the argument provided, the repetition can take one of the following behaviors.
- Zero or more times: When the expression doesn’t have repetition arguments, the repetition is optional and unbounded.
- At least
min
times: When it only has a the min
argument, it must be repeated at least the indicated times.
- Between
min
and max
times: When it has min,max
, it must be repeated at least the min
times but no more than the max
times.
- Exactly
count
times: When it has count;
, it must be repeated exactly the indicated amount of times.
Example
dec = `0..9`
hex = `a..f A..F 0..9`
uni-format = "\\u" {4; hex}
dec-format = {1 dec}
hex-format = "0x" {1,4 hex}
@pass '\uABCD' uni-format
@pass '123' dec-format
@pass '0x0E0F' hex-format
@fail '\\u0'
Negation
Keeps moving forward until the expression is matched.
!expression
Example
Predicate
Only matches one character by comparing against characters and character ranges.
`option1 option2 optionN`
The options are separated by whitespace and can be any of following patterns:
- Exact character: Matches exactly the specified character, e.g.
c
.
- Character range: Matches all character between the indicated begin and end characters, e.g.
a..z
.
The characters can be also specified by using Escaped Characters.
Example
Begin of string
This expression matches only if is evaluated at the beginning of the input string.
^
End of string
This expression matches only if is evaluated at the end of the input string.
$