Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions bibtex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ func TestParser(t *testing.T) {
}
}

// Test bug (Issue #24) where there is no parse error, but fields are missing
func TestTextOutsideEntries(t *testing.T) {
// Re-create the exact failing scenario
expected := NewBibTex()
entry := NewBibEntry("article", "CitekeyArticle")
entry.AddField("author", NewBibConst("John Doe"))
entry.AddField("title", NewBibConst("The independence of the continuum hypothesis"))
entry.AddField("journal", NewBibConst("Proceedings of the National Academy of Sciences"))
entry.AddField("year", NewBibConst("1963"))
entry.AddField("volume", NewBibConst("50"))
entry.AddField("number", NewBibConst("6"))
entry.AddField("pages", NewBibConst("1143--1148"))
expected.AddEntry(entry)

// Parse file with same data as above, also with text in between the entries
ex := "example/text-outside-entries.bib"
b, err := os.ReadFile(ex)
if err != nil {
t.Errorf("Cannot read %s: %v", ex, err)
}
s, err := Parse(bytes.NewReader(b))
if err != nil {
t.Errorf("Cannot parse valid bibtex file %s: %v", ex, err)
}

// Check equality
AssertEntryListsEqual(t, expected.Entries, s.Entries)
}

// Tests that multiple parse returns different instances of the parsed BibTex.
// Otherwise the number of entries will pile up. (Issue #4)
func TestMultiParse(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions example/text-outside-entries.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
% Encoding: UTF-8
@article{CitekeyArticle,
author = "John Doe",
title = "The independence of the continuum hypothesis",
journal = "Proceedings of the National Academy of Sciences",
year = 1963,
volume = "50",
number = "6",
pages = "1143--1148",
}
20 changes: 20 additions & 0 deletions example/text-outside-entries2.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
% Encoding: UTF-8
@article{CitekeyArticle,
author = "John Doe",
title = "The independence of the continuum hypothesis",
journal = "Proceedings of the National Academy of Sciences",
year = 1963,
volume = "50",
number = "6",
pages = "1143--1148",
}
% Same entry again
@article{CitekeyArticle2,
author = "John Doe",
title = "The independence of the continuum hypothesis",
journal = "Proceedings of the National Academy of Sciences",
year = 1963,
volume = "50",
number = "6",
pages = "1143--1148",
}
9 changes: 8 additions & 1 deletion scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ var parseField bool
// scanner is a lexical scanner
type scanner struct {
commentMode bool
ignoreMode bool
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if it's better to call this "outsideEntry" or something like that to be clear that we're not switching into a different context mode (comment mode is when we're scanning comments, not affected by the grammar), but actually not inside an entry so we can ignore everything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's a great idea, definitely a much clearer name. I've updated it in a new commit.

r *bufio.Reader
pos tokenPos
}

// newScanner returns a new instance of scanner.
func newScanner(r io.Reader) *scanner {
return &scanner{r: bufio.NewReader(r), pos: tokenPos{Char: 0, Lines: []int{}}}
return &scanner{ignoreMode: true, r: bufio.NewReader(r), pos: tokenPos{Char: 0, Lines: []int{}}}
}

// read reads the next rune from the buffered reader.
Expand Down Expand Up @@ -51,6 +52,11 @@ func (s *scanner) unread() {

// Scan returns the next token and literal value.
func (s *scanner) Scan() (tok token, lit string, err error) {
if s.ignoreMode {
// Ordinary comment scanning, but without generating a token
s.scanCommentBody()
s.ignoreMode = false
}
ch := s.read()
if isWhitespace(ch) {
s.ignoreWhitespace()
Expand Down Expand Up @@ -91,6 +97,7 @@ func (s *scanner) Scan() (tok token, lit string, err error) {
case '}':
if parseField { // reset parseField if reached end of entry.
parseField = false
s.ignoreMode = true
}
return tRBRACE, string(ch), nil
case '#':
Expand Down