src/Pure/General/antiquote.scala
author wenzelm
Fri, 06 Nov 2015 23:31:50 +0100
changeset 61595 3591274c607e
parent 61492 3480725c71d2
child 64824 330ec9bc4b75
permissions -rw-r--r--
more formal treatment of control symbols;
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
import scala.util.parsing.input.CharSequenceReader
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    11
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    12
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    13
object Antiquote
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    14
{
61492
3480725c71d2 added isabelle update_cartouches option -t;
wenzelm
parents: 61491
diff changeset
    15
  sealed abstract class Antiquote { def source: String }
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    16
  case class Text(source: String) extends Antiquote
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    17
  case class Control(source: String) extends Antiquote
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    18
  case class Antiq(source: String) extends Antiquote
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
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    21
  /* parsers */
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    22
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    23
  object Parsers extends Parsers
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    24
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    25
  trait Parsers extends Scan.Parsers
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    26
  {
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    27
    private val txt: Parser[String] =
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61481
diff changeset
    28
      rep1(many1(s => !Symbol.is_control(s) && !Symbol.is_open(s) && s != "@") |
61471
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    29
        "@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString)
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    30
9d4c08af61b8 support control symbol antiquotations;
wenzelm
parents: 59720
diff changeset
    31
    val control: Parser[String] =
61595
3591274c607e more formal treatment of control symbols;
wenzelm
parents: 61492
diff changeset
    32
      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
    33
      one(Symbol.is_control)
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    34
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    35
    val antiq_other: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    36
      many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s))
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    37
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    38
    private val antiq_body: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    39
      quoted("\"") | (quoted("`") | (cartouche | antiq_other))
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    40
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    41
    val antiq: Parser[String] =
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    42
      "@{" ~ 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
    43
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    44
    val antiquote: Parser[Antiquote] =
61481
wenzelm
parents: 61471
diff changeset
    45
      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
    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
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    51
  def read(input: CharSequence): List[Antiquote] =
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    52
  {
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    53
    Parsers.parseAll(Parsers.rep(Parsers.antiquote), new CharSequenceReader(input)) 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
  }
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    60
}
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    61