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

Parse returns error instead of terminating with log.Fatalf #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions bibtex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bibtex
import (
"bytes"
"fmt"
"log"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -272,16 +271,24 @@ func (bib *BibTex) AddStringVar(key string, val BibString) {
}

// GetStringVar looks up a string by its key.
func (bib *BibTex) GetStringVar(key string) *BibVar {
func (bib *BibTex) GetStringVar(key string) (*BibVar, error) {
if bv, ok := bib.StringVar[key]; ok {
return bv
return bv, nil
}
if v, ok := bib.getDefaultVar(key); ok {
return v
return v, nil
}
// This is undefined.
log.Fatalf("%s: %s", ErrUnknownStringVar, key)
return nil
return nil, fmt.Errorf("%s: %s", ErrUnknownStringVar, key)
}

// MustGetStringVar is like GetStringVar but panics on errors
func (bib *BibTex) MustGetStringVar(key string) *BibVar {
v, err := bib.GetStringVar(key)
if err != nil {
panic(err)
}
return v
}

// getDefaultVar is a fallback for looking up keys (e.g. 3-character month)
Expand Down
5 changes: 3 additions & 2 deletions bibtex.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bibtex

import (
"io"
"fmt"
)

type bibTag struct {
Expand Down Expand Up @@ -60,9 +61,9 @@ preambleentry : tATSIGN tPREAMBLE tLBRACE longstring tRBRACE { $$ = $4 }
;

longstring : tIDENT { $$ = NewBibConst($1) }
| tBAREIDENT { $$ = bib.GetStringVar($1) }
| tBAREIDENT { v,err := bib.GetStringVar($1); if err != nil { bibtexlex.Error(fmt.Sprintf("%v",err)) } else {$$ = v} }
Copy link
Owner

Choose a reason for hiding this comment

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

whitespaces and I'd probably use the Error() to convert to error to string than fmt it.

Suggested change
| tBAREIDENT { v,err := bib.GetStringVar($1); if err != nil { bibtexlex.Error(fmt.Sprintf("%v",err)) } else {$$ = v} }
| tBAREIDENT { v, err := bib.GetStringVar($1); if err != nil { bibtexlex.Error(err.Error()) } else {$$ = v} }

| longstring tPOUND tIDENT { $$ = NewBibComposite($1); $$.(*BibComposite).Append(NewBibConst($3))}
| longstring tPOUND tBAREIDENT { $$ = NewBibComposite($1); $$.(*BibComposite).Append(bib.GetStringVar($3)) }
| longstring tPOUND tBAREIDENT { $$ = NewBibComposite($1); v,err := bib.GetStringVar($3); if err != nil {bibtexlex.Error(fmt.Sprintf("%v",err))} else {$$.(*BibComposite).Append(v)} }
Copy link
Owner

Choose a reason for hiding this comment

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

as above

Suggested change
| longstring tPOUND tBAREIDENT { $$ = NewBibComposite($1); v,err := bib.GetStringVar($3); if err != nil {bibtexlex.Error(fmt.Sprintf("%v",err))} else {$$.(*BibComposite).Append(v)} }
| longstring tPOUND tBAREIDENT { $$ = NewBibComposite($1); v, err := bib.GetStringVar($3); if err != nil { bibtexlex.Error(err.Error()) } else {$$.(*BibComposite).Append(v)} }

;

tag : /* empty */ { }
Expand Down
Loading