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