src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Jul 12 14:13:42 2016 +0200 (2016-07-12)
changeset 63459 8d68204d97d7
parent 63458 723f9c673c1c
child 63460 f41070510341
permissions -rw-r--r--
clarified;
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@63429
    88
  def + (name: String, kind: String = "", tags: List[String] = Nil, replace: Option[String] = None)
wenzelm@58901
    89
    : Outer_Syntax =
wenzelm@53280
    90
  {
wenzelm@63429
    91
    val keywords1 = keywords + (name, kind, tags)
wenzelm@53280
    92
    val completion1 =
wenzelm@58853
    93
      if (replace == Some("")) completion
wenzelm@53280
    94
      else completion + (name, replace getOrElse name)
wenzelm@58900
    95
    new Outer_Syntax(keywords1, completion1, language_context, true)
wenzelm@53280
    96
  }
wenzelm@48706
    97
wenzelm@48873
    98
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    99
    (this /: keywords) {
wenzelm@63429
   100
      case (syntax, (name, ((kind, tags), _), replace)) =>
wenzelm@50128
   101
        syntax +
wenzelm@63429
   102
          (Symbol.decode(name), kind, tags, replace) +
wenzelm@63429
   103
          (Symbol.encode(name), kind, tags, replace)
wenzelm@46940
   104
    }
wenzelm@34166
   105
wenzelm@58695
   106
wenzelm@59073
   107
  /* merge */
wenzelm@59073
   108
wenzelm@59077
   109
  def ++ (other: Prover.Syntax): Prover.Syntax =
wenzelm@59073
   110
    if (this eq other) this
wenzelm@59073
   111
    else {
wenzelm@59077
   112
      val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
wenzelm@59077
   113
      val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
wenzelm@59077
   114
      if ((keywords eq keywords1) && (completion eq completion1)) this
wenzelm@59077
   115
      else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
wenzelm@59073
   116
    }
wenzelm@59073
   117
wenzelm@59073
   118
wenzelm@59735
   119
  /* load commands */
wenzelm@58900
   120
wenzelm@63441
   121
  def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
wenzelm@58900
   122
  def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
wenzelm@58900
   123
wenzelm@58900
   124
wenzelm@58706
   125
  /* language context */
wenzelm@34166
   126
wenzelm@58706
   127
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@58900
   128
    new Outer_Syntax(keywords, completion, context, has_tokens)
wenzelm@58706
   129
wenzelm@58706
   130
  def no_tokens: Outer_Syntax =
wenzelm@46969
   131
  {
wenzelm@58900
   132
    require(keywords.is_empty)
wenzelm@58706
   133
    new Outer_Syntax(
wenzelm@58706
   134
      completion = completion,
wenzelm@58706
   135
      language_context = language_context,
wenzelm@58706
   136
      has_tokens = false)
wenzelm@46969
   137
  }
wenzelm@40454
   138
wenzelm@58706
   139
wenzelm@40454
   140
wenzelm@58706
   141
  /** parsing **/
wenzelm@34166
   142
wenzelm@58697
   143
  /* line-oriented structure */
wenzelm@58696
   144
wenzelm@59924
   145
  def line_structure(tokens: List[Token], structure: Outer_Syntax.Line_Structure)
wenzelm@58700
   146
    : Outer_Syntax.Line_Structure =
wenzelm@58696
   147
  {
wenzelm@58700
   148
    val improper1 = tokens.forall(_.is_improper)
wenzelm@58700
   149
    val command1 = tokens.exists(_.is_command)
wenzelm@58700
   150
wenzelm@63458
   151
    val command_depth =
wenzelm@63458
   152
      tokens.iterator.filter(_.is_proper).toStream.headOption match {
wenzelm@63458
   153
        case Some(tok) =>
wenzelm@63458
   154
          if (keywords.is_command(tok, Keyword.QED_BLOCK == _))
wenzelm@63458
   155
            Some(structure.after_span_depth - 2)
wenzelm@63458
   156
          else if (keywords.is_command(tok, Keyword.PRF_CLOSE == _))
wenzelm@63458
   157
            Some(structure.after_span_depth - 1)
wenzelm@63458
   158
          else None
wenzelm@63458
   159
        case None => None
wenzelm@63458
   160
      }
wenzelm@63458
   161
wenzelm@58696
   162
    val depth1 =
wenzelm@63459
   163
      if (tokens.exists(tok =>
wenzelm@63459
   164
            keywords.is_before_command(tok) || keywords.is_command(tok, Keyword.theory))) 0
wenzelm@63458
   165
      else if (command_depth.isDefined) command_depth.get
wenzelm@59924
   166
      else if (command1) structure.after_span_depth
wenzelm@59924
   167
      else structure.span_depth
wenzelm@58700
   168
wenzelm@58700
   169
    val (span_depth1, after_span_depth1) =
wenzelm@59924
   170
      ((structure.span_depth, structure.after_span_depth) /: tokens) {
wenzelm@58703
   171
        case ((x, y), tok) =>
wenzelm@58703
   172
          if (tok.is_command) {
wenzelm@63424
   173
            if (keywords.is_command(tok, Keyword.theory_goal)) (2, 1)
wenzelm@63424
   174
            else if (keywords.is_command(tok, Keyword.theory)) (1, 0)
wenzelm@63424
   175
            else if (keywords.is_command(tok, Keyword.proof_open)) (y + 2, y + 1)
wenzelm@63424
   176
            else if (keywords.is_command(tok, Keyword.PRF_BLOCK == _)) (y + 2, y + 1)
wenzelm@63458
   177
            else if (keywords.is_command(tok, Keyword.QED_BLOCK == _)) (y - 1, y - 2)
wenzelm@63458
   178
            else if (keywords.is_command(tok, Keyword.PRF_CLOSE == _)) (y, y - 1)
wenzelm@63424
   179
            else if (keywords.is_command(tok, Keyword.proof_close)) (y + 1, y - 1)
wenzelm@63424
   180
            else if (keywords.is_command(tok, Keyword.qed_global)) (1, 0)
wenzelm@58703
   181
            else (x, y)
wenzelm@58703
   182
          }
wenzelm@58703
   183
          else (x, y)
wenzelm@58696
   184
      }
wenzelm@58700
   185
wenzelm@58700
   186
    Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
wenzelm@58696
   187
  }
wenzelm@58696
   188
wenzelm@58696
   189
wenzelm@58706
   190
  /* command spans */
wenzelm@57905
   191
wenzelm@57905
   192
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   193
  {
wenzelm@57905
   194
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   195
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   196
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   197
wenzelm@57905
   198
    def ship(span: List[Token])
wenzelm@57905
   199
    {
wenzelm@57905
   200
      val kind =
wenzelm@59924
   201
        if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@59924
   202
        else if (span.exists(_.is_error)) Command_Span.Malformed_Span
wenzelm@59924
   203
        else
wenzelm@59924
   204
          span.find(_.is_command) match {
wenzelm@59924
   205
            case None => Command_Span.Malformed_Span
wenzelm@59924
   206
            case Some(cmd) =>
wenzelm@59924
   207
              val name = cmd.source
wenzelm@59924
   208
              val offset =
wenzelm@59924
   209
                (0 /: span.takeWhile(_ != cmd)) {
wenzelm@59924
   210
                  case (i, tok) => i + Symbol.iterator(tok.source).length }
wenzelm@59924
   211
              val end_offset = offset + Symbol.iterator(name).length
wenzelm@59924
   212
              val pos = Position.Range(Text.Range(offset, end_offset) + 1)
wenzelm@59924
   213
              Command_Span.Command_Span(name, pos)
wenzelm@59924
   214
          }
wenzelm@57905
   215
      result += Command_Span.Span(kind, span)
wenzelm@57905
   216
    }
wenzelm@57905
   217
wenzelm@57905
   218
    def flush()
wenzelm@57905
   219
    {
wenzelm@59319
   220
      if (content.nonEmpty) { ship(content.toList); content.clear }
wenzelm@59319
   221
      if (improper.nonEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   222
    }
wenzelm@57905
   223
wenzelm@57905
   224
    for (tok <- toks) {
wenzelm@59924
   225
      if (tok.is_improper) improper += tok
wenzelm@63441
   226
      else if (keywords.is_before_command(tok) ||
wenzelm@63441
   227
        tok.is_command &&
wenzelm@63441
   228
          (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
wenzelm@59924
   229
      { flush(); content += tok }
wenzelm@57905
   230
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   231
    }
wenzelm@57905
   232
    flush()
wenzelm@57905
   233
wenzelm@57905
   234
    result.toList
wenzelm@57905
   235
  }
wenzelm@57905
   236
wenzelm@57906
   237
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@59083
   238
    parse_spans(Token.explode(keywords, input))
wenzelm@57906
   239
wenzelm@57905
   240
wenzelm@58706
   241
  /* overall document structure */
wenzelm@55616
   242
wenzelm@58706
   243
  def heading_level(command: Command): Option[Int] =
wenzelm@58706
   244
  {
wenzelm@59735
   245
    val name = command.span.name
wenzelm@59735
   246
    name match {
wenzelm@59735
   247
      case Thy_Header.CHAPTER => Some(0)
wenzelm@62453
   248
      case Thy_Header.SECTION => Some(1)
wenzelm@59735
   249
      case Thy_Header.SUBSECTION => Some(2)
wenzelm@59735
   250
      case Thy_Header.SUBSUBSECTION => Some(3)
wenzelm@61463
   251
      case Thy_Header.PARAGRAPH => Some(4)
wenzelm@61463
   252
      case Thy_Header.SUBPARAGRAPH => Some(5)
wenzelm@58868
   253
      case _ =>
wenzelm@63441
   254
        keywords.kinds.get(name) match {
wenzelm@61463
   255
          case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
wenzelm@58868
   256
          case _ => None
wenzelm@58868
   257
        }
wenzelm@58706
   258
    }
wenzelm@58706
   259
  }
wenzelm@58706
   260
wenzelm@58743
   261
  def parse_document(node_name: Document.Node.Name, text: CharSequence):
wenzelm@58743
   262
    List[Outer_Syntax.Document] =
wenzelm@58706
   263
  {
wenzelm@58706
   264
    /* stack operations */
wenzelm@58706
   265
wenzelm@58706
   266
    def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
wenzelm@58706
   267
      new mutable.ListBuffer[Outer_Syntax.Document]
wenzelm@58706
   268
wenzelm@58747
   269
    var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
wenzelm@58747
   270
      List((0, Command.empty, buffer()))
wenzelm@55616
   271
wenzelm@58706
   272
    @tailrec def close(level: Int => Boolean)
wenzelm@58706
   273
    {
wenzelm@58706
   274
      stack match {
wenzelm@58747
   275
        case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@59735
   276
          body2 += Outer_Syntax.Document_Block(command.span.name, command.source, body.toList)
wenzelm@58706
   277
          stack = stack.tail
wenzelm@58706
   278
          close(level)
wenzelm@58706
   279
        case _ =>
wenzelm@58706
   280
      }
wenzelm@58706
   281
    }
wenzelm@58706
   282
wenzelm@58743
   283
    def result(): List[Outer_Syntax.Document] =
wenzelm@58706
   284
    {
wenzelm@58706
   285
      close(_ => true)
wenzelm@58743
   286
      stack.head._3.toList
wenzelm@58706
   287
    }
wenzelm@58706
   288
wenzelm@58706
   289
    def add(command: Command)
wenzelm@58706
   290
    {
wenzelm@58706
   291
      heading_level(command) match {
wenzelm@58706
   292
        case Some(i) =>
wenzelm@58706
   293
          close(_ > i)
wenzelm@58747
   294
          stack = (i + 1, command, buffer()) :: stack
wenzelm@58706
   295
        case None =>
wenzelm@58706
   296
      }
wenzelm@58706
   297
      stack.head._3 += Outer_Syntax.Document_Atom(command)
wenzelm@58706
   298
    }
wenzelm@58706
   299
wenzelm@58706
   300
wenzelm@58706
   301
    /* result structure */
wenzelm@58706
   302
wenzelm@58706
   303
    val spans = parse_spans(text)
wenzelm@59702
   304
    spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
wenzelm@58706
   305
    result()
wenzelm@55616
   306
  }
wenzelm@34166
   307
}