src/Pure/Isar/outer_syntax.scala
author wenzelm
Sun Feb 16 13:18:08 2014 +0100 (2014-02-16)
changeset 55510 1585a65aad64
parent 55494 009b71c1ed23
child 55616 25a7a998852a
permissions -rw-r--r--
tuned signature -- emphasize line-oriented aspect;
     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   val has_tokens: Boolean = true)
    47 {
    48   override def toString: String =
    49     (for ((name, (kind, files)) <- keywords) yield {
    50       if (kind == Keyword.MINOR) quote(name)
    51       else
    52         quote(name) + " :: " + quote(kind) +
    53         (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
    54     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    55 
    56   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    57   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    58 
    59   def thy_load(span: List[Token]): Option[List[String]] =
    60     keywords.get(Command.name(span)) match {
    61       case Some((Keyword.THY_LOAD, exts)) => Some(exts)
    62       case _ => None
    63     }
    64 
    65   val thy_load_commands: List[(String, List[String])] =
    66     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    67 
    68   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    69   {
    70     val keywords1 = keywords + (name -> kind)
    71     val lexicon1 = lexicon + name
    72     val completion1 =
    73       if (Keyword.control(kind._1) || replace == Some("")) completion
    74       else completion + (name, replace getOrElse name)
    75     new Outer_Syntax(keywords1, lexicon1, completion1, true)
    76   }
    77 
    78   def + (name: String, kind: (String, List[String])): Outer_Syntax =
    79     this + (name, kind, Some(name))
    80   def + (name: String, kind: String): Outer_Syntax =
    81     this + (name, (kind, Nil), Some(name))
    82   def + (name: String, replace: Option[String]): Outer_Syntax =
    83     this + (name, (Keyword.MINOR, Nil), replace)
    84   def + (name: String): Outer_Syntax = this + (name, None)
    85 
    86   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    87     (this /: keywords) {
    88       case (syntax, (name, Some((kind, _)), replace)) =>
    89         syntax +
    90           (Symbol.decode(name), kind, replace) +
    91           (Symbol.encode(name), kind, replace)
    92       case (syntax, (name, None, replace)) =>
    93         syntax +
    94           (Symbol.decode(name), replace) +
    95           (Symbol.encode(name), replace)
    96     }
    97 
    98   def is_command(name: String): Boolean =
    99     keyword_kind(name) match {
   100       case Some(kind) => kind != Keyword.MINOR
   101       case None => false
   102     }
   103 
   104   def heading_level(name: String): Option[Int] =
   105   {
   106     keyword_kind(name) match {
   107       case _ if name == "header" => Some(0)
   108       case Some(Keyword.THY_HEADING1) => Some(1)
   109       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   110       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   111       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   112       case Some(kind) if Keyword.theory(kind) => Some(5)
   113       case _ => None
   114     }
   115   }
   116 
   117   def heading_level(command: Command): Option[Int] =
   118     heading_level(command.name)
   119 
   120 
   121   /* token language */
   122 
   123   def no_tokens: Outer_Syntax =
   124   {
   125     require(keywords.isEmpty && lexicon.isEmpty)
   126     new Outer_Syntax(completion = completion, has_tokens = false)
   127   }
   128 
   129   def scan(input: Reader[Char]): List[Token] =
   130   {
   131     Token.Parsers.parseAll(Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), input) match {
   132       case Token.Parsers.Success(tokens, _) => tokens
   133       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   134     }
   135   }
   136 
   137   def scan(input: CharSequence): List[Token] =
   138     scan(new CharSequenceReader(input))
   139 
   140   def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
   141   {
   142     var in: Reader[Char] = new CharSequenceReader(input)
   143     val toks = new mutable.ListBuffer[Token]
   144     var ctxt = context
   145     while (!in.atEnd) {
   146       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   147         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   148         case Token.Parsers.NoSuccess(_, rest) =>
   149           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   150       }
   151     }
   152     (toks.toList, ctxt)
   153   }
   154 }