src/Pure/General/antiquote.scala
author wenzelm
Mon, 18 Aug 2014 12:17:31 +0200
changeset 57978 8f4a332500e4
parent 55512 75c68e05f9ea
child 59720 f893472fff31
permissions -rw-r--r--
Added tag Isabelle2014-RC4 for changeset 113b43b84412
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/ML/antiquote.scala
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
{
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    15
  sealed abstract class Antiquote
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    16
  case class Text(source: String) extends Antiquote
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    17
  case class Antiq(source: String) extends Antiquote
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
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    20
  /* 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
  object Parsers extends 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
  trait Parsers extends Scan.Parsers
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    25
  {
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    26
    private val txt: Parser[String] =
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    27
      rep1("@" ~ guard(one(s => s != "{")) | many1(s => s != "@")) ^^ (x => x.mkString)
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    28
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    29
    val antiq_other: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    30
      many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s))
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    31
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    32
    private val antiq_body: Parser[String] =
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    33
      quoted("\"") | (quoted("`") | (cartouche | antiq_other))
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    34
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    35
    val antiq: Parser[String] =
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    36
      "@{" ~ 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
    37
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    38
    val antiquote: Parser[Antiquote] =
55511
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    39
      antiq ^^ (x => Antiq(x)) | txt ^^ (x => Text(x))
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
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
  /* read */
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
  def read(input: CharSequence): List[Antiquote] =
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    46
  {
55512
75c68e05f9ea support ML antiquotations in Scala;
wenzelm
parents: 55511
diff changeset
    47
    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
    48
      case Parsers.Success(xs, _) => xs
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    49
      case Parsers.NoSuccess(_, next) =>
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    50
        error("Malformed quotation/antiquotation source" +
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    51
          Position.here(Position.Line(next.pos.line)))
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    52
    }
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
}
984e210d412e antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
diff changeset
    55