made duplicated fields and keys error for safety

This commit is contained in:
Brandon Taylor 2017-08-02 17:51:43 -04:00
parent 251f16ce9f
commit 75d1b6d74c
1 changed files with 39 additions and 7 deletions

View File

@ -87,9 +87,13 @@ field!(parser, dict) = begin
while token == ","
token = next_token!(parser, "a new entry or }")
if token != "}"
key = token
expect!(parser, "=")
token, dict[lowercase(key)] = value!(parser)
key = lowercase(token)
if haskey(dict, key)
error("Duplicated field $key $(location(parser))")
else
expect!(parser, "=")
token, dict[key] = value!(parser)
end
end
end
expect(parser, token, "}")
@ -139,6 +143,29 @@ julia> parse_bibtex("@book@")
ERROR: Expected { on line 1
[...]
```
Repeated fields and keys are not allowed:
```jldoctest
julia> using BibTeX: parse_bibtex
julia> parse_bibtex(""\"
@book{abook,
title = A}
@book{abook,
title = B}
""\")
ERROR: Duplicated id abook on line 3
[...]
julia> parse_bibtex(""\"
@book{abook,
title = A,
title = B}
""\")
ERROR: Duplicated field title on line 3
[...]
```
"""
parse_bibtex(text) = begin
parser = parse_text(text)
@ -155,10 +182,15 @@ parse_bibtex(text) = begin
field!(parser, parser.substitutions)
else
id = next_token!(parser)
dict = Dict("__type__" => record_type)
expect!(parser, ",")
field!(parser, dict)
parser.records[id] = dict
records = parser.records
if haskey(records, id)
error("Duplicated id $id $(location(parser))")
else
dict = Dict("__type__" => record_type)
expect!(parser, ",")
field!(parser, dict)
records[id] = dict
end
end
end
end