public class GroovyRecognizer extends LLkParser
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 | Name and description |
---|---|
void |
aCase() |
void |
addWarning(String warning, String solution) |
void |
additiveExpression(int lc_stmt) |
void |
andExpression(int lc_stmt) |
void |
annotation() |
void |
annotationArguments() |
void |
annotationBlock() |
void |
annotationDefinition(AST modifiers) |
void |
annotationField() |
void |
annotationIdent() |
void |
annotationMemberArrayValueInitializer() |
void |
annotationMemberValueInitializer() |
void |
annotationMemberValuePair() |
void |
annotationMemberValuePairs() |
void |
annotationsInternal() |
void |
annotationsOpt() |
void |
appendedBlock(AST callee) An appended block follows any expression. |
void |
argList() |
byte |
argument() A single argument in (...) or [...]. |
void |
argumentLabel() A label for an argument is of the form a:b, 'a':b, "a":b, (a):b, etc.. |
void |
argumentLabelStart() For lookahead only. |
void |
assignmentExpression(int lc_stmt) |
void |
assignmentLessExpression() |
void |
balancedBrackets() Fast lookahead across balanced brackets of all sorts. |
void |
balancedTokens() |
void |
blockBody(int prevToken) A block body is a parade of zero or more statements or expressions. |
void |
branchStatement() In Groovy, return, break, continue, throw, and assert can be used in a parenthesized expression context. |
protected void |
buildTokenTypeASTClassMap() |
void |
builtInType() |
void |
builtInTypeArraySpec(boolean addImagNode) |
void |
builtInTypeSpec(boolean addImagNode) |
void |
caseSList() |
void |
casesGroup() |
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. |
void |
classBlock() |
void |
classDefinition(AST modifiers) |
void |
classField() |
void |
classOrInterfaceType(boolean addImagNode) |
void |
classTypeSpec(boolean addImagNode) |
Token |
cloneToken(Token t) Clones the token |
void |
closableBlock() A block which is known to be a closure, even if it has no apparent arguments. |
void |
closableBlockConstructorExpression() |
void |
closableBlockParam() Simple names, as in {x|...}, are completely equivalent to {(def x)|...}. |
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. |
void |
closableBlockParamsStart() Lookahead to check whether a block begins with explicit closure arguments. |
void |
closureList() |
void |
commandArgument() |
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. |
void |
commandArgumentsGreedy(AST head) |
void |
compatibleBodyStatement() In Java, "if", "while", and "for" statements can take random, non-braced statements as their bodies. |
void |
compilationUnit() |
void |
compoundStatement() |
void |
conditionalExpression(int lc_stmt) |
void |
constant() Numeric, string, regexp, boolean, or null constant. |
void |
constantNumber() Numeric constant. |
void |
constructorBody() |
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? |
void |
constructorStart() Used to look ahead for a constructor |
void |
controlExpressionList() |
AST |
create(int type, String txt, AST first) Create an AST node with the token type and text passed in, but with the same background information as another supplied Token (e.g. |
AST |
create(int type, String txt, Token first, Token last) |
AST |
create(int type, String txt, AST first, Token last) |
AST |
create(int type, String txt, AST first, AST last) |
void |
declaration() A declaration is the creation of a reference or primitive-type variable, or (if arguments are present) of a method. |
void |
declarationStart() Used only as a lookahead predicate, before diving in and parsing a declaration. |
void |
declaratorBrackets(AST typ) After some type names, where zero or more empty bracket pairs are allowed. |
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. |
void |
enumBlock() |
void |
enumConstant() |
void |
enumConstantBlock() |
void |
enumConstantField() |
protected void |
enumConstantFieldInternal(AST mods, AST tp, AST t, Token first) |
void |
enumConstants() Comma-separated list of one or more enum constant definitions. |
void |
enumConstantsStart() Guard for enumConstants. |
void |
enumDefinition(AST modifiers) |
void |
equalityExpression(int lc_stmt) |
void |
exclusiveOrExpression(int lc_stmt) |
void |
explicitConstructorInvocation() Catch obvious constructor calls, but not the expr.super(...) calls |
void |
expression(int lc_stmt) |
void |
expressionStatement(int prevToken) An expression statement can be any general expression. |
void |
expressionStatementNoCheck() |
void |
finallyClause() |
void |
forCond() |
void |
forInClause() |
void |
forInit() |
void |
forIter() |
void |
forStatement() |
void |
genericMethod() |
void |
genericMethodStart() lookahead predicate for usage of generics in methods as parameter for the method. |
org.codehaus.groovy.antlr.parser.GroovyLexer |
getLexer() |
List |
getWarningList() |
void |
handler() |
void |
identifier() |
void |
identifierStar() |
void |
implementsClause() |
void |
implicitParameters() A block known to be a closure, but which omits its arguments, is given this placeholder. |
void |
importStatement() |
void |
inclusiveOrExpression(int lc_stmt) |
void |
indexPropertyArgs(AST indexee) An expression may be followed by [...]. |
void |
interfaceBlock() |
void |
interfaceDefinition(AST modifiers) |
void |
interfaceExtends() |
void |
interfaceField() |
void |
keywordPropertyNames() |
void |
listOfVariables(AST mods, AST t, Token first) |
void |
listOrMapConstructorExpression() A list constructor is a argument list enclosed in square brackets, without labels. |
void |
logicalAndExpression(int lc_stmt) |
void |
logicalOrExpression(int lc_stmt) |
static GroovyRecognizer |
make(org.codehaus.groovy.antlr.parser.GroovyLexer lexer) This factory is the correct way to wire together a Groovy parser and lexer. |
static GroovyRecognizer |
make(InputStream in) |
static GroovyRecognizer |
make(Reader in) |
static GroovyRecognizer |
make(InputBuffer in) |
static GroovyRecognizer |
make(LexerSharedInputState in) |
void |
matchGenericTypeBracketsFailed(String problem, String solution) |
void |
methodCallArgs(AST callee) An expression may be followed by one or both of (...) and {...}. |
void |
modifier() |
void |
modifiers() A list of one or more modifier, annotation, or "def". |
void |
modifiersInternal() |
void |
modifiersOpt() A list of zero or more modifiers, annotations, or "def". |
void |
multicatch() |
void |
multicatch_types() |
void |
multipleAssignment(int lc_stmt) |
void |
multipleAssignmentDeclaration() |
void |
multipleAssignmentDeclarationStart() |
void |
multiplicativeExpression(int lc_stmt) |
void |
namePart() This is the grammar for what can follow a dot: x.a, x. |
void |
newArrayDeclarator() |
void |
newExpression() object instantiation. |
void |
nls() Zero or more insignificant newlines, all gobbled up and thrown away. |
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. |
void |
openBlock() An open block is not allowed to have closure arguments. |
void |
openOrClosableBlock() A sub-block of a block can be either open or closable. |
void |
packageDefinition() |
void |
parameterDeclaration() A formal parameter for a method or closure. |
void |
parameterDeclarationList() A list of zero or more formal parameters. |
void |
parameterModifiersOpt() |
void |
parenthesizedExpression() |
void |
pathChain(int lc_stmt, AST prefix) |
void |
pathElement(AST prefix) |
void |
pathElementStart() |
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. |
void |
postfixExpression(int lc_stmt) |
void |
powerExpression(int lc_stmt) |
void |
powerExpressionNotPlusMinus(int lc_stmt) |
void |
primaryExpression() |
void |
qualifiedTypeName() |
void |
regexExpression(int lc_stmt) |
void |
relationalExpression(int lc_stmt) |
void |
requireFailed(String problem, String solution) |
void |
sep() A statement separator is either a semicolon or a significant newline. |
void |
setFilename(String f) |
void |
setSourceBuffer(SourceBuffer sourceBuffer) |
void |
shiftExpression(int lc_stmt) |
void |
singleDeclaration() A declaration with one declarator and optional initialization, like a parameterDeclaration. |
void |
singleDeclarationNoInit() A declaration with one declarator and no initialization, like a parameterDeclaration. |
void |
singleVariable(AST mods, AST t) Used in cases where a declaration cannot have commas, or ends with the "in" operator instead of '='. |
void |
snippetUnit() A Groovy script or simple expression. |
void |
statement(int prevToken) A statement is an element of a block. |
void |
statementLabelPrefix() A labeled statement, consisting of a vanilla identifier followed by a colon. |
boolean |
strictContextExpression(boolean allowDeclaration) Things that can show up as expressions, but only in strict contexts like inside parentheses, argument lists, and list constructors. |
void |
stringConstructorExpression() |
void |
stringConstructorValuePart() |
void |
superClassClause() |
void |
suspiciousExpressionStatementStart() Lookahead for suspicious statement warnings and errors. |
void |
throwsClause() |
void |
traceIn(String rname) |
void |
traceOut(String rname) |
void |
traitDefinition(AST modifiers) |
void |
tryBlock() |
void |
type() |
void |
typeArgument() |
void |
typeArgumentBounds() |
void |
typeArgumentSpec() |
void |
typeArguments() |
void |
typeArgumentsDiamond() |
protected void |
typeArgumentsOrParametersEnd() |
void |
typeDeclarationStart() Used only as a lookahead predicate for nested type declarations. |
protected void |
typeDefinitionInternal(AST mods) |
void |
typeNamePairs(AST mods, Token first) |
void |
typeParameter() |
void |
typeParameterBounds() |
void |
typeParameters() |
void |
typeSpec(boolean addImagNode) |
void |
unaryExpression(int lc_stmt) |
void |
unaryExpressionNotPlusMinus(int lc_stmt) |
void |
upperCaseIdent() An IDENT token whose spelling is required to start with an uppercase letter. |
void |
varInitializer() An assignment operator '=' followed by an expression. |
void |
variableDeclarator(AST mods, AST t, Token first) Declaration of a variable. |
void |
variableDefinitions(AST mods, AST t) The tail of a declaration. |
void |
variableName() |
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
Create 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) todo - change antlr.ASTFactory to do this instead...
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'.)
*TODO* The declarationStart production needs to be strengthened to recognize
things like {List
After some type names, where zero or more empty bracket pairs are allowed. We use ARRAY_DECLARATOR to represent this. TODO: Is there some more Groovy way to view this in terms of the indexed property syntax?
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 declarations.
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-2014 The Codehaus. All rights reserved.