src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 02 17:35:18 2016 +0200 (2016-08-02)
changeset 63579 73939a9b70a3
parent 63528 0f39f59317c1
child 63584 68751fe1c036
permissions -rw-r--r--
support 'abbrevs' within theory header;
simplified 'keywords': no abbreviations here;
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@58706
    11
import scala.annotation.tailrec
wenzelm@34166
    12
wenzelm@34166
    13
wenzelm@43774
    14
object Outer_Syntax
wenzelm@43774
    15
{
wenzelm@58706
    16
  /* syntax */
wenzelm@58706
    17
wenzelm@58706
    18
  val empty: Outer_Syntax = new Outer_Syntax()
wenzelm@58706
    19
wenzelm@58706
    20
  def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
wenzelm@58706
    21
wenzelm@58706
    22
wenzelm@58706
    23
  /* string literals */
wenzelm@58706
    24
wenzelm@43774
    25
  def quote_string(str: String): String =
wenzelm@43774
    26
  {
wenzelm@43774
    27
    val result = new StringBuilder(str.length + 10)
wenzelm@43774
    28
    result += '"'
wenzelm@43774
    29
    for (s <- Symbol.iterator(str)) {
wenzelm@43774
    30
      if (s.length == 1) {
wenzelm@43774
    31
        val c = s(0)
wenzelm@43774
    32
        if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
wenzelm@43774
    33
          result += '\\'
wenzelm@43774
    34
          if (c < 10) result += '0'
wenzelm@43774
    35
          if (c < 100) result += '0'
wenzelm@60215
    36
          result ++= c.asInstanceOf[Int].toString
wenzelm@43774
    37
        }
wenzelm@43774
    38
        else result += c
wenzelm@43774
    39
      }
wenzelm@43774
    40
      else result ++= s
wenzelm@43774
    41
    }
wenzelm@43774
    42
    result += '"'
wenzelm@43774
    43
    result.toString
wenzelm@43774
    44
  }
wenzelm@46626
    45
wenzelm@58696
    46
wenzelm@58697
    47
  /* line-oriented structure */
wenzelm@58696
    48
wenzelm@58697
    49
  object Line_Structure
wenzelm@58696
    50
  {
wenzelm@58700
    51
    val init = Line_Structure()
wenzelm@58696
    52
  }
wenzelm@58696
    53
wenzelm@58700
    54
  sealed case class Line_Structure(
wenzelm@58700
    55
    improper: Boolean = true,
wenzelm@58700
    56
    command: Boolean = false,
wenzelm@58700
    57
    depth: Int = 0,
wenzelm@58700
    58
    span_depth: Int = 0,
wenzelm@58700
    59
    after_span_depth: Int = 0)
wenzelm@58706
    60
wenzelm@58706
    61
wenzelm@58706
    62
  /* overall document structure */
wenzelm@58706
    63
wenzelm@58706
    64
  sealed abstract class Document { def length: Int }
wenzelm@58747
    65
  case class Document_Block(name: String, text: String, body: List[Document]) extends Document
wenzelm@58706
    66
  {
wenzelm@58706
    67
    val length: Int = (0 /: body)(_ + _.length)
wenzelm@58706
    68
  }
wenzelm@58747
    69
  case class Document_Atom(command: Command) extends Document
wenzelm@58706
    70
  {
wenzelm@58706
    71
    def length: Int = command.length
wenzelm@58706
    72
  }
wenzelm@43774
    73
}
wenzelm@43774
    74
wenzelm@46712
    75
final class Outer_Syntax private(
wenzelm@58900
    76
  val keywords: Keyword.Keywords = Keyword.Keywords.empty,
wenzelm@53280
    77
  val completion: Completion = Completion.empty,
wenzelm@55749
    78
  val language_context: Completion.Language_Context = Completion.Language_Context.outer,
wenzelm@56393
    79
  val has_tokens: Boolean = true) extends Prover.Syntax
wenzelm@34166
    80
{
wenzelm@58706
    81
  /** syntax content **/
wenzelm@58706
    82
wenzelm@58900
    83
  override def toString: String = keywords.toString
wenzelm@56393
    84
wenzelm@58695
    85
wenzelm@58695
    86
  /* add keywords */
wenzelm@58695
    87
wenzelm@63579
    88
  def + (name: String, kind: String = "", tags: List[String] = Nil): Outer_Syntax =
wenzelm@53280
    89
  {
wenzelm@63429
    90
    val keywords1 = keywords + (name, kind, tags)
wenzelm@53280
    91
    val completion1 =
wenzelm@63579
    92
      completion.add_keyword(name).add_abbrevs(
wenzelm@63579
    93
        if (Keyword.theory_block.contains(kind))
wenzelm@63579
    94
          List((name, name + "\nbegin\n\u0007\nend"), (name, name))
wenzelm@63579
    95
        else List((name, name)))
wenzelm@58900
    96
    new Outer_Syntax(keywords1, completion1, language_context, true)
wenzelm@53280
    97
  }
wenzelm@48706
    98
wenzelm@48873
    99
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
   100
    (this /: keywords) {
wenzelm@63579
   101
      case (syntax, (name, ((kind, tags), _))) =>
wenzelm@63579
   102
        syntax + (Symbol.decode(name), kind, tags) + (Symbol.encode(name), kind, tags)
wenzelm@63579
   103
    }
wenzelm@63579
   104
wenzelm@63579
   105
  def add_abbrevs(abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
wenzelm@63579
   106
    if (abbrevs.isEmpty) this
wenzelm@63579
   107
    else {
wenzelm@63579
   108
      val completion1 =
wenzelm@63579
   109
        completion.add_abbrevs(
wenzelm@63579
   110
          (for ((a, b) <- abbrevs) yield {
wenzelm@63579
   111
            val a1 = Symbol.decode(a)
wenzelm@63579
   112
            val a2 = Symbol.encode(a)
wenzelm@63579
   113
            val b1 = Symbol.decode(b)
wenzelm@63579
   114
            List((a1, b1), (a2, b1))
wenzelm@63579
   115
          }).flatten)
wenzelm@63579
   116
      new Outer_Syntax(keywords, completion1, language_context, has_tokens)
wenzelm@46940
   117
    }
wenzelm@34166
   118
wenzelm@58695
   119
wenzelm@59073
   120
  /* merge */
wenzelm@59073
   121
wenzelm@59077
   122
  def ++ (other: Prover.Syntax): Prover.Syntax =
wenzelm@59073
   123
    if (this eq other) this
wenzelm@59073
   124
    else {
wenzelm@59077
   125
      val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
wenzelm@59077
   126
      val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
wenzelm@59077
   127
      if ((keywords eq keywords1) && (completion eq completion1)) this
wenzelm@59077
   128
      else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
wenzelm@59073
   129
    }
wenzelm@59073
   130
wenzelm@59073
   131
wenzelm@59735
   132
  /* load commands */
wenzelm@58900
   133
wenzelm@63441
   134
  def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
wenzelm@58900
   135
  def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
wenzelm@58900
   136
wenzelm@58900
   137
wenzelm@58706
   138
  /* language context */
wenzelm@34166
   139
wenzelm@58706
   140
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@58900
   141
    new Outer_Syntax(keywords, completion, context, has_tokens)
wenzelm@58706
   142
wenzelm@58706
   143
  def no_tokens: Outer_Syntax =
wenzelm@46969
   144
  {
wenzelm@58900
   145
    require(keywords.is_empty)
wenzelm@58706
   146
    new Outer_Syntax(
wenzelm@58706
   147
      completion = completion,
wenzelm@58706
   148
      language_context = language_context,
wenzelm@58706
   149
      has_tokens = false)
wenzelm@46969
   150
  }
wenzelm@40454
   151
wenzelm@58706
   152
wenzelm@40454
   153
wenzelm@58706
   154
  /** parsing **/
wenzelm@34166
   155
wenzelm@58697
   156
  /* line-oriented structure */
wenzelm@58696
   157
wenzelm@63460
   158
  private val close_structure =
wenzelm@63460
   159
    Set(Keyword.NEXT_BLOCK, Keyword.QED_BLOCK, Keyword.PRF_CLOSE)
wenzelm@63460
   160
wenzelm@59924
   161
  def line_structure(tokens: List[Token], structure: Outer_Syntax.Line_Structure)
wenzelm@58700
   162
    : Outer_Syntax.Line_Structure =
wenzelm@58696
   163
  {
wenzelm@58700
   164
    val improper1 = tokens.forall(_.is_improper)
wenzelm@58700
   165
    val command1 = tokens.exists(_.is_command)
wenzelm@58700
   166
wenzelm@63458
   167
    val command_depth =
wenzelm@63458
   168
      tokens.iterator.filter(_.is_proper).toStream.headOption match {
wenzelm@63458
   169
        case Some(tok) =>
wenzelm@63460
   170
          if (keywords.is_command(tok, close_structure))
wenzelm@63458
   171
            Some(structure.after_span_depth - 1)
wenzelm@63458
   172
          else None
wenzelm@63458
   173
        case None => None
wenzelm@63458
   174
      }
wenzelm@63458
   175
wenzelm@58696
   176
    val depth1 =
wenzelm@63459
   177
      if (tokens.exists(tok =>
wenzelm@63459
   178
            keywords.is_before_command(tok) || keywords.is_command(tok, Keyword.theory))) 0
wenzelm@63458
   179
      else if (command_depth.isDefined) command_depth.get
wenzelm@59924
   180
      else if (command1) structure.after_span_depth
wenzelm@59924
   181
      else structure.span_depth
wenzelm@58700
   182
wenzelm@58700
   183
    val (span_depth1, after_span_depth1) =
wenzelm@59924
   184
      ((structure.span_depth, structure.after_span_depth) /: tokens) {
wenzelm@58703
   185
        case ((x, y), tok) =>
wenzelm@58703
   186
          if (tok.is_command) {
wenzelm@63424
   187
            if (keywords.is_command(tok, Keyword.theory_goal)) (2, 1)
wenzelm@63424
   188
            else if (keywords.is_command(tok, Keyword.theory)) (1, 0)
wenzelm@63424
   189
            else if (keywords.is_command(tok, Keyword.proof_open)) (y + 2, y + 1)
wenzelm@63479
   190
            else if (keywords.is_command(tok, Set(Keyword.PRF_BLOCK))) (y + 2, y + 1)
wenzelm@63479
   191
            else if (keywords.is_command(tok, Set(Keyword.QED_BLOCK))) (y - 1, y - 2)
wenzelm@63479
   192
            else if (keywords.is_command(tok, Set(Keyword.PRF_CLOSE))) (y, y - 1)
wenzelm@63424
   193
            else if (keywords.is_command(tok, Keyword.proof_close)) (y + 1, y - 1)
wenzelm@63424
   194
            else if (keywords.is_command(tok, Keyword.qed_global)) (1, 0)
wenzelm@58703
   195
            else (x, y)
wenzelm@58703
   196
          }
wenzelm@58703
   197
          else (x, y)
wenzelm@58696
   198
      }
wenzelm@58700
   199
wenzelm@58700
   200
    Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
wenzelm@58696
   201
  }
wenzelm@58696
   202
wenzelm@58696
   203
wenzelm@58706
   204
  /* command spans */
wenzelm@57905
   205
wenzelm@57905
   206
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   207
  {
wenzelm@57905
   208
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   209
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   210
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   211
wenzelm@57905
   212
    def ship(span: List[Token])
wenzelm@57905
   213
    {
wenzelm@57905
   214
      val kind =
wenzelm@59924
   215
        if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@59924
   216
        else if (span.exists(_.is_error)) Command_Span.Malformed_Span
wenzelm@59924
   217
        else
wenzelm@59924
   218
          span.find(_.is_command) match {
wenzelm@59924
   219
            case None => Command_Span.Malformed_Span
wenzelm@59924
   220
            case Some(cmd) =>
wenzelm@59924
   221
              val name = cmd.source
wenzelm@59924
   222
              val offset =
wenzelm@59924
   223
                (0 /: span.takeWhile(_ != cmd)) {
wenzelm@59924
   224
                  case (i, tok) => i + Symbol.iterator(tok.source).length }
wenzelm@59924
   225
              val end_offset = offset + Symbol.iterator(name).length
wenzelm@59924
   226
              val pos = Position.Range(Text.Range(offset, end_offset) + 1)
wenzelm@59924
   227
              Command_Span.Command_Span(name, pos)
wenzelm@59924
   228
          }
wenzelm@57905
   229
      result += Command_Span.Span(kind, span)
wenzelm@57905
   230
    }
wenzelm@57905
   231
wenzelm@57905
   232
    def flush()
wenzelm@57905
   233
    {
wenzelm@59319
   234
      if (content.nonEmpty) { ship(content.toList); content.clear }
wenzelm@59319
   235
      if (improper.nonEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   236
    }
wenzelm@57905
   237
wenzelm@57905
   238
    for (tok <- toks) {
wenzelm@59924
   239
      if (tok.is_improper) improper += tok
wenzelm@63441
   240
      else if (keywords.is_before_command(tok) ||
wenzelm@63441
   241
        tok.is_command &&
wenzelm@63441
   242
          (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
wenzelm@59924
   243
      { flush(); content += tok }
wenzelm@57905
   244
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   245
    }
wenzelm@57905
   246
    flush()
wenzelm@57905
   247
wenzelm@57905
   248
    result.toList
wenzelm@57905
   249
  }
wenzelm@57905
   250
wenzelm@57906
   251
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@59083
   252
    parse_spans(Token.explode(keywords, input))
wenzelm@57906
   253
wenzelm@57905
   254
wenzelm@58706
   255
  /* overall document structure */
wenzelm@55616
   256
wenzelm@58706
   257
  def heading_level(command: Command): Option[Int] =
wenzelm@58706
   258
  {
wenzelm@59735
   259
    val name = command.span.name
wenzelm@59735
   260
    name match {
wenzelm@59735
   261
      case Thy_Header.CHAPTER => Some(0)
wenzelm@62453
   262
      case Thy_Header.SECTION => Some(1)
wenzelm@59735
   263
      case Thy_Header.SUBSECTION => Some(2)
wenzelm@59735
   264
      case Thy_Header.SUBSUBSECTION => Some(3)
wenzelm@61463
   265
      case Thy_Header.PARAGRAPH => Some(4)
wenzelm@61463
   266
      case Thy_Header.SUBPARAGRAPH => Some(5)
wenzelm@58868
   267
      case _ =>
wenzelm@63441
   268
        keywords.kinds.get(name) match {
wenzelm@61463
   269
          case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
wenzelm@58868
   270
          case _ => None
wenzelm@58868
   271
        }
wenzelm@58706
   272
    }
wenzelm@58706
   273
  }
wenzelm@58706
   274
wenzelm@58743
   275
  def parse_document(node_name: Document.Node.Name, text: CharSequence):
wenzelm@58743
   276
    List[Outer_Syntax.Document] =
wenzelm@58706
   277
  {
wenzelm@58706
   278
    /* stack operations */
wenzelm@58706
   279
wenzelm@58706
   280
    def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
wenzelm@58706
   281
      new mutable.ListBuffer[Outer_Syntax.Document]
wenzelm@58706
   282
wenzelm@58747
   283
    var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
wenzelm@58747
   284
      List((0, Command.empty, buffer()))
wenzelm@55616
   285
wenzelm@58706
   286
    @tailrec def close(level: Int => Boolean)
wenzelm@58706
   287
    {
wenzelm@58706
   288
      stack match {
wenzelm@58747
   289
        case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@59735
   290
          body2 += Outer_Syntax.Document_Block(command.span.name, command.source, body.toList)
wenzelm@58706
   291
          stack = stack.tail
wenzelm@58706
   292
          close(level)
wenzelm@58706
   293
        case _ =>
wenzelm@58706
   294
      }
wenzelm@58706
   295
    }
wenzelm@58706
   296
wenzelm@58743
   297
    def result(): List[Outer_Syntax.Document] =
wenzelm@58706
   298
    {
wenzelm@58706
   299
      close(_ => true)
wenzelm@58743
   300
      stack.head._3.toList
wenzelm@58706
   301
    }
wenzelm@58706
   302
wenzelm@58706
   303
    def add(command: Command)
wenzelm@58706
   304
    {
wenzelm@58706
   305
      heading_level(command) match {
wenzelm@58706
   306
        case Some(i) =>
wenzelm@58706
   307
          close(_ > i)
wenzelm@58747
   308
          stack = (i + 1, command, buffer()) :: stack
wenzelm@58706
   309
        case None =>
wenzelm@58706
   310
      }
wenzelm@58706
   311
      stack.head._3 += Outer_Syntax.Document_Atom(command)
wenzelm@58706
   312
    }
wenzelm@58706
   313
wenzelm@58706
   314
wenzelm@58706
   315
    /* result structure */
wenzelm@58706
   316
wenzelm@58706
   317
    val spans = parse_spans(text)
wenzelm@59702
   318
    spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
wenzelm@58706
   319
    result()
wenzelm@55616
   320
  }
wenzelm@34166
   321
}