author | wenzelm |
Fri, 03 Oct 2014 20:19:42 +0200 | |
changeset 58527 | 4b190c763097 |
parent 58526 | f05ccce3eca2 |
child 58528 | 7d6b8f8893e8 |
permissions | -rw-r--r-- |
58523 | 1 |
/* Title: Pure/Tools/bibtex.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Some support for bibtex files. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import scala.util.parsing.input.{Reader, CharSequenceReader} |
|
11 |
import scala.util.parsing.combinator.RegexParsers |
|
12 |
||
13 |
||
14 |
object Bibtex |
|
15 |
{ |
|
16 |
/** content **/ |
|
17 |
||
18 |
val months = List( |
|
19 |
"jan", |
|
20 |
"feb", |
|
21 |
"mar", |
|
22 |
"apr", |
|
23 |
"may", |
|
24 |
"jun", |
|
25 |
"jul", |
|
26 |
"aug", |
|
27 |
"sep", |
|
28 |
"oct", |
|
29 |
"nov", |
|
30 |
"dec") |
|
31 |
||
32 |
val commands = List("preamble", "string") |
|
33 |
||
58524 | 34 |
sealed case class Entry( |
58526 | 35 |
kind: String, |
58523 | 36 |
required: List[String], |
37 |
optional_crossref: List[String], |
|
38 |
optional: List[String]) |
|
58524 | 39 |
{ |
40 |
def fields: List[String] = required ::: optional_crossref ::: optional |
|
41 |
def template: String = |
|
58526 | 42 |
"@" + kind + "{,\n" + fields.map(x => " " + x + " = {},\n").mkString + "}\n" |
58524 | 43 |
} |
58523 | 44 |
|
58524 | 45 |
val entries: List[Entry] = |
46 |
List( |
|
47 |
Entry("Article", |
|
48 |
List("author", "title"), |
|
49 |
List("journal", "year"), |
|
50 |
List("volume", "number", "pages", "month", "note")), |
|
51 |
Entry("InProceedings", |
|
52 |
List("author", "title"), |
|
53 |
List("booktitle", "year"), |
|
54 |
List("editor", "volume", "number", "series", "pages", "month", "address", |
|
55 |
"organization", "publisher", "note")), |
|
56 |
Entry("InCollection", |
|
57 |
List("author", "title", "booktitle"), |
|
58 |
List("publisher", "year"), |
|
59 |
List("editor", "volume", "number", "series", "type", "chapter", "pages", |
|
60 |
"edition", "month", "address", "note")), |
|
61 |
Entry("InBook", |
|
62 |
List("author", "editor", "title", "chapter"), |
|
63 |
List("publisher", "year"), |
|
64 |
List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")), |
|
65 |
Entry("Proceedings", |
|
66 |
List("title", "year"), |
|
67 |
List(), |
|
68 |
List("booktitle", "editor", "volume", "number", "series", "address", "month", |
|
69 |
"organization", "publisher", "note")), |
|
70 |
Entry("Book", |
|
71 |
List("author", "editor", "title"), |
|
72 |
List("publisher", "year"), |
|
73 |
List("volume", "number", "series", "address", "edition", "month", "note")), |
|
74 |
Entry("Booklet", |
|
75 |
List("title"), |
|
76 |
List(), |
|
77 |
List("author", "howpublished", "address", "month", "year", "note")), |
|
78 |
Entry("PhdThesis", |
|
79 |
List("author", "title", "school", "year"), |
|
80 |
List(), |
|
81 |
List("type", "address", "month", "note")), |
|
82 |
Entry("MastersThesis", |
|
83 |
List("author", "title", "school", "year"), |
|
84 |
List(), |
|
85 |
List("type", "address", "month", "note")), |
|
86 |
Entry("TechReport", |
|
87 |
List("author", "title", "institution", "year"), |
|
88 |
List(), |
|
89 |
List("type", "number", "address", "month", "note")), |
|
90 |
Entry("Manual", |
|
91 |
List("title"), |
|
92 |
List(), |
|
93 |
List("author", "organization", "address", "edition", "month", "year", "note")), |
|
94 |
Entry("Unpublished", |
|
95 |
List("author", "title", "note"), |
|
96 |
List(), |
|
97 |
List("month", "year")), |
|
98 |
Entry("Misc", |
|
99 |
List(), |
|
100 |
List(), |
|
101 |
List("author", "title", "howpublished", "month", "year", "note"))) |
|
58523 | 102 |
|
103 |
||
104 |
||
105 |
/** tokens and chunks **/ |
|
106 |
||
107 |
object Token |
|
108 |
{ |
|
109 |
object Kind extends Enumeration |
|
110 |
{ |
|
111 |
val KEYWORD = Value("keyword") |
|
112 |
val NAT = Value("natural number") |
|
113 |
val IDENT = Value("identifier") |
|
114 |
val STRING = Value("string") |
|
115 |
val SPACE = Value("white space") |
|
116 |
val ERROR = Value("bad input") |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
sealed case class Token(kind: Token.Kind.Value, val source: String) |
|
121 |
{ |
|
122 |
def is_space: Boolean = kind == Token.Kind.SPACE |
|
123 |
def is_error: Boolean = kind == Token.Kind.ERROR |
|
124 |
} |
|
125 |
||
58526 | 126 |
abstract class Chunk { def size: Int; def kind: String = "" } |
127 |
case class Ignored(source: String) extends Chunk { def size: Int = source.size } |
|
128 |
case class Malformed(source: String) extends Chunk { def size: Int = source.size } |
|
58523 | 129 |
case class Item(tokens: List[Token]) extends Chunk |
130 |
{ |
|
58526 | 131 |
def size: Int = (0 /: tokens)({ case (n, token) => n + token.source.size }) |
132 |
||
133 |
private val content: (String, List[Token]) = |
|
58523 | 134 |
tokens match { |
135 |
case Token(Token.Kind.KEYWORD, "@") :: body |
|
136 |
if !body.isEmpty && !body.exists(_.is_error) => |
|
58526 | 137 |
(body.init.filterNot(_.is_space), body.last) match { |
138 |
case (Token(Token.Kind.IDENT, id) :: Token(Token.Kind.KEYWORD, "{") :: toks, |
|
139 |
Token(Token.Kind.KEYWORD, "}")) => (id, toks) |
|
140 |
case (Token(Token.Kind.IDENT, id) :: Token(Token.Kind.KEYWORD, "(") :: toks, |
|
141 |
Token(Token.Kind.KEYWORD, ")")) => (id, toks) |
|
142 |
case _ => ("", Nil) |
|
58523 | 143 |
} |
58526 | 144 |
case _ => ("", Nil) |
145 |
} |
|
146 |
override def kind: String = content._1 |
|
147 |
def content_tokens: List[Token] = content._2 |
|
148 |
||
149 |
def is_wellformed: Boolean = kind != "" |
|
150 |
||
151 |
def name: String = |
|
152 |
content_tokens match { |
|
153 |
case Token(Token.Kind.IDENT, name) :: _ if is_wellformed => name |
|
58523 | 154 |
case _ => "" |
155 |
} |
|
156 |
} |
|
157 |
||
158 |
||
159 |
||
160 |
/** parsing **/ |
|
161 |
||
162 |
// context of partial line-oriented scans |
|
163 |
abstract class Line_Context |
|
164 |
case class Delimited(quoted: Boolean, depth: Int) extends Line_Context |
|
165 |
val Finished = Delimited(false, 0) |
|
166 |
||
167 |
private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source) |
|
168 |
private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source) |
|
169 |
||
170 |
||
171 |
// See also http://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web |
|
172 |
// module @<Scan for and process a \.{.bib} command or database entry@>. |
|
173 |
||
174 |
object Parsers extends RegexParsers |
|
175 |
{ |
|
176 |
/* white space and comments */ |
|
177 |
||
178 |
override val whiteSpace = "".r |
|
179 |
||
180 |
private val space = """[ \t\n\r]+""".r ^^ token(Token.Kind.SPACE) |
|
58527
4b190c763097
strict spaces for item_start: despite actual bibtex syntax, but in accordance to bibtex modes in Emacs and jEdit;
wenzelm
parents:
58526
diff
changeset
|
181 |
private val strict_space = """[ \t]+""".r ^^ token(Token.Kind.SPACE) |
58523 | 182 |
|
183 |
private val ignored = |
|
184 |
rep1("""(?mi)([^@]+|@[ \t\n\r]*comment)""".r) ^^ { case ss => Ignored(ss.mkString) } |
|
185 |
||
186 |
||
187 |
/* delimited string: outermost "..." or {...} and body with balanced {...} */ |
|
188 |
||
189 |
private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] = |
|
190 |
new Parser[(String, Delimited)] |
|
191 |
{ |
|
192 |
require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0) |
|
193 |
||
194 |
def apply(in: Input) = |
|
195 |
{ |
|
196 |
val start = in.offset |
|
197 |
val end = in.source.length |
|
198 |
||
199 |
var i = start |
|
200 |
var q = delim.quoted |
|
201 |
var d = delim.depth |
|
202 |
var finished = false |
|
203 |
while (!finished && i < end) { |
|
204 |
val c = in.source.charAt(i) |
|
205 |
if (c == '"' && d == 0) { i += 1; d = 1; q = true } |
|
206 |
else if (c == '"' && d == 1) { i += 1; d = 0; q = false; finished = true } |
|
207 |
else if (c == '{') { i += 1; d += 1 } |
|
208 |
else if (c == '}' && d > 0) { i += 1; d -= 1; if (d == 0) finished = true } |
|
209 |
else if (d > 0) i += 1 |
|
210 |
else finished = true |
|
211 |
} |
|
212 |
if (i == start) Failure("bad input", in) |
|
213 |
else |
|
214 |
Success((in.source.subSequence(start, i).toString, |
|
215 |
Delimited(q, d)), in.drop(i - start)) |
|
216 |
} |
|
217 |
}.named("delimited_depth") |
|
218 |
||
219 |
private def delimited: Parser[String] = |
|
220 |
delimited_depth(Finished) ^? { case (x, delim) if delim == Finished => x } |
|
221 |
||
222 |
private def delimited_line(ctxt: Line_Context): Parser[(String, Line_Context)] = |
|
223 |
{ |
|
224 |
ctxt match { |
|
225 |
case delim: Delimited => delimited_depth(delim) |
|
226 |
case _ => failure("") |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
private val recover_delimited: Parser[String] = |
|
231 |
delimited_depth(Finished) ^^ (_._1) |
|
232 |
||
233 |
private val delimited_token = |
|
234 |
delimited ^^ token(Token.Kind.STRING) | |
|
235 |
recover_delimited ^^ token(Token.Kind.ERROR) |
|
236 |
||
237 |
||
238 |
/* other tokens */ |
|
239 |
||
240 |
private val at = "@" ^^ keyword |
|
241 |
private val left_brace = "{" ^^ keyword |
|
242 |
private val right_brace = "}" ^^ keyword |
|
243 |
private val left_paren = "(" ^^ keyword |
|
244 |
private val right_paren = ")" ^^ keyword |
|
245 |
||
246 |
private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT) |
|
247 |
||
248 |
private val ident = |
|
249 |
"""[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r ^^ token(Token.Kind.IDENT) |
|
250 |
||
251 |
||
252 |
/* chunks */ |
|
253 |
||
254 |
private val item_start = |
|
58527
4b190c763097
strict spaces for item_start: despite actual bibtex syntax, but in accordance to bibtex modes in Emacs and jEdit;
wenzelm
parents:
58526
diff
changeset
|
255 |
at ~ rep(strict_space) ~ ident ~ rep(strict_space) ^^ |
58523 | 256 |
{ case a ~ b ~ c ~ d => List(a) ::: b ::: List(c) ::: d } |
257 |
||
258 |
private val body_token = delimited_token | ("[=#,]".r ^^ keyword | (nat | (ident | space))) |
|
259 |
||
260 |
private val item = |
|
261 |
(item_start ~ left_brace ~ rep(body_token) ~ opt(right_brace) | |
|
262 |
item_start ~ left_paren ~ rep(body_token) ~ opt(right_paren)) ^^ |
|
263 |
{ case a ~ b ~ c ~ d => Item(a ::: List(b) ::: c ::: d.toList) } |
|
264 |
||
265 |
private val recover_item = "(?m)@[^@]+".r ^^ (s => Malformed(s)) |
|
266 |
||
267 |
val chunks: Parser[List[Chunk]] = rep(ignored | (item | recover_item)) |
|
268 |
} |
|
269 |
||
270 |
def parse(input: CharSequence): List[Chunk] = |
|
271 |
{ |
|
272 |
val in: Reader[Char] = new CharSequenceReader(input) |
|
273 |
Parsers.parseAll(Parsers.chunks, in) match { |
|
274 |
case Parsers.Success(result, _) => result |
|
58526 | 275 |
case _ => error("Unexpected failure to parse input:\n" + input.toString) |
58523 | 276 |
} |
277 |
} |
|
278 |
} |
|
279 |