src/Pure/General/antiquote.scala
author wenzelm
Sat, 24 Jun 2017 21:23:48 +0200
changeset 66191 d91108ba9474
parent 65897 94b0da1b242e
child 67132 336831647779
permissions -rw-r--r--
back to scala-2.12.2, after a41435469559;
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
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
3480725c71d2 added isabelle update_cartouches option -t;
wenzelm
parents: 61491
diff changeset
    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
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    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
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    26
        "@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString)
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    27
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    28
    val control: Parser[String] =
61595
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61492
diff changeset
    29
      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
    30
      one(Symbol.is_control)
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    31
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    32
    val antiq_other: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    33
      many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s))
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    34
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    35
    private val antiq_body: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    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
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    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
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    41
    val antiquote: Parser[Antiquote] =
61481
wenzelm
parents: 61471
diff changeset
    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
330ec9bc4b75 tuned signature;
wenzelm
parents: 61595
diff changeset
    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
  }
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    57
}