Skip to content

Commit

Permalink
Merge pull request #20 from nickng/lexer-error-slice
Browse files Browse the repository at this point in the history
Use a slice to accumulate errors
  • Loading branch information
nickng authored Aug 18, 2022
2 parents c4b3d54 + b77ccd8 commit 443fee2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions bibtex.y
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ tags : tag { if $1 != nil { $$ = []*bibTag{$1}; } }
func Parse(r io.Reader) (*BibTex, error) {
l := newLexer(r)
bibtexParse(l)
select {
case err := <-l.Errors: // Non-yacc errors
return nil, err
case err := <-l.ParseErrors:
return nil, err
switch {
case len(l.Errors) > 0: // Non-yacc errors
return nil, l.Errors[0]
case len(l.ParseErrors) > 0:
return nil, l.ParseErrors[0]
default:
return bib, nil
}
Expand Down
10 changes: 5 additions & 5 deletions bibtex.y.go

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

12 changes: 5 additions & 7 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ import (
// lexer for bibtex.
type lexer struct {
scanner *scanner
ParseErrors chan error // Parse errors from yacc
Errors chan error // Other errors
ParseErrors []error // Parse errors from yacc
Errors []error // Other errors
}

// newLexer returns a new yacc-compatible lexer.
func newLexer(r io.Reader) *lexer {
return &lexer{
scanner: newScanner(r),
ParseErrors: make(chan error, 1),
Errors: make(chan error, 1),
scanner: newScanner(r),
}
}

// Lex is provided for yacc-compatible parser.
func (l *lexer) Lex(yylval *bibtexSymType) int {
token, strval, err := l.scanner.Scan()
if err != nil {
l.Errors <- fmt.Errorf("%w at %s", err, l.scanner.pos)
l.Errors = append(l.Errors, fmt.Errorf("%w at %s", err, l.scanner.pos))
return int(0)
}
yylval.strval = strval
Expand All @@ -36,5 +34,5 @@ func (l *lexer) Lex(yylval *bibtexSymType) int {

// Error handles error.
func (l *lexer) Error(err string) {
l.ParseErrors <- &ErrParse{Err: err, Pos: l.scanner.pos}
l.ParseErrors = append(l.ParseErrors, &ErrParse{Err: err, Pos: l.scanner.pos})
}

0 comments on commit 443fee2

Please sign in to comment.