67438
|
1 |
/* Title: Pure/General/comment.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Formal comments.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Comment
|
|
11 |
{
|
|
12 |
object Kind extends Enumeration
|
|
13 |
{
|
|
14 |
val COMMENT = Value("formal comment")
|
|
15 |
val CANCEL = Value("canceled text")
|
|
16 |
val LATEX = Value("embedded LaTeX")
|
|
17 |
}
|
|
18 |
|
|
19 |
lazy val symbols: Set[Symbol.Symbol] =
|
|
20 |
Set(Symbol.comment, Symbol.comment_decoded,
|
|
21 |
Symbol.cancel, Symbol.cancel_decoded,
|
|
22 |
Symbol.latex, Symbol.latex_decoded)
|
|
23 |
|
|
24 |
def symbols_blanks(sym: Symbol.Symbol): Boolean = Symbol.is_comment(sym)
|
|
25 |
|
|
26 |
def content(source: String): String =
|
|
27 |
{
|
|
28 |
def err(): Nothing = error("Malformed formal comment: " + quote(source))
|
|
29 |
|
|
30 |
Symbol.explode(source) match {
|
|
31 |
case sym :: rest if symbols_blanks(sym) =>
|
|
32 |
val body = if (symbols_blanks(sym)) rest.dropWhile(Symbol.is_blank) else rest
|
|
33 |
try { Scan.Parsers.cartouche_content(body.mkString) }
|
|
34 |
catch { case ERROR(_) => err() }
|
|
35 |
case _ => err()
|
|
36 |
}
|
|
37 |
}
|
|
38 |
|
|
39 |
trait Parsers extends Scan.Parsers
|
|
40 |
{
|
|
41 |
def comment_prefix: Parser[String] =
|
|
42 |
one(symbols_blanks) ~ many(Symbol.is_blank) ^^ { case a ~ b => a + b.mkString } |
|
|
43 |
one(symbols)
|
|
44 |
|
|
45 |
def comment_cartouche: Parser[String] =
|
|
46 |
comment_prefix ~ cartouche ^^ { case a ~ b => a + b }
|
|
47 |
|
|
48 |
def comment_cartouche_line(ctxt: Scan.Line_Context): Parser[(String, Scan.Line_Context)] =
|
|
49 |
{
|
|
50 |
def cartouche_context(d: Int): Scan.Line_Context =
|
|
51 |
if (d == 0) Scan.Finished else Scan.Cartouche_Comment(d)
|
|
52 |
|
|
53 |
ctxt match {
|
|
54 |
case Scan.Finished =>
|
|
55 |
comment_prefix ~ opt(cartouche_depth(0)) ^^ {
|
|
56 |
case a ~ None => (a, Scan.Comment_Prefix(a))
|
|
57 |
case a ~ Some((c, d)) => (a + c, cartouche_context(d)) }
|
|
58 |
case Scan.Comment_Prefix(a) if symbols_blanks(a) =>
|
|
59 |
many1(Symbol.is_blank) ~ opt(cartouche_depth(0)) ^^ {
|
|
60 |
case b ~ None => (b.mkString, Scan.Comment_Prefix(a))
|
|
61 |
case b ~ Some((c, d)) => (b.mkString + c, cartouche_context(d)) } |
|
|
62 |
cartouche_depth(0) ^^ { case (c, d) => (c, cartouche_context(d)) }
|
|
63 |
case Scan.Comment_Prefix(_) =>
|
|
64 |
cartouche_depth(0) ^^ { case (c, d) => (c, cartouche_context(d)) }
|
|
65 |
case Scan.Cartouche_Comment(depth) =>
|
|
66 |
cartouche_depth(depth) ^^ { case (c, d) => (c, cartouche_context(d)) }
|
|
67 |
case _ => failure("")
|
|
68 |
}
|
|
69 |
}
|
|
70 |
}
|
|
71 |
}
|