Skip to content

Commit

Permalink
Merge pull request #488 from groovy/issue474
Browse files Browse the repository at this point in the history
Fix #474
  • Loading branch information
eric-milles authored Feb 19, 2018
2 parents f50d975 + e469682 commit bcf8dba
Show file tree
Hide file tree
Showing 14 changed files with 28,225 additions and 28,293 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -386,22 +386,11 @@ protected void packageDef(AST packageDef) {
processAnnotations(annotations, node);
node = node.getNextSibling();
}
// GRECLIPSE add
// cope with a parsed 'null' package (GRE439)
if (node == null) return;
// GRECLIPSE end
String name = qualifiedName(node);
// TODO should we check package node doesn't already exist? conflict?
PackageNode packageNode = setPackage(name, annotations);
// GRECLIPSE edit
//PackageNode packageNode = setPackage(name, annotations);
//configureAST(packageNode, packageDef);
setPackageName(name);
if (name != null && !name.isEmpty()) {
name += '.';
}
PackageNode packageNode = new PackageNode(name);
packageNode.addAnnotations(annotations);
output.setPackage(packageNode);
configureAST(packageNode, node);
// GRECLIPSE end
}
Expand Down Expand Up @@ -431,19 +420,6 @@ protected void importDef(AST importNode) {
alias = identifier(aliasNode);
}

// GRECLIPSE add
// node == null means it is a broken entry and the parser recovered. The error will be already
// be recorded against the file
if (node == null) {
if (isStatic) {
addStaticImport(ClassHelper.OBJECT_TYPE, "", null, annotations);
} else {
addImport(ClassHelper.OBJECT_TYPE, "", null, annotations);
}
return;
}
// GRECLIPSE end

if (node.getNumberOfChildren() == 0) {
String name = identifier(node);
// import is like "import Foo"
Expand Down Expand Up @@ -3454,7 +3430,7 @@ protected GenericsType[] makeGenericsType(AST rootNode) {
ret.add(gt);
typeParameter = typeParameter.getNextSibling();
}
return (GenericsType[]) ret.toArray(new GenericsType[0]);
return (GenericsType[]) ret.toArray(new GenericsType[ret.size()]);
}

protected ClassNode makeType(AST typeNode) {
Expand Down Expand Up @@ -3700,7 +3676,7 @@ protected void dump(AST node) {
System.out.println("Type: " + getTokenName(node) + " text: " + node.getText());
}

// GRECLIPSE add
// GRECLIPSE add
private void fixModuleNodeLocations() {
// only occurs if in a script

Expand Down Expand Up @@ -3827,5 +3803,5 @@ protected void configureAnnotationAST(AnnotationNode node, AST ast) {

node.setEnd(node.getEnd() - 1); // Eclipse wants this for error reporting
}
// GRECLIPSE end
// GRECLIPSE end
}
77 changes: 28 additions & 49 deletions base/org.codehaus.groovy24/src/org/codehaus/groovy/antlr/groovy.g
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ tokens {
ident.setText("?");
ident.setLine(line);
ident.setColumn(column);
ident.setLineLast(line);
ident.setColumnLast(column + 1);
return #(create(ident.getType(), ident.getText(), ident, next));
}

Expand Down Expand Up @@ -453,37 +455,33 @@ tokens {
Token lt = null;
try { lt = LT(1); }
catch (TokenStreamException e) { }
if (lt == null) lt = Token.badToken;
if (lt == null) lt = Token.badToken;
reportError(message, lt.getLine(), lt.getColumn());
}
Map row = new HashMap();
row.put("error", message);
row.put("filename", getFilename());
row.put("line", Integer.valueOf(lt.getLine()));
row.put("column", Integer.valueOf(lt.getColumn()));
errorList.add(row);
/**
* Report a recovered error and specify the node.
*/
public void reportError(String message, AST ln) {
reportError(message, ln.getLine(), ln.getColumn());
}
/**
* Report a recovered error and specify the token.
*/
public void reportError(String message, Token lt) {
Map row = new HashMap();
row.put("error", message);
row.put("filename", getFilename());
row.put("line", Integer.valueOf(lt.getLine()));
row.put("column", Integer.valueOf(lt.getColumn()));
errorList.add(row);
reportError(message, lt.getLine(), lt.getColumn());
}
/**
* Report a recovered error and specify the node.
* Report a recovered error and specify the line and column.
*/
public void reportError(String message, AST ln) {
public void reportError(String message, int line, int column) {
Map row = new HashMap();
row.put("error", message);
row.put("filename", getFilename());
row.put("line", Integer.valueOf(ln.getLine()));
row.put("column", Integer.valueOf(ln.getColumn()));
row.put("line", Integer.valueOf(line));
row.put("column", Integer.valueOf(column));
errorList.add(row);
}
Expand Down Expand Up @@ -647,60 +645,42 @@ snippetUnit
: nls! blockBody[EOF]
;
// Package statement: optional annotations followed by "package" then the package identifier.
// Package statement: optional annotations followed by "package" then the package identifier
packageDefinition
{Token first = LT(1);}
// GRECLIPSE edit
// GRECLIPSE edit -- recovery for missing identifier
//: an:annotationsOpt! "package"! id:identifier!
// {#packageDefinition = #(create(PACKAGE_DEF,"package",first,LT(1)),an,id);}
: an:annotationsOpt! "package"! (id:identifier!)?
{ // error recovery for missing package name
{
if (#id == null) {
reportError("Invalid package specification", LT(0));
} else {
#packageDefinition = #(create(PACKAGE_DEF,"package",first,LT(1)),an,id);
#id = missingIdentifier(LT(0), null);
reportError("Invalid package specification", LT(0).getLine(), LT(0).getColumn()-1);
}
#packageDefinition = #(create(PACKAGE_DEF,"package",first,LT(1)),an,id);
}
// GRECLIPSE end
;
// Import statement: import followed by a package or class name
importStatement
//TODO? options {defaultErrorHandler = true;}
{ Token first = LT(1); boolean isStatic = false; }
// GRECLIPSE edit
{Token first = LT(1); boolean isStatic = false;}
// GRECLIPSE edit -- recovery for missing identifier
//: an:annotationsOpt "import"! ( "static"! {isStatic=true;} )? is:identifierStar!
// {if (isStatic)
// #importStatement = #(create(STATIC_IMPORT,"static_import",first,LT(1)),an,is);
// else
// #importStatement = #(create(IMPORT,"import",first,LT(1)),an,is);}
/* RECOVERY: NOTES:
* The aim here is just to allow for the type to be optional. If not specified
* it is clearly an error but we want to recover and continue parsing the file.
* Here if the type is missing (is_AST==null) then we report an error and create
* a fake import statement with a null type reference. There is a corresponding
* change in AntlrParserPlugin that deals with a null type reference and constructs
* a suitable 'fake' ImportNode.
*/
: an:annotationsOpt "import"! ( "static"! {isStatic=true;} )? (is:identifierStar!)?
: an:annotationsOpt "import"! ("static"! {isStatic=true;})? (is:identifierStar!)?
{
if (isStatic) {
if (#is == null) {
reportError("Invalid import static specification", first);
#importStatement = #(create(STATIC_IMPORT,"static_import",first,null),an,is);
} else {
#importStatement = #(create(STATIC_IMPORT,"static_import",first,LT(1)),an,is);
#is = missingIdentifier(LT(0), null);
}
} else {
if (#is == null) {
reportError("Invalid import specification", LT(0));
#importStatement = #(create(IMPORT,"import",first,null),an,is);
if (!isStatic) {
#importStatement = #(create(IMPORT,"import",first,LT(1)),an,is);
} else {
#importStatement = #(create(IMPORT,"import",first,LT(1)),an,is);
#importStatement = #(create(STATIC_IMPORT,"static_import",first,LT(1)),an,is);
}
}
}
// GRECLIPSE end
;
Expand Down Expand Up @@ -1987,7 +1967,6 @@ multicatch
// GRECLIPSE add
exception
catch [RecognitionException e] {
// finish invalid member-value pair if the closing parenthesis is next
if (#m != null && LT(1).getType() == RPAREN) {
reportError(e);
#id = missingIdentifier(first, LT(1));
Expand Down
Loading

0 comments on commit bcf8dba

Please sign in to comment.