src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Dec 20 10:45:20 2016 +0100 (2016-12-20)
changeset 64616 dc3ec40fe41b
parent 63867 fb46c031c841
child 65383 089f2edefb77
permissions -rw-r--r--
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@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@63579
    60
  def + (name: String, kind: String = "", tags: List[String] = Nil): Outer_Syntax =
wenzelm@53280
    61
  {
wenzelm@63429
    62
    val keywords1 = keywords + (name, kind, tags)
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@63579
    74
      case (syntax, (name, ((kind, tags), _))) =>
wenzelm@63579
    75
        syntax + (Symbol.decode(name), kind, tags) + (Symbol.encode(name), kind, tags)
wenzelm@63579
    76
    }
wenzelm@63579
    77
wenzelm@63867
    78
wenzelm@63867
    79
  /* abbrevs */
wenzelm@63867
    80
wenzelm@63867
    81
  def abbrevs: Thy_Header.Abbrevs = rev_abbrevs.reverse
wenzelm@63867
    82
wenzelm@63867
    83
  def add_abbrevs(new_abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
wenzelm@63867
    84
    if (new_abbrevs.isEmpty) this
wenzelm@63579
    85
    else {
wenzelm@63579
    86
      val completion1 =
wenzelm@63579
    87
        completion.add_abbrevs(
wenzelm@63867
    88
          (for ((a, b) <- new_abbrevs) yield {
wenzelm@63579
    89
            val a1 = Symbol.decode(a)
wenzelm@63579
    90
            val a2 = Symbol.encode(a)
wenzelm@63579
    91
            val b1 = Symbol.decode(b)
wenzelm@63579
    92
            List((a1, b1), (a2, b1))
wenzelm@63579
    93
          }).flatten)
wenzelm@63867
    94
      val rev_abbrevs1 = Library.distinct(new_abbrevs) reverse_::: rev_abbrevs
wenzelm@63867
    95
      new Outer_Syntax(keywords, completion1, rev_abbrevs1, language_context, has_tokens)
wenzelm@46940
    96
    }
wenzelm@34166
    97
wenzelm@58695
    98
wenzelm@59073
    99
  /* merge */
wenzelm@59073
   100
wenzelm@63584
   101
  def ++ (other: Outer_Syntax): Outer_Syntax =
wenzelm@59073
   102
    if (this eq other) this
wenzelm@59073
   103
    else {
wenzelm@63865
   104
      val keywords1 = keywords ++ other.keywords
wenzelm@63865
   105
      val completion1 = completion ++ other.completion
wenzelm@63867
   106
      val rev_abbrevs1 = Library.merge(rev_abbrevs, other.rev_abbrevs)
wenzelm@59077
   107
      if ((keywords eq keywords1) && (completion eq completion1)) this
wenzelm@63867
   108
      else new Outer_Syntax(keywords1, completion1, rev_abbrevs1, language_context, has_tokens)
wenzelm@59073
   109
    }
wenzelm@59073
   110
wenzelm@59073
   111
wenzelm@59735
   112
  /* load commands */
wenzelm@58900
   113
wenzelm@63441
   114
  def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
wenzelm@58900
   115
  def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
wenzelm@58900
   116
wenzelm@58900
   117
wenzelm@58706
   118
  /* language context */
wenzelm@34166
   119
wenzelm@58706
   120
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@63867
   121
    new Outer_Syntax(keywords, completion, rev_abbrevs, context, has_tokens)
wenzelm@58706
   122
wenzelm@58706
   123
  def no_tokens: Outer_Syntax =
wenzelm@46969
   124
  {
wenzelm@58900
   125
    require(keywords.is_empty)
wenzelm@58706
   126
    new Outer_Syntax(
wenzelm@58706
   127
      completion = completion,
wenzelm@63867
   128
      rev_abbrevs = rev_abbrevs,
wenzelm@58706
   129
      language_context = language_context,
wenzelm@58706
   130
      has_tokens = false)
wenzelm@46969
   131
  }
wenzelm@40454
   132
wenzelm@58706
   133
wenzelm@40454
   134
wenzelm@58706
   135
  /** parsing **/
wenzelm@34166
   136
wenzelm@58706
   137
  /* command spans */
wenzelm@57905
   138
wenzelm@57905
   139
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   140
  {
wenzelm@57905
   141
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   142
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   143
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   144
wenzelm@57905
   145
    def ship(span: List[Token])
wenzelm@57905
   146
    {
wenzelm@57905
   147
      val kind =
wenzelm@59924
   148
        if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@59924
   149
        else if (span.exists(_.is_error)) Command_Span.Malformed_Span
wenzelm@59924
   150
        else
wenzelm@59924
   151
          span.find(_.is_command) match {
wenzelm@59924
   152
            case None => Command_Span.Malformed_Span
wenzelm@59924
   153
            case Some(cmd) =>
wenzelm@59924
   154
              val name = cmd.source
wenzelm@59924
   155
              val offset =
wenzelm@59924
   156
                (0 /: span.takeWhile(_ != cmd)) {
wenzelm@64616
   157
                  case (i, tok) => i + Symbol.length(tok.source) }
wenzelm@64616
   158
              val end_offset = offset + Symbol.length(name)
wenzelm@59924
   159
              val pos = Position.Range(Text.Range(offset, end_offset) + 1)
wenzelm@59924
   160
              Command_Span.Command_Span(name, pos)
wenzelm@59924
   161
          }
wenzelm@57905
   162
      result += Command_Span.Span(kind, span)
wenzelm@57905
   163
    }
wenzelm@57905
   164
wenzelm@57905
   165
    def flush()
wenzelm@57905
   166
    {
wenzelm@59319
   167
      if (content.nonEmpty) { ship(content.toList); content.clear }
wenzelm@59319
   168
      if (improper.nonEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   169
    }
wenzelm@57905
   170
wenzelm@57905
   171
    for (tok <- toks) {
wenzelm@59924
   172
      if (tok.is_improper) improper += tok
wenzelm@63441
   173
      else if (keywords.is_before_command(tok) ||
wenzelm@63441
   174
        tok.is_command &&
wenzelm@63441
   175
          (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
wenzelm@59924
   176
      { flush(); content += tok }
wenzelm@57905
   177
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   178
    }
wenzelm@57905
   179
    flush()
wenzelm@57905
   180
wenzelm@57905
   181
    result.toList
wenzelm@57905
   182
  }
wenzelm@57905
   183
wenzelm@57906
   184
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@59083
   185
    parse_spans(Token.explode(keywords, input))
wenzelm@34166
   186
}