Grammar Documentation
Grammar Reference
grammarDef
entry pointThe top-level rule. A grammar file begins with an optional doc comment,
an optional grammar type keyword ('lexer', 'parser', or 'tree'), the
'grammar' keyword, a name, a semicolon, optional options, tokens, and
attribute scope sections, followed by one or more rules.
an optional grammar type keyword ('lexer', 'parser', or 'tree'), the
'grammar' keyword, a name, a semicolon, optional options, tokens, and
attribute scope sections, followed by one or more rules.
Grammar definition
grammarDef
: DOC_COMMENT?
( 'lexer' {gtype=LEXER_GRAMMAR;} // pure lexer
| 'parser' {gtype=PARSER_GRAMMAR;} // pure parser
| 'tree' {gtype=TREE_GRAMMAR;} // a tree parser
| {gtype=COMBINED_GRAMMAR;} // merged parser/lexer
)
g='grammar' id ';' optionsSpec? tokensSpec? attrScope* action*
rule+
EOF
-> ^( {adaptor.create(gtype,$g)}
id DOC_COMMENT? optionsSpec? tokensSpec? attrScope* action* rule+
)
;
Railroad diagram
References
tokensSpec
The tokens section. Declares imaginary token types used in AST
construction that do not correspond to real input tokens.
Delimited by the 'tokens' keyword and a closing '}'.
construction that do not correspond to real input tokens.
Delimited by the 'tokens' keyword and a closing '}'.
Grammar definition
tokensSpec
: TOKENS tokenSpec+ '}' -> ^(TOKENS tokenSpec+)
;
Railroad diagram
Referenced by
References
tokenSpec
A single token declaration within a tokens section. A token may
optionally be assigned a string literal or character literal value
using '='. Terminated by ';'.
optionally be assigned a string literal or character literal value
using '='. Terminated by ';'.
Grammar definition
tokenSpec
: TOKEN_REF
( '=' (lit=STRING_LITERAL|lit=CHAR_LITERAL) -> ^('=' TOKEN_REF $lit)
| -> TOKEN_REF
)
';'
;
Railroad diagram
Referenced by
References
attrScope
An attribute scope declaration at grammar level. Defines a named
set of shared variables accessible within a dynamic scope using
'scope id ACTION'.
set of shared variables accessible within a dynamic scope using
'scope id ACTION'.
Grammar definition
attrScope
: 'scope' id ACTION -> ^('scope' id ACTION)
;
Railroad diagram
Referenced by
References
action
A grammar-level named action such as @members or @header.
Optionally scoped to a specific component (e.g. @parser::members).
Optionally scoped to a specific component (e.g. @parser::members).
Grammar definition
action
: '@' (actionScopeName '::')? id ACTION -> ^('@' actionScopeName? id ACTION)
;
Railroad diagram
Referenced by
References
actionScopeName
The scope name in a grammar-level action. Normally an identifier,
but 'lexer' and 'parser' are also permitted as scope names even
though they are keywords.
but 'lexer' and 'parser' are also permitted as scope names even
though they are keywords.
Grammar definition
actionScopeName
: id
| l='lexer' -> ID[$l]
| p='parser' -> ID[$p]
;
Railroad diagram
Referenced by
References
optionsSpec
The options section. Contains one or more key=value option pairs
delimited by the 'options' keyword and a closing '}'.
delimited by the 'options' keyword and a closing '}'.
Grammar definition
optionsSpec
: OPTIONS (option ';')+ '}' -> ^(OPTIONS option+)
;
Railroad diagram
Referenced by
References
option
A single key=value option pair within an options section.
Grammar definition
option
: id '=' optionValue -> ^('=' id optionValue)
;
Railroad diagram
Referenced by
References
optionValue
The value of an option. May be an identifier, string literal,
character literal, integer, or '*' (used for k=*).
character literal, integer, or '*' (used for k=*).
Grammar definition
optionValue
: id
| STRING_LITERAL
| CHAR_LITERAL
| INT
| s='*' -> STRING_LITERAL[$s] // used for k=*
;
Railroad diagram
Referenced by
References
rule
A single grammar rule. May be a parser rule (lowercase name) or a
lexer rule (uppercase name). Optionally preceded by a doc comment
and an access modifier ('protected', 'public', 'private', or
'fragment'). May include arguments, return values, a throws clause,
options, scope declarations, and named actions. The rule body is a
colon-separated list of alternatives terminated by ';'.
lexer rule (uppercase name). Optionally preceded by a doc comment
and an access modifier ('protected', 'public', 'private', or
'fragment'). May include arguments, return values, a throws clause,
options, scope declarations, and named actions. The rule body is a
colon-separated list of alternatives terminated by ';'.
Grammar definition
rule
scope {
String name;
}
: DOC_COMMENT?
( modifier=('protected'|'public'|'private'|'fragment') )?
id {$rule::name = $id.text;}
'!'?
( arg=ARG_ACTION )?
( 'returns' rt=ARG_ACTION )?
throwsSpec? optionsSpec? ruleScopeSpec? ruleAction*
':' altList ';'
exceptionGroup?
-> ^( RULE id {modifier!=null?adaptor.create(modifier):null} ^(ARG $arg)? ^(RET $rt)?
optionsSpec? ruleScopeSpec? ruleAction*
altList
exceptionGroup?
EOR["EOR"]
)
;
Railroad diagram
Referenced by
References
ruleAction
A named action attached to a rule, such as @init or @after.
Executed at the start or end of the rule respectively.
Executed at the start or end of the rule respectively.
Grammar definition
ruleAction
: '@' id ACTION -> ^('@' id ACTION)
;
Railroad diagram
Referenced by
References
throwsSpec
A throws clause listing the exception types a rule may throw.
Introduced by 'throws' followed by a comma-separated list of
identifiers.
Introduced by 'throws' followed by a comma-separated list of
identifiers.
Grammar definition
throwsSpec
: 'throws' id ( ',' id )* -> ^('throws' id+)
;
Railroad diagram
Referenced by
References
ruleScopeSpec
The scope specification for a rule. Defines a dynamic scope local
to the rule (ACTION form), references a previously declared named
scope (id list form), or both.
to the rule (ACTION form), references a previously declared named
scope (id list form), or both.
Grammar definition
ruleScopeSpec
: 'scope' ACTION -> ^('scope' ACTION)
| 'scope' id (',' id)* ';' -> ^('scope' id+)
| 'scope' ACTION
'scope' id (',' id)* ';'
-> ^('scope' ACTION id+ )
;
Railroad diagram
Referenced by
References
block
A parenthesized block of alternatives. May have an optional options
section and colon separator before the first alternative. Used for
subrules and token sets.
section and colon separator before the first alternative. Used for
subrules and token sets.
Grammar definition
block
: lp='('
( (opts=optionsSpec)? ':' )?
a1=alternative rewrite ( '|' a2=alternative rewrite )*
rp=')'
-> ^( BLOCK[$lp,"BLOCK"] optionsSpec? (alternative rewrite?)+ EOB[$rp,"EOB"] )
;
Railroad diagram
Referenced by
References
altList
A list of alternatives at rule level, separated by '|'. Forms the
body of a rule between ':' and ';'.
body of a rule between ':' and ';'.
Grammar definition
altList
@init {
// must create root manually as it's used by invoked rules in real antlr tool.
// leave here to demonstrate use of {...} in rewrite rule
// it's really BLOCK[firstToken,"BLOCK"]; set line/col to previous ( or : token.
CommonTree blkRoot = (CommonTree)adaptor.create(BLOCK,input.LT(-1),"BLOCK");
}
: a1=alternative rewrite ( '|' a2=alternative rewrite )*
-> ^( {blkRoot} (alternative rewrite?)+ EOB["EOB"] )
;
Railroad diagram
Referenced by
References
alternative
A single alternative — a sequence of one or more elements. An empty
alternative (epsilon) is also permitted and produces an EPSILON node
in the AST.
alternative (epsilon) is also permitted and produces an EPSILON node
in the AST.
Grammar definition
alternative
@init {
Token firstToken = input.LT(1);
Token prevToken = input.LT(-1); // either : or | I think
}
: element+ -> ^(ALT[firstToken,"ALT"] element+ EOA["EOA"])
| -> ^(ALT[prevToken,"ALT"] EPSILON[prevToken,"EPSILON"] EOA["EOA"])
;
Railroad diagram
Referenced by
References
exceptionGroup
A group of exception handlers and an optional finally clause
following a rule body. At least one exceptionHandler or a
finallyClause must be present.
following a rule body. At least one exceptionHandler or a
finallyClause must be present.
Grammar definition
exceptionGroup
: ( exceptionHandler )+ ( finallyClause )?
| finallyClause
;
Railroad diagram
Referenced by
References
exceptionHandler
A single catch clause. Consists of 'catch', an argument action
specifying the exception type and variable, and an action body.
specifying the exception type and variable, and an action body.
Grammar definition
exceptionHandler
: 'catch' ARG_ACTION ACTION -> ^('catch' ARG_ACTION ACTION)
;
Railroad diagram
Referenced by
References
finallyClause
A finally clause. Consists of 'finally' and an action body that
is always executed after the rule completes.
is always executed after the rule completes.
Grammar definition
finallyClause
: 'finally' ACTION -> ^('finally' ACTION)
;
Railroad diagram
Referenced by
References
element
A single element within an alternative. Delegates to
elementNoOptionSpec.
elementNoOptionSpec.
Grammar definition
element
: elementNoOptionSpec
;
Railroad diagram
Referenced by
References
elementNoOptionSpec
An element within an alternative, without options. May be a labeled
atom or block (using '=' or '+=' assignment), an atom or block with
an optional EBNF suffix, a standalone EBNF expression, an action,
a semantic predicate, or a tree specification.
atom or block (using '=' or '+=' assignment), an atom or block with
an optional EBNF suffix, a standalone EBNF expression, an action,
a semantic predicate, or a tree specification.
Grammar definition
elementNoOptionSpec
: id (labelOp='='|labelOp='+=') atom
( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] ^($labelOp id atom) EOA["EOA"]) EOB["EOB"]))
| -> ^($labelOp id atom)
)
| id (labelOp='='|labelOp='+=') block
( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] ^($labelOp id block) EOA["EOA"]) EOB["EOB"]))
| -> ^($labelOp id block)
)
| atom
( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] atom EOA["EOA"]) EOB["EOB"]) )
| -> atom
)
| ebnf
| ACTION
| SEMPRED ( '=>' -> GATED_SEMPRED | -> SEMPRED )
| treeSpec
( ebnfSuffix -> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] treeSpec EOA["EOA"]) EOB["EOB"]) )
| -> treeSpec
)
;
Railroad diagram
Referenced by
References
atom
An atomic grammar element. May be a character range, a terminal
symbol, a negated set, or a rule reference. Rule references and
terminals may optionally be suffixed with '^' (AST root) or '!'
(exclude from AST). Rule references may also carry an argument
action.
symbol, a negated set, or a rule reference. Rule references and
terminals may optionally be suffixed with '^' (AST root) or '!'
(exclude from AST). Rule references may also carry an argument
action.
Grammar definition
atom: range ( (op='^'|op='!') -> ^($op range) | -> range )
| terminal
| notSet ( (op='^'|op='!') -> ^($op notSet) | -> notSet )
| RULE_REF ( arg=ARG_ACTION )? ( (op='^'|op='!') )?
-> {$arg!=null&&op!=null}? ^($op RULE_REF $arg)
-> {$arg!=null}? ^(RULE_REF $arg)
-> {$op!=null}? ^($op RULE_REF)
-> RULE_REF
;
Railroad diagram
Referenced by
References
notSet
A negated element using '~'. Matches any token or character not in
the specified terminal or block. Used in both parser and lexer rules.
the specified terminal or block. Used in both parser and lexer rules.
Grammar definition
notSet
: '~'
( notTerminal -> ^('~' notTerminal)
| block -> ^('~' block)
)
;
Railroad diagram
Referenced by
References
treeSpec
A tree construction pattern used in tree grammar rules.
Introduced by '^(' and closed by ')'. Must contain at least two
elements — a root node and one or more child elements.
Introduced by '^(' and closed by ')'. Must contain at least two
elements — a root node and one or more child elements.
Grammar definition
treeSpec
: '^(' element ( element )+ ')' -> ^(TREE_BEGIN element+)
;
Railroad diagram
Referenced by
References
ebnf
An EBNF expression: a block optionally followed by '?' (optional),
'*' (closure), or '+' (positive closure). '=>' introduces a
syntactic predicate. A block with no suffix is treated as a plain
grouped alternative.
'*' (closure), or '+' (positive closure). '=>' introduces a
syntactic predicate. A block with no suffix is treated as a plain
grouped alternative.
Grammar definition
ebnf
@init {
Token firstToken = input.LT(1);
}
@after {
$ebnf.tree.getToken().setLine(firstToken.getLine());
$ebnf.tree.getToken().setCharPositionInLine(firstToken.getCharPositionInLine());
}
: block
( op='?' -> ^(OPTIONAL[op] block)
| op='*' -> ^(CLOSURE[op] block)
| op='+' -> ^(POSITIVE_CLOSURE[op] block)
| '=>' // syntactic predicate
-> {gtype==COMBINED_GRAMMAR &&
Character.isUpperCase($rule::name.charAt(0))}?
// if lexer rule in combined, leave as pred for lexer
^(SYNPRED["=>"] block)
// in real antlr tool, text for SYN_SEMPRED is predname
-> SYN_SEMPRED
| -> block
)
;
Railroad diagram
Referenced by
References
range
A character range used in lexer rules. Matches a single character
between two character literals, inclusive, using the '..' operator.
between two character literals, inclusive, using the '..' operator.
Grammar definition
range!
: c1=CHAR_LITERAL RANGE c2=CHAR_LITERAL -> ^(CHAR_RANGE[$c1,".."] $c1 $c2)
;
Railroad diagram
Referenced by
References
CHAR_LITERALRANGE
terminal
A terminal element. May be a character literal, a token reference
(optionally with arguments), a string literal, or '.' (wildcard
matching any token or character). May be suffixed with '^' (AST
root) or '!' (exclude from AST).
(optionally with arguments), a string literal, or '.' (wildcard
matching any token or character). May be suffixed with '^' (AST
root) or '!' (exclude from AST).
Grammar definition
terminal
: ( CHAR_LITERAL -> CHAR_LITERAL
// Args are only valid for lexer rules
| TOKEN_REF
( ARG_ACTION -> ^(TOKEN_REF ARG_ACTION)
| -> TOKEN_REF
)
| STRING_LITERAL -> STRING_LITERAL
| '.' -> '.'
)
( '^' -> ^('^' $terminal)
| '!' -> ^('!' $terminal)
)?
;
Railroad diagram
Referenced by
References
notTerminal
A terminal element used within a negated set ('~'). May be a
character literal, token reference, or string literal. Does not
permit AST operators or arguments.
character literal, token reference, or string literal. Does not
permit AST operators or arguments.
Grammar definition
notTerminal
: CHAR_LITERAL
| TOKEN_REF
| STRING_LITERAL
;
Railroad diagram
Referenced by
References
ebnfSuffix
An EBNF suffix operator applied to an atom or block. '?' makes the
preceding element optional, '*' allows zero or more repetitions,
and '+' requires one or more repetitions.
preceding element optional, '*' allows zero or more repetitions,
and '+' requires one or more repetitions.
Grammar definition
ebnfSuffix
@init {
Token op = input.LT(1);
}
: '?' -> OPTIONAL[op]
| '*' -> CLOSURE[op]
| '+' -> POSITIVE_CLOSURE[op]
;
Railroad diagram
Referenced by
rewrite
A rewrite rule following an alternative, introduced by '->'.
Multiple predicated rewrites may precede the final unconditional
rewrite. If no '->' is present the alternative produces no rewrite.
Multiple predicated rewrites may precede the final unconditional
rewrite. If no '->' is present the alternative produces no rewrite.
Grammar definition
rewrite
@init {
Token firstToken = input.LT(1);
}
: (rew+='->' preds+=SEMPRED predicated+=rewrite_alternative)*
rew2='->' last=rewrite_alternative
-> ^($rew $preds $predicated)* ^($rew2 $last)
|
;
Railroad diagram
Referenced by
References
SEMPREDrewrite_alternative
rewrite_alternative
A single rewrite alternative. May be a StringTemplate template
reference, a tree construction alternative, or empty (epsilon).
reference, a tree construction alternative, or empty (epsilon).
Grammar definition
rewrite_alternative
options {backtrack=true;}
: rewrite_template
| rewrite_tree_alternative
| /* empty rewrite */ -> ^(ALT["ALT"] EPSILON["EPSILON"] EOA["EOA"])
;
Railroad diagram
Referenced by
References
rewrite_tree_block
A parenthesized block within a tree rewrite expression.
Grammar definition
rewrite_tree_block
: lp='(' rewrite_tree_alternative ')'
-> ^(BLOCK[$lp,"BLOCK"] rewrite_tree_alternative EOB[$lp,"EOB"])
;
Railroad diagram
Referenced by
References
rewrite_tree_alternative
A sequence of one or more tree rewrite elements forming a single
rewrite alternative.
rewrite alternative.
Grammar definition
rewrite_tree_alternative
: rewrite_tree_element+ -> ^(ALT["ALT"] rewrite_tree_element+ EOA["EOA"])
;
Railroad diagram
Referenced by
References
rewrite_tree_element
A single element within a tree rewrite alternative. May be an atom,
an atom with an EBNF suffix, a tree node with an optional suffix,
or an EBNF block.
an atom with an EBNF suffix, a tree node with an optional suffix,
or an EBNF block.
Grammar definition
rewrite_tree_element
: rewrite_tree_atom
| rewrite_tree_atom ebnfSuffix
-> ^( ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] rewrite_tree_atom EOA["EOA"]) EOB["EOB"]))
| rewrite_tree
( ebnfSuffix
-> ^(ebnfSuffix ^(BLOCK["BLOCK"] ^(ALT["ALT"] rewrite_tree EOA["EOA"]) EOB["EOB"]))
| -> rewrite_tree
)
| rewrite_tree_ebnf
;
Railroad diagram
Referenced by
References
rewrite_tree_atom
An atomic element within a tree rewrite expression. May be a
character literal, token reference (with optional arguments),
rule reference, string literal, label reference (via '$'), or
an action.
character literal, token reference (with optional arguments),
rule reference, string literal, label reference (via '$'), or
an action.
Grammar definition
rewrite_tree_atom
: CHAR_LITERAL
| TOKEN_REF ARG_ACTION? -> ^(TOKEN_REF ARG_ACTION?) // for imaginary nodes
| RULE_REF
| STRING_LITERAL
| d='$' id -> LABEL[$d,$id.text] // reference to a label in a rewrite rule
| ACTION
;
Railroad diagram
Referenced by
References
rewrite_tree_ebnf
An EBNF expression within a tree rewrite: a parenthesized block
followed by an EBNF suffix operator ('?', '*', or '+').
followed by an EBNF suffix operator ('?', '*', or '+').
Grammar definition
rewrite_tree_ebnf
@init {
Token firstToken = input.LT(1);
}
@after {
$rewrite_tree_ebnf.tree.getToken().setLine(firstToken.getLine());
$rewrite_tree_ebnf.tree.getToken().setCharPositionInLine(firstToken.getCharPositionInLine());
}
: rewrite_tree_block ebnfSuffix -> ^(ebnfSuffix rewrite_tree_block)
;
Railroad diagram
Referenced by
References
rewrite_tree
A tree node construction in a rewrite rule. Introduced by '^('
with a root atom followed by zero or more child elements, closed
by ')'.
with a root atom followed by zero or more child elements, closed
by ')'.
Grammar definition
rewrite_tree
: '^(' rewrite_tree_atom rewrite_tree_element* ')'
-> ^(TREE_BEGIN rewrite_tree_atom rewrite_tree_element* )
;
Railroad diagram
Referenced by
References
rewrite_template
A StringTemplate template rewrite. May be an inline template
definition, a named template reference, an indirect template
reference (expression in parentheses), or a raw action that
evaluates to a StringTemplate instance.
definition, a named template reference, an indirect template
reference (expression in parentheses), or a raw action that
evaluates to a StringTemplate instance.
Grammar definition
rewrite_template
: // -> template(a={...},...) "..." inline template
id lp='(' rewrite_template_args ')'
( str=DOUBLE_QUOTE_STRING_LITERAL | str=DOUBLE_ANGLE_STRING_LITERAL )
-> ^(TEMPLATE[$lp,"TEMPLATE"] id rewrite_template_args $str)
| // -> foo(a={...}, ...)
rewrite_template_ref
| // -> ({expr})(a={...}, ...)
rewrite_indirect_template_head
| // -> {...}
ACTION
;
Railroad diagram
Referenced by
References
rewrite_template_ref
A named StringTemplate template reference with arguments.
e.g. -> foo(a={...}, ...)
e.g. -> foo(a={...}, ...)
Grammar definition
rewrite_template_ref
: id lp='(' rewrite_template_args ')'
-> ^(TEMPLATE[$lp,"TEMPLATE"] id rewrite_template_args)
;
Railroad diagram
Referenced by
References
rewrite_indirect_template_head
An indirect StringTemplate template reference where the template
name is computed by an expression in parentheses.
e.g. -> ({expr})(a={...}, ...)
name is computed by an expression in parentheses.
e.g. -> ({expr})(a={...}, ...)
Grammar definition
rewrite_indirect_template_head
: lp='(' ACTION ')' '(' rewrite_template_args ')'
-> ^(TEMPLATE[$lp,"TEMPLATE"] ACTION rewrite_template_args)
;
Railroad diagram
Referenced by
References
rewrite_template_args
The argument list for a StringTemplate template rewrite. A
comma-separated list of named arguments, or empty.
comma-separated list of named arguments, or empty.
Grammar definition
rewrite_template_args
: rewrite_template_arg (',' rewrite_template_arg)*
-> ^(ARGLIST rewrite_template_arg+)
| -> ARGLIST
;
Railroad diagram
Referenced by
References
rewrite_template_arg
A single named argument in a template argument list.
e.g. a={...}
e.g. a={...}
Grammar definition
rewrite_template_arg
: id '=' ACTION -> ^(ARG[$id.start] id ACTION)
;
Railroad diagram
Referenced by
References
id
An identifier. TOKEN_REF and RULE_REF are both mapped to the
imaginary ID token type, normalizing the distinction between
uppercase and lowercase names where a plain identifier is needed.
imaginary ID token type, normalizing the distinction between
uppercase and lowercase names where a plain identifier is needed.
Grammar definition
id : TOKEN_REF -> ID[$TOKEN_REF]
| RULE_REF -> ID[$RULE_REF]
;
Railroad diagram
Referenced by
grammarDefattrScopeactionactionScopeNameoptionoptionValueruleruleActionthrowsSpecruleScopeSpecelementNoOptionSpecrewrite_tree_atomrewrite_templaterewrite_template_refrewrite_template_arg
References
SL_COMMENT
A single-line comment beginning with '//'. Routed to the hidden
channel. Also handles the special '$ANTLR src' directive which
resets the reported file and line number.
channel. Also handles the special '$ANTLR src' directive which
resets the reported file and line number.
Grammar definition
SL_COMMENT
: '//'
( ' $ANTLR ' SRC // src directive
| ~('\r'|'\n')*
)
'\r'? '\n'
{$channel=HIDDEN;}
;
Railroad diagram
Referenced by
References
ML_COMMENT
A multi-line comment delimited by '/*' and '* /'. Routed to the
hidden channel unless the next character after '/*' is '*', in
which case it is classified as a DOC_COMMENT.
hidden channel unless the next character after '/*' is '*', in
which case it is classified as a DOC_COMMENT.
Grammar definition
ML_COMMENT
: '/*' {if (input.LA(1)=='*') $type=DOC_COMMENT; else $channel=HIDDEN;} .* '*/'
;
Railroad diagram
Referenced by
CHAR_LITERAL
A single-quoted character literal. Contains exactly one
LITERAL_CHAR (which may be an escape sequence).
LITERAL_CHAR (which may be an escape sequence).
Grammar definition
CHAR_LITERAL
: '\'' LITERAL_CHAR '\''
;
Railroad diagram
Referenced by
References
STRING_LITERAL
A single-quoted string literal. Contains two or more LITERAL_CHAR
characters. (A single character is matched as CHAR_LITERAL instead.)
characters. (A single character is matched as CHAR_LITERAL instead.)
Grammar definition
STRING_LITERAL
: '\'' LITERAL_CHAR LITERAL_CHAR* '\''
;
Railroad diagram
Referenced by
References
LITERAL_CHAR
A single character within a quoted literal. May be an escape
sequence or any character except single quote and backslash.
sequence or any character except single quote and backslash.
Grammar definition
fragment
LITERAL_CHAR
: ESC
| ~('\''|'\\')
;
Railroad diagram
Referenced by
References
DOUBLE_QUOTE_STRING_LITERAL
A double-quoted string literal. Used in StringTemplate template
inline definitions in rewrite rules.
inline definitions in rewrite rules.
Grammar definition
DOUBLE_QUOTE_STRING_LITERAL
: '"' (ESC | ~('\\'|'"'))* '"'
;
Railroad diagram
Referenced by
References
DOUBLE_ANGLE_STRING_LITERAL
A double-angle-bracket string literal using '<<' and '>>'.
Used as an alternative form of inline StringTemplate definition.
Used as an alternative form of inline StringTemplate definition.
Grammar definition
DOUBLE_ANGLE_STRING_LITERAL
: '<<' .* '>>'
;
Railroad diagram
Referenced by
ESC
An escape sequence within a string or character literal. Supports
standard escapes (n, r, t, b, f, quote, backslash), '>' (used in
StringTemplate), and Unicode escapes (\uXXXX). Unknown escapes are
passed through as-is.
standard escapes (n, r, t, b, f, quote, backslash), '>' (used in
StringTemplate), and Unicode escapes (\uXXXX). Unknown escapes are
passed through as-is.
Grammar definition
fragment
ESC : '\\'
( 'n'
| 'r'
| 't'
| 'b'
| 'f'
| '"'
| '\''
| '\\'
| '>'
| 'u' XDIGIT XDIGIT XDIGIT XDIGIT
| . // unknown, leave as it is
)
;
Railroad diagram
Referenced by
References
XDIGIT
A single hexadecimal digit. Used within Unicode escape sequences.
Grammar definition
fragment
XDIGIT :
'0' .. '9'
| 'a' .. 'f'
| 'A' .. 'F'
;
Railroad diagram
Referenced by
INT
One or more decimal digits forming a non-negative integer literal.
Used in options (e.g. k=2) and character ranges.
Used in options (e.g. k=2) and character ranges.
Grammar definition
INT : '0'..'9'+
;
Railroad diagram
Referenced by
ARG_ACTION
An argument action enclosed in square brackets '[' and ']'.
Used to pass arguments to rules and token references. The
surrounding brackets are stripped from the token text.
Used to pass arguments to rules and token references. The
surrounding brackets are stripped from the token text.
Grammar definition
ARG_ACTION
: NESTED_ARG_ACTION
;
Railroad diagram
Referenced by
References
NESTED_ARG_ACTION
The inner matching logic for ARG_ACTION. Handles nested argument
actions, string literals, character literals, and arbitrary content
between '[' and ']'.
actions, string literals, character literals, and arbitrary content
between '[' and ']'.
Grammar definition
fragment
NESTED_ARG_ACTION :
'['
( options {greedy=false; k=1;}
: NESTED_ARG_ACTION
| ACTION_STRING_LITERAL
| ACTION_CHAR_LITERAL
| .
)*
']'
{setText(getText().substring(1, getText().length()-1));}
;
Railroad diagram
Referenced by
References
ACTION
A target-language action enclosed in curly braces '{' and '}'.
If immediately followed by '?' it is reclassified as a SEMPRED
(semantic predicate).
If immediately followed by '?' it is reclassified as a SEMPRED
(semantic predicate).
Grammar definition
ACTION
: NESTED_ACTION ( '?' {$type = SEMPRED;} )?
;
Railroad diagram
Referenced by
attrScopeactionruleActionruleScopeSpecexceptionHandlerfinallyClauseelementNoOptionSpecrewrite_tree_atomrewrite_templaterewrite_indirect_template_headrewrite_template_arg
References
NESTED_ACTION
The inner matching logic for ACTION. Handles nested actions,
single-line comments, multi-line comments, string literals,
character literals, and arbitrary content between '{' and '}'.
single-line comments, multi-line comments, string literals,
character literals, and arbitrary content between '{' and '}'.
Grammar definition
fragment
NESTED_ACTION :
'{'
( options {greedy=false; k=2;}
: NESTED_ACTION
| SL_COMMENT
| ML_COMMENT
| ACTION_STRING_LITERAL
| ACTION_CHAR_LITERAL
| .
)*
'}'
;
Railroad diagram
Referenced by
References
ACTION_CHAR_LITERAL
A character literal appearing inside an action. Handles escaped
characters within single quotes.
characters within single quotes.
Grammar definition
fragment
ACTION_CHAR_LITERAL
: '\'' (ACTION_ESC|~('\\'|'\'')) '\''
;
Railroad diagram
Referenced by
References
ACTION_STRING_LITERAL
A string literal appearing inside an action. Handles escaped
characters within double quotes.
characters within double quotes.
Grammar definition
fragment
ACTION_STRING_LITERAL
: '"' (ACTION_ESC|~('\\'|'"'))* '"'
;
Railroad diagram
Referenced by
References
ACTION_ESC
An escape sequence within an action string or character literal.
Handles escaped single quote, escaped double quote, and any other
backslash-escaped character.
Handles escaped single quote, escaped double quote, and any other
backslash-escaped character.
Grammar definition
fragment
ACTION_ESC
: '\\\''
| '\\' '"' // ANTLR doesn't like: '\\"'
| '\\' ~('\''|'"')
;
Railroad diagram
Referenced by
TOKEN_REF
A token reference. Begins with an uppercase letter followed by
zero or more letters, digits, or underscores. Used to name lexer
rules and imaginary token types.
zero or more letters, digits, or underscores. Used to name lexer
rules and imaginary token types.
Grammar definition
TOKEN_REF
: 'A'..'Z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;
Railroad diagram
Referenced by
RULE_REF
A rule reference. Begins with a lowercase letter followed by
zero or more letters, digits, or underscores. Used to name parser
and tree grammar rules.
zero or more letters, digits, or underscores. Used to name parser
and tree grammar rules.
Grammar definition
RULE_REF
: 'a'..'z' ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;
Railroad diagram
Referenced by
OPTIONS
The 'options' keyword followed by optional whitespace and '{'.
Matched as a single token to prevent the '{' from being treated
as the start of an action.
Matched as a single token to prevent the '{' from being treated
as the start of an action.
Grammar definition
OPTIONS
: 'options' WS_LOOP '{'
;
Railroad diagram
Referenced by
References
TOKENS
The 'tokens' keyword followed by optional whitespace and '{'.
Matched as a single token to prevent the '{' from being treated
as the start of an action.
Matched as a single token to prevent the '{' from being treated
as the start of an action.
Grammar definition
TOKENS
: 'tokens' WS_LOOP '{'
;
Railroad diagram
Referenced by
References
SRC
The '$ANTLR src' directive embedded in a single-line comment.
Resets the reported source file name and line number, useful when
the grammar file has been generated from another source.
Resets the reported source file name and line number, useful when
the grammar file has been generated from another source.
Grammar definition
fragment
SRC : 'src' ' ' file=ACTION_STRING_LITERAL ' ' line=INT
;
Railroad diagram
Referenced by
References
WS
Whitespace: spaces, tabs, carriage returns, and newlines.
Routed to the hidden channel so the parser does not see it.
Routed to the hidden channel so the parser does not see it.
Grammar definition
WS : ( ' '
| '\t'
| '\r'? '\n'
)+
{$channel=HIDDEN;}
;
Railroad diagram
Referenced by
WS_LOOP
Whitespace and comment loop used within OPTIONS and TOKENS token
rules to skip over any whitespace or comments between the keyword
and the opening '{'.
rules to skip over any whitespace or comments between the keyword
and the opening '{'.
Grammar definition
fragment
WS_LOOP
: ( WS
| SL_COMMENT
| ML_COMMENT
)*
;
Railroad diagram
Referenced by
References