-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBibTeX.py
executable file
·175 lines (148 loc) · 5.17 KB
/
BibTeX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# Copyright (c) 2011 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
BNF
BIBTEXES ::= BIBTEX+
BIBTEX ::= '@Book{' IDENTIFIER ',' KEY_VALUES '}'
IDENTIFIER ::= [a-zA-Z][^,\s]*
KEY_VALUES ::= KEY_VALUE | KEY_VALUE ',' KEY_VALUES
KEY_VALUE ::= KEY '=' VALUE
KEY ::= [a-zA-Z]\w*
VALUE ::= "[^"]+" | \d+
"""
import pprint
import ply.lex
try:
from pyparsing import (alphas, alphanums, delimitedList, nums,
OneOrMore, ParseException, QuotedString, Regex, Suppress, Word)
except ImportError:
from pyparsing_py3 import (alphas, alphanums, delimitedList, nums,
OneOrMore, ParseException, QuotedString, Regex, Suppress, Word)
import re
# The examples are copied from Wikipedia
TEXT = """
@Book{blanchette+summerfield,
author = "Jasmin Blanchette and Mark Summerfield",
title = "C++ GUI Programming with Qt 4,
Second Edition",
publisher = "Prentice Hall",
year = 2008,
address = "New York"
}
@Book{abramowitz+stegun,
author = "Milton {Abramowitz} and Irene A. {Stegun}",
title = "Handbook of Mathematical Functions with
Formulas, Graphs, and Mathematical Tables",
publisher = "Dover",
year = 1964,
address = "New York",
edition = "ninth Dover printing, tenth GPO printing"
}
@Book{hicks2001,
author = "von Hicks, III, Michael",
title = "Design of a Carbon Fiber Composite Grid Structure for the GLAST
Spacecraft Using a Novel Manufacturing Technique",
publisher = "Stanford Press",
year = 2001,
address = "Palo Alto",
edition = "1st,",
isbn = "0-69-697269-4"
}
@Book{Torre2008,
author = "Joe Torre and Tom Verducci",
publisher = "Doubleday",
title = "The Yankee Years",
year = 2008,
isbn = "0385527403"
}
"""
def pyparsing_parse(text):
WHITESPACE = re.compile(r"\s+")
books = {}
key_values = {}
def normalize(tokens):
return WHITESPACE.sub(" ", tokens[0])
def add_key_value(tokens):
key_values[tokens.key] = tokens.value
def add_book(tokens):
books[tokens.identifier] = key_values.copy()
key_values.clear()
left_brace, right_brace, comma, equals = map(Suppress, "{},=")
start = Suppress("@Book") + left_brace
identifier = Regex(r"[a-zA-Z][^,\s]*")("identifier") + comma
key = Word(alphas, alphanums)("key")
value = (Word(nums).setParseAction(lambda t: int(t[0])) |
QuotedString('"', multiline=True).setParseAction(normalize)
)("value")
key_value = (key + equals + value).setParseAction(add_key_value)
end = right_brace
bibtex = (start + identifier + delimitedList(key_value) + end
).setParseAction(add_book)
parser = OneOrMore(bibtex)
try:
parser.parseString(text)
except ParseException as err:
print("parse error: {0}".format(err))
return books
def ply_parse(text):
WHITESPACE = re.compile(r"\s+")
tokens = ("START", "IDENTIFIER", "KEY", "NUMBER", "QUOTEDSTRING",
"COMMA", "END")
t_ignore_START = r"@Book"
def t_IDENTIFIER(t):
r"\{[a-zA-Z][^,\s]*"
t.value = t.value[1:]
return t
t_KEY = r"[a-zA-Z]\w*"
def t_NUMBER(t):
r"=\s*\d+"
t.value = int(t.value[1:].lstrip())
return t
def t_QUOTEDSTRING(t):
r'=\s*"[^="]+"'
t.value = WHITESPACE.sub(" ", t.value[1:].lstrip()[1:-1].strip())
return t
t_ignore_COMMA = r","
t_ignore_END = r"\}"
t_ignore = " \t\n"
def t_newline(t):
r"\n+"
t.lexer.lineno += len(t.value)
def t_error(t):
line = t.value.lstrip()
i = line.find("\n")
line = line if i == -1 else line[:i]
print("failed to parse line {0}: {1}".format(t.lineno + 1,
line))
books = {}
book = key = None
lexer = ply.lex.lex()
lexer.input(text.replace("\n", " "))
for token in lexer:
if token.type == "IDENTIFIER":
books[token.value] = book = {}
continue
if book is None:
print("missing start of book line {0}".format(token.lineno))
if token.type == "KEY":
key = token.value
continue
if key is None:
print("missing key line {0}".format(token.lineno))
if token.type in ("QUOTEDSTRING", "NUMBER"):
book[key] = token.value
return books
def main():
books_ply = ply_parse(TEXT)
books_pyparsing = pyparsing_parse(TEXT)
pprint.pprint(books_pyparsing)
assert books_ply == books_pyparsing
main()