From 8a5b585ba57f712dd7ad2372da3355e3ad9628c4 Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Wed, 29 Nov 2023 19:30:12 +0800 Subject: [PATCH] fix: make support for Java.ParseEndOfFileError work --- .../java/parse_end_of_file_error.go | 59 ++++++++++++++++++- .../parse_end_of_file_error/test.txt | 29 +++++---- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/error_templates/java/parse_end_of_file_error.go b/error_templates/java/parse_end_of_file_error.go index 900d033..94de308 100644 --- a/error_templates/java/parse_end_of_file_error.go +++ b/error_templates/java/parse_end_of_file_error.go @@ -2,10 +2,12 @@ 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{ @@ -13,12 +15,63 @@ var ParseEndOfFileError = lib.ErrorTemplate{ 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 + } + } +} diff --git a/error_templates/java/test_files/parse_end_of_file_error/test.txt b/error_templates/java/test_files/parse_end_of_file_error/test.txt index e178c85..f88fc8d 100644 --- a/error_templates/java/test_files/parse_end_of_file_error/test.txt +++ b/error_templates/java/test_files/parse_end_of_file_error/test.txt @@ -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. +```