Skip to content

Commit 8a5b585

Browse files
committed
fix: make support for Java.ParseEndOfFileError work
1 parent fee825d commit 8a5b585

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

error_templates/java/parse_end_of_file_error.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,76 @@ package java
22

33
import (
44
lib "github.com/nedpals/errgoengine"
5+
sitter "github.com/smacker/go-tree-sitter"
56
)
67

78
type parseEofErrorCtx struct {
8-
missingSymStack []string
9+
missingSymStack []string
10+
missingCharacter string
911
}
1012

1113
var ParseEndOfFileError = lib.ErrorTemplate{
1214
Name: "ParseEndOfFileError",
1315
Pattern: comptimeErrorPattern("reached end of file while parsing"),
1416
StackTracePattern: comptimeStackTracePattern,
1517
OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
18+
ctx := parseEofErrorCtx{}
1619

20+
// traverse the tree and get the nearest "missing" node
21+
rootNode := m.Document.Tree.RootNode()
22+
cursor := sitter.NewTreeCursor(rootNode)
23+
rawNearestMissingNode := nearestMissingNodeFromPos(cursor, m.ErrorNode.StartPos)
24+
nearestMissingNode := lib.WrapNode(m.Document, rawNearestMissingNode)
25+
m.Nearest = nearestMissingNode
26+
nearestStr := m.Nearest.String()
27+
prefix := "(MISSING \""
28+
ctx.missingCharacter = nearestStr[len(prefix) : len(prefix)+1]
29+
m.Context = ctx
1730
},
1831
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
19-
gen.Add("The compiler was not able to compile your program because one or more closing brackets were missing in the program.")
32+
gen.Add("This error occurs when the compiler expects more code but encounters the end of the file.")
2033
},
2134
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
22-
// TODO:
35+
ctx := cd.MainError.Context.(parseEofErrorCtx)
36+
37+
gen.Add("Complete the code", func(s *lib.BugFixSuggestion) {
38+
endPos := cd.MainError.Nearest.EndPosition()
39+
40+
s.AddStep("Add the missing `%s` in line %d", ctx.missingCharacter, endPos.Line+1).
41+
AddFix(lib.FixSuggestion{
42+
NewText: "\n" + ctx.missingCharacter,
43+
StartPosition: endPos,
44+
EndPosition: endPos,
45+
})
46+
})
2347
},
2448
}
49+
50+
func nearestMissingNodeFromPos(cursor *sitter.TreeCursor, pos lib.Position) *sitter.Node {
51+
defer cursor.GoToParent()
52+
53+
// hope it executes to avoid stack overflow
54+
if !cursor.GoToFirstChild() {
55+
return nil
56+
}
57+
58+
for {
59+
currentNode := cursor.CurrentNode()
60+
pointA := currentNode.StartPoint()
61+
pointB := currentNode.EndPoint()
62+
63+
if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
64+
if currentNode.IsMissing() {
65+
return currentNode
66+
} else if currentNode.ChildCount() != 0 {
67+
if gotNode := nearestMissingNodeFromPos(cursor, pos); gotNode != nil {
68+
return gotNode
69+
}
70+
}
71+
}
72+
73+
if !cursor.GoToNextSibling() {
74+
return nil
75+
}
76+
}
77+
}

error_templates/java/test_files/parse_end_of_file_error/test.txt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ EOF.java:4: error: reached end of file while parsing
88
template: "Java.ParseEndOfFileError"
99
---
1010
# ParseEndOfFileError
11-
This error indicates that the compiler reached the end of the file unexpectedly while it was expecting something more in the code.
12-
13-
## Explanation local to the file
14-
The error occurred due to an incomplete block of code in the `EOF.java` file. The compiler was expecting more code or the completion of a block but encountered the end of the file prematurely.
11+
This error occurs when the compiler expects more code but encounters the end of the file.
12+
```
13+
System.out.println("This is a sample program.");
14+
}
15+
^
1516

17+
```
1618
## Steps to fix
17-
1. Ensure that all opening braces `{` have their corresponding closing braces `}` to complete the code blocks.
18-
19+
### Complete the code
20+
Add the missing `}` in line 4
1921
```diff
20-
public class EOF {
21-
public static void main(String[] args) {
22-
System.out.println("This is a sample program.");
23-
+ }
24-
}
25-
```
22+
public static void main(String[] args) {
23+
System.out.println("This is a sample program.");
24+
- }
25+
+ }
26+
+ }
2627

27-
This fix completes the `main` method by adding the closing curly brace `}`.
28-
29-
2. If the error persists, check the entire file for missing or misplaced braces, ensuring they are correctly matched and closed. Double-check that each opening brace has a corresponding closing brace to resolve this issue.
28+
```

0 commit comments

Comments
 (0)