| author | wenzelm | 
| Sat, 30 Nov 2024 22:33:21 +0100 | |
| changeset 81519 | cdc43c0fdbfc | 
| parent 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))) | 
| 77011 
3e48f8c6afc9
parse citations from raw source, without formal context;
 wenzelm parents: 
75420diff
changeset | 41 | |
| 
3e48f8c6afc9
parse citations from raw source, without formal context;
 wenzelm parents: 
75420diff
changeset | 42 | def antiquote_special(special: Symbol.Symbol => Boolean): Parser[Antiquote] = | 
| 
3e48f8c6afc9
parse citations from raw source, without formal context;
 wenzelm parents: 
75420diff
changeset | 43 | many1(s => !special(s)) ^^ Text.apply | | 
| 
3e48f8c6afc9
parse citations from raw source, without formal context;
 wenzelm parents: 
75420diff
changeset | 44 | one(special) ~ opt(cartouche) ^^ | 
| 
3e48f8c6afc9
parse citations from raw source, without formal context;
 wenzelm parents: 
75420diff
changeset | 45 |         { case x ~ Some(y) => Control(x + y) case x ~ None => Control(x) }
 | 
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 46 | } | 
| 
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 | |
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 49 | /* read */ | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 50 | |
| 75393 | 51 |   def read(input: CharSequence): List[Antiquote] = {
 | 
| 75420 | 52 | val result = Parsers.parseAll(Parsers.rep(Parsers.antiquote), Scan.char_reader(input)) | 
| 53 |     (result : @unchecked) match {
 | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 54 | case Parsers.Success(xs, _) => xs | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 55 | case Parsers.NoSuccess(_, next) => | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 56 |         error("Malformed quotation/antiquotation source" +
 | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 57 | Position.here(Position.Line(next.pos.line))) | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 58 | } | 
| 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 59 | } | 
| 67132 | 60 | |
| 75393 | 61 |   def read_antiq_body(input: CharSequence): Option[String] = {
 | 
| 67132 | 62 |     read(input) match {
 | 
| 63 | case List(Antiq(source)) => Some(source.substring(2, source.length - 1)) | |
| 64 | case _ => None | |
| 65 | } | |
| 66 | } | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 67 | } |