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