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

@Comment cause ParserException::unexpectedCharacter #49

Closed
Nahihilo opened this issue May 9, 2018 · 3 comments · Fixed by #51
Closed

@Comment cause ParserException::unexpectedCharacter #49

Nahihilo opened this issue May 9, 2018 · 3 comments · Fixed by #51

Comments

@Nahihilo
Copy link

Nahihilo commented May 9, 2018

Hi,

I'm working on a .bib file that contain some "@comment{}" and when the parser arrive to one of them, it cause an error, because they don't have to be valid entry and some aren't...

Actually, the rule is that everything from the @comment and to the end of line is ignored. The remainder lines of the commented entry is ignored by the first comment mechanism we described; in particular a @comment does not need to be a valid entry, i.e. it can for example skip comas between two fields
http://maverick.inria.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html#comment

In my case "@comment" are always on one line and no line is very long.
So I just fixed it with a "fgets" instead of "fread" and a small exeption in the parseFile function :

public function parseFile($file)
{
    $handle = fopen($file, 'r');
    try {
        $this->reset();
        while (!feof($handle)) {
            $buffer = fgets($handle);//fread($handle, 128);
            if (strpos($buffer, '@Comment{') === false) {
                $this->parse($buffer);
            }
        }
        $this->throwExceptionIfReadingEntry("\0");
    } finally {
        fclose($handle);
    }
}

"@comment" in my .bib file :

@Comment{jabref-meta: databaseType:bibtex;}
@Comment{jabref-entrytype: Poster: req[author;title;year;organization] opt[abstract]}

I found that they are automaticaly added by JabRef (http://www.jabref.org/) at the end of the .bib file.
JabRef/jabref#2944

Have a nice day!

@renanbr
Copy link
Owner

renanbr commented May 9, 2018

Thanks for reporting this issue.

Your solution is a good start, and I would like to fix it for all the cases I found in the link you bring up:

@Comment{jabref-meta: databaseType:bibtex;}

@Comment{jabref-entrytype: Poster: req[author;title;year;organization] opt[abstract]}

@Comment{
  @Book{steward03,
    author =	 {Martha Steward},
    title =	 {Cooking behind bars},
    publisher =	 {Culinary Expert Series},
    year =	 2003
  }
}

If I got your point, in the exemple above, the @Book entry should be take in account, the rest ignored, and no error thrown.

A alternative way to solve the problem is skiping/ignoring @comment type:

  1. Not trigger the listeners when a @comment type is found; and
  2. Then switch the current state to COMMENT at the moment this "type" is found

Advantage: We can keep the parser able to read char-by-char, as currently it does.

What do you think?

Note: This change could break this test: https://github.com/renanbr/bibtex-parser/blob/master/tests/Parser/CommentTest.php#L34-L67

@renanbr
Copy link
Owner

renanbr commented May 11, 2018

JabRef case is fixed (not yet released):


I'm having problems in understanding this sentence:

Actually, the rule is that everything from the @comment and to the end of line is ignored.

So, what's the braces' role for an comment "entry"?

In the bibtex bellow, I'm willing to consider the existence of 3 entries (note the missing "}" at the end of the file):

@comment{@book{title={I do exist}}}

@comment{
  @book{title={I really do exist}}
}

@comment{ bla bla bla
@book{title={I'm real}}

A first approach is available : https://github.com/renanbr/bibtex-parser/tree/comment-is-not-an-entry

@renanbr
Copy link
Owner

renanbr commented Jul 19, 2018

@Nahihilo I pushed another branch with a better and simpler fix IMO, see #51

I'm going to merge it if you don't stand up against it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants