Skip to content

Commit

Permalink
Refactor to clean up some duplication (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney authored Jan 22, 2024
1 parent 205736c commit 497f506
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 59 deletions.
87 changes: 39 additions & 48 deletions amod/amod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package amod
import (
"errors"
"fmt"
"io"
"os"
"reflect"
"slices"
"strings"
Expand Down Expand Up @@ -53,61 +55,18 @@ func OutputEBNF() {
func GenerateModel(buffer string) (model *actr.Model, iLog *issues.Log, err error) {
r := strings.NewReader(buffer)

log := newLog()
iLog = &log.Log

amod, err := parseAMOD(r)
if err != nil {
pErr, ok := err.(participle.Error)
if ok {
location := issues.Location{
Line: pErr.Position().Line,
ColumnStart: pErr.Position().Column,
ColumnEnd: pErr.Position().Column,
}
log.Error(&location, pErr.Message())
} else {
log.Error(&issues.Location{}, err.Error())
}

err = ErrParse
return
}

model, err = generateModel(amod, log)
if err != nil {
return
}

model.FinalizeImplicitChunks()
return
return modelReader(r)
}

// GenerateModelFromFile generates a model from the file 'fileName'.
func GenerateModelFromFile(fileName string) (model *actr.Model, iLog *issues.Log, err error) {
log := newLog()
iLog = &log.Log

amod, err := parseAMODFile(fileName)
file, err := os.Open(fileName)
if err != nil {
pErr, ok := err.(participle.Error)
if ok {
location := issues.Location{
Line: pErr.Position().Line,
ColumnStart: pErr.Position().Column,
ColumnEnd: pErr.Position().Column,
}
log.Error(&location, pErr.Message())
} else {
log.Error(&issues.Location{}, err.Error())
}

err = ErrParse
return
return nil, nil, err
}
defer file.Close()

model, err = generateModel(amod, log)
return
return modelReader(file)
}

// ParseChunk is used to parse goals when given as input from a user.
Expand Down Expand Up @@ -151,6 +110,38 @@ func ParseChunk(model *actr.Model, chunk string) (*actr.Pattern, error) {
return createChunkPattern(model, log, p)
}

// modelReader reads the model from a reader and generates the actr.Model
func modelReader(r io.Reader) (model *actr.Model, iLog *issues.Log, err error) {
log := newLog()
iLog = &log.Log

amod, err := parseAMOD(r)
if err != nil {
pErr, ok := err.(participle.Error)
if ok {
location := issues.Location{
Line: pErr.Position().Line,
ColumnStart: pErr.Position().Column,
ColumnEnd: pErr.Position().Column,
}
log.Error(&location, pErr.Message())
} else {
log.Error(&issues.Location{}, err.Error())
}

err = ErrParse
return
}

model, err = generateModel(amod, log)
if err != nil {
return
}

model.FinalizeImplicitChunks()
return
}

// generateModel runs through the parsed structures and creates an actr.Model from them
func generateModel(amod *amodFile, log *issueLog) (model *actr.Model, err error) {
model = &actr.Model{
Expand Down
11 changes: 0 additions & 11 deletions amod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package amod
import (
"fmt"
"io"
"os"

"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
Expand Down Expand Up @@ -451,13 +450,3 @@ func parseAMOD(r io.Reader) (amod *amodFile, err error) {

return
}

func parseAMODFile(filename string) (*amodFile, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

return parseAMOD(file)
}

0 comments on commit 497f506

Please sign in to comment.