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