src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Apr 04 22:56:28 2017 +0200 (2017-04-04)
changeset 65384 36255c43c64c
parent 65383 089f2edefb77
child 66717 67dbf5cdc056
permissions -rw-r--r--
more explicit types;
     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.collection.mutable
    11 
    12 
    13 object Outer_Syntax
    14 {
    15   /* syntax */
    16 
    17   val empty: Outer_Syntax = new Outer_Syntax()
    18 
    19   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    20 
    21 
    22   /* string literals */
    23 
    24   def quote_string(str: String): String =
    25   {
    26     val result = new StringBuilder(str.length + 10)
    27     result += '"'
    28     for (s <- Symbol.iterator(str)) {
    29       if (s.length == 1) {
    30         val c = s(0)
    31         if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
    32           result += '\\'
    33           if (c < 10) result += '0'
    34           if (c < 100) result += '0'
    35           result ++= c.asInstanceOf[Int].toString
    36         }
    37         else result += c
    38       }
    39       else result ++= s
    40     }
    41     result += '"'
    42     result.toString
    43   }
    44 }
    45 
    46 final class Outer_Syntax private(
    47   val keywords: Keyword.Keywords = Keyword.Keywords.empty,
    48   val completion: Completion = Completion.empty,
    49   val rev_abbrevs: Thy_Header.Abbrevs = Nil,
    50   val language_context: Completion.Language_Context = Completion.Language_Context.outer,
    51   val has_tokens: Boolean = true)
    52 {
    53   /** syntax content **/
    54 
    55   override def toString: String = keywords.toString
    56 
    57 
    58   /* keywords */
    59 
    60   def + (name: String, kind: String = "", exts: List[String] = Nil): Outer_Syntax =
    61   {
    62     val keywords1 = keywords + (name, kind, exts)
    63     val completion1 =
    64       completion.add_keyword(name).
    65         add_abbrevs(
    66           (if (Keyword.theory_block.contains(kind)) List((name, name + "\nbegin\n\u0007\nend"))
    67            else Nil) :::
    68           (if (Completion.Word_Parsers.is_word(name)) List((name, name)) else Nil))
    69     new Outer_Syntax(keywords1, completion1, rev_abbrevs, language_context, true)
    70   }
    71 
    72   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    73     (this /: keywords) {
    74       case (syntax, (name, spec)) =>
    75         syntax +
    76           (Symbol.decode(name), spec.kind, spec.exts) +
    77           (Symbol.encode(name), spec.kind, spec.exts)
    78     }
    79 
    80 
    81   /* abbrevs */
    82 
    83   def abbrevs: Thy_Header.Abbrevs = rev_abbrevs.reverse
    84 
    85   def add_abbrevs(new_abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
    86     if (new_abbrevs.isEmpty) this
    87     else {
    88       val completion1 =
    89         completion.add_abbrevs(
    90           (for ((a, b) <- new_abbrevs) yield {
    91             val a1 = Symbol.decode(a)
    92             val a2 = Symbol.encode(a)
    93             val b1 = Symbol.decode(b)
    94             List((a1, b1), (a2, b1))
    95           }).flatten)
    96       val rev_abbrevs1 = Library.distinct(new_abbrevs) reverse_::: rev_abbrevs
    97       new Outer_Syntax(keywords, completion1, rev_abbrevs1, language_context, has_tokens)
    98     }
    99 
   100 
   101   /* merge */
   102 
   103   def ++ (other: Outer_Syntax): Outer_Syntax =
   104     if (this eq other) this
   105     else {
   106       val keywords1 = keywords ++ other.keywords
   107       val completion1 = completion ++ other.completion
   108       val rev_abbrevs1 = Library.merge(rev_abbrevs, other.rev_abbrevs)
   109       if ((keywords eq keywords1) && (completion eq completion1)) this
   110       else new Outer_Syntax(keywords1, completion1, rev_abbrevs1, language_context, has_tokens)
   111     }
   112 
   113 
   114   /* load commands */
   115 
   116   def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
   117   def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
   118 
   119 
   120   /* language context */
   121 
   122   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   123     new Outer_Syntax(keywords, completion, rev_abbrevs, context, has_tokens)
   124 
   125   def no_tokens: Outer_Syntax =
   126   {
   127     require(keywords.is_empty)
   128     new Outer_Syntax(
   129       completion = completion,
   130       rev_abbrevs = rev_abbrevs,
   131       language_context = language_context,
   132       has_tokens = false)
   133   }
   134 
   135 
   136 
   137   /** parsing **/
   138 
   139   /* command spans */
   140 
   141   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   142   {
   143     val result = new mutable.ListBuffer[Command_Span.Span]
   144     val content = new mutable.ListBuffer[Token]
   145     val improper = new mutable.ListBuffer[Token]
   146 
   147     def ship(span: List[Token])
   148     {
   149       val kind =
   150         if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   151         else if (span.exists(_.is_error)) Command_Span.Malformed_Span
   152         else
   153           span.find(_.is_command) match {
   154             case None => Command_Span.Malformed_Span
   155             case Some(cmd) =>
   156               val name = cmd.source
   157               val offset =
   158                 (0 /: span.takeWhile(_ != cmd)) {
   159                   case (i, tok) => i + Symbol.length(tok.source) }
   160               val end_offset = offset + Symbol.length(name)
   161               val pos = Position.Range(Text.Range(offset, end_offset) + 1)
   162               Command_Span.Command_Span(name, pos)
   163           }
   164       result += Command_Span.Span(kind, span)
   165     }
   166 
   167     def flush()
   168     {
   169       if (content.nonEmpty) { ship(content.toList); content.clear }
   170       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   171     }
   172 
   173     for (tok <- toks) {
   174       if (tok.is_improper) improper += tok
   175       else if (keywords.is_before_command(tok) ||
   176         tok.is_command &&
   177           (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
   178       { flush(); content += tok }
   179       else { content ++= improper; improper.clear; content += tok }
   180     }
   181     flush()
   182 
   183     result.toList
   184   }
   185 
   186   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   187     parse_spans(Token.explode(keywords, input))
   188 }