Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a slice to accumulate errors #20

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be more deterministic as this case is checked first (specs)

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})
}