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