author | wenzelm |
Wed, 21 Jun 2017 14:06:16 +0200 | |
changeset 66150 | c2e19b9e1398 |
parent 66118 | 03dd799fe042 |
child 66152 | 18e1aba549f6 |
permissions | -rw-r--r-- |
58523 | 1 |
/* Title: Pure/Tools/bibtex.scala |
2 |
Author: Makarius |
|
3 |
||
58544
340f130b3d38
bibtex support in ML: document antiquotation @{cite} with markup;
wenzelm
parents:
58543
diff
changeset
|
4 |
BibTeX support. |
58523 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
58528 | 10 |
import scala.collection.mutable |
58523 | 11 |
import scala.util.parsing.combinator.RegexParsers |
64824 | 12 |
import scala.util.parsing.input.Reader |
58523 | 13 |
|
14 |
||
15 |
object Bibtex |
|
16 |
{ |
|
64828 | 17 |
/** document model **/ |
18 |
||
19 |
def check_name(name: String): Boolean = name.endsWith(".bib") |
|
20 |
def check_name(name: Document.Node.Name): Boolean = check_name(name.node) |
|
21 |
||
66150 | 22 |
def entries(text: String): List[Text.Info[String]] = |
64828 | 23 |
{ |
64831 | 24 |
val result = new mutable.ListBuffer[Text.Info[String]] |
64828 | 25 |
var offset = 0 |
26 |
for (chunk <- Bibtex.parse(text)) { |
|
64831 | 27 |
val end_offset = offset + chunk.source.length |
28 |
if (chunk.name != "" && !chunk.is_command) |
|
29 |
result += Text.Info(Text.Range(offset, end_offset), chunk.name) |
|
30 |
offset = end_offset |
|
64828 | 31 |
} |
32 |
result.toList |
|
33 |
} |
|
34 |
||
66150 | 35 |
def entries_iterator[A, B <: Document.Model](models: Map[A, B]) |
36 |
: Iterator[Text.Info[(String, B)]] = |
|
37 |
{ |
|
38 |
for { |
|
39 |
(_, model) <- models.iterator |
|
40 |
info <- model.bibtex_entries.iterator |
|
41 |
} yield info.map((_, model)) |
|
42 |
} |
|
43 |
||
64828 | 44 |
|
45 |
||
58523 | 46 |
/** content **/ |
47 |
||
58529 | 48 |
private val months = List( |
58523 | 49 |
"jan", |
50 |
"feb", |
|
51 |
"mar", |
|
52 |
"apr", |
|
53 |
"may", |
|
54 |
"jun", |
|
55 |
"jul", |
|
56 |
"aug", |
|
57 |
"sep", |
|
58 |
"oct", |
|
59 |
"nov", |
|
60 |
"dec") |
|
58529 | 61 |
def is_month(s: String): Boolean = months.contains(s.toLowerCase) |
58523 | 62 |
|
58529 | 63 |
private val commands = List("preamble", "string") |
64 |
def is_command(s: String): Boolean = commands.contains(s.toLowerCase) |
|
58523 | 65 |
|
58524 | 66 |
sealed case class Entry( |
58526 | 67 |
kind: String, |
58523 | 68 |
required: List[String], |
69 |
optional_crossref: List[String], |
|
58529 | 70 |
optional_other: List[String]) |
58524 | 71 |
{ |
58533 | 72 |
def is_required(s: String): Boolean = required.contains(s.toLowerCase) |
58529 | 73 |
def is_optional(s: String): Boolean = |
58533 | 74 |
optional_crossref.contains(s.toLowerCase) || optional_other.contains(s.toLowerCase) |
58529 | 75 |
|
76 |
def fields: List[String] = required ::: optional_crossref ::: optional_other |
|
58524 | 77 |
def template: String = |
58526 | 78 |
"@" + kind + "{,\n" + fields.map(x => " " + x + " = {},\n").mkString + "}\n" |
58524 | 79 |
} |
58523 | 80 |
|
66150 | 81 |
val known_entries: List[Entry] = |
58524 | 82 |
List( |
83 |
Entry("Article", |
|
84 |
List("author", "title"), |
|
85 |
List("journal", "year"), |
|
86 |
List("volume", "number", "pages", "month", "note")), |
|
87 |
Entry("InProceedings", |
|
88 |
List("author", "title"), |
|
89 |
List("booktitle", "year"), |
|
90 |
List("editor", "volume", "number", "series", "pages", "month", "address", |
|
91 |
"organization", "publisher", "note")), |
|
92 |
Entry("InCollection", |
|
93 |
List("author", "title", "booktitle"), |
|
94 |
List("publisher", "year"), |
|
95 |
List("editor", "volume", "number", "series", "type", "chapter", "pages", |
|
96 |
"edition", "month", "address", "note")), |
|
97 |
Entry("InBook", |
|
98 |
List("author", "editor", "title", "chapter"), |
|
99 |
List("publisher", "year"), |
|
100 |
List("volume", "number", "series", "type", "address", "edition", "month", "pages", "note")), |
|
101 |
Entry("Proceedings", |
|
102 |
List("title", "year"), |
|
103 |
List(), |
|
104 |
List("booktitle", "editor", "volume", "number", "series", "address", "month", |
|
105 |
"organization", "publisher", "note")), |
|
106 |
Entry("Book", |
|
107 |
List("author", "editor", "title"), |
|
108 |
List("publisher", "year"), |
|
109 |
List("volume", "number", "series", "address", "edition", "month", "note")), |
|
110 |
Entry("Booklet", |
|
111 |
List("title"), |
|
112 |
List(), |
|
113 |
List("author", "howpublished", "address", "month", "year", "note")), |
|
114 |
Entry("PhdThesis", |
|
115 |
List("author", "title", "school", "year"), |
|
116 |
List(), |
|
117 |
List("type", "address", "month", "note")), |
|
118 |
Entry("MastersThesis", |
|
119 |
List("author", "title", "school", "year"), |
|
120 |
List(), |
|
121 |
List("type", "address", "month", "note")), |
|
122 |
Entry("TechReport", |
|
123 |
List("author", "title", "institution", "year"), |
|
124 |
List(), |
|
125 |
List("type", "number", "address", "month", "note")), |
|
126 |
Entry("Manual", |
|
127 |
List("title"), |
|
128 |
List(), |
|
129 |
List("author", "organization", "address", "edition", "month", "year", "note")), |
|
130 |
Entry("Unpublished", |
|
131 |
List("author", "title", "note"), |
|
132 |
List(), |
|
133 |
List("month", "year")), |
|
134 |
Entry("Misc", |
|
135 |
List(), |
|
136 |
List(), |
|
137 |
List("author", "title", "howpublished", "month", "year", "note"))) |
|
58523 | 138 |
|
58529 | 139 |
def get_entry(kind: String): Option[Entry] = |
66150 | 140 |
known_entries.find(entry => entry.kind.toLowerCase == kind.toLowerCase) |
58529 | 141 |
|
58530 | 142 |
def is_entry(kind: String): Boolean = get_entry(kind).isDefined |
143 |
||
58523 | 144 |
|
145 |
||
146 |
/** tokens and chunks **/ |
|
147 |
||
148 |
object Token |
|
149 |
{ |
|
150 |
object Kind extends Enumeration |
|
151 |
{ |
|
58529 | 152 |
val COMMAND = Value("command") |
153 |
val ENTRY = Value("entry") |
|
58523 | 154 |
val KEYWORD = Value("keyword") |
155 |
val NAT = Value("natural number") |
|
58529 | 156 |
val STRING = Value("string") |
58531 | 157 |
val NAME = Value("name") |
58523 | 158 |
val IDENT = Value("identifier") |
58535 | 159 |
val SPACE = Value("white space") |
160 |
val COMMENT = Value("ignored text") |
|
58523 | 161 |
val ERROR = Value("bad input") |
162 |
} |
|
163 |
} |
|
164 |
||
60215 | 165 |
sealed case class Token(kind: Token.Kind.Value, source: String) |
58523 | 166 |
{ |
58530 | 167 |
def is_kind: Boolean = |
168 |
kind == Token.Kind.COMMAND || |
|
169 |
kind == Token.Kind.ENTRY || |
|
170 |
kind == Token.Kind.IDENT |
|
58531 | 171 |
def is_name: Boolean = |
172 |
kind == Token.Kind.NAME || |
|
173 |
kind == Token.Kind.IDENT |
|
58535 | 174 |
def is_ignored: Boolean = |
175 |
kind == Token.Kind.SPACE || |
|
176 |
kind == Token.Kind.COMMENT |
|
177 |
def is_malformed: Boolean = kind == |
|
178 |
Token.Kind.ERROR |
|
58523 | 179 |
} |
180 |
||
58530 | 181 |
case class Chunk(kind: String, tokens: List[Token]) |
58523 | 182 |
{ |
58529 | 183 |
val source = tokens.map(_.source).mkString |
58526 | 184 |
|
58530 | 185 |
private val content: Option[List[Token]] = |
58523 | 186 |
tokens match { |
59319 | 187 |
case Token(Token.Kind.KEYWORD, "@") :: body if body.nonEmpty => |
58529 | 188 |
(body.init.filterNot(_.is_ignored), body.last) match { |
58530 | 189 |
case (tok :: Token(Token.Kind.KEYWORD, "{") :: toks, Token(Token.Kind.KEYWORD, "}")) |
190 |
if tok.is_kind => Some(toks) |
|
191 |
||
192 |
case (tok :: Token(Token.Kind.KEYWORD, "(") :: toks, Token(Token.Kind.KEYWORD, ")")) |
|
193 |
if tok.is_kind => Some(toks) |
|
194 |
||
58528 | 195 |
case _ => None |
58523 | 196 |
} |
58528 | 197 |
case _ => None |
58526 | 198 |
} |
199 |
||
200 |
def name: String = |
|
58530 | 201 |
content match { |
58531 | 202 |
case Some(tok :: _) if tok.is_name => tok.source |
58523 | 203 |
case _ => "" |
204 |
} |
|
58530 | 205 |
|
206 |
def is_ignored: Boolean = kind == "" && tokens.forall(_.is_ignored) |
|
207 |
def is_malformed: Boolean = kind == "" || tokens.exists(_.is_malformed) |
|
58543 | 208 |
def is_command: Boolean = Bibtex.is_command(kind) && name != "" && content.isDefined |
209 |
def is_entry: Boolean = Bibtex.is_entry(kind) && name != "" && content.isDefined |
|
58523 | 210 |
} |
211 |
||
212 |
||
213 |
||
214 |
/** parsing **/ |
|
215 |
||
216 |
// context of partial line-oriented scans |
|
217 |
abstract class Line_Context |
|
58589 | 218 |
case object Ignored extends Line_Context |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
219 |
case object At extends Line_Context |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
220 |
case class Item_Start(kind: String) extends Line_Context |
58591 | 221 |
case class Item_Open(kind: String, end: String) extends Line_Context |
222 |
case class Item(kind: String, end: String, delim: Delimited) extends Line_Context |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
223 |
|
58528 | 224 |
case class Delimited(quoted: Boolean, depth: Int) |
225 |
val Closed = Delimited(false, 0) |
|
58523 | 226 |
|
227 |
private def token(kind: Token.Kind.Value)(source: String): Token = Token(kind, source) |
|
228 |
private def keyword(source: String): Token = Token(Token.Kind.KEYWORD, source) |
|
229 |
||
230 |
||
231 |
// See also http://ctan.org/tex-archive/biblio/bibtex/base/bibtex.web |
|
232 |
// module @<Scan for and process a \.{.bib} command or database entry@>. |
|
233 |
||
234 |
object Parsers extends RegexParsers |
|
235 |
{ |
|
236 |
/* white space and comments */ |
|
237 |
||
238 |
override val whiteSpace = "".r |
|
239 |
||
58535 | 240 |
private val space = """[ \t\n\r]+""".r ^^ token(Token.Kind.SPACE) |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
241 |
private val spaces = rep(space) |
58523 | 242 |
|
58528 | 243 |
|
58535 | 244 |
/* ignored text */ |
58528 | 245 |
|
246 |
private val ignored: Parser[Chunk] = |
|
58609 | 247 |
rep1("""(?i)([^@]+|@[ \t]*comment)""".r) ^^ { |
58535 | 248 |
case ss => Chunk("", List(Token(Token.Kind.COMMENT, ss.mkString))) } |
58523 | 249 |
|
58536
402a8e8107a7
more total chunk_line: recovery via ignored_line;
wenzelm
parents:
58535
diff
changeset
|
250 |
private def ignored_line: Parser[(Chunk, Line_Context)] = |
58589 | 251 |
ignored ^^ { case a => (a, Ignored) } |
58536
402a8e8107a7
more total chunk_line: recovery via ignored_line;
wenzelm
parents:
58535
diff
changeset
|
252 |
|
58523 | 253 |
|
254 |
/* delimited string: outermost "..." or {...} and body with balanced {...} */ |
|
255 |
||
58534 | 256 |
// see also bibtex.web: scan_a_field_token_and_eat_white, scan_balanced_braces |
58523 | 257 |
private def delimited_depth(delim: Delimited): Parser[(String, Delimited)] = |
258 |
new Parser[(String, Delimited)] |
|
259 |
{ |
|
260 |
require(if (delim.quoted) delim.depth > 0 else delim.depth >= 0) |
|
261 |
||
262 |
def apply(in: Input) = |
|
263 |
{ |
|
264 |
val start = in.offset |
|
265 |
val end = in.source.length |
|
266 |
||
267 |
var i = start |
|
268 |
var q = delim.quoted |
|
269 |
var d = delim.depth |
|
270 |
var finished = false |
|
271 |
while (!finished && i < end) { |
|
272 |
val c = in.source.charAt(i) |
|
58534 | 273 |
|
58523 | 274 |
if (c == '"' && d == 0) { i += 1; d = 1; q = true } |
58532 | 275 |
else if (c == '"' && d == 1 && q) { |
276 |
i += 1; d = 0; q = false; finished = true |
|
277 |
} |
|
58523 | 278 |
else if (c == '{') { i += 1; d += 1 } |
58534 | 279 |
else if (c == '}') { |
280 |
if (d == 1 && !q || d > 1) { i += 1; d -= 1; if (d == 0) finished = true } |
|
281 |
else {i = start; finished = true } |
|
58532 | 282 |
} |
58523 | 283 |
else if (d > 0) i += 1 |
284 |
else finished = true |
|
285 |
} |
|
286 |
if (i == start) Failure("bad input", in) |
|
58528 | 287 |
else { |
288 |
val s = in.source.subSequence(start, i).toString |
|
289 |
Success((s, Delimited(q, d)), in.drop(i - start)) |
|
290 |
} |
|
58523 | 291 |
} |
292 |
}.named("delimited_depth") |
|
293 |
||
58528 | 294 |
private def delimited: Parser[Token] = |
295 |
delimited_depth(Closed) ^? |
|
296 |
{ case (s, delim) if delim == Closed => Token(Token.Kind.STRING, s) } |
|
58523 | 297 |
|
58534 | 298 |
private def recover_delimited: Parser[Token] = |
58609 | 299 |
"""["{][^@]*""".r ^^ token(Token.Kind.ERROR) |
58534 | 300 |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
301 |
def delimited_line(ctxt: Item): Parser[(Chunk, Line_Context)] = |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
302 |
delimited_depth(ctxt.delim) ^^ { case (s, delim1) => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
303 |
(Chunk(ctxt.kind, List(Token(Token.Kind.STRING, s))), ctxt.copy(delim = delim1)) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
304 |
recover_delimited ^^ { case a => (Chunk(ctxt.kind, List(a)), Ignored) } |
58523 | 305 |
|
306 |
||
307 |
/* other tokens */ |
|
308 |
||
309 |
private val at = "@" ^^ keyword |
|
310 |
||
311 |
private val nat = "[0-9]+".r ^^ token(Token.Kind.NAT) |
|
312 |
||
58591 | 313 |
private val name = """[\x21-\x7f&&[^"#%'(),={}]]+""".r ^^ token(Token.Kind.NAME) |
314 |
||
58529 | 315 |
private val identifier = |
316 |
"""[\x21-\x7f&&[^"#%'(),={}0-9]][\x21-\x7f&&[^"#%'(),={}]]*""".r |
|
317 |
||
318 |
private val ident = identifier ^^ token(Token.Kind.IDENT) |
|
58523 | 319 |
|
58528 | 320 |
val other_token = "[=#,]".r ^^ keyword | (nat | (ident | space)) |
321 |
||
322 |
||
58591 | 323 |
/* body */ |
324 |
||
325 |
private val body = |
|
326 |
delimited | (recover_delimited | other_token) |
|
327 |
||
328 |
private def body_line(ctxt: Item) = |
|
329 |
if (ctxt.delim.depth > 0) |
|
330 |
delimited_line(ctxt) |
|
331 |
else |
|
332 |
delimited_line(ctxt) | |
|
333 |
other_token ^^ { case a => (Chunk(ctxt.kind, List(a)), ctxt) } | |
|
334 |
ctxt.end ^^ { case a => (Chunk(ctxt.kind, List(keyword(a))), Ignored) } |
|
335 |
||
336 |
||
58530 | 337 |
/* items: command or entry */ |
58528 | 338 |
|
58530 | 339 |
private val item_kind = |
58529 | 340 |
identifier ^^ { case a => |
341 |
val kind = |
|
342 |
if (is_command(a)) Token.Kind.COMMAND |
|
58530 | 343 |
else if (is_entry(a)) Token.Kind.ENTRY |
58529 | 344 |
else Token.Kind.IDENT |
345 |
Token(kind, a) |
|
346 |
} |
|
347 |
||
58591 | 348 |
private val item_begin = |
349 |
"{" ^^ { case a => ("}", keyword(a)) } | |
|
350 |
"(" ^^ { case a => (")", keyword(a)) } |
|
351 |
||
352 |
private def item_name(kind: String) = |
|
353 |
kind.toLowerCase match { |
|
354 |
case "preamble" => failure("") |
|
355 |
case "string" => identifier ^^ token(Token.Kind.NAME) |
|
356 |
case _ => name |
|
357 |
} |
|
358 |
||
58531 | 359 |
private val item_start = |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
360 |
at ~ spaces ~ item_kind ~ spaces ^^ |
58528 | 361 |
{ case a ~ b ~ c ~ d => (c.source, List(a) ::: b ::: List(c) ::: d) } |
362 |
||
58530 | 363 |
private val item: Parser[Chunk] = |
58591 | 364 |
(item_start ~ item_begin ~ spaces) into |
365 |
{ case (kind, a) ~ ((end, b)) ~ c => |
|
366 |
opt(item_name(kind)) ~ rep(body) ~ opt(end ^^ keyword) ^^ { |
|
367 |
case d ~ e ~ f => Chunk(kind, a ::: List(b) ::: c ::: d.toList ::: e ::: f.toList) } } |
|
58528 | 368 |
|
58530 | 369 |
private val recover_item: Parser[Chunk] = |
58609 | 370 |
at ~ "[^@]*".r ^^ { case a ~ b => Chunk("", List(a, Token(Token.Kind.ERROR, b))) } |
58528 | 371 |
|
58591 | 372 |
|
373 |
/* chunks */ |
|
58523 | 374 |
|
58528 | 375 |
val chunk: Parser[Chunk] = ignored | (item | recover_item) |
58523 | 376 |
|
58528 | 377 |
def chunk_line(ctxt: Line_Context): Parser[(Chunk, Line_Context)] = |
58530 | 378 |
{ |
379 |
ctxt match { |
|
58589 | 380 |
case Ignored => |
58538
299b82d12d53
proper treatment of @comment (amending 402a8e8107a7);
wenzelm
parents:
58536
diff
changeset
|
381 |
ignored_line | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
382 |
at ^^ { case a => (Chunk("", List(a)), At) } |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
383 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
384 |
case At => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
385 |
space ^^ { case a => (Chunk("", List(a)), ctxt) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
386 |
item_kind ^^ { case a => (Chunk(a.source, List(a)), Item_Start(a.source)) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
387 |
recover_item ^^ { case a => (a, Ignored) } | |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
388 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
389 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
390 |
case Item_Start(kind) => |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
391 |
space ^^ { case a => (Chunk(kind, List(a)), ctxt) } | |
58591 | 392 |
item_begin ^^ { case (end, a) => (Chunk(kind, List(a)), Item_Open(kind, end)) } | |
58596 | 393 |
recover_item ^^ { case a => (a, Ignored) } | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
394 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
395 |
|
58591 | 396 |
case Item_Open(kind, end) => |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
397 |
space ^^ { case a => (Chunk(kind, List(a)), ctxt) } | |
58591 | 398 |
item_name(kind) ^^ { case a => (Chunk(kind, List(a)), Item(kind, end, Closed)) } | |
399 |
body_line(Item(kind, end, Closed)) | |
|
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
400 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
401 |
|
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
402 |
case item_ctxt: Item => |
58591 | 403 |
body_line(item_ctxt) | |
58590
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
404 |
ignored_line |
472b9fbcc7f0
back to liberal spaces of official syntax (in contrast to 4b190c763097), e.g. relevant for easychair.bib;
wenzelm
parents:
58589
diff
changeset
|
405 |
|
58530 | 406 |
case _ => failure("") |
407 |
} |
|
408 |
} |
|
58528 | 409 |
} |
58523 | 410 |
|
411 |
||
58528 | 412 |
/* parse */ |
58523 | 413 |
|
414 |
def parse(input: CharSequence): List[Chunk] = |
|
64824 | 415 |
Parsers.parseAll(Parsers.rep(Parsers.chunk), Scan.char_reader(input)) match { |
58523 | 416 |
case Parsers.Success(result, _) => result |
58526 | 417 |
case _ => error("Unexpected failure to parse input:\n" + input.toString) |
58523 | 418 |
} |
58528 | 419 |
|
420 |
def parse_line(input: CharSequence, context: Line_Context): (List[Chunk], Line_Context) = |
|
421 |
{ |
|
64824 | 422 |
var in: Reader[Char] = Scan.char_reader(input) |
58528 | 423 |
val chunks = new mutable.ListBuffer[Chunk] |
424 |
var ctxt = context |
|
425 |
while (!in.atEnd) { |
|
426 |
Parsers.parse(Parsers.chunk_line(ctxt), in) match { |
|
60215 | 427 |
case Parsers.Success((x, c), rest) => chunks += x; ctxt = c; in = rest |
58528 | 428 |
case Parsers.NoSuccess(_, rest) => |
429 |
error("Unepected failure to parse input:\n" + rest.source.toString) |
|
430 |
} |
|
431 |
} |
|
432 |
(chunks.toList, ctxt) |
|
433 |
} |
|
66118 | 434 |
|
435 |
||
436 |
/* completion */ |
|
437 |
||
438 |
def completion(history: Completion.History, rendering: Rendering, caret: Text.Offset, |
|
439 |
complete: String => List[String]): Option[Completion.Result] = |
|
440 |
{ |
|
441 |
for { |
|
442 |
Text.Info(r, name) <- rendering.citation(rendering.before_caret_range(caret)) |
|
443 |
name1 <- Completion.clean_name(name) |
|
444 |
||
445 |
original <- rendering.model.try_get_text(r) |
|
446 |
original1 <- Completion.clean_name(Library.perhaps_unquote(original)) |
|
447 |
||
448 |
entries = complete(name1).filter(_ != original1) |
|
449 |
if entries.nonEmpty |
|
450 |
||
451 |
items = |
|
452 |
entries.sorted.map({ |
|
453 |
case entry => |
|
454 |
val full_name = Long_Name.qualify(Markup.CITATION, entry) |
|
455 |
val description = List(entry, "(BibTeX entry)") |
|
456 |
val replacement = quote(entry) |
|
457 |
Completion.Item(r, original, full_name, description, replacement, 0, false) |
|
458 |
}).sorted(history.ordering).take(rendering.options.int("completion_limit")) |
|
459 |
} yield Completion.Result(r, original, false, items) |
|
460 |
} |
|
58523 | 461 |
} |