| author | wenzelm | 
| Wed, 30 Dec 2015 20:12:26 +0100 | |
| changeset 61992 | 6d02bb8b5fe1 | 
| parent 61595 | 3591274c607e | 
| child 64824 | 330ec9bc4b75 | 
| 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 | |
| 
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 | 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 | 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: 
61481diff
changeset | 28 | rep1(many1(s => !Symbol.is_control(s) && !Symbol.is_open(s) && s != "@") | | 
| 61471 | 29 |         "@" <~ guard(opt_term(one(s => s != "{")))) ^^ (x => x.mkString)
 | 
| 30 | ||
| 31 | val control: Parser[String] = | |
| 61595 | 32 |       opt(one(Symbol.is_control)) ~ cartouche ^^ { case Some(x) ~ y => x + y case None ~ x => x } |
 | 
| 33 | one(Symbol.is_control) | |
| 55511 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 wenzelm parents: diff
changeset | 34 | |
| 55512 | 35 | val antiq_other: Parser[String] = | 
| 36 | many1(s => s != "\"" && s != "`" && s != "}" && !Symbol.is_open(s) && !Symbol.is_close(s)) | |
| 37 | ||
| 38 | private val antiq_body: Parser[String] = | |
| 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 | 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 | 44 | val antiquote: Parser[Antiquote] = | 
| 61481 | 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 | 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 |