| author | wenzelm | 
| Thu, 07 Jul 2022 16:37:56 +0200 | |
| changeset 75656 | 7900336c82b6 | 
| parent 75420 | 73a2f3fe0e8c | 
| child 77011 | 3e48f8c6afc9 | 
| 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 | |
| 75393 | 10 | object Antiquote {
 | 
| 61492 | 11 |   sealed abstract class Antiquote { def source: String }
 | 
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 12 | case class Text(source: String) extends Antiquote | 
| 61471 | 13 | case class Control(source: String) extends Antiquote | 
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 14 | case class Antiq(source: String) extends Antiquote | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 15 | |
| 
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 | /* parsers */ | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 18 | |
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 19 | object Parsers extends Parsers | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 20 | |
| 75393 | 21 |   trait Parsers extends Scan.Parsers {
 | 
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 22 | private val txt: Parser[String] = | 
| 61491 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 wenzelm parents: 
61481diff
changeset | 23 | rep1(many1(s => !Symbol.is_control(s) && !Symbol.is_open(s) && s != "@") | | 
| 61471 | 24 |         "@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString)
 | 
| 25 | ||
| 26 | val control: Parser[String] = | |
| 61595 | 27 |       opt(one(Symbol.is_control)) ~ cartouche ^^ { case Some(x) ~ y => x + y case None ~ x => x } |
 | 
| 28 | one(Symbol.is_control) | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 29 | |
| 55512 | 30 | val antiq_other: Parser[String] = | 
| 31 | many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s)) | |
| 32 | ||
| 33 | private val antiq_body: Parser[String] = | |
| 34 |       quoted("\"") | (quoted("`") | (cartouche | antiq_other))
 | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 35 | |
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 36 | val antiq: Parser[String] = | 
| 55512 | 37 |       "@{" ~ 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 | 38 | |
| 55512 | 39 | val antiquote: Parser[Antiquote] = | 
| 61481 | 40 | 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 | 41 | } | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 42 | |
| 
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 | /* read */ | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 45 | |
| 75393 | 46 |   def read(input: CharSequence): List[Antiquote] = {
 | 
| 75420 | 47 | val result = Parsers.parseAll(Parsers.rep(Parsers.antiquote), Scan.char_reader(input)) | 
| 48 |     (result : @unchecked) match {
 | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 49 | case Parsers.Success(xs, _) => xs | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 50 | case Parsers.NoSuccess(_, next) => | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 51 |         error("Malformed quotation/antiquotation source" +
 | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 52 | Position.here(Position.Line(next.pos.line))) | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 53 | } | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 54 | } | 
| 67132 | 55 | |
| 75393 | 56 |   def read_antiq_body(input: CharSequence): Option[String] = {
 | 
| 67132 | 57 |     read(input) match {
 | 
| 58 | case List(Antiq(source)) => Some(source.substring(2, source.length - 1)) | |
| 59 | case _ => None | |
| 60 | } | |
| 61 | } | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 62 | } |