public class GroovyRecognizer extends antlrLLkParser implements GroovyTokenTypes
JSR-241 Groovy Recognizer. Run 'java Main [-showtree] directory-full-of-groovy-files' [The -showtree option pops up a Swing frame that shows the AST constructed from the parser.] Contributing authors: John Mitchell johnm
Constructor and description |
---|
protected GroovyRecognizer
(TokenBuffer tokenBuf, int k) |
GroovyRecognizer
(TokenBuffer tokenBuf) |
protected GroovyRecognizer
(TokenStream lexer, int k) |
GroovyRecognizer
(TokenStream lexer) |
GroovyRecognizer
(ParserSharedInputState state) |
Type Params | Return Type | Name and description |
---|---|---|
|
public final void |
aCase() |
|
public void |
addWarning(String warning, String solution) |
|
public final void |
additiveExpression(int lc_stmt) |
|
public final void |
andExpression(int lc_stmt) |
|
public final void |
annotation() |
|
public final void |
annotationArguments() |
|
public final void |
annotationBlock() |
|
public final void |
annotationDefinition(AST modifiers) |
|
public final void |
annotationField() |
|
public final void |
annotationIdent() |
|
public final void |
annotationMemberValueInitializer() |
|
public final void |
annotationMemberValuePair() |
|
public final void |
annotationMemberValuePairs() |
|
public final void |
annotationsInternal() |
|
public final void |
annotationsOpt() |
|
public final void |
appendedBlock(AST callee) An appended block follows any expression. |
|
public final void |
argList() |
|
public final byte |
argument() A single argument in (...) or [...]. |
|
public final void |
argumentLabel() A label for an argument is of the form a:b, 'a':b, "a":b, (a):b, etc.. |
|
public final void |
argumentLabelStart() For lookahead only. |
|
public final void |
assignmentExpression(int lc_stmt) |
|
public final void |
assignmentLessExpression() |
|
public final void |
balancedBrackets() Fast lookahead across balanced brackets of all sorts. |
|
public final void |
balancedTokens() |
|
public final void |
blockBody(int prevToken) A block body is a parade of zero or more statements or expressions. |
|
public final void |
branchStatement() In Groovy, return, break, continue, throw, and assert can be used in a parenthesized expression context. |
|
protected void |
buildTokenTypeASTClassMap() |
|
public final void |
builtInType() |
|
public final void |
builtInTypeArraySpec(boolean addImagNode) |
|
public final void |
builtInTypeSpec(boolean addImagNode) |
|
public final void |
caseSList() |
|
public final void |
casesGroup() |
|
public final void |
checkSuspiciousExpressionStatement(int prevToken) If two statements are separated by newline (not SEMI), the second had better not look like the latter half of an expression. |
|
public final void |
classBlock() |
|
public final void |
classDefinition(AST modifiers) |
|
public final void |
classField() |
|
public final void |
classOrInterfaceType(boolean addImagNode) |
|
public final void |
classTypeSpec(boolean addImagNode) |
|
public Token |
cloneToken(Token t) |
|
public final void |
closableBlock() A block which is known to be a closure, even if it has no apparent arguments. |
|
public final void |
closableBlockConstructorExpression() |
|
public final void |
closableBlockParam() Simple names, as in {x|...}, are completely equivalent to {(def x)|...}. |
|
public final void |
closableBlockParamsOpt(boolean addImplicit) Closure parameters are exactly like method parameters, except that they are not enclosed in parentheses, but rather are prepended to the front of a block, just after the brace. |
|
public final void |
closableBlockParamsStart() Lookahead to check whether a block begins with explicit closure arguments. |
|
public final void |
closureList() |
|
public final void |
commandArgument() |
|
public final void |
commandArguments(AST head) A member name (x.y) or element name (x[y]) can serve as a command name, which may be followed by a list of arguments. |
|
public final void |
commandArgumentsGreedy(AST head) |
|
public final void |
compatibleBodyStatement() In Java, "if", "while", and "for" statements can take random, non-braced statements as their bodies. |
|
public final void |
compilationUnit() |
|
public final void |
compoundStatement() |
|
public final void |
conditionalExpression(int lc_stmt) |
|
public final void |
constant() Numeric, string, regexp, boolean, or null constant. |
|
public final void |
constantNumber() Numeric constant. |
|
public final void |
constructorBody() |
|
public final void |
constructorDefinition(AST mods) I've split out constructors separately; we could maybe integrate back into variableDefinitions later on if we maybe simplified 'def' to be a type declaration? |
|
public final void |
constructorStart() Used to look ahead for a constructor |
|
public final void |
controlExpressionList() |
|
public AST |
create(int type, String txt, AST first) Creates an AST node with the token type and text passed in, but with the same background information as another supplied Token (e.g. |
|
public AST |
create(int type, String txt, Token first, Token last) |
|
public AST |
create(int type, String txt, AST first, Token last) |
|
public AST |
create(int type, String txt, AST first, AST last) |
|
public final void |
declaration() A declaration is the creation of a reference or primitive-type variable, or (if arguments are present) of a method. |
|
public final void |
declarationStart() Used only as a lookahead predicate, before diving in and parsing a declaration. |
|
public final void |
declaratorBrackets(AST typ) After some type names, where zero or more empty bracket pairs are allowed. |
|
public final void |
dynamicMemberName() If a dot is followed by a parenthesized or quoted expression, the member is computed dynamically, and the member selection is done only at runtime. |
|
public final void |
enumBlock() |
|
public final void |
enumConstant() |
|
public final void |
enumConstantBlock() |
|
public final void |
enumConstantField() |
|
protected final void |
enumConstantFieldInternal(AST mods, AST tp, AST t, Token first) |
|
public final void |
enumConstants() Comma-separated list of one or more enum constant definitions. |
|
public final void |
enumConstantsEnd() |
|
public final void |
enumConstantsStart() Guard for enumConstants. |
|
public final void |
enumDefinition(AST modifiers) |
|
public final void |
equalityExpression(int lc_stmt) |
|
public final void |
exclusiveOrExpression(int lc_stmt) |
|
public final void |
explicitConstructorInvocation() Catch obvious constructor calls, but not the expr.super(...) calls |
|
public final void |
expression(int lc_stmt) |
|
public final void |
expressionStatement(int prevToken) An expression statement can be any general expression. |
|
public final void |
expressionStatementNoCheck() |
|
public final void |
finallyClause() |
|
public final void |
forCond() |
|
public final void |
forInClause() |
|
public final void |
forInit() |
|
public final void |
forIter() |
|
public final void |
forStatement() |
|
public final void |
genericMethod() |
|
public final void |
genericMethodStart() lookahead predicate for usage of generics in methods as parameter for the method. |
|
public GroovyLexer |
getLexer() |
|
public List |
getWarningList() |
|
public final void |
handler() |
|
public final void |
identifier() |
|
public final void |
identifierStar() |
|
public final void |
implementsClause() |
|
public final void |
implicitParameters() A block known to be a closure, but which omits its arguments, is given this placeholder. |
|
public final void |
importStatement() |
|
public final void |
inclusiveOrExpression(int lc_stmt) |
|
public final void |
indexPropertyArgs(AST indexee) An expression may be followed by [...]. |
|
public final void |
interfaceBlock() |
|
public final void |
interfaceDefinition(AST modifiers) |
|
public final void |
interfaceExtends() |
|
public final void |
interfaceField() |
|
public final void |
keywordPropertyNames() |
|
public final void |
listOfVariables(AST mods, AST t, Token first) |
|
public final void |
listOrMapConstructorExpression() A list constructor is a argument list enclosed in square brackets, without labels. |
|
public final void |
logicalAndExpression(int lc_stmt) |
|
public final void |
logicalOrExpression(int lc_stmt) |
|
public static GroovyRecognizer |
make(GroovyLexer lexer) This factory is the correct way to wire together a Groovy parser and lexer. |
|
public static GroovyRecognizer |
make(InputStream in) |
|
public static GroovyRecognizer |
make(Reader in) |
|
public static GroovyRecognizer |
make(InputBuffer in) |
|
public static GroovyRecognizer |
make(LexerSharedInputState in) |
|
public void |
matchGenericTypeBracketsFailed(String problem, String solution) |
|
public final void |
methodCallArgs(AST callee) An expression may be followed by one or both of (...) and {...}. |
|
public final void |
modifier() |
|
public final void |
modifiers() A list of one or more modifier, annotation, or "def". |
|
public final void |
modifiersInternal() |
|
public final void |
modifiersOpt() A list of zero or more modifiers, annotations, or "def". |
|
public final void |
multicatch() |
|
public final void |
multicatch_types() |
|
public final void |
multipleAssignment(int lc_stmt) |
|
public final void |
multipleAssignmentDeclaration() |
|
public final void |
multipleAssignmentDeclarationStart() |
|
public final void |
multiplicativeExpression(int lc_stmt) |
|
public final void |
namePart() This is the grammar for what can follow a dot: x.a, x. |
|
public final void |
newArrayDeclarator() |
|
public final void |
newExpression() object instantiation. |
|
public final void |
nls() Zero or more insignificant newlines, all gobbled up and thrown away. |
|
public final void |
nlsWarn() Zero or more insignificant newlines, all gobbled up and thrown away, but a warning message is left for the user, if there was a newline. |
|
public final void |
openBlock() An open block is not allowed to have closure arguments. |
|
public final void |
openOrClosableBlock() A sub-block of a block can be either open or closable. |
|
public final void |
packageDefinition() |
|
public final void |
parameterDeclaration() A formal parameter for a method or closure. |
|
public final void |
parameterDeclarationList() A list of zero or more formal parameters. |
|
public final void |
parameterModifiersOpt() |
|
public final void |
parenthesizedExpression() |
|
public final void |
pathChain(int lc_stmt, AST prefix) |
|
public final void |
pathElement(AST prefix) |
|
public final void |
pathElementStart() |
|
public final void |
pathExpression(int lc_stmt) A "path expression" is a name or other primary, possibly qualified by various forms of dot, and/or followed by various kinds of brackets. |
|
public final void |
postfixExpression(int lc_stmt) |
|
public final void |
powerExpression(int lc_stmt) |
|
public final void |
powerExpressionNotPlusMinus(int lc_stmt) |
|
public final void |
primaryExpression() |
|
public final void |
qualifiedTypeName() |
|
public final void |
regexExpression(int lc_stmt) |
|
public final void |
relationalExpression(int lc_stmt) |
|
public void |
requireFailed(String problem, String solution) |
|
public final void |
sep() A statement separator is either a semicolon or a significant newline. |
|
public void |
setFilename(String f) |
|
public void |
setSourceBuffer(SourceBuffer sourceBuffer) |
|
public final void |
shiftExpression(int lc_stmt) |
|
public final void |
singleDeclaration() A declaration with one declarator and optional initialization, like a parameterDeclaration. |
|
public final void |
singleDeclarationNoInit() A declaration with one declarator and no initialization, like a parameterDeclaration. |
|
public final void |
singleVariable(AST mods, AST t) Used in cases where a declaration cannot have commas, or ends with the "in" operator instead of '='. |
|
public final void |
snippetUnit() A Groovy script or simple expression. |
|
public final void |
statement(int prevToken) A statement is an element of a block. |
|
public final void |
statementLabelPrefix() A labeled statement, consisting of a vanilla identifier followed by a colon. |
|
public final boolean |
strictContextExpression(boolean allowDeclaration) Things that can show up as expressions, but only in strict contexts like inside parentheses, argument lists, and list constructors. |
|
public final void |
stringConstructorExpression() |
|
public final void |
stringConstructorValuePart() |
|
public final void |
superClassClause() |
|
public final void |
suspiciousExpressionStatementStart() Lookahead for suspicious statement warnings and errors. |
|
public final void |
throwsClause() |
|
public void |
traceIn(String rname) |
|
public void |
traceOut(String rname) |
|
public final void |
traitDefinition(AST modifiers) |
|
public final void |
tryBlock() |
|
public final void |
type() |
|
public final void |
typeArgument() |
|
public final void |
typeArgumentBounds() |
|
public final void |
typeArgumentSpec() |
|
public final void |
typeArguments() |
|
public final void |
typeArgumentsDiamond() |
|
protected final void |
typeArgumentsOrParametersEnd() |
|
protected final void |
typeDefinitionInternal(AST mods) |
|
public final void |
typeDefinitionStart() Used only as a lookahead predicate for nested type definitions. |
|
public final void |
typeNamePairs(AST mods, Token first) |
|
public final void |
typeParameter() |
|
public final void |
typeParameterBounds() |
|
public final void |
typeParameters() |
|
public final void |
typeSpec(boolean addImagNode) |
|
public final void |
unaryExpression(int lc_stmt) |
|
public final void |
unaryExpressionNotPlusMinus(int lc_stmt) |
|
public final void |
upperCaseIdent() An IDENT token whose spelling is required to start with an uppercase letter. |
|
public final void |
varInitializer() An assignment operator '=' followed by an expression. |
|
public final void |
variableDeclarator(AST mods, AST t, Token first) Declaration of a variable. |
|
public final void |
variableDefinitions(AST mods, AST t) The tail of a declaration. |
|
public final void |
variableName() |
|
public final void |
wildcardType() |
An appended block follows any expression. If the expression is not a method call, it is given an empty argument list.
A single argument in (...) or [...]. Corresponds to to a method or closure parameter. May be labeled. May be modified by the spread operator '*' ('*:' for keywords).
A label for an argument is of the form a:b, 'a':b, "a":b, (a):b, etc.. The labels in (a:b), ('a':b), and ("a":b) are in all ways equivalent, except that the quotes allow more spellings. Equivalent dynamically computed labels are (('a'):b) and ("${'a'}":b) but not ((a):b) or "$a":b, since the latter cases evaluate (a) as a normal identifier. Bottom line: If you want a truly variable label, use parens and say ((a):b).
For lookahead only. Fast approximate parse of an argumentLabel followed by a colon.
Fast lookahead across balanced brackets of all sorts.
A block body is a parade of zero or more statements or expressions.
In Groovy, return, break, continue, throw, and assert can be used in a parenthesized expression context. Example: println (x || (return)); println assert x, "won't print a false value!" If an optional expression is missing, its value is void (this coerces to null when a value is required).
If two statements are separated by newline (not SEMI), the second had better not look like the latter half of an expression. If it does, issue a warning.
Also, if the expression starts with a closure, it needs to have an explicit parameter list, in order to avoid the appearance of a compound statement. This is a hard error.
These rules are different from Java's "dumb expression" restriction. Unlike Java, Groovy blocks can end with arbitrary (even dumb) expressions, as a consequence of optional 'return' and 'continue' tokens.
To make the programmer's intention clear, a leading closure must have an explicit parameter list, and must not follow a previous statement separated only by newlines.
A block which is known to be a closure, even if it has no apparent arguments. A block inside an expression or after a method call is always assumed to be a closure. Only labeled, unparameterized blocks which occur directly as substatements are kept open.
Simple names, as in {x|...}, are completely equivalent to {(def x)|...}. Build the right AST.
Closure parameters are exactly like method parameters, except that they are not enclosed in parentheses, but rather are prepended to the front of a block, just after the brace. They are separated from the closure body by a CLOSABLE_BLOCK_OP token '->'.
Lookahead to check whether a block begins with explicit closure arguments.
A member name (x.y) or element name (x[y]) can serve as a command name, which may be followed by a list of arguments. Unlike parenthesized arguments, these must be plain expressions, without labels or spread operators.
In Java, "if", "while", and "for" statements can take random, non-braced statements as their bodies. Support this practice, even though it isn't very Groovy.
Numeric, string, regexp, boolean, or null constant.
Numeric constant.
I've split out constructors separately; we could maybe integrate back into variableDefinitions later on if we maybe simplified 'def' to be a type declaration?
Used to look ahead for a constructor
Creates an AST node with the token type and text passed in, but with the same background information as another supplied Token (e.g. line numbers). To be used in place of antlr tree construction syntax, i.e. #[TOKEN,"text"] becomes create(TOKEN,"text",anotherToken)
A declaration is the creation of a reference or primitive-type variable, or (if arguments are present) of a method. Generically, this is called a 'variable' definition, even in the case of a class field or method. It may start with the modifiers and/or a declaration keyword "def". It may also start with the modifiers and a capitalized type name.
AST effect: Create a separate Type/Var tree for each var in the var list. Must be guarded, as in (declarationStart) => declaration.
Used only as a lookahead predicate, before diving in and parsing a declaration. A declaration can be unambiguously introduced with "def", an annotation or a modifier token like "final". It may also be introduced by a simple identifier whose first character is an uppercase letter, as in {String x}. A declaration can also be introduced with a built in type like 'int' or 'void'. Brackets (array and generic) are allowed, as in {List[] x} or {int[][] y}. Anything else is parsed as a statement of some sort (expression or command).
(In the absence of explicit method-call parens, we assume a capitalized name is a type name. Yes, this is a little hacky. Alternatives are to complicate the declaration or command syntaxes, or to have the parser query the symbol table. Parse-time queries are evil. And we want both {String x} and {println x}. So we need a syntactic razor-edge to slip between 'println' and 'String'.)
After some type names, where zero or more empty bracket pairs are allowed. We use ARRAY_DECLARATOR to represent this.
If a dot is followed by a parenthesized or quoted expression, the member is computed dynamically, and the member selection is done only at runtime. This forces a statically unchecked member access.
Comma-separated list of one or more enum constant definitions.
Guard for enumConstants.
Catch obvious constructor calls, but not the expr.super(...) calls
An expression statement can be any general expression.
An expression statement can also be a command, which is a simple method call in which the outermost parentheses are omitted.
Certain "suspicious" looking forms are flagged for the user to disambiguate.
lookahead predicate for usage of generics in methods
as parameter for the method. Example:
static
A block known to be a closure, but which omits its arguments, is given this placeholder. A subsequent pass is responsible for deciding if there is an implicit 'it' parameter, or if the parameter list should be empty.
An expression may be followed by [...]. Unlike Java, these brackets may contain a general argument list, which is passed to the array element operator, which can make of it what it wants. The brackets may also be empty, as in T[]. This is how Groovy names array types.
Returned AST is [INDEX_OP, indexee, ELIST].
A list constructor is a argument list enclosed in square brackets, without labels. Any argument can be decorated with a spread operator (*x), but not a label (a:x). Examples: [], [1], [1,2], [1,*l1,2], [*l1,*l2]. (The l1, l2 must be a sequence or null.)
A map constructor is an argument list enclosed in square brackets, with labels everywhere, except on spread arguments, which stand for whole maps spliced in. A colon alone between the brackets also forces the expression to be an empty map constructor. Examples: [:], [a:1], [a:1,b:2], [a:1,*:m1,b:2], [*:m1,*:m2] (The m1, m2 must be a map or null.) Values associated with identical keys overwrite from left to right: [a:1,a:2] === [a:2]
Some malformed constructor expressions are not detected in the parser, but in a post-pass. Bad examples: [1,b:2], [a:1,2], [:1]. (Note that method call arguments, by contrast, can be a mix of keyworded and non-keyworded arguments.)
This factory is the correct way to wire together a Groovy parser and lexer.
An expression may be followed by one or both of (...) and {...}. Note: If either is (...) or {...} present, it is a method call. The {...} is appended to the argument list, and matches a formal of type Closure. If there is no method member, a property (or field) is used instead, and must itself be callable.
If the methodCallArgs are absent, it is a property reference. If there is no property, it is treated as a field reference, but never a method reference.
Arguments in the (...) can be labeled, and the appended block can be labeled also. If there is a mix of unlabeled and labeled arguments, all the labeled arguments must follow the unlabeled arguments, except that the closure (labeled or not) is always a separate final argument. Labeled arguments are collected up and passed as a single argument to a formal of type Map.
Therefore, f(x,y, a:p, b:q) {s} is equivalent in all ways to f(x,y, [a:p,b:q], {s}). Spread arguments of sequence type count as unlabeled arguments, while spread arguments of map type count as labeled arguments. (This distinction must sometimes be checked dynamically.) A plain unlabeled argument is allowed to match a trailing Map or Closure argument: f(x, a:p) {s} === f(*[ x, [a:p], {s} ])
A list of one or more modifier, annotation, or "def".
A list of zero or more modifiers, annotations, or "def".
This is the grammar for what can follow a dot: x.a, x.@a, x.&a, x.'a', etc.
Note: typeArguments
is handled by the caller of namePart
.
object instantiation. Trees are built as illustrated by the following input/tree pairs: new T() new | T -- ELIST | arg1 -- arg2 -- .. -- argn new int[] new | int -- ARRAY_DECLARATOR new int[] {1,2} new | int -- ARRAY_DECLARATOR -- ARRAY_INIT | EXPR -- EXPR | | 1 2 new int[3] new | int -- ARRAY_DECLARATOR | EXPR | 3 new int[1][2] new | int -- ARRAY_DECLARATOR | ARRAY_DECLARATOR -- EXPR | | EXPR 1 | 2
Zero or more insignificant newlines, all gobbled up and thrown away.
Zero or more insignificant newlines, all gobbled up and thrown away, but a warning message is left for the user, if there was a newline.
An open block is not allowed to have closure arguments.
A sub-block of a block can be either open or closable. It is closable if and only if there are explicit closure arguments. Compare this to a block which is appended to a method call, which is given closure arguments, even if they are not explicit in the code.
A formal parameter for a method or closure.
A list of zero or more formal parameters. If a parameter is variable length (e.g. String... myArg) it should be to the right of any other parameters of the same kind. General form: (req, ..., opt, ..., [rest], key, ..., [restKeys], [block] This must be sorted out after parsing, since the various declaration forms are impossible to tell apart without backtracking.
A "path expression" is a name or other primary, possibly qualified by various forms of dot, and/or followed by various kinds of brackets. It can be used for value or assigned to, or else further qualified, indexed, or called. It is called a "path" because it looks like a linear path through a data structure. Examples: x.y, x?.y, x*.y, x.@y; x[], x[y], x[y,z]; x(), x(y), x(y,z); x{s}; a.b[n].c(x).d{s} (Compare to a C lvalue, or LeftHandSide in the JLS section 15.26.) General expressions are built up from path expressions, using operators like '+' and '='.
A statement separator is either a semicolon or a significant newline. Any number of additional (insignificant) newlines may accompany it.
A declaration with one declarator and optional initialization, like a parameterDeclaration.
Used to parse declarations used for both binding and effect, in places like argument
lists and while
statements.
A declaration with one declarator and no initialization, like a parameterDeclaration.
Used to parse loops like for (int x in y)
(up to the in
keyword).
Used in cases where a declaration cannot have commas, or ends with the "in" operator instead of '='.
A Groovy script or simple expression. Can be anything legal inside {...}.
A statement is an element of a block. Typical statements are declarations (which are scoped to the block) and expressions.
A labeled statement, consisting of a vanilla identifier followed by a colon.
Things that can show up as expressions, but only in strict contexts like inside parentheses, argument lists, and list constructors.
Lookahead for suspicious statement warnings and errors.
Used only as a lookahead predicate for nested type definitions.
An IDENT token whose spelling is required to start with an uppercase letter. In the case of a simple statement {UpperID name} the identifier is taken to be a type name, not a command name.
An assignment operator '=' followed by an expression. (Never empty.)
Declaration of a variable. This can be a class/instance variable, or a local variable in a method It can also include possible initialization.
The tail of a declaration. Either v1, v2, ... (with possible initializers) or else m(args){body}. The two arguments are the modifier list (if any) and the declaration head (if any). The declaration head is the variable type, or (for a method) the return type. If it is missing, then the variable type is taken from its initializer (if there is one). Otherwise, the variable type defaults to 'any'. DECIDE: Method return types default to the type of the method body, as an expression.
Copyright © 2003-2021 The Apache Software Foundation. All rights reserved.