src/Pure/Isar/outer_syntax.scala
author wenzelm
Mon Jun 24 23:33:14 2013 +0200 (2013-06-24)
changeset 52439 4cf3f6153eb8
parent 52066 83b7b88770c9
child 53280 c63a016805b9
permissions -rw-r--r--
improved "isabelle keywords" and "isabelle update_keywords" based on Isabelle/Scala, without requiring to build sessions first;
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 
    39   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    40 }
    41 
    42 final class Outer_Syntax private(
    43   keywords: Map[String, (String, List[String])] = Map.empty,
    44   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    45   val completion: Completion = Completion.empty)
    46 {
    47   override def toString: String =
    48     (for ((name, (kind, files)) <- keywords) yield {
    49       if (kind == Keyword.MINOR) quote(name)
    50       else
    51         quote(name) + " :: " + quote(kind) +
    52         (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
    53     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    54 
    55   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    56   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    57 
    58   def thy_load_commands: List[(String, List[String])] =
    59     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    60 
    61   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    62     new Outer_Syntax(
    63       keywords + (name -> kind),
    64       lexicon + name,
    65       if (Keyword.control(kind._1) || replace == Some("")) completion
    66       else completion + (name, replace getOrElse name))
    67 
    68   def + (name: String, kind: (String, List[String])): Outer_Syntax = this + (name, kind, Some(name))
    69   def + (name: String, kind: String): Outer_Syntax = this + (name, (kind, Nil), Some(name))
    70   def + (name: String, replace: Option[String]): Outer_Syntax =
    71     this + (name, (Keyword.MINOR, Nil), replace)
    72   def + (name: String): Outer_Syntax = this + (name, None)
    73 
    74   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    75     (this /: keywords) {
    76       case (syntax, (name, Some((kind, _)), replace)) =>
    77         syntax +
    78           (Symbol.decode(name), kind, replace) +
    79           (Symbol.encode(name), kind, replace)
    80       case (syntax, (name, None, replace)) =>
    81         syntax +
    82           (Symbol.decode(name), replace) +
    83           (Symbol.encode(name), replace)
    84     }
    85 
    86   def is_command(name: String): Boolean =
    87     keyword_kind(name) match {
    88       case Some(kind) => kind != Keyword.MINOR
    89       case None => false
    90     }
    91 
    92   def heading_level(name: String): Option[Int] =
    93   {
    94     keyword_kind(name) match {
    95       case _ if name == "header" => Some(0)
    96       case Some(Keyword.THY_HEADING1) => Some(1)
    97       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
    98       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
    99       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   100       case Some(kind) if Keyword.theory(kind) => Some(5)
   101       case _ => None
   102     }
   103   }
   104 
   105   def heading_level(command: Command): Option[Int] =
   106     heading_level(command.name)
   107 
   108 
   109   /* tokenize */
   110 
   111   def scan(input: Reader[Char]): List[Token] =
   112   {
   113     import lexicon._
   114 
   115     parseAll(rep(token(is_command)), input) match {
   116       case Success(tokens, _) => tokens
   117       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   118     }
   119   }
   120 
   121   def scan(input: CharSequence): List[Token] =
   122     scan(new CharSequenceReader(input))
   123 
   124   def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
   125   {
   126     import lexicon._
   127 
   128     var in: Reader[Char] = new CharSequenceReader(input)
   129     val toks = new mutable.ListBuffer[Token]
   130     var ctxt = context
   131     while (!in.atEnd) {
   132       parse(token_context(is_command, ctxt), in) match {
   133         case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   134         case NoSuccess(_, rest) =>
   135           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   136       }
   137     }
   138     (toks.toList, ctxt)
   139   }
   140 }