Skip to content

Commit

Permalink
Merge branch 'improve-1' of github.com:nedpals/errgoengine into impro…
Browse files Browse the repository at this point in the history
…ve-1
  • Loading branch information
nedpals committed Nov 29, 2023
2 parents 58ac53e + 658ae1f commit bce4fda
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
59 changes: 56 additions & 3 deletions error_templates/java/parse_end_of_file_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,76 @@ package java

import (
lib "github.com/nedpals/errgoengine"
sitter "github.com/smacker/go-tree-sitter"
)

type parseEofErrorCtx struct {
missingSymStack []string
missingSymStack []string
missingCharacter string
}

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

// traverse the tree and get the nearest "missing" node
rootNode := m.Document.Tree.RootNode()
cursor := sitter.NewTreeCursor(rootNode)
rawNearestMissingNode := nearestMissingNodeFromPos(cursor, m.ErrorNode.StartPos)
nearestMissingNode := lib.WrapNode(m.Document, rawNearestMissingNode)
m.Nearest = nearestMissingNode
nearestStr := m.Nearest.String()
prefix := "(MISSING \""
ctx.missingCharacter = nearestStr[len(prefix) : len(prefix)+1]
m.Context = ctx
},
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("The compiler was not able to compile your program because one or more closing brackets were missing in the program.")
gen.Add("This error occurs when the compiler expects more code but encounters the end of the file.")
},
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
// TODO:
ctx := cd.MainError.Context.(parseEofErrorCtx)

gen.Add("Complete the code", func(s *lib.BugFixSuggestion) {
endPos := cd.MainError.Nearest.EndPosition()

s.AddStep("Add the missing `%s` in line %d", ctx.missingCharacter, endPos.Line+1).
AddFix(lib.FixSuggestion{
NewText: "\n" + ctx.missingCharacter,
StartPosition: endPos,
EndPosition: endPos,
})
})
},
}

func nearestMissingNodeFromPos(cursor *sitter.TreeCursor, pos lib.Position) *sitter.Node {
defer cursor.GoToParent()

// hope it executes to avoid stack overflow
if !cursor.GoToFirstChild() {
return nil
}

for {
currentNode := cursor.CurrentNode()
pointA := currentNode.StartPoint()
pointB := currentNode.EndPoint()

if uint32(pos.Line) >= pointA.Row+1 && uint32(pos.Line) <= pointB.Row+1 {
if currentNode.IsMissing() {
return currentNode
} else if currentNode.ChildCount() != 0 {
if gotNode := nearestMissingNodeFromPos(cursor, pos); gotNode != nil {
return gotNode
}
}
}

if !cursor.GoToNextSibling() {
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ This error is raised when you try to perform arithmetic operations that are not
^
System.out.println(out);
}
}
```
## Steps to fix
### Avoid dividing by zero.
Expand Down
29 changes: 14 additions & 15 deletions error_templates/java/test_files/parse_end_of_file_error/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ EOF.java:4: error: reached end of file while parsing
template: "Java.ParseEndOfFileError"
---
# ParseEndOfFileError
This error indicates that the compiler reached the end of the file unexpectedly while it was expecting something more in the code.

## Explanation local to the file
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.
This error occurs when the compiler expects more code but encounters the end of the file.
```
System.out.println("This is a sample program.");
}
^

```
## Steps to fix
1. Ensure that all opening braces `{` have their corresponding closing braces `}` to complete the code blocks.

### Complete the code
Add the missing `}` in line 4
```diff
public class EOF {
public static void main(String[] args) {
System.out.println("This is a sample program.");
+ }
}
```
public static void main(String[] args) {
System.out.println("This is a sample program.");
- }
+ }
+ }

This fix completes the `main` method by adding the closing curly brace `}`.

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.
```

0 comments on commit bce4fda

Please sign in to comment.