src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Apr 03 20:53:35 2014 +0200 (2014-04-03)
changeset 56393 22f533e6a049
parent 56314 9a513737a0b2
child 57901 e1abca2527da
permissions -rw-r--r--
more abstract Prover.Syntax, as proposed by Carst Tankink;
     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 language_context: Completion.Language_Context = Completion.Language_Context.outer,
    47   val has_tokens: Boolean = true) extends Prover.Syntax
    48 {
    49   override def toString: String =
    50     (for ((name, (kind, files)) <- keywords) yield {
    51       if (kind == Keyword.MINOR) quote(name)
    52       else
    53         quote(name) + " :: " + quote(kind) +
    54         (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
    55     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    56 
    57   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    58   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    59 
    60   def load(span: List[Token]): Option[List[String]] =
    61     keywords.get(Command.name(span)) match {
    62       case Some((Keyword.THY_LOAD, exts)) => Some(exts)
    63       case _ => None
    64     }
    65 
    66   val load_commands: List[(String, List[String])] =
    67     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    68 
    69   def load_commands_in(text: String): Boolean =
    70     load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
    71 
    72   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    73   {
    74     val keywords1 = keywords + (name -> kind)
    75     val lexicon1 = lexicon + name
    76     val completion1 =
    77       if (Keyword.control(kind._1) || replace == Some("")) completion
    78       else completion + (name, replace getOrElse name)
    79     new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
    80   }
    81 
    82   def + (name: String, kind: (String, List[String])): Outer_Syntax =
    83     this + (name, kind, Some(name))
    84   def + (name: String, kind: String): Outer_Syntax =
    85     this + (name, (kind, Nil), Some(name))
    86   def + (name: String, replace: Option[String]): Outer_Syntax =
    87     this + (name, (Keyword.MINOR, Nil), replace)
    88   def + (name: String): Outer_Syntax = this + (name, None)
    89 
    90   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    91     (this /: keywords) {
    92       case (syntax, (name, Some((kind, _)), replace)) =>
    93         syntax +
    94           (Symbol.decode(name), kind, replace) +
    95           (Symbol.encode(name), kind, replace)
    96       case (syntax, (name, None, replace)) =>
    97         syntax +
    98           (Symbol.decode(name), replace) +
    99           (Symbol.encode(name), replace)
   100     }
   101 
   102   def is_command(name: String): Boolean =
   103     keyword_kind(name) match {
   104       case Some(kind) => kind != Keyword.MINOR
   105       case None => false
   106     }
   107 
   108   def heading_level(name: String): Option[Int] =
   109   {
   110     keyword_kind(name) match {
   111       case _ if name == "header" => Some(0)
   112       case Some(Keyword.THY_HEADING1) => Some(1)
   113       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   114       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   115       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   116       case Some(kind) if Keyword.theory(kind) => Some(5)
   117       case _ => None
   118     }
   119   }
   120 
   121   def heading_level(command: Command): Option[Int] =
   122     heading_level(command.name)
   123 
   124 
   125   /* token language */
   126 
   127   def scan(input: Reader[Char]): List[Token] =
   128   {
   129     Token.Parsers.parseAll(
   130         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), input) match {
   131       case Token.Parsers.Success(tokens, _) => tokens
   132       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   133     }
   134   }
   135 
   136   def scan(input: CharSequence): List[Token] =
   137     scan(new CharSequenceReader(input))
   138 
   139   def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
   140   {
   141     var in: Reader[Char] = new CharSequenceReader(input)
   142     val toks = new mutable.ListBuffer[Token]
   143     var ctxt = context
   144     while (!in.atEnd) {
   145       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   146         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   147         case Token.Parsers.NoSuccess(_, rest) =>
   148           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   149       }
   150     }
   151     (toks.toList, ctxt)
   152   }
   153 
   154 
   155   /* language context */
   156 
   157   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   158     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   159 
   160   def no_tokens: Outer_Syntax =
   161   {
   162     require(keywords.isEmpty && lexicon.isEmpty)
   163     new Outer_Syntax(
   164       completion = completion,
   165       language_context = language_context,
   166       has_tokens = false)
   167   }
   168 }