src/Pure/Isar/outer_syntax.scala
author wenzelm
Sat Feb 22 15:07:33 2014 +0100 (2014-02-22)
changeset 55666 cc350eb1087e
parent 55616 25a7a998852a
child 55749 75a48dc4383e
permissions -rw-r--r--
refined language context: antiquotes;
support default completions, with move of caret position;
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   val completion_context: Completion.Context = Completion.Context.outer,
    47   val has_tokens: Boolean = true)
    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 thy_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 thy_load_commands: List[(String, List[String])] =
    67     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    68 
    69   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    70   {
    71     val keywords1 = keywords + (name -> kind)
    72     val lexicon1 = lexicon + name
    73     val completion1 =
    74       if (Keyword.control(kind._1) || replace == Some("")) completion
    75       else completion + (name, replace getOrElse name)
    76     new Outer_Syntax(keywords1, lexicon1, completion1, completion_context, true)
    77   }
    78 
    79   def + (name: String, kind: (String, List[String])): Outer_Syntax =
    80     this + (name, kind, Some(name))
    81   def + (name: String, kind: String): Outer_Syntax =
    82     this + (name, (kind, Nil), Some(name))
    83   def + (name: String, replace: Option[String]): Outer_Syntax =
    84     this + (name, (Keyword.MINOR, Nil), replace)
    85   def + (name: String): Outer_Syntax = this + (name, None)
    86 
    87   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    88     (this /: keywords) {
    89       case (syntax, (name, Some((kind, _)), replace)) =>
    90         syntax +
    91           (Symbol.decode(name), kind, replace) +
    92           (Symbol.encode(name), kind, replace)
    93       case (syntax, (name, None, replace)) =>
    94         syntax +
    95           (Symbol.decode(name), replace) +
    96           (Symbol.encode(name), replace)
    97     }
    98 
    99   def is_command(name: String): Boolean =
   100     keyword_kind(name) match {
   101       case Some(kind) => kind != Keyword.MINOR
   102       case None => false
   103     }
   104 
   105   def heading_level(name: String): Option[Int] =
   106   {
   107     keyword_kind(name) match {
   108       case _ if name == "header" => Some(0)
   109       case Some(Keyword.THY_HEADING1) => Some(1)
   110       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   111       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   112       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   113       case Some(kind) if Keyword.theory(kind) => Some(5)
   114       case _ => None
   115     }
   116   }
   117 
   118   def heading_level(command: Command): Option[Int] =
   119     heading_level(command.name)
   120 
   121 
   122   /* token language */
   123 
   124   def scan(input: Reader[Char]): List[Token] =
   125   {
   126     Token.Parsers.parseAll(
   127         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), input) match {
   128       case Token.Parsers.Success(tokens, _) => tokens
   129       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
   130     }
   131   }
   132 
   133   def scan(input: CharSequence): List[Token] =
   134     scan(new CharSequenceReader(input))
   135 
   136   def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
   137   {
   138     var in: Reader[Char] = new CharSequenceReader(input)
   139     val toks = new mutable.ListBuffer[Token]
   140     var ctxt = context
   141     while (!in.atEnd) {
   142       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   143         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   144         case Token.Parsers.NoSuccess(_, rest) =>
   145           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   146       }
   147     }
   148     (toks.toList, ctxt)
   149   }
   150 
   151 
   152   /* language context */
   153 
   154   def set_completion_context(context: Completion.Context): Outer_Syntax =
   155     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   156 
   157   def no_tokens: Outer_Syntax =
   158   {
   159     require(keywords.isEmpty && lexicon.isEmpty)
   160     new Outer_Syntax(
   161       completion = completion,
   162       completion_context = completion_context,
   163       has_tokens = false)
   164   }
   165 }