Skip to content

Commit

Permalink
Add GetError in dictionary parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 10, 2024
1 parent 262f726 commit 68734c1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
68 changes: 44 additions & 24 deletions backend/pkg/textdic/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 31 additions & 11 deletions backend/pkg/textdic/parser.y
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
%{
package textdic

import (
"fmt"
)

// Define Node and Nodes types
type Node struct {
Word string
Expand All @@ -13,10 +9,10 @@ type Node struct {

type Nodes []Node

func ParseAndGetNodes(yylex yyLexer) []Node {
yyparser := &yyParserImpl{}
yyparser.Parse(yylex)
return yyparser.getNodes()
type Error struct {
state int
token int
msg string
}

%}
Expand All @@ -25,6 +21,7 @@ func ParseAndGetNodes(yylex yyLexer) []Node {
str string
node Node
nodes Nodes
errors []Error
}

%token<str> WORD DEFINITION NEWLINE EOF
Expand Down Expand Up @@ -52,14 +49,37 @@ entry

%%

func yyError(s string) {
fmt.Println("Error:", s)
type Parser interface {
Parse(yyLexer) int
GetNodes() []Node
GetErrors() []Error
}

func NewParser(yylex yyLexer) Parser {
yyparser := &yyParserImpl{}
yyparser.Parse(yylex)
return yyparser
}

func (yyrcvr *yyParserImpl) setNodes(nodes []Node) {
yyrcvr.lval.nodes = nodes
}

func (yyrcvr *yyParserImpl) getNodes() []Node {
func (yyrcvr *yyParserImpl) GetNodes() []Node {
return yyrcvr.lval.nodes
}

func (yyrcvr *yyParserImpl) GetErrors() []Error {
// Slice of Error structs
var errors []Error

// Loop over yyErrorMessages and add to errors slice
for _, errMsg := range yyErrorMessages {
errors = append(errors, Error{
state: errMsg.state,
token: errMsg.token,
msg: errMsg.msg,
})
}
return errors
}
3 changes: 2 additions & 1 deletion backend/pkg/textdic/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ There is no leeway to provide services free of charge for the sake of others.
l := newLexer(tc.input)

// Parse the input using the parser instance
parsedNodes := ParseAndGetNodes(l)
parser := NewParser(l)
parsedNodes := parser.GetNodes()

if len(parsedNodes) != len(tc.expected) {
t.Errorf("expected %d nodes, but got %d", len(tc.expected), len(parsedNodes))
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/textdic/text_dictionary_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func (tds *textDictionaryService) Process(ctx context.Context, dic string) ([]No
l := newLexer(dic)

// Parse the input using the new parser
parsedNodes := ParseAndGetNodes(l)
parser := NewParser(l)
parsedNodes := parser.GetNodes()

if len(parsedNodes) == 0 {
err := fmt.Errorf("no nodes were parsed")
Expand Down

0 comments on commit 68734c1

Please sign in to comment.