Grammar Documentation
Grammar Reference
Generated May 5, 2026 · 135 rules · ANTLR4 · Generated by DevDiagrams

grammarSpec

entry point
The 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.
grammarSpec
: grammarDecl prequelConstruct* rules modeSpec* EOF
;
grammarDecl prequelConstruct rules modeSpec EOF

grammarDecl

The grammar declaration. Specifies the grammar type and name,
terminated by ';'. e.g. 'grammar MyGrammar;'
grammarDecl
: grammarType identifier SEMI
;
grammarType identifier SEMI

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.
grammarType
: LEXER GRAMMAR
| PARSER GRAMMAR
| GRAMMAR
;
LEXER GRAMMAR PARSER GRAMMAR GRAMMAR

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.
prequelConstruct
: optionsSpec
| delegateGrammars
| tokensSpec
| channelsSpec
| action_
;
optionsSpec delegateGrammars tokensSpec channelsSpec action_

optionsSpec

An options block. Contains one or more key=value option pairs
delimited by the 'options' keyword and a closing '}'.
optionsSpec
: OPTIONS (option SEMI)* RBRACE
;
OPTIONS option SEMI RBRACE

option

A single key=value option pair within an options block.
option
: identifier ASSIGN optionValue
;
identifier ASSIGN optionValue

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.
optionValue
: identifier (DOT identifier)*
| STRING_LITERAL
| actionBlock
| INT
;
identifier DOT identifier STRING_LITERAL actionBlock INT

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 ';'.
delegateGrammars
: IMPORT delegateGrammar (COMMA delegateGrammar)* SEMI
;
IMPORT delegateGrammar COMMA delegateGrammar SEMI

delegateGrammar

A single delegate grammar reference. May optionally assign a local
alias using '='. e.g. 'MyAlias=MyGrammar' or just 'MyGrammar'.
delegateGrammar
: identifier ASSIGN identifier
| identifier
;
identifier ASSIGN identifier identifier

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 '}'.
tokensSpec
: TOKENS idList? RBRACE
;
TOKENS idList RBRACE

channelsSpec

A channels declaration. Declares named token channels beyond the
default and hidden channels. Delimited by 'channels' and '}'.
channelsSpec
: CHANNELS idList? RBRACE
;
CHANNELS idList RBRACE

idList

A comma-separated list of identifiers with an optional trailing
comma. Used in tokensSpec and channelsSpec.
idList
: identifier (COMMA identifier)* COMMA?
;
identifier COMMA identifier COMMA

action_

A grammar-level named action such as @members or @header.
Optionally scoped to a specific component using '::'.
e.g. '@parser::members { int i; }'
action_
: AT (actionScopeName COLONCOLON)? identifier actionBlock
;
AT actionScopeName COLONCOLON identifier actionBlock

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.
actionScopeName
: identifier
| LEXER
| PARSER
;
identifier LEXER PARSER

actionBlock

A target-language action block enclosed in curly braces '{' and '}'.
Contains arbitrary target-language code.
actionBlock
: ACTION
;
ACTION

argActionBlock

An argument action block enclosed in square brackets '[' and ']'.
Used to pass arguments to rule invocations or specify rule parameters.
argActionBlock
: BEGIN_ARGUMENT ARGUMENT_CONTENT*? END_ARGUMENT
;
BEGIN_ARGUMENT ARGUMENT_CONTENT END_ARGUMENT

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.
modeSpec
: MODE identifier SEMI lexerRuleSpec*
;
MODE identifier SEMI lexerRuleSpec

rules

The rules section of a grammar. Contains zero or more rule
specifications (parser or lexer).
rules
: ruleSpec*
;
ruleSpec

ruleSpec

A single rule specification. Either a parser rule or a lexer rule.
ruleSpec
: parserRuleSpec
| lexerRuleSpec
;
parserRuleSpec lexerRuleSpec

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 ';'.
parserRuleSpec
: ruleModifiers? RULE_REF argActionBlock? ruleReturns? throwsSpec? localsSpec? rulePrequel* COLON ruleBlock SEMI
exceptionGroup
;
ruleModifiers RULE_REF argActionBlock ruleReturns throwsSpec localsSpec rulePrequel COLON ruleBlock SEMI exceptionGroup

exceptionGroup

A group of zero or more exception handlers and an optional finally
clause following a parser rule body.
exceptionGroup
: exceptionHandler* finallyClause?
;
exceptionHandler finallyClause

exceptionHandler

A single catch clause. Consists of 'catch', an argument action
specifying the exception type and variable, and an action body.
exceptionHandler
: CATCH argActionBlock actionBlock
;
CATCH argActionBlock actionBlock

finallyClause

A finally clause. Consists of 'finally' and an action body that
is always executed after the rule completes.
finallyClause
: FINALLY actionBlock
;
FINALLY actionBlock

rulePrequel

A single prequel construct within a rule. Either an options block
or a named rule action such as @init or @after.
rulePrequel
: optionsSpec
| ruleAction
;
optionsSpec ruleAction

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]'
ruleReturns
: RETURNS argActionBlock
;
RETURNS argActionBlock

throwsSpec

A throws clause listing exception types a rule may throw.
Introduced by 'throws' followed by a comma-separated list of
qualified identifiers.
throwsSpec
: THROWS qualifiedIdentifier (COMMA qualifiedIdentifier)*
;
THROWS qualifiedIdentifier COMMA qualifiedIdentifier

localsSpec

A locals declaration. Declares local variables available throughout
the rule using an argument action block.
e.g. 'locals [int i = 0]'
localsSpec
: LOCALS argActionBlock
;
LOCALS argActionBlock

ruleAction

A named action attached to a rule, such as @init or @after.
Executed at the start or end of the rule body respectively.
ruleAction
: AT identifier actionBlock
;
AT identifier actionBlock

ruleModifiers

One or more rule modifier keywords preceding a rule definition.
ruleModifiers
: ruleModifier+
;
ruleModifier

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.
ruleModifier
: PUBLIC
| PRIVATE
| PROTECTED
| FRAGMENT
;
PUBLIC PRIVATE PROTECTED FRAGMENT

ruleBlock

The body of a parser rule. Contains the list of labeled alternatives.
ruleBlock
: ruleAltList
;
ruleAltList

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.
ruleAltList
: labeledAlt (OR labeledAlt)*
;
labeledAlt OR labeledAlt

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.
labeledAlt
: alternative (POUND identifier)?
;
alternative POUND identifier

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 ';'.
lexerRuleSpec
: FRAGMENT? TOKEN_REF optionsSpec? COLON lexerRuleBlock SEMI
;
FRAGMENT TOKEN_REF optionsSpec COLON lexerRuleBlock SEMI

lexerRuleBlock

The body of a lexer rule. Contains the list of lexer alternatives.
lexerRuleBlock
: lexerAltList
;
lexerAltList

lexerAltList

A list of lexer alternatives separated by '|'.
lexerAltList
: lexerAlt (OR lexerAlt)*
;
lexerAlt OR lexerAlt

lexerAlt

A single lexer alternative. Consists of zero or more lexer elements
optionally followed by lexer commands. An empty alternative is
explicitly permitted.
lexerAlt
: lexerElements lexerCommands?
|
// explicitly allow empty alts
;
lexerElements lexerCommands

lexerElements

One or more lexer elements forming a lexer alternative, or empty.
lexerElements
: lexerElement+
|
;
lexerElement

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.
lexerElement
: lexerAtom ebnfSuffix?
| lexerBlock ebnfSuffix?
| actionBlock QUESTION?
;
lexerAtom ebnfSuffix lexerBlock ebnfSuffix actionBlock QUESTION

lexerBlock

A parenthesized group of lexer alternatives used as a subrule
within a lexer rule.
lexerBlock
: LPAREN lexerAltList RPAREN
;
LPAREN lexerAltList RPAREN

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)'
lexerCommands
: RARROW lexerCommand (COMMA lexerCommand)*
;
RARROW lexerCommand COMMA lexerCommand

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)'
lexerCommand
: lexerCommandName LPAREN lexerCommandExpr RPAREN
| lexerCommandName
;
lexerCommandName LPAREN lexerCommandExpr RPAREN lexerCommandName

lexerCommandName

The name of a lexer command. Either an identifier or the 'mode'
keyword.
lexerCommandName
: identifier
| MODE
;
identifier MODE

lexerCommandExpr

The argument to a lexer command. Either an identifier or an integer.
lexerCommandExpr
: identifier
| INT
;
identifier INT

altList

A list of parser alternatives separated by '|'. Used within
parenthesized blocks.
altList
: alternative (OR alternative)*
;
alternative OR alternative

alternative

A single parser alternative. Consists of optional element options
followed by one or more elements, or empty.
alternative
: elementOptions? element+
|
// explicitly allow empty alts
;
elementOptions element

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
: labeledElement (ebnfSuffix |)
| atom (ebnfSuffix |)
| ebnf
| actionBlock QUESTION? predicateOptions?
;
labeledElement ebnfSuffix atom ebnfSuffix ebnf actionBlock QUESTION predicateOptions

predicateOptions

Options attached to a semantic predicate using angle brackets.
e.g. {myPred}?<fail='message'>
predicateOptions
: LT predicateOption (COMMA predicateOption)* GT
;
LT predicateOption COMMA predicateOption GT

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.
predicateOption
: elementOption
| identifier ASSIGN (actionBlock | INT | STRING_LITERAL)
;
elementOption identifier ASSIGN actionBlock INT STRING_LITERAL

labeledElement

A labeled element. An atom or block assigned a label using '='
(single value) or '+=' (list append).
e.g. 'lhs=expr', 'items+=item'
labeledElement
: identifier (ASSIGN | PLUS_ASSIGN) (atom | block)
;
identifier ASSIGN PLUS_ASSIGN atom block

ebnf

An EBNF expression: a parenthesized block with an optional suffix.
The suffix controls repetition behavior.
ebnf
: block blockSuffix?
;
block blockSuffix

blockSuffix

The suffix of an EBNF block. Delegates to ebnfSuffix.
blockSuffix
: ebnfSuffix
;
ebnfSuffix

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.
ebnfSuffix
: QUESTION QUESTION?
| STAR QUESTION?
| PLUS QUESTION?
;
QUESTION QUESTION STAR QUESTION PLUS QUESTION

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 '.'.
lexerAtom
: characterRange
| terminalDef
| notSet
| LEXER_CHAR_SET
| wildcard
;
characterRange terminalDef notSet LEXER_CHAR_SET wildcard

atom

An atomic parser element. May be a terminal definition, a rule
reference, a negated set, or a wildcard '.'.
atom
: terminalDef
| ruleref
| notSet
| wildcard
;
terminalDef ruleref notSet wildcard

wildcard

The wildcard element '.'. Matches any single token in a parser rule
or any single character in a lexer rule. May have element options.
wildcard
: DOT elementOptions?
;
DOT elementOptions

notSet

A negated element using '~'. Matches any token or character not in
the specified set element or block set.
notSet
: NOT setElement
| NOT blockSet
;
NOT setElement NOT blockSet

blockSet

A parenthesized set of alternatives used with the '~' negation
operator.
blockSet
: LPAREN setElement (OR setElement)* RPAREN
;
LPAREN setElement OR setElement RPAREN

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.
setElement
: TOKEN_REF elementOptions?
| STRING_LITERAL elementOptions?
| characterRange
| LEXER_CHAR_SET
;
TOKEN_REF elementOptions STRING_LITERAL elementOptions characterRange LEXER_CHAR_SET

block

A parenthesized block of alternatives used as a subrule within a
parser rule. May optionally contain options and named actions before
a ':' separator.
block
: LPAREN (optionsSpec? ruleAction* COLON)? altList RPAREN
;
LPAREN optionsSpec ruleAction COLON altList RPAREN

ruleref

A parser rule reference. References a rule by its RULE_REF name,
with optional argument action and element options.
ruleref
: RULE_REF argActionBlock? elementOptions?
;
RULE_REF argActionBlock elementOptions

characterRange

A character range used in lexer rules. Matches any single character
between two string literals, inclusive, using the '..' operator.
e.g. 'a'..'z'
characterRange
: STRING_LITERAL RANGE STRING_LITERAL
;
STRING_LITERAL RANGE STRING_LITERAL

terminalDef

A terminal element reference. Either a token reference or a string
literal, optionally adorned with element options in angle brackets.
terminalDef
: TOKEN_REF elementOptions?
| STRING_LITERAL elementOptions?
;
TOKEN_REF elementOptions STRING_LITERAL elementOptions

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>
elementOptions
: LT elementOption (COMMA elementOption)* GT
;
LT elementOption COMMA elementOption GT

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.
elementOption
: qualifiedIdentifier
| identifier ASSIGN (qualifiedIdentifier | STRING_LITERAL | INT)
;
qualifiedIdentifier identifier ASSIGN qualifiedIdentifier STRING_LITERAL INT

identifier

An identifier. Either a RULE_REF (lowercase) or a TOKEN_REF
(uppercase). Used wherever a plain name is needed regardless of
case convention.
identifier
: RULE_REF
| TOKEN_REF
;
RULE_REF TOKEN_REF
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'
qualifiedIdentifier
: identifier (DOT identifier)*
;
identifier DOT identifier

DOC_COMMENT

entry point
A documentation comment beginning with '/**'. Routed to the
COMMENT channel. Consumes everything up to the closing '* /' or
end of file.
DOC_COMMENT
: '/**' .*? ('*/' | EOF) -> channel (COMMENT)
;
'/**' . '*/' EOF
EOF

BLOCK_COMMENT

entry point
A block comment beginning with '/*'. Routed to the COMMENT channel.
Consumes everything up to the closing '* /' or end of file.
BLOCK_COMMENT
: '/*' .*? ('*/' | EOF) -> channel (COMMENT)
;
'/*' . '*/' EOF
EOF

LINE_COMMENT

entry point
A single-line comment beginning with '//'. Routed to the COMMENT
channel. Consumes all characters up to but not including the newline.
LINE_COMMENT
: '//' ~ [\r\n]* -> channel (COMMENT)
;
'//' ~ [\r\n]

INT

A non-negative integer literal. Either '0' or a sequence of digits
beginning with a non-zero digit.
INT
: '0'
| [1-9] [0-9]*
;
'0' [1-9] [0-9]

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.
STRING_LITERAL
: '\'' (ESC_SEQUENCE | ~ ['\r\n\\])* '\''
;
'\'' ESC_SEQUENCE ~ ['\r\n\\] '\''

UNTERMINATED_STRING_LITERAL

entry point
An unterminated string literal. Matched when a single-quoted string
is not closed before a newline or end of file. Used for error
recovery.
UNTERMINATED_STRING_LITERAL
: '\'' (ESC_SEQUENCE | ~ ['\r\n\\])*
;
'\'' ESC_SEQUENCE ~ ['\r\n\\]

BEGIN_ARGUMENT

The opening delimiter of an argument action block. Matched when '['
is encountered. Delegates to handleBeginArgument() to switch the
lexer into Argument mode.
BEGIN_ARGUMENT
: '[' { this.handleBeginArgument(); }
;
'['

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.
ACTION
: NESTED_ACTION
;
NESTED_ACTION

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 '}'.
fragment NESTED_ACTION
: '{' (
NESTED_ACTION
| STRING_LITERAL
| DoubleQuoteLiteral
| TripleQuoteLiteral
| BacktickQuoteLiteral
| '/*' .*? '*/'
| '//' ~[\r\n]*
| '\\' .
| ~(
'\\'
| '"'
| '\''
| '`'
| '{'
)
)*? '}'
;
'{' NESTED_ACTION STRING_LITERAL DoubleQuoteLiteral TripleQuoteLiteral BacktickQuoteLiteral '/*' . '*/' '//' ~ [\r\n] '\\' . ~ '\\' '"' '\'' '`' '{' '}'

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.
OPTIONS
: 'options' WS* '{'
;
'options' WS '{'

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.
TOKENS
: 'tokens' WS* '{'
;
'tokens' WS '{'

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.
CHANNELS
: 'channels' WS* '{'
;
'channels' WS '{'

IMPORT

The 'import' keyword. Introduces a delegate grammar declaration.
IMPORT
: 'import'
;
'import'

FRAGMENT

The 'fragment' keyword. Marks a lexer rule as a non-matching
subroutine reusable within other lexer rules.
FRAGMENT
: 'fragment'
;
'fragment'

LEXER

The 'lexer' keyword. Used in grammar type declarations and action
scope names.
LEXER
: 'lexer'
;
'lexer'

PARSER

The 'parser' keyword. Used in grammar type declarations and action
scope names.
PARSER
: 'parser'
;
'parser'

GRAMMAR

The 'grammar' keyword. Introduces the grammar declaration.
GRAMMAR
: 'grammar'
;
'grammar'

PROTECTED

The 'protected' keyword. An access modifier for rule definitions.
PROTECTED
: 'protected'
;
'protected'

PUBLIC

The 'public' keyword. An access modifier for rule definitions.
PUBLIC
: 'public'
;
'public'

PRIVATE

The 'private' keyword. An access modifier for rule definitions.
PRIVATE
: 'private'
;
'private'

RETURNS

The 'returns' keyword. Introduces the return value specification
of a rule.
RETURNS
: 'returns'
;
'returns'

LOCALS

The 'locals' keyword. Introduces local variable declarations
within a rule.
LOCALS
: 'locals'
;
'locals'

THROWS

The 'throws' keyword. Introduces the exception type list for a rule.
THROWS
: 'throws'
;
'throws'

CATCH

The 'catch' keyword. Introduces an exception handler clause.
CATCH
: 'catch'
;
'catch'

FINALLY

The 'finally' keyword. Introduces a finally clause.
FINALLY
: 'finally'
;
'finally'

MODE

The 'mode' keyword. Introduces a lexer mode specification.
MODE
: 'mode'
;
'mode'

COLON

The ':' character. Separates a rule name from its body.
COLON
: ':'
;
':'

COLONCOLON

The '::' sequence. Separates a scope name from an action name
in grammar-level actions. e.g. '@parser::members'
COLONCOLON
: '::'
;
'::'

COMMA

The ',' character. Separates items in lists.
COMMA
: ','
;
','

SEMI

The ';' character. Terminates rule definitions and option pairs.
SEMI
: ';'
;
';'

LPAREN

The '(' character. Opens a parenthesized block or argument list.
LPAREN
: '('
;
'('

RPAREN

The ')' character. Closes a parenthesized block or argument list.
RPAREN
: ')'
;
')'

RBRACE

The '}' character. Closes an options, tokens, or channels block.
RBRACE
: '}'
;
'}'

RARROW

The '->' sequence. Introduces lexer commands or rewrite rules.
RARROW
: '->'
;
'->'

LT

The '<' character. Opens an element options list.
LT
: '<'
;
'<'

GT

The '>' character. Closes an element options list.
GT
: '>'
;
'>'

ASSIGN

The '=' character. Used for option assignment and element labeling.
ASSIGN
: '='
;
'='

QUESTION

The '?' character. Used as an EBNF suffix for optional elements,
and to mark semantic predicates when following an action block.
QUESTION
: '?'
;
'?'

STAR

The '*' character. Used as an EBNF suffix for zero-or-more repetition.
STAR
: '*'
;
'*'

PLUS_ASSIGN

The '+=' sequence. Used for list-append labeling of elements.
PLUS_ASSIGN
: '+='
;
'+='

PLUS

The '+' character. Used as an EBNF suffix for one-or-more repetition.
PLUS
: '+'
;
'+'

OR

The '|' character. Separates alternatives within a rule or block.
OR
: '|'
;
'|'

DOLLAR

entry point
The '$' character. Used to reference labels within rewrite rules
and actions.
DOLLAR
: '$'
;
'$'

RANGE

The '..' sequence. Separates the bounds of a character range.
e.g. 'a'..'z'
RANGE
: '..'
;
'..'

DOT

The '.' character. Used as a wildcard element and as a separator
in qualified identifiers.
DOT
: '.'
;
'.'

AT

The '@' character. Introduces grammar-level and rule-level named
actions.
AT
: '@'
;
'@'

POUND

The '#' character. Introduces a label on an alternative within a
parser rule, used to generate named context classes.
POUND
: '#'
;
'#'

NOT

The '~' character. Introduces a negated element or set.
NOT
: '~'
;
'~'

ID

entry point
An 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.
ID
: NameStartChar NameChar*
;
NameStartChar NameChar

WS

Whitespace: spaces, tabs, carriage returns, newlines, and form feeds.
Routed to the OFF_CHANNEL so the parser does not see it.
WS
: [ \t\r\n\f]+ -> channel (OFF_CHANNEL)
;
[ \t\r\n\f]

NESTED_ARGUMENT

entry point
A nested argument action opened by '['. Pushes a new Argument mode
to handle nested bracket pairs. Reclassified as ARGUMENT_CONTENT.
NESTED_ARGUMENT
: '[' -> type (ARGUMENT_CONTENT), pushMode (Argument)
;
'['

ARGUMENT_ESCAPE

entry point
An escape sequence within an argument action. The backslash and the
following character are reclassified as ARGUMENT_CONTENT.
ARGUMENT_ESCAPE
: '\\' . -> type (ARGUMENT_CONTENT)
;
'\\' .

ARGUMENT_STRING_LITERAL

entry point
A double-quoted string literal within an argument action.
Reclassified as ARGUMENT_CONTENT.
ARGUMENT_STRING_LITERAL
: DoubleQuoteLiteral -> type (ARGUMENT_CONTENT)
;
DoubleQuoteLiteral

ARGUMENT_CHAR_LITERAL

entry point
A single-quoted string literal within an argument action.
Reclassified as ARGUMENT_CONTENT.
ARGUMENT_CHAR_LITERAL
: STRING_LITERAL -> type (ARGUMENT_CONTENT)
;
STRING_LITERAL

END_ARGUMENT

The closing ']' of an argument action. Delegates to
handleEndArgument() to pop the Argument mode.
END_ARGUMENT
: ']' { this.handleEndArgument(); }
;
']'

UNTERMINATED_ARGUMENT

entry point
Matches EOF within an argument action to recover from an unterminated
argument. Pops the Argument mode without consuming any input.
UNTERMINATED_ARGUMENT
: EOF -> popMode
;
EOF
EOF

ARGUMENT_CONTENT

Any other single character within an argument action body.
ARGUMENT_CONTENT
: .
;
.

LEXER_CHAR_SET_BODY

entry point
One 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.
LEXER_CHAR_SET_BODY
: (~ [\]\\] | '\\' .)+ -> more
;
~ [\]\\] '\\' .

LEXER_CHAR_SET

The closing ']' of a lexer character set. Completes the
LEXER_CHAR_SET token and pops LexerCharSet mode.
LEXER_CHAR_SET
: ']' -> popMode
;
']'

UNTERMINATED_CHAR_SET

entry point
Matches EOF within a lexer character set to recover from an
unterminated character set. Pops LexerCharSet mode.
UNTERMINATED_CHAR_SET
: EOF -> popMode
;
EOF
EOF

ESC_SEQUENCE

An escape sequence within a string literal. Handles standard
character escapes, Unicode escapes (\uXXXX), and any other
character following a backslash.
fragment ESC_SEQUENCE
: '\\' ([btnfr"'\\] | UnicodeESC | . | EOF)
;
'\\' [btnfr"'\\] UnicodeESC . EOF

HexDigit

A single hexadecimal digit. Used within Unicode escape sequences.
fragment HexDigit
: [0-9a-fA-F]
;
[0-9a-fA-F]

UnicodeESC

A Unicode escape sequence of the form \uXXXX. The hex digits are
individually optional to allow partial matches during error recovery.
fragment UnicodeESC
: 'u' (HexDigit (HexDigit (HexDigit HexDigit?)?)?)?
;
'u' HexDigit HexDigit HexDigit HexDigit

DoubleQuoteLiteral

A double-quoted string literal. Used within action blocks as a
target-language string. Handles escape sequences.
fragment DoubleQuoteLiteral
: '"' (ESC_SEQUENCE | ~["\r\n\\])*? '"'
;
'"' ESC_SEQUENCE ~ ["\r\n\\] '"'

TripleQuoteLiteral

A triple-quoted string literal. Used within action blocks as a
target-language multi-line string. Handles escape sequences.
fragment TripleQuoteLiteral
: '"""' (ESC_SEQUENCE | .)*? '"""'
;
'"""' ESC_SEQUENCE . '"""'

BacktickQuoteLiteral

A backtick-quoted string literal. Used within action blocks in
target languages that support backtick strings. Handles escape
sequences.
fragment BacktickQuoteLiteral
: '`' (ESC_SEQUENCE | ~["\r\n\\])*? '`'
;
'`' ESC_SEQUENCE ~ ["\r\n\\] '`'

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.
fragment NameChar
: NameStartChar
| '0' .. '9'
| '_'
| '\u00B7'
| '\u0300' .. '\u036F'
| '\u203F' .. '\u2040'
;
NameStartChar '0'..'9' '_' '\u00B7' '\u0300'..'\u036F' '\u203F'..'\u2040'

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.
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]
;
'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'