src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 07 13:21:29 2012 +0200 (2012-08-07)
changeset 48706 e2b512024eab
parent 48671 951bc4c3ee17
child 48707 ba531af91148
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/Isar/outer_syntax.scala
     2     Author:     Makarius
     3 
     4 Isabelle/Isar outer syntax.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.util.parsing.input.{Reader, CharSequenceReader}
    11 import scala.collection.mutable
    12 
    13 
    14 object Outer_Syntax
    15 {
    16   def quote_string(str: String): String =
    17   {
    18     val result = new StringBuilder(str.length + 10)
    19     result += '"'
    20     for (s <- Symbol.iterator(str)) {
    21       if (s.length == 1) {
    22         val c = s(0)
    23         if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
    24           result += '\\'
    25           if (c < 10) result += '0'
    26           if (c < 100) result += '0'
    27           result ++= (c.asInstanceOf[Int].toString)
    28         }
    29         else result += c
    30       }
    31       else result ++= s
    32     }
    33     result += '"'
    34     result.toString
    35   }
    36 
    37   val empty: Outer_Syntax = new Outer_Syntax()
    38   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    39 }
    40 
    41 final class Outer_Syntax private(
    42   keywords: Map[String, String] = Map.empty,
    43   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    44   val completion: Completion = Completion.empty)
    45 {
    46   override def toString: String =
    47     (for ((name, kind) <- keywords) yield {
    48       if (kind == Keyword.MINOR) quote(name)
    49       else quote(name) + " :: " + quote(kind)
    50     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    51 
    52   def keyword_kind(name: String): Option[String] = keywords.get(name)
    53 
    54   def + (name: String, kind: String, replace: String): Outer_Syntax =
    55     new Outer_Syntax(
    56       keywords + (name -> kind),
    57       lexicon + name,
    58       if (Keyword.control(kind)) completion else completion + (name, replace))
    59 
    60   def + (name: String, kind: String): Outer_Syntax = this + (name, kind, name)
    61   def + (name: String): Outer_Syntax = this + (name, Keyword.MINOR)
    62 
    63   def add_keywords(header: Document.Node_Header): Outer_Syntax =
    64     header match {
    65       case Exn.Res(deps) =>
    66         (this /: deps.keywords) {
    67           case (syntax, ((name, Some((kind, _))))) => syntax + (name, kind)
    68           case (syntax, ((name, None))) => syntax + name
    69         }
    70       case Exn.Exn(_) => this
    71     }
    72 
    73   def is_command(name: String): Boolean =
    74     keyword_kind(name) match {
    75       case Some(kind) => kind != Keyword.MINOR
    76       case None => false
    77     }
    78 
    79   def heading_level(name: String): Option[Int] =
    80   {
    81     keyword_kind(name) match {
    82       case _ if name == "header" => Some(0)
    83       case Some(Keyword.THY_HEADING1) => Some(1)
    84       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
    85       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
    86       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
    87       case Some(kind) if Keyword.theory(kind) => Some(5)
    88       case _ => None
    89     }
    90   }
    91 
    92   def heading_level(command: Command): Option[Int] =
    93     heading_level(command.name)
    94 
    95 
    96   /* tokenize */
    97 
    98   def scan(input: Reader[Char]): List[Token] =
    99   {
   100     import lexicon._
   101 
   102     parseAll(rep(token(is_command)), input) match {
   103       case Success(tokens, _) => tokens
   104       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   105     }
   106   }
   107 
   108   def scan(input: CharSequence): List[Token] =
   109     scan(new CharSequenceReader(input))
   110 
   111   def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
   112   {
   113     import lexicon._
   114 
   115     var in: Reader[Char] = new CharSequenceReader(input)
   116     val toks = new mutable.ListBuffer[Token]
   117     var ctxt = context
   118     while (!in.atEnd) {
   119       parse(token_context(is_command, ctxt), in) match {
   120         case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   121         case NoSuccess(_, rest) =>
   122           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   123       }
   124     }
   125     (toks.toList, ctxt)
   126   }
   127 }