src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Aug 04 11:17:11 2016 +0200 (2016-08-04)
changeset 63604 d8de4f8b95eb
parent 63603 9d9ea2c6bc38
child 63865 ccac33e291b1
permissions -rw-r--r--
clarified modules;
     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 language_context: Completion.Language_Context = Completion.Language_Context.outer,
    50   val has_tokens: Boolean = true)
    51 {
    52   /** syntax content **/
    53 
    54   override def toString: String = keywords.toString
    55 
    56 
    57   /* add keywords */
    58 
    59   def + (name: String, kind: String = "", tags: List[String] = Nil): Outer_Syntax =
    60   {
    61     val keywords1 = keywords + (name, kind, tags)
    62     val completion1 =
    63       completion.add_keyword(name).
    64         add_abbrevs(
    65           (if (Keyword.theory_block.contains(kind)) List((name, name + "\nbegin\n\u0007\nend"))
    66            else Nil) :::
    67           (if (Completion.Word_Parsers.is_word(name)) List((name, name)) else Nil))
    68     new Outer_Syntax(keywords1, completion1, language_context, true)
    69   }
    70 
    71   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    72     (this /: keywords) {
    73       case (syntax, (name, ((kind, tags), _))) =>
    74         syntax + (Symbol.decode(name), kind, tags) + (Symbol.encode(name), kind, tags)
    75     }
    76 
    77   def add_abbrevs(abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
    78     if (abbrevs.isEmpty) this
    79     else {
    80       val completion1 =
    81         completion.add_abbrevs(
    82           (for ((a, b) <- abbrevs) yield {
    83             val a1 = Symbol.decode(a)
    84             val a2 = Symbol.encode(a)
    85             val b1 = Symbol.decode(b)
    86             List((a1, b1), (a2, b1))
    87           }).flatten)
    88       new Outer_Syntax(keywords, completion1, language_context, has_tokens)
    89     }
    90 
    91 
    92   /* merge */
    93 
    94   def ++ (other: Outer_Syntax): Outer_Syntax =
    95     if (this eq other) this
    96     else {
    97       val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
    98       val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
    99       if ((keywords eq keywords1) && (completion eq completion1)) this
   100       else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
   101     }
   102 
   103 
   104   /* load commands */
   105 
   106   def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
   107   def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
   108 
   109 
   110   /* language context */
   111 
   112   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   113     new Outer_Syntax(keywords, completion, context, has_tokens)
   114 
   115   def no_tokens: Outer_Syntax =
   116   {
   117     require(keywords.is_empty)
   118     new Outer_Syntax(
   119       completion = completion,
   120       language_context = language_context,
   121       has_tokens = false)
   122   }
   123 
   124 
   125 
   126   /** parsing **/
   127 
   128   /* command spans */
   129 
   130   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   131   {
   132     val result = new mutable.ListBuffer[Command_Span.Span]
   133     val content = new mutable.ListBuffer[Token]
   134     val improper = new mutable.ListBuffer[Token]
   135 
   136     def ship(span: List[Token])
   137     {
   138       val kind =
   139         if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   140         else if (span.exists(_.is_error)) Command_Span.Malformed_Span
   141         else
   142           span.find(_.is_command) match {
   143             case None => Command_Span.Malformed_Span
   144             case Some(cmd) =>
   145               val name = cmd.source
   146               val offset =
   147                 (0 /: span.takeWhile(_ != cmd)) {
   148                   case (i, tok) => i + Symbol.iterator(tok.source).length }
   149               val end_offset = offset + Symbol.iterator(name).length
   150               val pos = Position.Range(Text.Range(offset, end_offset) + 1)
   151               Command_Span.Command_Span(name, pos)
   152           }
   153       result += Command_Span.Span(kind, span)
   154     }
   155 
   156     def flush()
   157     {
   158       if (content.nonEmpty) { ship(content.toList); content.clear }
   159       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   160     }
   161 
   162     for (tok <- toks) {
   163       if (tok.is_improper) improper += tok
   164       else if (keywords.is_before_command(tok) ||
   165         tok.is_command &&
   166           (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
   167       { flush(); content += tok }
   168       else { content ++= improper; improper.clear; content += tok }
   169     }
   170     flush()
   171 
   172     result.toList
   173   }
   174 
   175   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   176     parse_spans(Token.explode(keywords, input))
   177 }