Grammar Documentation
Grammar Reference
grammarSpec
entry pointThe top-level rule. A complete grammar file consists of a grammar
declaration, zero or more prequel constructs (options, imports, tokens,
channels, actions), the rules section, zero or more lexer mode
specifications, and end of file.
declaration, zero or more prequel constructs (options, imports, tokens,
channels, actions), the rules section, zero or more lexer mode
specifications, and end of file.
Grammar definition
grammarSpec
: grammarDecl prequelConstruct* rules modeSpec* EOF
;
Railroad diagram
References
grammarDecl
The grammar declaration. Specifies the grammar type and name,
terminated by ';'. e.g. 'grammar MyGrammar;'
terminated by ';'. e.g. 'grammar MyGrammar;'
Grammar definition
grammarDecl
: grammarType identifier SEMI
;
Railroad diagram
Referenced by
References
grammarType
The grammar type. A grammar may be a pure lexer grammar ('lexer grammar'),
a pure parser grammar ('parser grammar'), or a combined grammar
('grammar') containing both lexer and parser rules.
a pure parser grammar ('parser grammar'), or a combined grammar
('grammar') containing both lexer and parser rules.
Grammar definition
grammarType
: LEXER GRAMMAR
| PARSER GRAMMAR
| GRAMMAR
;
Railroad diagram
Referenced by
References
prequelConstruct
A single prequel construct appearing before the rules section.
May be an options block, a grammar import, a tokens declaration,
a channels declaration, or a named action.
May be an options block, a grammar import, a tokens declaration,
a channels declaration, or a named action.
Grammar definition
prequelConstruct
: optionsSpec
| delegateGrammars
| tokensSpec
| channelsSpec
| action_
;
Railroad diagram
Referenced by
References
optionsSpec
An options block. 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 SEMI)* RBRACE
;
Railroad diagram
Referenced by
References
option
A single key=value option pair within an options block.
Grammar definition
option
: identifier ASSIGN optionValue
;
Railroad diagram
Referenced by
References
optionValue
The value of an option. May be a qualified identifier, a string
literal, an action block, or an integer. e.g. output=AST, k=2.
literal, an action block, or an integer. e.g. output=AST, k=2.
Grammar definition
optionValue
: identifier (DOT identifier)*
| STRING_LITERAL
| actionBlock
| INT
;
Railroad diagram
Referenced by
References
delegateGrammars
A grammar import declaration. Imports one or more delegate grammars
whose rules are inherited by the current grammar. Introduced by
'import' and terminated by ';'.
whose rules are inherited by the current grammar. Introduced by
'import' and terminated by ';'.
Grammar definition
delegateGrammars
: IMPORT delegateGrammar (COMMA delegateGrammar)* SEMI
;
Railroad diagram
Referenced by
References
delegateGrammar
A single delegate grammar reference. May optionally assign a local
alias using '='. e.g. 'MyAlias=MyGrammar' or just 'MyGrammar'.
alias using '='. e.g. 'MyAlias=MyGrammar' or just 'MyGrammar'.
Grammar definition
delegateGrammar
: identifier ASSIGN identifier
| identifier
;
Railroad diagram
Referenced by
References
tokensSpec
A tokens declaration. 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 idList? RBRACE
;
Railroad diagram
Referenced by
References
channelsSpec
A channels declaration. Declares named token channels beyond the
default and hidden channels. Delimited by 'channels' and '}'.
default and hidden channels. Delimited by 'channels' and '}'.
Grammar definition
channelsSpec
: CHANNELS idList? RBRACE
;
Railroad diagram
Referenced by
References
idList
A comma-separated list of identifiers with an optional trailing
comma. Used in tokensSpec and channelsSpec.
comma. Used in tokensSpec and channelsSpec.
Grammar definition
idList
: identifier (COMMA identifier)* COMMA?
;
Railroad diagram
Referenced by
References
action_
A grammar-level named action such as @members or @header.
Optionally scoped to a specific component using '::'.
e.g. '@parser::members { int i; }'
Optionally scoped to a specific component using '::'.
e.g. '@parser::members { int i; }'
Grammar definition
action_
: AT (actionScopeName COLONCOLON)? identifier actionBlock
;
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
: identifier
| LEXER
| PARSER
;
Railroad diagram
Referenced by
References
actionBlock
A target-language action block enclosed in curly braces '{' and '}'.
Contains arbitrary target-language code.
Contains arbitrary target-language code.
Grammar definition
actionBlock
: ACTION
;
Railroad diagram
Referenced by
References
argActionBlock
An argument action block enclosed in square brackets '[' and ']'.
Used to pass arguments to rule invocations or specify rule parameters.
Used to pass arguments to rule invocations or specify rule parameters.
Grammar definition
argActionBlock
: BEGIN_ARGUMENT ARGUMENT_CONTENT*? END_ARGUMENT
;
Railroad diagram
Referenced by
References
modeSpec
A lexer mode specification. Introduces a named mode with 'mode'
followed by an identifier and ';', then zero or more lexer rules
that apply when the lexer is in that mode.
followed by an identifier and ';', then zero or more lexer rules
that apply when the lexer is in that mode.
Grammar definition
modeSpec
: MODE identifier SEMI lexerRuleSpec*
;
Railroad diagram
Referenced by
References
rules
The rules section of a grammar. Contains zero or more rule
specifications (parser or lexer).
specifications (parser or lexer).
Grammar definition
rules
: ruleSpec*
;
Railroad diagram
Referenced by
References
ruleSpec
A single rule specification. Either a parser rule or a lexer rule.
Grammar definition
ruleSpec
: parserRuleSpec
| lexerRuleSpec
;
Railroad diagram
Referenced by
References
parserRuleSpec
A complete parser rule specification. Parser rule names begin with
a lowercase letter. May include optional modifiers, arguments,
return values, a throws clause, local variables, prequel constructs
(options and named actions), and an exception group. The rule body
is a colon-separated block of alternatives terminated by ';'.
a lowercase letter. May include optional modifiers, arguments,
return values, a throws clause, local variables, prequel constructs
(options and named actions), and an exception group. The rule body
is a colon-separated block of alternatives terminated by ';'.
Grammar definition
parserRuleSpec
: ruleModifiers? RULE_REF argActionBlock? ruleReturns? throwsSpec? localsSpec? rulePrequel* COLON ruleBlock SEMI
exceptionGroup
;
Railroad diagram
Referenced by
References
exceptionGroup
A group of zero or more exception handlers and an optional finally
clause following a parser rule body.
clause following a parser rule body.
Grammar definition
exceptionGroup
: exceptionHandler* 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 argActionBlock actionBlock
;
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 actionBlock
;
Railroad diagram
Referenced by
References
rulePrequel
A single prequel construct within a rule. Either an options block
or a named rule action such as @init or @after.
or a named rule action such as @init or @after.
Grammar definition
rulePrequel
: optionsSpec
| ruleAction
;
Railroad diagram
Referenced by
References
ruleReturns
A return value specification for a rule. Declares the types and
names of values returned by the rule using an argument action block.
e.g. 'returns [int value]'
names of values returned by the rule using an argument action block.
e.g. 'returns [int value]'
Grammar definition
ruleReturns
: RETURNS argActionBlock
;
Railroad diagram
Referenced by
References
throwsSpec
A throws clause listing exception types a rule may throw.
Introduced by 'throws' followed by a comma-separated list of
qualified identifiers.
Introduced by 'throws' followed by a comma-separated list of
qualified identifiers.
Grammar definition
throwsSpec
: THROWS qualifiedIdentifier (COMMA qualifiedIdentifier)*
;
Railroad diagram
Referenced by
References
localsSpec
A locals declaration. Declares local variables available throughout
the rule using an argument action block.
e.g. 'locals [int i = 0]'
the rule using an argument action block.
e.g. 'locals [int i = 0]'
Grammar definition
localsSpec
: LOCALS argActionBlock
;
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 body respectively.
Executed at the start or end of the rule body respectively.
Grammar definition
ruleAction
: AT identifier actionBlock
;
Railroad diagram
Referenced by
References
ruleModifiers
One or more rule modifier keywords preceding a rule definition.
Grammar definition
ruleModifiers
: ruleModifier+
;
Railroad diagram
Referenced by
References
ruleModifier
An individual access modifier for a rule. 'fragment' marks a lexer
rule as a subroutine that is never directly matched from input but
may be reused within other lexer rules. 'public', 'private', and
'protected' are passed to code generation templates.
rule as a subroutine that is never directly matched from input but
may be reused within other lexer rules. 'public', 'private', and
'protected' are passed to code generation templates.
Grammar definition
ruleModifier
: PUBLIC
| PRIVATE
| PROTECTED
| FRAGMENT
;
Railroad diagram
Referenced by
References
ruleBlock
The body of a parser rule. Contains the list of labeled alternatives.
Grammar definition
ruleBlock
: ruleAltList
;
Railroad diagram
Referenced by
References
ruleAltList
A list of labeled alternatives separated by '|'. Each alternative
may optionally be given a label using '#' for use in generated
listener and visitor methods.
may optionally be given a label using '#' for use in generated
listener and visitor methods.
Grammar definition
ruleAltList
: labeledAlt (OR labeledAlt)*
;
Railroad diagram
Referenced by
References
labeledAlt
A single alternative within a parser rule, optionally followed by
a '#' label. The label is used to generate named context classes
in the target language.
a '#' label. The label is used to generate named context classes
in the target language.
Grammar definition
labeledAlt
: alternative (POUND identifier)?
;
Railroad diagram
Referenced by
References
lexerRuleSpec
A complete lexer rule specification. Lexer rule names begin with
an uppercase letter. May optionally be preceded by 'fragment'.
May include an options block. The rule body is a colon-separated
block of lexer alternatives terminated by ';'.
an uppercase letter. May optionally be preceded by 'fragment'.
May include an options block. The rule body is a colon-separated
block of lexer alternatives terminated by ';'.
Grammar definition
lexerRuleSpec
: FRAGMENT? TOKEN_REF optionsSpec? COLON lexerRuleBlock SEMI
;
Railroad diagram
Referenced by
References
lexerRuleBlock
The body of a lexer rule. Contains the list of lexer alternatives.
Grammar definition
lexerRuleBlock
: lexerAltList
;
Railroad diagram
Referenced by
References
lexerAltList
A list of lexer alternatives separated by '|'.
Grammar definition
lexerAltList
: lexerAlt (OR lexerAlt)*
;
Railroad diagram
Referenced by
References
lexerAlt
A single lexer alternative. Consists of zero or more lexer elements
optionally followed by lexer commands. An empty alternative is
explicitly permitted.
optionally followed by lexer commands. An empty alternative is
explicitly permitted.
Grammar definition
lexerAlt
: lexerElements lexerCommands?
|
// explicitly allow empty alts
;
Railroad diagram
Referenced by
References
lexerElements
One or more lexer elements forming a lexer alternative, or empty.
Grammar definition
lexerElements
: lexerElement+
|
;
Railroad diagram
Referenced by
References
lexerElement
A single element within a lexer alternative. May be a lexer atom
with an optional EBNF suffix, a lexer block with an optional EBNF
suffix, or an action block with an optional '?' making it a
semantic predicate.
with an optional EBNF suffix, a lexer block with an optional EBNF
suffix, or an action block with an optional '?' making it a
semantic predicate.
Grammar definition
lexerElement
: lexerAtom ebnfSuffix?
| lexerBlock ebnfSuffix?
| actionBlock QUESTION?
;
Railroad diagram
Referenced by
References
lexerBlock
A parenthesized group of lexer alternatives used as a subrule
within a lexer rule.
within a lexer rule.
Grammar definition
lexerBlock
: LPAREN lexerAltList RPAREN
;
Railroad diagram
Referenced by
References
lexerCommands
One or more lexer commands following a lexer alternative, introduced
by '->'. Commands direct the lexer to perform actions such as
changing channel, skipping, or switching mode.
e.g. '-> skip', '-> channel(HIDDEN)', '-> mode(INSIDE)'
by '->'. Commands direct the lexer to perform actions such as
changing channel, skipping, or switching mode.
e.g. '-> skip', '-> channel(HIDDEN)', '-> mode(INSIDE)'
Grammar definition
lexerCommands
: RARROW lexerCommand (COMMA lexerCommand)*
;
Railroad diagram
Referenced by
References
lexerCommand
A single lexer command. May be a named command with a parenthesized
argument or a bare command name.
e.g. 'channel(HIDDEN)', 'skip', 'pushMode(INSIDE)'
argument or a bare command name.
e.g. 'channel(HIDDEN)', 'skip', 'pushMode(INSIDE)'
Grammar definition
lexerCommand
: lexerCommandName LPAREN lexerCommandExpr RPAREN
| lexerCommandName
;
Railroad diagram
Referenced by
References
lexerCommandName
The name of a lexer command. Either an identifier or the 'mode'
keyword.
keyword.
Grammar definition
lexerCommandName
: identifier
| MODE
;
Railroad diagram
Referenced by
References
lexerCommandExpr
The argument to a lexer command. Either an identifier or an integer.
Grammar definition
lexerCommandExpr
: identifier
| INT
;
Railroad diagram
Referenced by
References
altList
A list of parser alternatives separated by '|'. Used within
parenthesized blocks.
parenthesized blocks.
Grammar definition
altList
: alternative (OR alternative)*
;
Railroad diagram
Referenced by
References
alternative
A single parser alternative. Consists of optional element options
followed by one or more elements, or empty.
followed by one or more elements, or empty.
Grammar definition
alternative
: elementOptions? element+
|
// explicitly allow empty alts
;
Railroad diagram
Referenced by
References
element
A single element within a parser alternative. May be a labeled
element with an optional EBNF suffix, an atom with an optional
EBNF suffix, an EBNF block, or an action block optionally followed
by '?' (semantic predicate) and predicate options.
element with an optional EBNF suffix, an atom with an optional
EBNF suffix, an EBNF block, or an action block optionally followed
by '?' (semantic predicate) and predicate options.
Grammar definition
element
: labeledElement (ebnfSuffix |)
| atom (ebnfSuffix |)
| ebnf
| actionBlock QUESTION? predicateOptions?
;
Railroad diagram
Referenced by
References
predicateOptions
Options attached to a semantic predicate using angle brackets.
e.g. {myPred}?<fail='message'>
e.g. {myPred}?<fail='message'>
Grammar definition
predicateOptions
: LT predicateOption (COMMA predicateOption)* GT
;
Railroad diagram
Referenced by
References
predicateOption
A single predicate option. Either an element option or a named
key=value pair where the value is an action block, integer, or
string literal.
key=value pair where the value is an action block, integer, or
string literal.
Grammar definition
predicateOption
: elementOption
| identifier ASSIGN (actionBlock | INT | STRING_LITERAL)
;
Railroad diagram
Referenced by
References
labeledElement
A labeled element. An atom or block assigned a label using '='
(single value) or '+=' (list append).
e.g. 'lhs=expr', 'items+=item'
(single value) or '+=' (list append).
e.g. 'lhs=expr', 'items+=item'
Grammar definition
labeledElement
: identifier (ASSIGN | PLUS_ASSIGN) (atom | block)
;
Railroad diagram
Referenced by
References
ebnf
An EBNF expression: a parenthesized block with an optional suffix.
The suffix controls repetition behavior.
The suffix controls repetition behavior.
Grammar definition
ebnf
: block blockSuffix?
;
Railroad diagram
Referenced by
References
blockSuffix
The suffix of an EBNF block. Delegates to ebnfSuffix.
Grammar definition
blockSuffix
: ebnfSuffix
;
Railroad diagram
Referenced by
References
ebnfSuffix
An EBNF suffix operator. '?' makes the preceding element optional,
'*' allows zero or more repetitions, '+' requires one or more
repetitions. Each may be followed by an additional '?' to enable
non-greedy (lazy) matching.
'*' allows zero or more repetitions, '+' requires one or more
repetitions. Each may be followed by an additional '?' to enable
non-greedy (lazy) matching.
Grammar definition
ebnfSuffix
: QUESTION QUESTION?
| STAR QUESTION?
| PLUS QUESTION?
;
Railroad diagram
Referenced by
References
lexerAtom
An atomic lexer element. May be a character range, a terminal
definition, a negated set, a lexer character set (e.g. [a-z]),
or a wildcard '.'.
definition, a negated set, a lexer character set (e.g. [a-z]),
or a wildcard '.'.
Grammar definition
lexerAtom
: characterRange
| terminalDef
| notSet
| LEXER_CHAR_SET
| wildcard
;
Railroad diagram
Referenced by
References
atom
An atomic parser element. May be a terminal definition, a rule
reference, a negated set, or a wildcard '.'.
reference, a negated set, or a wildcard '.'.
Grammar definition
atom
: terminalDef
| ruleref
| notSet
| wildcard
;
Railroad diagram
Referenced by
References
wildcard
The wildcard element '.'. Matches any single token in a parser rule
or any single character in a lexer rule. May have element options.
or any single character in a lexer rule. May have element options.
Grammar definition
wildcard
: DOT elementOptions?
;
Railroad diagram
Referenced by
References
notSet
A negated element using '~'. Matches any token or character not in
the specified set element or block set.
the specified set element or block set.
Grammar definition
notSet
: NOT setElement
| NOT blockSet
;
Railroad diagram
Referenced by
References
blockSet
A parenthesized set of alternatives used with the '~' negation
operator.
operator.
Grammar definition
blockSet
: LPAREN setElement (OR setElement)* RPAREN
;
Railroad diagram
Referenced by
References
setElement
A single element within a set or negated set. May be a token
reference, a string literal, a character range, or a lexer
character set. May have element options.
reference, a string literal, a character range, or a lexer
character set. May have element options.
Grammar definition
setElement
: TOKEN_REF elementOptions?
| STRING_LITERAL elementOptions?
| characterRange
| LEXER_CHAR_SET
;
Railroad diagram
Referenced by
References
block
A parenthesized block of alternatives used as a subrule within a
parser rule. May optionally contain options and named actions before
a ':' separator.
parser rule. May optionally contain options and named actions before
a ':' separator.
Grammar definition
block
: LPAREN (optionsSpec? ruleAction* COLON)? altList RPAREN
;
Railroad diagram
Referenced by
References
ruleref
A parser rule reference. References a rule by its RULE_REF name,
with optional argument action and element options.
with optional argument action and element options.
Grammar definition
ruleref
: RULE_REF argActionBlock? elementOptions?
;
Railroad diagram
Referenced by
References
characterRange
A character range used in lexer rules. Matches any single character
between two string literals, inclusive, using the '..' operator.
e.g. 'a'..'z'
between two string literals, inclusive, using the '..' operator.
e.g. 'a'..'z'
Grammar definition
characterRange
: STRING_LITERAL RANGE STRING_LITERAL
;
Railroad diagram
Referenced by
References
terminalDef
A terminal element reference. Either a token reference or a string
literal, optionally adorned with element options in angle brackets.
literal, optionally adorned with element options in angle brackets.
Grammar definition
terminalDef
: TOKEN_REF elementOptions?
| STRING_LITERAL elementOptions?
;
Railroad diagram
Referenced by
References
elementOptions
Element options attached to a terminal or wildcard reference using
angle brackets. Contains one or more comma-separated options.
e.g. ID<assoc=right>
angle brackets. Contains one or more comma-separated options.
e.g. ID<assoc=right>
Grammar definition
elementOptions
: LT elementOption (COMMA elementOption)* GT
;
Railroad diagram
Referenced by
References
elementOption
A single element option. Either a qualified identifier or a named
key=value pair where the value is a qualified identifier, string
literal, or integer.
key=value pair where the value is a qualified identifier, string
literal, or integer.
Grammar definition
elementOption
: qualifiedIdentifier
| identifier ASSIGN (qualifiedIdentifier | STRING_LITERAL | INT)
;
Railroad diagram
Referenced by
References
identifier
An identifier. Either a RULE_REF (lowercase) or a TOKEN_REF
(uppercase). Used wherever a plain name is needed regardless of
case convention.
(uppercase). Used wherever a plain name is needed regardless of
case convention.
Grammar definition
identifier
: RULE_REF
| TOKEN_REF
;
Railroad diagram
Referenced by
grammarDecloptionoptionValuedelegateGrammaridListaction_actionScopeNamemodeSpecruleActionlabeledAltlexerCommandNamelexerCommandExprpredicateOptionlabeledElementelementOptionqualifiedIdentifier
References
RULE_REFTOKEN_REF
qualifiedIdentifier
A dot-separated qualified identifier. Used in throws clauses and
option values to reference fully qualified type names.
e.g. 'java.lang.RuntimeException'
option values to reference fully qualified type names.
e.g. 'java.lang.RuntimeException'
Grammar definition
qualifiedIdentifier
: identifier (DOT identifier)*
;
Railroad diagram
Referenced by
References
DOC_COMMENT
entry pointA documentation comment beginning with '/**'. Routed to the
COMMENT channel. Consumes everything up to the closing '* /' or
end of file.
COMMENT channel. Consumes everything up to the closing '* /' or
end of file.
Grammar definition
DOC_COMMENT
: '/**' .*? ('*/' | EOF) -> channel (COMMENT)
;
Railroad diagram
References
EOF
BLOCK_COMMENT
entry pointA block comment beginning with '/*'. Routed to the COMMENT channel.
Consumes everything up to the closing '* /' or end of file.
Consumes everything up to the closing '* /' or end of file.
Grammar definition
BLOCK_COMMENT
: '/*' .*? ('*/' | EOF) -> channel (COMMENT)
;
Railroad diagram
References
EOF
LINE_COMMENT
entry pointA single-line comment beginning with '//'. Routed to the COMMENT
channel. Consumes all characters up to but not including the newline.
channel. Consumes all characters up to but not including the newline.
Grammar definition
LINE_COMMENT
: '//' ~ [\r\n]* -> channel (COMMENT)
;
Railroad diagram
INT
A non-negative integer literal. Either '0' or a sequence of digits
beginning with a non-zero digit.
beginning with a non-zero digit.
Grammar definition
INT
: '0'
| [1-9] [0-9]*
;
Railroad diagram
Referenced by
STRING_LITERAL
A single-quoted string literal. ANTLR makes no distinction between
a single character and a multi-character string. May contain Unicode
escape sequences of the form \uXXXX.
a single character and a multi-character string. May contain Unicode
escape sequences of the form \uXXXX.
Grammar definition
STRING_LITERAL
: '\'' (ESC_SEQUENCE | ~ ['\r\n\\])* '\''
;
Railroad diagram
Referenced by
optionValuepredicateOptionsetElementcharacterRangeterminalDefelementOptionNESTED_ACTIONARGUMENT_CHAR_LITERAL
References
UNTERMINATED_STRING_LITERAL
entry pointAn unterminated string literal. Matched when a single-quoted string
is not closed before a newline or end of file. Used for error
recovery.
is not closed before a newline or end of file. Used for error
recovery.
Grammar definition
UNTERMINATED_STRING_LITERAL
: '\'' (ESC_SEQUENCE | ~ ['\r\n\\])*
;
Railroad diagram
References
BEGIN_ARGUMENT
The opening delimiter of an argument action block. Matched when '['
is encountered. Delegates to handleBeginArgument() to switch the
lexer into Argument mode.
is encountered. Delegates to handleBeginArgument() to switch the
lexer into Argument mode.
Grammar definition
BEGIN_ARGUMENT
: '[' { this.handleBeginArgument(); }
;
Railroad diagram
Referenced by
ACTION
A target-language action block enclosed in curly braces '{' and '}'.
Handles nested blocks, string literals, comments, and escape sequences
within the action body. If immediately followed by '?' it is
reclassified as a semantic predicate by the parser.
Handles nested blocks, string literals, comments, and escape sequences
within the action body. If immediately followed by '?' it is
reclassified as a semantic predicate by the parser.
Grammar definition
ACTION
: NESTED_ACTION
;
Railroad diagram
Referenced by
References
NESTED_ACTION
The inner matching logic for ACTION. Handles nested '{}' blocks,
single-quoted strings, double-quoted strings, triple-quoted strings,
backtick-quoted strings, block comments, line comments, escape
sequences, and any other single character between '{' and '}'.
single-quoted strings, double-quoted strings, triple-quoted strings,
backtick-quoted strings, block comments, line comments, escape
sequences, and any other single character between '{' and '}'.
Grammar definition
fragment NESTED_ACTION
: '{' (
NESTED_ACTION
| STRING_LITERAL
| DoubleQuoteLiteral
| TripleQuoteLiteral
| BacktickQuoteLiteral
| '/*' .*? '*/'
| '//' ~[\r\n]*
| '\\' .
| ~(
'\\'
| '"'
| '\''
| '`'
| '{'
)
)*? '}'
;
Railroad diagram
Referenced by
References
OPTIONS
The 'options' keyword followed by optional whitespace and '{'.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Grammar definition
OPTIONS
: 'options' WS* '{'
;
Railroad diagram
Referenced by
References
TOKENS
The 'tokens' keyword followed by optional whitespace and '{'.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Grammar definition
TOKENS
: 'tokens' WS* '{'
;
Railroad diagram
Referenced by
References
CHANNELS
The 'channels' keyword followed by optional whitespace and '{'.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Matched as a single token to prevent '{' from being treated as
the start of an action.
Grammar definition
CHANNELS
: 'channels' WS* '{'
;
Railroad diagram
Referenced by
References
IMPORT
The 'import' keyword. Introduces a delegate grammar declaration.
Grammar definition
IMPORT
: 'import'
;
Railroad diagram
Referenced by
FRAGMENT
The 'fragment' keyword. Marks a lexer rule as a non-matching
subroutine reusable within other lexer rules.
subroutine reusable within other lexer rules.
Grammar definition
FRAGMENT
: 'fragment'
;
Railroad diagram
Referenced by
LEXER
The 'lexer' keyword. Used in grammar type declarations and action
scope names.
scope names.
Grammar definition
LEXER
: 'lexer'
;
Railroad diagram
Referenced by
PARSER
The 'parser' keyword. Used in grammar type declarations and action
scope names.
scope names.
Grammar definition
PARSER
: 'parser'
;
Railroad diagram
Referenced by
GRAMMAR
The 'grammar' keyword. Introduces the grammar declaration.
Grammar definition
GRAMMAR
: 'grammar'
;
Railroad diagram
Referenced by
PROTECTED
The 'protected' keyword. An access modifier for rule definitions.
Grammar definition
PROTECTED
: 'protected'
;
Railroad diagram
Referenced by
PUBLIC
The 'public' keyword. An access modifier for rule definitions.
Grammar definition
PUBLIC
: 'public'
;
Railroad diagram
Referenced by
PRIVATE
The 'private' keyword. An access modifier for rule definitions.
Grammar definition
PRIVATE
: 'private'
;
Railroad diagram
Referenced by
RETURNS
The 'returns' keyword. Introduces the return value specification
of a rule.
of a rule.
Grammar definition
RETURNS
: 'returns'
;
Railroad diagram
Referenced by
LOCALS
The 'locals' keyword. Introduces local variable declarations
within a rule.
within a rule.
Grammar definition
LOCALS
: 'locals'
;
Railroad diagram
Referenced by
THROWS
The 'throws' keyword. Introduces the exception type list for a rule.
Grammar definition
THROWS
: 'throws'
;
Railroad diagram
Referenced by
CATCH
The 'catch' keyword. Introduces an exception handler clause.
Grammar definition
CATCH
: 'catch'
;
Railroad diagram
Referenced by
FINALLY
The 'finally' keyword. Introduces a finally clause.
Grammar definition
FINALLY
: 'finally'
;
Railroad diagram
Referenced by
MODE
The 'mode' keyword. Introduces a lexer mode specification.
Grammar definition
MODE
: 'mode'
;
Railroad diagram
Referenced by
COLON
The ':' character. Separates a rule name from its body.
Grammar definition
COLON
: ':'
;
Railroad diagram
Referenced by
COLONCOLON
The '::' sequence. Separates a scope name from an action name
in grammar-level actions. e.g. '@parser::members'
in grammar-level actions. e.g. '@parser::members'
Grammar definition
COLONCOLON
: '::'
;
Railroad diagram
Referenced by
COMMA
The ',' character. Separates items in lists.
Grammar definition
COMMA
: ','
;
Railroad diagram
Referenced by
SEMI
The ';' character. Terminates rule definitions and option pairs.
Grammar definition
SEMI
: ';'
;
Railroad diagram
Referenced by
LPAREN
The '(' character. Opens a parenthesized block or argument list.
Grammar definition
LPAREN
: '('
;
Railroad diagram
Referenced by
RPAREN
The ')' character. Closes a parenthesized block or argument list.
Grammar definition
RPAREN
: ')'
;
Railroad diagram
Referenced by
RBRACE
The '}' character. Closes an options, tokens, or channels block.
Grammar definition
RBRACE
: '}'
;
Railroad diagram
Referenced by
RARROW
The '->' sequence. Introduces lexer commands or rewrite rules.
Grammar definition
RARROW
: '->'
;
Railroad diagram
Referenced by
LT
The '<' character. Opens an element options list.
Grammar definition
LT
: '<'
;
Railroad diagram
Referenced by
GT
The '>' character. Closes an element options list.
Grammar definition
GT
: '>'
;
Railroad diagram
Referenced by
ASSIGN
The '=' character. Used for option assignment and element labeling.
Grammar definition
ASSIGN
: '='
;
Railroad diagram
Referenced by
QUESTION
The '?' character. Used as an EBNF suffix for optional elements,
and to mark semantic predicates when following an action block.
and to mark semantic predicates when following an action block.
Grammar definition
QUESTION
: '?'
;
Railroad diagram
Referenced by
STAR
The '*' character. Used as an EBNF suffix for zero-or-more repetition.
Grammar definition
STAR
: '*'
;
Railroad diagram
Referenced by
PLUS_ASSIGN
The '+=' sequence. Used for list-append labeling of elements.
Grammar definition
PLUS_ASSIGN
: '+='
;
Railroad diagram
Referenced by
PLUS
The '+' character. Used as an EBNF suffix for one-or-more repetition.
Grammar definition
PLUS
: '+'
;
Railroad diagram
Referenced by
OR
The '|' character. Separates alternatives within a rule or block.
Grammar definition
OR
: '|'
;
Railroad diagram
Referenced by
DOLLAR
entry pointThe '$' character. Used to reference labels within rewrite rules
and actions.
and actions.
Grammar definition
DOLLAR
: '$'
;
Railroad diagram
RANGE
The '..' sequence. Separates the bounds of a character range.
e.g. 'a'..'z'
e.g. 'a'..'z'
Grammar definition
RANGE
: '..'
;
Railroad diagram
Referenced by
DOT
The '.' character. Used as a wildcard element and as a separator
in qualified identifiers.
in qualified identifiers.
Grammar definition
DOT
: '.'
;
Railroad diagram
Referenced by
AT
The '@' character. Introduces grammar-level and rule-level named
actions.
actions.
Grammar definition
AT
: '@'
;
Railroad diagram
Referenced by
POUND
The '#' character. Introduces a label on an alternative within a
parser rule, used to generate named context classes.
parser rule, used to generate named context classes.
Grammar definition
POUND
: '#'
;
Railroad diagram
Referenced by
NOT
The '~' character. Introduces a negated element or set.
Grammar definition
NOT
: '~'
;
Railroad diagram
Referenced by
ID
entry pointAn identifier. Begins with a letter or Unicode name start character,
followed by zero or more name characters. Supports Unicode identifiers
for rule and token names.
followed by zero or more name characters. Supports Unicode identifiers
for rule and token names.
Grammar definition
ID
: NameStartChar NameChar*
;
Railroad diagram
References
WS
Whitespace: spaces, tabs, carriage returns, newlines, and form feeds.
Routed to the OFF_CHANNEL so the parser does not see it.
Routed to the OFF_CHANNEL so the parser does not see it.
Grammar definition
WS
: [ \t\r\n\f]+ -> channel (OFF_CHANNEL)
;
Railroad diagram
Referenced by
NESTED_ARGUMENT
entry pointA nested argument action opened by '['. Pushes a new Argument mode
to handle nested bracket pairs. Reclassified as ARGUMENT_CONTENT.
to handle nested bracket pairs. Reclassified as ARGUMENT_CONTENT.
Grammar definition
NESTED_ARGUMENT
: '[' -> type (ARGUMENT_CONTENT), pushMode (Argument)
;
Railroad diagram
ARGUMENT_ESCAPE
entry pointAn escape sequence within an argument action. The backslash and the
following character are reclassified as ARGUMENT_CONTENT.
following character are reclassified as ARGUMENT_CONTENT.
Grammar definition
ARGUMENT_ESCAPE
: '\\' . -> type (ARGUMENT_CONTENT)
;
Railroad diagram
ARGUMENT_STRING_LITERAL
entry pointA double-quoted string literal within an argument action.
Reclassified as ARGUMENT_CONTENT.
Reclassified as ARGUMENT_CONTENT.
Grammar definition
ARGUMENT_STRING_LITERAL
: DoubleQuoteLiteral -> type (ARGUMENT_CONTENT)
;
Railroad diagram
References
ARGUMENT_CHAR_LITERAL
entry pointA single-quoted string literal within an argument action.
Reclassified as ARGUMENT_CONTENT.
Reclassified as ARGUMENT_CONTENT.
Grammar definition
ARGUMENT_CHAR_LITERAL
: STRING_LITERAL -> type (ARGUMENT_CONTENT)
;
Railroad diagram
References
END_ARGUMENT
The closing ']' of an argument action. Delegates to
handleEndArgument() to pop the Argument mode.
handleEndArgument() to pop the Argument mode.
Grammar definition
END_ARGUMENT
: ']' { this.handleEndArgument(); }
;
Railroad diagram
Referenced by
UNTERMINATED_ARGUMENT
entry pointMatches EOF within an argument action to recover from an unterminated
argument. Pops the Argument mode without consuming any input.
argument. Pops the Argument mode without consuming any input.
Grammar definition
UNTERMINATED_ARGUMENT
: EOF -> popMode
;
Railroad diagram
References
EOF
ARGUMENT_CONTENT
Any other single character within an argument action body.
Grammar definition
ARGUMENT_CONTENT
: .
;
Railroad diagram
Referenced by
LEXER_CHAR_SET_BODY
entry pointOne or more characters forming the body of a lexer character set
'[...]'. Handles escape sequences and any character except ']' and
'\'. Uses 'more' to accumulate characters into the final token.
'[...]'. Handles escape sequences and any character except ']' and
'\'. Uses 'more' to accumulate characters into the final token.
Grammar definition
LEXER_CHAR_SET_BODY
: (~ [\]\\] | '\\' .)+ -> more
;
Railroad diagram
LEXER_CHAR_SET
The closing ']' of a lexer character set. Completes the
LEXER_CHAR_SET token and pops LexerCharSet mode.
LEXER_CHAR_SET token and pops LexerCharSet mode.
Grammar definition
LEXER_CHAR_SET
: ']' -> popMode
;
Railroad diagram
Referenced by
UNTERMINATED_CHAR_SET
entry pointMatches EOF within a lexer character set to recover from an
unterminated character set. Pops LexerCharSet mode.
unterminated character set. Pops LexerCharSet mode.
Grammar definition
UNTERMINATED_CHAR_SET
: EOF -> popMode
;
Railroad diagram
References
EOF
ESC_SEQUENCE
An escape sequence within a string literal. Handles standard
character escapes, Unicode escapes (\uXXXX), and any other
character following a backslash.
character escapes, Unicode escapes (\uXXXX), and any other
character following a backslash.
Grammar definition
fragment ESC_SEQUENCE
: '\\' ([btnfr"'\\] | UnicodeESC | . | EOF)
;
Railroad diagram
Referenced by
References
UnicodeESCEOF
HexDigit
A single hexadecimal digit. Used within Unicode escape sequences.
Grammar definition
fragment HexDigit
: [0-9a-fA-F]
;
Railroad diagram
Referenced by
UnicodeESC
A Unicode escape sequence of the form \uXXXX. The hex digits are
individually optional to allow partial matches during error recovery.
individually optional to allow partial matches during error recovery.
Grammar definition
fragment UnicodeESC
: 'u' (HexDigit (HexDigit (HexDigit HexDigit?)?)?)?
;
Railroad diagram
Referenced by
References
DoubleQuoteLiteral
A double-quoted string literal. Used within action blocks as a
target-language string. Handles escape sequences.
target-language string. Handles escape sequences.
Grammar definition
fragment DoubleQuoteLiteral
: '"' (ESC_SEQUENCE | ~["\r\n\\])*? '"'
;
Railroad diagram
Referenced by
References
TripleQuoteLiteral
A triple-quoted string literal. Used within action blocks as a
target-language multi-line string. Handles escape sequences.
target-language multi-line string. Handles escape sequences.
Grammar definition
fragment TripleQuoteLiteral
: '"""' (ESC_SEQUENCE | .)*? '"""'
;
Railroad diagram
Referenced by
References
BacktickQuoteLiteral
A backtick-quoted string literal. Used within action blocks in
target languages that support backtick strings. Handles escape
sequences.
target languages that support backtick strings. Handles escape
sequences.
Grammar definition
fragment BacktickQuoteLiteral
: '`' (ESC_SEQUENCE | ~["\r\n\\])*? '`'
;
Railroad diagram
Referenced by
References
NameChar
A character that may appear after the first character of an
identifier. Includes letters, digits, underscore, and a range of
Unicode characters used in identifiers per the Unicode standard.
identifier. Includes letters, digits, underscore, and a range of
Unicode characters used in identifiers per the Unicode standard.
Grammar definition
fragment NameChar
: NameStartChar
| '0' .. '9'
| '_'
| '\u00B7'
| '\u0300' .. '\u036F'
| '\u203F' .. '\u2040'
;
Railroad diagram
Referenced by
References
NameStartChar
The first character of an identifier. Must be a letter or one of
a range of Unicode characters designated as name start characters
per the Unicode standard.
a range of Unicode characters designated as name start characters
per the Unicode standard.
Grammar definition
fragment NameStartChar
: 'A' .. 'Z'
| 'a' .. 'z'
| '\u00C0' .. '\u00D6'
| '\u00D8' .. '\u00F6'
| '\u00F8' .. '\u02FF'
| '\u0370' .. '\u037D'
| '\u037F' .. '\u1FFF'
| '\u200C' .. '\u200D'
| '\u2070' .. '\u218F'
| '\u2C00' .. '\u2FEF'
| '\u3001' .. '\uD7FF'
| '\uF900' .. '\uFDCF'
| '\uFDF0' .. '\uFFFD'
// ignores | ['\u10000-'\uEFFFF]
;
Railroad diagram
Referenced by