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