58523
|
1 |
/* Title: Pure/Tools/bibtex.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Some support for bibtex files.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
58528
|
10 |
import scala.collection.mutable
|
58523
|
11 |
import scala.util.parsing.input.{Reader, CharSequenceReader}
|
|
12 |
import scala.util.parsing.combinator.RegexParsers
|
|
13 |
|
|
14 |
|
|
15 |
object Bibtex
|
|
16 |
{
|
|
17 |
/** content **/
|
|
18 |
|
58529
|
19 |
private val months = List(
|
58523
|
20 |
"jan",
|
|
21 |
"feb",
|
|
22 |
"mar",
|
|
23 |
"apr",
|
|
24 |
"may",
|
|
25 |
"jun",
|
|
26 |
"jul",
|
|
27 |
"aug",
|
|
28 |
"sep",
|
|
29 |
"oct",
|
|
30 |
"nov",
|
|
31 |
"dec")
|
58529
|
32 |
def is_month(s: String): Boolean = months.contains(s.toLowerCase)
|
58523
|
33 |
|
58529
|
34 |
private val commands = List("preamble", "string")
|
|
35 |
def is_command(s: String): Boolean = commands.contains(s.toLowerCase)
|
58523
|
36 |
|
58524
|
37 |
sealed case class Entry(
|
58526
|
38 |
kind: String,
|
58523
|
39 |
required: List[String],
|
|
40 |
optional_crossref: List[String],
|
58529
|
41 |
optional_other: List[String])
|
58524
|
42 |
{
|
58533
|
43 |
def is_required(s: String): Boolean = required.contains(s.toLowerCase)
|
58529
|
44 |
def is_optional(s: String): Boolean =
|
58533
|
45 |
optional_crossref.contains(s.toLowerCase) || optional_other.contains(s.toLowerCase)
|
58529
|
46 |
|
|
47 |
def fields: List[String] = required ::: optional_crossref ::: optional_other
|
58524
|
48 |
def template: String =
|
58526
|
49 |
"@" + kind + "{,\n" + fields.map(x => " " + x + " = {},\n").mkString + "}\n"
|
58524
|
50 |
}
|
58523
|
51 |
|
58524
|
52 |
val entries: List[Entry] =
|
|
53 |
List(
|
|
54 |
Entry("Article",
|
|
55 |
List("author", "title"),
|
|
56 |
List("journal", "year"),
|
|
57 |
List("volume", "number", "pages", "month", "note")),
|
|
58 |
Entry("InProceedings",
|
|
59 |
List("author", "title"),
|
|
60 |
List("booktitle", "year"),
|
|
61 |
List("editor", "volume", "number", "series", "pages", "month", "address",
|
|
62 |
"organization", "publisher", "note")),
|
|
63 |
Entry("InCollection",
|
|
64 |
List("author", "title", "booktitle"),
|
|
65 |
List("publisher", "year"),
|
|
66 |
List("editor", "volume", "number", "series", "type", "chapter", "pages",
|
|
67 |
"edition", "month", "address", "note")),
|
|
68 |
Entry("InBook",
|
|
69 |
List("author", "editor", "title", "chapter"),
|
|
70 |
List("publisher", "year"),
|
|
71 |
List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")),
|
|
72 |
Entry("Proceedings",
|
|
73 |
List("title", "year"),
|
|
74 |
List(),
|
|
75 |
List("booktitle", "editor", "volume", "number", "series", "address", "month",
|
|
76 |
"organization", "publisher", "note")),
|
|
77 |
Entry("Book",
|
|
78 |
List("author", "editor", "title"),
|
|
79 |
List("publisher", "year"),
|
|
80 |
List("volume", "number", "series", "address", "edition", "month", "note")),
|
|
81 |
Entry("Booklet",
|
|
82 |
List("title"),
|
|
83 |
List(),
|
|
84 |
List("author", "howpublished", "address", "month", "year", "note")),
|
|
85 |
Entry("PhdThesis",
|
|
86 |
List("author", "title", "school", "year"),
|
|
87 |
List(),
|
|
88 |
List("type", "address", "month", "note")),
|
|
89 |
Entry("MastersThesis",
|
|
90 |
List("author", "title", "school", "year"),
|
|
91 |
List(),
|
|
92 |
List("type", "address", "month", "note")),
|
|
93 |
Entry("TechReport",
|
|
94 |
List("author", "title", "institution", "year"),
|
|
95 |
List(),
|
|
96 |
List("type", "number", "address", "month", "note")),
|
|
97 |
Entry("Manual",
|
|
98 |
List("title"),
|
|
99 |
List(),
|
|
100 |
List("author", "organization", "address", "edition", "month", "year", "note")),
|
|
101 |
Entry("Unpublished",
|
|
102 |
List("author", "title", "note"),
|
|
103 |
List(),
|
|
104 |
List("month", "year")),
|
|
105 |
Entry("Misc",
|
|
106 |
List(),
|
|
107 |
List(),
|
|
108 |
List("author", "title", "howpublished", "month", "year", "note")))
|
58523
|
109 |
|
58529
|
110 |
def get_entry(kind: String): Option[Entry] =
|
|
111 |
entries.find(entry => entry.kind.toLowerCase == kind.toLowerCase)
|
|
112 |
|
58530
|
113 |
def is_entry(kind: String): Boolean = get_entry(kind).isDefined
|
|
114 |
|
58523
|
115 |
|
|
116 |
|
|
117 |
/** tokens and chunks **/
|
|
118 |
|
|
119 |
object Token
|
|
120 |
{
|
|
121 |
object Kind extends Enumeration
|
|
122 |
{
|
58529
|
123 |
val COMMAND = Value("command")
|
|
124 |
val ENTRY = Value("entry")
|
58523
|
125 |
val KEYWORD = Value("keyword")
|
|
126 |
val NAT = Value("natural number")
|
58529
|
127 |
val STRING = Value("string")
|
58531
|
128 |
val NAME = Value("name")
|
58523
|
129 |
val IDENT = Value("identifier")
|
58529
|
130 |
val IGNORED = Value("ignored")
|
58523
|
131 |
val ERROR = Value("bad input")
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
sealed case class Token(kind: Token.Kind.Value, val source: String)
|
|
136 |
{
|
58530
|
137 |
def is_kind: Boolean =
|
|
138 |
kind == Token.Kind.COMMAND ||
|
|
139 |
kind == Token.Kind.ENTRY ||
|
|
140 |
kind == Token.Kind.IDENT
|
58531
|
141 |
def is_name: Boolean =
|
|
142 |
kind == Token.Kind.NAME ||
|
|
143 |
kind == Token.Kind.IDENT
|
58529
|
144 |
def is_ignored: Boolean = kind == Token.Kind.IGNORED
|
58530
|
145 |
def is_malformed: Boolean = kind == Token.Kind.ERROR
|
58523
|
146 |
}
|
|
147 |
|
58530
|
148 |
case class Chunk(kind: String, tokens: List[Token])
|
58523
|
149 |
{
|
58529
|
150 |
val source = tokens.map(_.source).mkString
|
58526
|
151 |
|
58530
|
152 |
private val content: Option[List[Token]] =
|
58523
|
153 |
tokens match {
|
58530
|
154 |
case Token(Token.Kind.KEYWORD, "@") :: body if !body.isEmpty =>
|
58529
|
155 |
(body.init.filterNot(_.is_ignored), body.last) match {
|
58530
|
156 |
case (tok :: Token(Token.Kind.KEYWORD, "{") :: toks, Token(Token.Kind.KEYWORD, "}"))
|
|
157 |
if tok.is_kind => Some(toks)
|
|
158 |
|
|
159 |
case (tok :: Token(Token.Kind.KEYWORD, "(") :: toks, Token(Token.Kind.KEYWORD, ")"))
|
|
160 |
if tok.is_kind => Some(toks)
|
|
161 |
|
58528
|
162 |
case _ => None
|
58523
|
163 |
}
|
58528
|
164 |
case _ => None
|
58526
|
165 |
}
|
|
166 |
|
|
167 |
def name: String =
|
58530
|
168 |
content match {
|
58531
|
169 |
case Some(tok :: _) if tok.is_name => tok.source
|
58523
|
170 |
case _ => ""
|
|
171 |
}
|
58530
|
172 |
|
|
173 |
def is_ignored: Boolean = kind == "" && tokens.forall(_.is_ignored)
|
|
174 |
def is_malformed: Boolean = kind == "" || tokens.exists(_.is_malformed)
|
|
175 |
def is_command: Boolean =
|
|
176 |
Bibtex.is_command(kind) && name != "" && content.isDefined && !is_malformed
|
|
177 |
def is_entry: Boolean =
|
|
178 |
Bibtex.is_entry(kind) && name != "" && content.isDefined && !is_malformed
|
58523
|
179 |
}
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
/** parsing **/
|
|
184 |
|
|
185 |
// context of partial line-oriented scans
|
|
186 |
abstract class Line_Context
|
58528
|
187 |
case object Ignored_Context extends Line_Context
|
|
188 |
case class Item_Context(kind: String, delim: Delimited, right: String) extends Line_Context
|
|
189 |
case class Delimited(quoted: Boolean, depth: Int)
|
|
190 |
val Closed = Delimited(false, 0)
|
58523
|
191 |
|
|
192 |
private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source)
|
|
193 |
private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source)
|
|
194 |
|
|
195 |
|
|
196 |
// See also http://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web
|
|
197 |
// module @<Scan for and process a \.{.bib} command or database entry@>.
|
|
198 |
|
|
199 |
object Parsers extends RegexParsers
|
|
200 |
{
|
|
201 |
/* white space and comments */
|
|
202 |
|
|
203 |
override val whiteSpace = "".r
|
|
204 |
|
58529
|
205 |
private val space = """[ \t\n\r]+""".r ^^ token(Token.Kind.IGNORED)
|
|
206 |
private val strict_space = """[ \t]+""".r ^^ token(Token.Kind.IGNORED)
|
58523
|
207 |
|
58528
|
208 |
|
58530
|
209 |
/* ignored material */
|
58528
|
210 |
|
|
211 |
private val ignored: Parser[Chunk] =
|
58530
|
212 |
rep1("""(?mi)([^@]+|@[ \t]*comment)""".r) ^^ {
|
|
213 |
case ss => Chunk("", List(Token(Token.Kind.IGNORED, ss.mkString))) }
|
58523
|
214 |
|
|
215 |
|
|
216 |
/* delimited string: outermost "..." or {...} and body with balanced {...} */
|
|
217 |
|
|
218 |
private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] =
|
|
219 |
new Parser[(String, Delimited)]
|
|
220 |
{
|
|
221 |
require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0)
|
|
222 |
|
|
223 |
def apply(in: Input) =
|
|
224 |
{
|
|
225 |
val start = in.offset
|
|
226 |
val end = in.source.length
|
|
227 |
|
|
228 |
var i = start
|
|
229 |
var q = delim.quoted
|
|
230 |
var d = delim.depth
|
|
231 |
var finished = false
|
|
232 |
while (!finished && i < end) {
|
|
233 |
val c = in.source.charAt(i)
|
|
234 |
if (c == '"' && d == 0) { i += 1; d = 1; q = true }
|
58532
|
235 |
else if (c == '"' && d == 1 && q) {
|
|
236 |
i += 1; d = 0; q = false; finished = true
|
|
237 |
}
|
58523
|
238 |
else if (c == '{') { i += 1; d += 1 }
|
58532
|
239 |
else if (c == '}' && (d > 1 || d == 1 && !q)) {
|
|
240 |
i += 1; d -= 1; if (d == 0) finished = true
|
|
241 |
}
|
58523
|
242 |
else if (d > 0) i += 1
|
|
243 |
else finished = true
|
|
244 |
}
|
|
245 |
if (i == start) Failure("bad input", in)
|
58528
|
246 |
else {
|
|
247 |
val s = in.source.subSequence(start, i).toString
|
|
248 |
Success((s, Delimited(q, d)), in.drop(i - start))
|
|
249 |
}
|
58523
|
250 |
}
|
|
251 |
}.named("delimited_depth")
|
|
252 |
|
58528
|
253 |
private def delimited: Parser[Token] =
|
|
254 |
delimited_depth(Closed) ^?
|
|
255 |
{ case (s, delim) if delim == Closed => Token(Token.Kind.STRING, s) }
|
58523
|
256 |
|
58530
|
257 |
private def delimited_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] =
|
58523
|
258 |
{
|
|
259 |
ctxt match {
|
58528
|
260 |
case Item_Context(kind, delim, right) =>
|
|
261 |
delimited_depth(delim) ^^ { case (s, delim1) =>
|
58530
|
262 |
(Chunk(kind, List(Token(Token.Kind.STRING, s))), Item_Context(kind, delim1, right)) }
|
58523
|
263 |
case _ => failure("")
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
58528
|
267 |
private def recover_delimited: Parser[Token] =
|
|
268 |
"""(?m)["{][^@]+""".r ^^ token(Token.Kind.ERROR)
|
58523
|
269 |
|
|
270 |
|
|
271 |
/* other tokens */
|
|
272 |
|
|
273 |
private val at = "@" ^^ keyword
|
|
274 |
private val left_brace = "{" ^^ keyword
|
|
275 |
private val right_brace = "}" ^^ keyword
|
|
276 |
private val left_paren = "(" ^^ keyword
|
|
277 |
private val right_paren = ")" ^^ keyword
|
|
278 |
|
|
279 |
private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT)
|
|
280 |
|
58529
|
281 |
private val identifier =
|
|
282 |
"""[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r
|
|
283 |
|
|
284 |
private val ident = identifier ^^ token(Token.Kind.IDENT)
|
58523
|
285 |
|
58528
|
286 |
val other_token = "[=#,]".r ^^ keyword | (nat | (ident | space))
|
|
287 |
|
|
288 |
|
58530
|
289 |
/* items: command or entry */
|
58528
|
290 |
|
58530
|
291 |
private val item_kind =
|
58529
|
292 |
identifier ^^ { case a =>
|
|
293 |
val kind =
|
|
294 |
if (is_command(a)) Token.Kind.COMMAND
|
58530
|
295 |
else if (is_entry(a)) Token.Kind.ENTRY
|
58529
|
296 |
else Token.Kind.IDENT
|
|
297 |
Token(kind, a)
|
|
298 |
}
|
|
299 |
|
58531
|
300 |
private val item_start =
|
58530
|
301 |
at ~ rep(strict_space) ~ item_kind ~ rep(strict_space) ^^
|
58528
|
302 |
{ case a ~ b ~ c ~ d => (c.source, List(a) ::: b ::: List(c) ::: d) }
|
|
303 |
|
58531
|
304 |
private val item_name =
|
|
305 |
rep(strict_space) ~ identifier ^^
|
|
306 |
{ case a ~ b => a ::: List(Token(Token.Kind.NAME, b)) }
|
|
307 |
|
58528
|
308 |
private val item_body =
|
|
309 |
delimited | (recover_delimited | other_token)
|
|
310 |
|
58530
|
311 |
private val item: Parser[Chunk] =
|
58531
|
312 |
(item_start ~ left_brace ~ item_name ~ rep(item_body) ~ opt(right_brace) |
|
|
313 |
item_start ~ left_paren ~ item_name ~ rep(item_body) ~ opt(right_paren)) ^^
|
|
314 |
{ case (kind, a) ~ b ~ c ~ d ~ e => Chunk(kind, a ::: List(b) ::: c ::: d ::: e.toList) }
|
58528
|
315 |
|
58530
|
316 |
private val recover_item: Parser[Chunk] =
|
|
317 |
at ~ "(?m)[^@]*".r ^^ { case a ~ b => Chunk("", List(a, Token(Token.Kind.ERROR, b))) }
|
58528
|
318 |
|
58523
|
319 |
|
|
320 |
/* chunks */
|
|
321 |
|
58528
|
322 |
val chunk: Parser[Chunk] = ignored | (item | recover_item)
|
58523
|
323 |
|
58528
|
324 |
def chunk_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] =
|
58530
|
325 |
{
|
|
326 |
ctxt match {
|
|
327 |
case Ignored_Context =>
|
|
328 |
ignored ^^ { case a => (a, ctxt) } |
|
58531
|
329 |
item_start ~ (left_brace | left_paren) ~ opt(item_name) ^^
|
|
330 |
{ case (kind, a) ~ b ~ c =>
|
58530
|
331 |
val right = if (b.source == "{") "}" else ")"
|
58531
|
332 |
val chunk = Chunk(kind, a ::: List(b) ::: (c getOrElse Nil))
|
|
333 |
(chunk, Item_Context(kind, Closed, right)) } |
|
58530
|
334 |
recover_item ^^ { case a => (a, Ignored_Context) }
|
|
335 |
case Item_Context(kind, delim, right) =>
|
|
336 |
if (delim.depth > 0)
|
|
337 |
delimited_line(ctxt)
|
|
338 |
else {
|
|
339 |
delimited_line(ctxt) |
|
|
340 |
other_token ^^ { case a => (Chunk(kind, List(a)), ctxt) } |
|
|
341 |
right ^^ { case a => (Chunk(kind, List(keyword(a))), Ignored_Context) }
|
|
342 |
}
|
|
343 |
case _ => failure("")
|
|
344 |
}
|
|
345 |
}
|
58528
|
346 |
}
|
58523
|
347 |
|
|
348 |
|
58528
|
349 |
/* parse */
|
58523
|
350 |
|
|
351 |
def parse(input: CharSequence): List[Chunk] =
|
|
352 |
{
|
|
353 |
val in: Reader[Char] = new CharSequenceReader(input)
|
58528
|
354 |
Parsers.parseAll(Parsers.rep(Parsers.chunk), in) match {
|
58523
|
355 |
case Parsers.Success(result, _) => result
|
58526
|
356 |
case _ => error("Unexpected failure to parse input:\n" + input.toString)
|
58523
|
357 |
}
|
|
358 |
}
|
58528
|
359 |
|
|
360 |
def parse_line(input: CharSequence, context: Line_Context): (List[Chunk], Line_Context) =
|
|
361 |
{
|
|
362 |
var in: Reader[Char] = new CharSequenceReader(input)
|
|
363 |
val chunks = new mutable.ListBuffer[Chunk]
|
|
364 |
var ctxt = context
|
|
365 |
while (!in.atEnd) {
|
|
366 |
Parsers.parse(Parsers.chunk_line(ctxt), in) match {
|
|
367 |
case Parsers.Success((x, c), rest) => { chunks += x; ctxt = c; in = rest }
|
|
368 |
case Parsers.NoSuccess(_, rest) =>
|
|
369 |
error("Unepected failure to parse input:\n" + rest.source.toString)
|
|
370 |
}
|
|
371 |
}
|
|
372 |
(chunks.toList, ctxt)
|
|
373 |
}
|
58523
|
374 |
}
|
|
375 |
|