src/Pure/General/antiquote.scala
author wenzelm
Fri, 08 Apr 2022 16:26:48 +0200
changeset 75420 73a2f3fe0e8c
parent 75393 87ebf5a50283
child 77011 3e48f8c6afc9
permissions -rw-r--r--
tuned --- fewer warnings in scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59720
f893472fff31 proper headers;
wenzelm
parents: 55512
diff changeset
     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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 67132
diff changeset
    10
object Antiquote {
61492
3480725c71d2 added isabelle update_cartouches option -t;
wenzelm
parents: 61491
diff changeset
    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
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 67132
diff changeset
    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: 61481
diff changeset
    23
      rep1(many1(s => !Symbol.is_control(s) && !Symbol.is_open(s) && s != "@") |
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    24
        "@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString)
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    25
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    26
    val control: Parser[String] =
61595
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61492
diff changeset
    27
      opt(one(Symbol.is_control)) ~ cartouche ^^ { case Some(x) ~ y => x + y case None ~ x => x } |
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61492
diff changeset
    28
      one(Symbol.is_control)
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    29
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    30
    val antiq_other: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    31
      many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s))
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    32
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    33
    private val antiq_body: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    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
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    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
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    39
    val antiquote: Parser[Antiquote] =
61481
wenzelm
parents: 61471
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 67132
diff changeset
    46
  def read(input: CharSequence): List[Antiquote] = {
75420
73a2f3fe0e8c tuned --- fewer warnings in scala3;
wenzelm
parents: 75393
diff changeset
    47
    val result = Parsers.parseAll(Parsers.rep(Parsers.antiquote), Scan.char_reader(input))
73a2f3fe0e8c tuned --- fewer warnings in scala3;
wenzelm
parents: 75393
diff changeset
    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
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    55
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 67132
diff changeset
    56
  def read_antiq_body(input: CharSequence): Option[String] = {
67132
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    57
    read(input) match {
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    58
      case List(Antiq(source)) => Some(source.substring(2, source.length - 1))
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    59
      case _ => None
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    60
    }
336831647779 added action to make antiquoted cartouche;
wenzelm
parents: 66191
diff changeset
    61
  }
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    62
}