fix issues

This commit is contained in:
Brandon Taylor 2017-07-31 02:11:57 -04:00
parent 19b3a8804e
commit 352997ae86
1 changed files with 26 additions and 15 deletions

View File

@ -59,7 +59,7 @@ token_and_counter!(parser, bracket_counter = 1) = begin
token, bracket_counter token, bracket_counter
end end
value!(parser, values) = begin value!(parser, values = eltype(parser)[]) = begin
token = next_token!(parser) token = next_token!(parser)
if token == "\"" if token == "\""
token = next_token!(parser, "\"") token = next_token!(parser, "\"")
@ -91,7 +91,7 @@ field!(parser, dict) = begin
if token != "}" if token != "}"
key = token key = token
expect!(parser, "=") expect!(parser, "=")
token, dict[key] = value!(parser, eltype(parser)[]) token, dict[key] = value!(parser)
end end
end end
expect(parser, token, "}") expect(parser, token, "}")
@ -102,12 +102,15 @@ export parse_bibtex
parse_bibtex(text) parse_bibtex(text)
This is a simple input parser for BibTex. I had trouble finding a standard This is a simple input parser for BibTex. I had trouble finding a standard
specification, but I've included several features of real BibTex. specification, but I've included several features of real BibTex. Returns
a preamble (or an empty string) and a dict of dicts.
```jldoctest ```jldoctest
julia> using BibTeX julia> using BibTeX
julia> result = parse_bibtex(""\" julia> preamble, result = parse_bibtex(""\"
@preamble{some instructions}
@comment blah blah
@string{short = long} @string{short = long}
@a{b, @a{b,
c = { {c} c}, c = { {c} c},
@ -116,6 +119,9 @@ julia> result = parse_bibtex(""\"
} }
""\"); ""\");
julia> preamble
"some instructions"
julia> result["b"]["type"] julia> result["b"]["type"]
"a" "a"
@ -140,23 +146,28 @@ ERROR: Expected { on line 1
parse_bibtex(text) = begin parse_bibtex(text) = begin
parser = parse_text(text) parser = parse_text(text)
token = next_token_default!(parser) token = next_token_default!(parser)
preamble = ""
while token != "" while token != ""
if token == "@" if token == "@"
record_type = next_token!(parser) record_type = lowercase(next_token!(parser))
expect!(parser, "{") if record_type == "preamble"
if lowercase(record_type) == "string" trash, preamble = value!(parser)
field!(parser, parser.substitutions) elseif record_type != "comment"
else expect!(parser, "{")
id = next_token!(parser) if record_type == "string"
dict = Dict("type" => record_type) field!(parser, parser.substitutions)
expect!(parser, ",") else
field!(parser, dict) id = next_token!(parser)
parser.records[id] = dict dict = Dict("type" => record_type)
expect!(parser, ",")
field!(parser, dict)
parser.records[id] = dict
end
end end
end end
token = next_token_default!(parser) token = next_token_default!(parser)
end end
parser.records preamble, parser.records
end end
end end