made duplicated fields and keys error for safety
This commit is contained in:
parent
251f16ce9f
commit
75d1b6d74c
|
@ -87,9 +87,13 @@ field!(parser, dict) = begin
|
||||||
while token == ","
|
while token == ","
|
||||||
token = next_token!(parser, "a new entry or }")
|
token = next_token!(parser, "a new entry or }")
|
||||||
if token != "}"
|
if token != "}"
|
||||||
key = token
|
key = lowercase(token)
|
||||||
|
if haskey(dict, key)
|
||||||
|
error("Duplicated field $key $(location(parser))")
|
||||||
|
else
|
||||||
expect!(parser, "=")
|
expect!(parser, "=")
|
||||||
token, dict[lowercase(key)] = value!(parser)
|
token, dict[key] = value!(parser)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
expect(parser, token, "}")
|
expect(parser, token, "}")
|
||||||
|
@ -139,6 +143,29 @@ julia> parse_bibtex("@book@")
|
||||||
ERROR: Expected { on line 1
|
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
|
parse_bibtex(text) = begin
|
||||||
parser = parse_text(text)
|
parser = parse_text(text)
|
||||||
|
@ -155,10 +182,15 @@ parse_bibtex(text) = begin
|
||||||
field!(parser, parser.substitutions)
|
field!(parser, parser.substitutions)
|
||||||
else
|
else
|
||||||
id = next_token!(parser)
|
id = next_token!(parser)
|
||||||
|
records = parser.records
|
||||||
|
if haskey(records, id)
|
||||||
|
error("Duplicated id $id $(location(parser))")
|
||||||
|
else
|
||||||
dict = Dict("__type__" => record_type)
|
dict = Dict("__type__" => record_type)
|
||||||
expect!(parser, ",")
|
expect!(parser, ",")
|
||||||
field!(parser, dict)
|
field!(parser, dict)
|
||||||
parser.records[id] = dict
|
records[id] = dict
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue