Skip to content

Commit

Permalink
Fix parser could not handle dictionaly with EOF right after
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 14, 2024
1 parent 4d055df commit f8724f3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
5 changes: 3 additions & 2 deletions backend/pkg/textdic/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package textdic

import (
"fmt"
"io"
"regexp"
"strings"
"unicode"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (l *lexer) isJapanese(r rune) bool {

func (l *lexer) Lex(lval *yySymType) int {
r, err := l.skipWhiteSpace()
if err != nil {
if err == io.EOF {
// Done with parsing
return 0
}
Expand All @@ -58,7 +59,7 @@ func (l *lexer) Lex(lval *yySymType) int {
if l.isJapanese(r) {
return l.lexDefinition(lval)
}
return EOF
return 0
}

func (l *lexer) lexWord(lval *yySymType) int {
Expand Down
20 changes: 9 additions & 11 deletions backend/pkg/textdic/parser.go

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

4 changes: 2 additions & 2 deletions backend/pkg/textdic/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Nodes []Node
nodes Nodes
}

%token<str> WORD DEFINITION NEWLINE EOF
%token<str> WORD DEFINITION NEWLINE
%type<node> entry
%type<nodes> entries
%type<nodes> start
Expand All @@ -36,7 +36,7 @@ entries
;

entry
: WORD DEFINITION NEWLINE { $$ = Node{Word: $1, Definition: $2} }
: WORD DEFINITION { $$ = Node{Word: $1, Definition: $2} }
| NEWLINE { $$ = Node{} } // Ignore empty line
;

Expand Down
10 changes: 10 additions & 0 deletions backend/pkg/textdic/text_dictionary_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ leeway 〔自分の好きなように行動・思考できる〕自由(裁量
There is no leeway to provide services free of charge for the sake of others. 他人のために無償でサービスをする余裕はない。
`
var inputEOF = `leeway 〔自分の好きなように行動・思考できる〕自由(裁量)度◆不可〔時間・金などの〕余裕、ゆとり
There is no leeway to provide services free of charge for the sake of others. 他人のために無償でサービスをする余裕はない。`

// Define the test cases
var testCases = []struct {
Expand All @@ -51,6 +53,14 @@ There is no leeway to provide services free of charge for the sake of others.
{Word: "There is no leeway to provide services free of charge for the sake of others.", Definition: "他人のために無償でサービスをする余裕はない。"},
},
},
{
name: "Valid input EOF",
input: inputEOF,
expected: []Node{
{Word: "leeway", Definition: "〔自分の好きなように行動・思考できる〕自由(裁量)度◆不可〔時間・金などの〕余裕、ゆとり"},
{Word: "There is no leeway to provide services free of charge for the sake of others.", Definition: "他人のために無償でサービスをする余裕はない。"},
},
},
}

// Run TestParserService
Expand Down

0 comments on commit f8724f3

Please sign in to comment.