author | wenzelm |
Mon, 01 Mar 2021 22:22:12 +0100 | |
changeset 73340 | 0ffcad1f6130 |
parent 67132 | 336831647779 |
child 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
59720 | 1 |
/* Title: Pure/General/antiquote.scala |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
3 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
4 |
Antiquotations within plain text. |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
6 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
8 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
9 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
10 |
object Antiquote |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
11 |
{ |
61492 | 12 |
sealed abstract class Antiquote { def source: String } |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
13 |
case class Text(source: String) extends Antiquote |
61471 | 14 |
case class Control(source: String) extends Antiquote |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
15 |
case class Antiq(source: String) extends Antiquote |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
16 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
17 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
18 |
/* parsers */ |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
19 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
20 |
object Parsers extends Parsers |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
21 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
22 |
trait Parsers extends Scan.Parsers |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
23 |
{ |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
24 |
private val txt: Parser[String] = |
61491
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents:
61481
diff
changeset
|
25 |
rep1(many1(s => !Symbol.is_control(s) && !Symbol.is_open(s) && s != "@") | |
61471 | 26 |
"@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString) |
27 |
||
28 |
val control: Parser[String] = |
|
61595 | 29 |
opt(one(Symbol.is_control)) ~ cartouche ^^ { case Some(x) ~ y => x + y case None ~ x => x } | |
30 |
one(Symbol.is_control) |
|
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
31 |
|
55512 | 32 |
val antiq_other: Parser[String] = |
33 |
many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s)) |
|
34 |
||
35 |
private val antiq_body: Parser[String] = |
|
36 |
quoted("\"") | (quoted("`") | (cartouche | antiq_other)) |
|
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
37 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
38 |
val antiq: Parser[String] = |
55512 | 39 |
"@{" ~ rep(antiq_body) ~ "}" ^^ { case x ~ y ~ z => x + y.mkString + z } |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
40 |
|
55512 | 41 |
val antiquote: Parser[Antiquote] = |
61481 | 42 |
txt ^^ (x => Text(x)) | (control ^^ (x => Control(x)) | antiq ^^ (x => Antiq(x))) |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
43 |
} |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
44 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
45 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
46 |
/* read */ |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
47 |
|
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
48 |
def read(input: CharSequence): List[Antiquote] = |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
49 |
{ |
64824 | 50 |
Parsers.parseAll(Parsers.rep(Parsers.antiquote), Scan.char_reader(input)) match { |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
51 |
case Parsers.Success(xs, _) => xs |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
52 |
case Parsers.NoSuccess(_, next) => |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
53 |
error("Malformed quotation/antiquotation source" + |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
54 |
Position.here(Position.Line(next.pos.line))) |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
55 |
} |
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
56 |
} |
67132 | 57 |
|
58 |
def read_antiq_body(input: CharSequence): Option[String] = |
|
59 |
{ |
|
60 |
read(input) match { |
|
61 |
case List(Antiq(source)) => Some(source.substring(2, source.length - 1)) |
|
62 |
case _ => None |
|
63 |
} |
|
64 |
} |
|
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff
changeset
|
65 |
} |