src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 07 15:01:48 2012 +0200 (2012-08-07)
changeset 48707 ba531af91148
parent 48706 e2b512024eab
child 48708 189ece4b4ff1
permissions -rw-r--r--
simplified Document.Node.Header -- internalized errors;
     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     (this /: header.keywords) {
    65       case (syntax, ((name, Some((kind, _))))) => syntax + (name, kind)
    66       case (syntax, ((name, None))) => syntax + name
    67     }
    68 
    69   def is_command(name: String): Boolean =
    70     keyword_kind(name) match {
    71       case Some(kind) => kind != Keyword.MINOR
    72       case None => false
    73     }
    74 
    75   def heading_level(name: String): Option[Int] =
    76   {
    77     keyword_kind(name) match {
    78       case _ if name == "header" => Some(0)
    79       case Some(Keyword.THY_HEADING1) => Some(1)
    80       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
    81       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
    82       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
    83       case Some(kind) if Keyword.theory(kind) => Some(5)
    84       case _ => None
    85     }
    86   }
    87 
    88   def heading_level(command: Command): Option[Int] =
    89     heading_level(command.name)
    90 
    91 
    92   /* tokenize */
    93 
    94   def scan(input: Reader[Char]): List[Token] =
    95   {
    96     import lexicon._
    97 
    98     parseAll(rep(token(is_command)), input) match {
    99       case Success(tokens, _) => tokens
   100       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   101     }
   102   }
   103 
   104   def scan(input: CharSequence): List[Token] =
   105     scan(new CharSequenceReader(input))
   106 
   107   def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
   108   {
   109     import lexicon._
   110 
   111     var in: Reader[Char] = new CharSequenceReader(input)
   112     val toks = new mutable.ListBuffer[Token]
   113     var ctxt = context
   114     while (!in.atEnd) {
   115       parse(token_context(is_command, ctxt), in) match {
   116         case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   117         case NoSuccess(_, rest) =>
   118           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   119       }
   120     }
   121     (toks.toList, ctxt)
   122   }
   123 }