Skip to content

Commit

Permalink
todo
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Jan 10, 2024
1 parent a0e2f5a commit a23d144
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 62 deletions.
4 changes: 0 additions & 4 deletions errgoengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
contextData := NewContextData(e.SharedStore, workingPath)
contextData.Analyzer = template.Language.AnalyzerFactory(contextData)

if template.Language.stubFs != nil {
e.FS.AttachOrReplace(template.Language.stubFs, 1)
}

groupNames := template.Pattern.SubexpNames()
for _, submatches := range template.Pattern.FindAllStringSubmatch(msg, -1) {
for idx, matchedContent := range submatches {
Expand Down
48 changes: 48 additions & 0 deletions extern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package errgoengine

import (
"fmt"
"io/fs"
)

type ExternSymbol struct {
Name string `json:"name"`
Type string `json:"type"`
ReturnType string `json:"returnType"`
Paremeters []ExternSymbol `json:"parameters"`
}

type ExternFile struct {
Name string `json:"name"`
Package string `json:"package"`
Constructors []ExternSymbol `json:"constructors"`
Methods []ExternSymbol `json:"methods"`
}

func ImportExternSymbols(externFs fs.ReadFileFS) (map[string]*SymbolTree, error) {
if externFs == nil {
return nil, nil
}

symbols := make(map[string]*SymbolTree)
matches, err := fs.Glob(externFs, "**/*.json")
if err != nil {
return nil, err
}

for _, match := range matches {
if err := compileExternSymbol(externFs, match); err != nil {
return symbols, err
}
}

return symbols, nil
}

func compileExternSymbol(externFs fs.FS, path string) error {
if externFs == nil {
return fmt.Errorf("externFs must not be nil")
}

return nil
}
34 changes: 17 additions & 17 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,45 @@ func (mfs *MultiReadFileFS) Open(name string) (fs.File, error) {
return nil, os.ErrNotExist
}

type stubFileInfo struct {
type virtualFileInfo struct {
name string
}

func (*stubFileInfo) Name() string { return "" }
func (f *virtualFileInfo) Name() string { return f.name }

func (*stubFileInfo) Size() int64 { return 0 }
func (*virtualFileInfo) Size() int64 { return 0 }

func (*stubFileInfo) Mode() fs.FileMode { return 0400 }
func (*virtualFileInfo) Mode() fs.FileMode { return 0400 }

func (*stubFileInfo) ModTime() time.Time { return time.Now() }
func (*virtualFileInfo) ModTime() time.Time { return time.Now() }

func (*stubFileInfo) IsDir() bool { return false }
func (*virtualFileInfo) IsDir() bool { return false }

func (*stubFileInfo) Sys() any { return nil }
func (*virtualFileInfo) Sys() any { return nil }

type StubFile struct {
type VirtualFile struct {
Name string
}

func (StubFile) Read(bt []byte) (int, error) { return 0, io.EOF }
func (VirtualFile) Read(bt []byte) (int, error) { return 0, io.EOF }

func (vf StubFile) Stat() (fs.FileInfo, error) { return &stubFileInfo{vf.Name}, nil }
func (vf VirtualFile) Stat() (fs.FileInfo, error) { return &virtualFileInfo{vf.Name}, nil }

func (StubFile) Close() error { return nil }
func (VirtualFile) Close() error { return nil }

type StubFS struct {
Files []StubFile
type VirtualFS struct {
Files []VirtualFile
}

func (vfs *StubFS) StubFile(name string) StubFile {
file := StubFile{
func (vfs *VirtualFS) StubFile(name string) VirtualFile {
file := VirtualFile{
Name: name,
}
vfs.Files = append(vfs.Files, file)
return file
}

func (vfs *StubFS) Open(name string) (fs.File, error) {
func (vfs *VirtualFS) Open(name string) (fs.File, error) {
for _, file := range vfs.Files {
if file.Name == name {
return file, nil
Expand All @@ -111,7 +111,7 @@ func (vfs *StubFS) Open(name string) (fs.File, error) {
return nil, os.ErrNotExist
}

func (vfs *StubFS) ReadFile(name string) ([]byte, error) {
func (vfs *VirtualFS) ReadFile(name string) ([]byte, error) {
for _, file := range vfs.Files {
if file.Name == name {
return make([]byte, 0), nil
Expand Down
51 changes: 46 additions & 5 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package errgoengine

import (
"context"
"encoding/json"
"fmt"
"io/fs"
"regexp"
"strings"

Expand All @@ -24,7 +26,7 @@ type LanguageAnalyzer interface {
type Language struct {
isCompiled bool
stackTraceRegex *regexp.Regexp
stubFs *StubFS
externSymbols map[string]*SymbolTree
Name string
FilePatterns []string
SitterLanguage *sitter.Language
Expand All @@ -33,7 +35,7 @@ type Language struct {
SymbolsToCapture string
LocationConverter func(path, pos string) Location
AnalyzerFactory func(cd *ContextData) LanguageAnalyzer
OnGenStubFS func(fs *StubFS)
ExternFS fs.ReadFileFS
}

func (lang *Language) MatchPath(path string) bool {
Expand Down Expand Up @@ -62,14 +64,53 @@ func (lang *Language) Compile() {
panic(fmt.Sprintf("[Language -> %s] AnalyzerFactory must not be nil", lang.Name))
}

if lang.stubFs == nil && lang.OnGenStubFS != nil {
lang.stubFs = &StubFS{}
lang.OnGenStubFS(lang.stubFs)
if err := lang.compileExternSymbols(); err != nil {
panic(err)
}

lang.isCompiled = true
}

func (lang *Language) compileExternSymbols() error {
if lang.isCompiled || lang.ExternFS == nil {
return nil
}

lang.externSymbols = make(map[string]*SymbolTree)

matches, err := fs.Glob(lang.ExternFS, "**/*.json")
if err != nil {
return err
}

for _, match := range matches {
if err := lang.compileExternSymbol(match); err != nil {
return err
}
}
}

Check failure on line 91 in language.go

View workflow job for this annotation

GitHub Actions / build

missing return

func (lang *Language) compileExternSymbol(path string) error {
if lang.isCompiled || lang.ExternFS == nil {
return nil
}

file, err := lang.ExternFS.Open(path)
if err != nil {
return err
}

defer file.Close()

var symTree SymbolTree
if err := json.NewDecoder(file).Decode(&symTree); err != nil {
return err
}

lang.externSymbols[path] = &symTree
return nil
}

// SetTemplateStackTraceRegex sets the language's regex pattern directly. for testing purposes only
func SetTemplateStackTraceRegex(lang *Language, pattern *regexp.Regexp) {
lang.stackTraceRegex = pattern
Expand Down
38 changes: 38 additions & 0 deletions languages/java/externs/java.lang/Object.java.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "Object",
"package": "java.lang",
"description": "The root class of the Java class hierarchy.",
"constructors": [
{
"name": "Object",
"parameters": [],
"description": "Creates a new instance of the Object class."
}
],
"methods": [
{
"name": "equals",
"returnType": "boolean",
"parameters": [
{
"name": "obj",
"type": "Object",
"description": "The object to compare to."
}
],
"description": "Indicates whether some other object is \"equal to\" this one."
},
{
"name": "hashCode",
"returnType": "int",
"parameters": [],
"description": "Returns a hash code value for this object."
},
{
"name": "toString",
"returnType": "String",
"parameters": [],
"description": "Returns a string representation of this object."
}
]
}
97 changes: 97 additions & 0 deletions languages/java/externs/java.lang/String.java.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "String",
"package": "java.lang",
"description": "Represents a sequence of characters.",
"constructors": [
{
"name": "String",
"parameters": [
{
"name": "value",
"type": "char[]",
"description": "The character array that is the initial value of the string."
}
]
},
{
"name": "String",
"parameters": [
{
"name": "value",
"type": "String",
"description": "The string that is the initial value of the string."
}
]
}
],
"methods": [
{
"name": "length",
"returnType": "int",
"description": "Returns the length of the string."
},
{
"name": "charAt",
"returnType": "char",
"parameters": [
{
"name": "index",
"type": "int",
"description": "The index of the character to return."
}
],
"description": "Returns the character at the specified index."
},
{
"name": "substring",
"returnType": "String",
"parameters": [
{
"name": "start",
"type": "int",
"description": "The starting index of the substring."
},
{
"name": "end",
"type": "int",
"description": "The ending index of the substring."
}
],
"description": "Returns a substring of the string."
},
{
"name": "concat",
"returnType": "String",
"parameters": [
{
"name": "str",
"type": "String",
"description": "The string to concatenate."
}
],
"description": "Concatenates the specified string to the end of this string."
},
{
"name": "equals",
"returnType": "boolean",
"parameters": [
{
"name": "obj",
"type": "Object",
"description": "The object to compare to."
}
],
"description": "Compares this string to the specified object."
},
{
"name": "hashCode",
"returnType": "int",
"description": "Returns the hash code of this string."
},
{
"name": "toString",
"returnType": "String",
"description": "Returns a string representation of this string."
}
]
}
Loading

0 comments on commit a23d144

Please sign in to comment.