Skip to content

Commit

Permalink
Refactor Dictionary Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 10, 2024
1 parent d2130e7 commit f2fb0b5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 94 deletions.
75 changes: 30 additions & 45 deletions backend/pkg/textdic/parser.go

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

49 changes: 10 additions & 39 deletions backend/pkg/textdic/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package textdic

import (
"fmt"
"sync"
)

// Define Node and Nodes types
Expand All @@ -14,21 +13,10 @@ type Node struct {

type Nodes []Node

// Modify yyParserImpl to hold a reference to the current Parser
type yyParserImpl struct {
lval yySymType
stack [yyInitialStackSize]yySymType
char int
parser *Parser // Add parser field
}

// Modify yyNewParser to accept a Parser instance
func yyNewParser(parser *Parser) yyParser {
return &yyParserImpl{parser: parser}
}

func yyParse(yylex yyLexer, parser *Parser) int {
return yyNewParser(parser).Parse(yylex)
func ParseAndGetNodes(yylex yyLexer) []Node {
yyparser := &yyParserImpl{}
yyparser.Parse(yylex)
return yyparser.getNodes()
}

%}
Expand All @@ -49,7 +37,7 @@ func yyParse(yylex yyLexer, parser *Parser) int {

%%
start
: entries EOF { $$ = $1; yyrcvr.parser.setNodes($1); }
: entries EOF { $$ = $1; yyrcvr.setNodes($1); }
;

entries
Expand All @@ -68,27 +56,10 @@ func yyError(s string) {
fmt.Println("Error:", s)
}

// Parser struct to encapsulate parsedNodes with a mutex for thread safety
type Parser struct {
mu sync.Mutex
parsedNodes Nodes
func (yyrcvr *yyParserImpl) setNodes(nodes []Node) {
yyrcvr.lval.nodes = nodes
}

// NewParser initializes and returns a new Parser instance
func NewParser() *Parser {
return &Parser{}
}

// getNodes returns the parsed nodes with a mutex lock
func (p *Parser) getNodes() Nodes {
p.mu.Lock()
defer p.mu.Unlock()
return p.parsedNodes
}

// setNodes sets the parsed nodes with a mutex lock
func (p *Parser) setNodes(nodes Nodes) {
p.mu.Lock()
defer p.mu.Unlock()
p.parsedNodes = nodes
}
func (yyrcvr *yyParserImpl) getNodes() []Node {
return yyrcvr.lval.nodes
}
6 changes: 1 addition & 5 deletions backend/pkg/textdic/parser_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ func NewParserService() *parserService {

// ProcessDictionary processes a given dictionary string and returns the parsed Nodes or an error
func (ps *parserService) ProcessDictionary(dic string) ([]Node, error) {
// Create a new Parser instance for each request
parser := NewParser()

// Use the new parser to parse the input
l := newLexer(dic)

// Parse the input using the new parser
yyParse(l, parser)
parsedNodes := ParseAndGetNodes(l)

// Retrieve the parsed nodes
parsedNodes := parser.getNodes()
if len(parsedNodes) == 0 {
return nil, fmt.Errorf("no nodes were parsed")
}
Expand Down
6 changes: 1 addition & 5 deletions backend/pkg/textdic/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ There is no leeway to provide services free of charge for the sake of others.
}

for _, tc := range testCases {
// Create a new parser
parser := NewParser()

// Create a new lexer with the input
l := newLexer(tc.input)

// Parse the input using the parser instance
yyParse(l, parser)
parsedNodes := ParseAndGetNodes(l)

// Compare the result with the expected output
parsedNodes := parser.getNodes()
if len(parsedNodes) != len(tc.expected) {
t.Errorf("expected %d nodes, but got %d", len(tc.expected), len(parsedNodes))
}
Expand Down

0 comments on commit f2fb0b5

Please sign in to comment.