src/Pure/Isar/outer_syntax.scala
author wenzelm
Wed Nov 05 16:57:12 2014 +0100 (2014-11-05)
changeset 58900 1435cc20b022
parent 58899 0a793c580685
child 58901 47809a811eba
permissions -rw-r--r--
explicit type Keyword.Keywords;
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@34166
    10
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@43411
    11
import scala.collection.mutable
wenzelm@58706
    12
import scala.annotation.tailrec
wenzelm@34166
    13
wenzelm@34166
    14
wenzelm@43774
    15
object Outer_Syntax
wenzelm@43774
    16
{
wenzelm@58706
    17
  /* syntax */
wenzelm@58706
    18
wenzelm@58706
    19
  val empty: Outer_Syntax = new Outer_Syntax()
wenzelm@58706
    20
wenzelm@58706
    21
  def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
wenzelm@58706
    22
wenzelm@58706
    23
wenzelm@58706
    24
  /* string literals */
wenzelm@58706
    25
wenzelm@43774
    26
  def quote_string(str: String): String =
wenzelm@43774
    27
  {
wenzelm@43774
    28
    val result = new StringBuilder(str.length + 10)
wenzelm@43774
    29
    result += '"'
wenzelm@43774
    30
    for (s <- Symbol.iterator(str)) {
wenzelm@43774
    31
      if (s.length == 1) {
wenzelm@43774
    32
        val c = s(0)
wenzelm@43774
    33
        if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
wenzelm@43774
    34
          result += '\\'
wenzelm@43774
    35
          if (c < 10) result += '0'
wenzelm@43774
    36
          if (c < 100) result += '0'
wenzelm@43774
    37
          result ++= (c.asInstanceOf[Int].toString)
wenzelm@43774
    38
        }
wenzelm@43774
    39
        else result += c
wenzelm@43774
    40
      }
wenzelm@43774
    41
      else result ++= s
wenzelm@43774
    42
    }
wenzelm@43774
    43
    result += '"'
wenzelm@43774
    44
    result.toString
wenzelm@43774
    45
  }
wenzelm@46626
    46
wenzelm@58696
    47
wenzelm@58697
    48
  /* line-oriented structure */
wenzelm@58696
    49
wenzelm@58697
    50
  object Line_Structure
wenzelm@58696
    51
  {
wenzelm@58700
    52
    val init = Line_Structure()
wenzelm@58696
    53
  }
wenzelm@58696
    54
wenzelm@58700
    55
  sealed case class Line_Structure(
wenzelm@58700
    56
    improper: Boolean = true,
wenzelm@58700
    57
    command: Boolean = false,
wenzelm@58700
    58
    depth: Int = 0,
wenzelm@58700
    59
    span_depth: Int = 0,
wenzelm@58700
    60
    after_span_depth: Int = 0)
wenzelm@58706
    61
wenzelm@58706
    62
wenzelm@58706
    63
  /* overall document structure */
wenzelm@58706
    64
wenzelm@58706
    65
  sealed abstract class Document { def length: Int }
wenzelm@58747
    66
  case class Document_Block(name: String, text: String, body: List[Document]) extends Document
wenzelm@58706
    67
  {
wenzelm@58706
    68
    val length: Int = (0 /: body)(_ + _.length)
wenzelm@58706
    69
  }
wenzelm@58747
    70
  case class Document_Atom(command: Command) extends Document
wenzelm@58706
    71
  {
wenzelm@58706
    72
    def length: Int = command.length
wenzelm@58706
    73
  }
wenzelm@43774
    74
}
wenzelm@43774
    75
wenzelm@46712
    76
final class Outer_Syntax private(
wenzelm@58900
    77
  val keywords: Keyword.Keywords = Keyword.Keywords.empty,
wenzelm@53280
    78
  val completion: Completion = Completion.empty,
wenzelm@55749
    79
  val language_context: Completion.Language_Context = Completion.Language_Context.outer,
wenzelm@56393
    80
  val has_tokens: Boolean = true) extends Prover.Syntax
wenzelm@34166
    81
{
wenzelm@58706
    82
  /** syntax content **/
wenzelm@58706
    83
wenzelm@58900
    84
  override def toString: String = keywords.toString
wenzelm@56393
    85
wenzelm@58695
    86
wenzelm@58695
    87
  /* add keywords */
wenzelm@58695
    88
wenzelm@50128
    89
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@53280
    90
  {
wenzelm@58900
    91
    val keywords1 = keywords + (name, kind)
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@34166
    97
wenzelm@53280
    98
  def + (name: String, kind: (String, List[String])): Outer_Syntax =
wenzelm@53280
    99
    this + (name, kind, Some(name))
wenzelm@53280
   100
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@53280
   101
    this + (name, (kind, Nil), Some(name))
wenzelm@50128
   102
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
   103
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
   104
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
   105
wenzelm@48873
   106
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
   107
    (this /: keywords) {
wenzelm@52439
   108
      case (syntax, (name, Some((kind, _)), replace)) =>
wenzelm@50128
   109
        syntax +
wenzelm@50128
   110
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
   111
          (Symbol.encode(name), kind, replace)
wenzelm@52439
   112
      case (syntax, (name, None, replace)) =>
wenzelm@50128
   113
        syntax +
wenzelm@50128
   114
          (Symbol.decode(name), replace) +
wenzelm@50128
   115
          (Symbol.encode(name), replace)
wenzelm@46940
   116
    }
wenzelm@34166
   117
wenzelm@58695
   118
wenzelm@58900
   119
  /* load commands */
wenzelm@58900
   120
wenzelm@58900
   121
  def load_command(name: String): Option[List[String]] = keywords.load_command(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@58700
   145
  def line_structure(tokens: List[Token], struct: 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@58696
   151
    val depth1 =
wenzelm@58900
   152
      if (tokens.exists(tok => keywords.command_kind(tok, Keyword.theory))) 0
wenzelm@58700
   153
      else if (command1) struct.after_span_depth
wenzelm@58700
   154
      else struct.span_depth
wenzelm@58700
   155
wenzelm@58700
   156
    val (span_depth1, after_span_depth1) =
wenzelm@58700
   157
      ((struct.span_depth, struct.after_span_depth) /: tokens) {
wenzelm@58703
   158
        case ((x, y), tok) =>
wenzelm@58703
   159
          if (tok.is_command) {
wenzelm@58900
   160
            if (keywords.command_kind(tok, Keyword.theory_goal))
wenzelm@58900
   161
              (2, 1)
wenzelm@58900
   162
            else if (keywords.command_kind(tok, Keyword.theory))
wenzelm@58900
   163
              (1, 0)
wenzelm@58900
   164
            else if (keywords.command_kind(tok, Keyword.proof_goal) || tok.is_begin_block)
wenzelm@58900
   165
              (y + 2, y + 1)
wenzelm@58900
   166
            else if (keywords.command_kind(tok, Keyword.qed) || tok.is_end_block)
wenzelm@58900
   167
              (y + 1, y - 1)
wenzelm@58900
   168
            else if (keywords.command_kind(tok, Keyword.qed_global))
wenzelm@58900
   169
              (1, 0)
wenzelm@58703
   170
            else (x, y)
wenzelm@58703
   171
          }
wenzelm@58703
   172
          else (x, y)
wenzelm@58696
   173
      }
wenzelm@58700
   174
wenzelm@58700
   175
    Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
wenzelm@58696
   176
  }
wenzelm@58696
   177
wenzelm@58696
   178
wenzelm@53280
   179
  /* token language */
wenzelm@53280
   180
wenzelm@57907
   181
  def scan(input: CharSequence): List[Token] =
wenzelm@52066
   182
  {
wenzelm@58503
   183
    val in: Reader[Char] = new CharSequenceReader(input)
wenzelm@58900
   184
    Token.Parsers.parseAll(Token.Parsers.rep(Token.Parsers.token(keywords)), in) match {
wenzelm@55494
   185
      case Token.Parsers.Success(tokens, _) => tokens
wenzelm@57907
   186
      case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
wenzelm@34166
   187
    }
wenzelm@52066
   188
  }
wenzelm@34166
   189
wenzelm@58748
   190
  def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
wenzelm@52066
   191
  {
wenzelm@52066
   192
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   193
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   194
    var ctxt = context
wenzelm@52066
   195
    while (!in.atEnd) {
wenzelm@58900
   196
      Token.Parsers.parse(Token.Parsers.token_line(keywords, ctxt), in) match {
wenzelm@55494
   197
        case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@55494
   198
        case Token.Parsers.NoSuccess(_, rest) =>
wenzelm@52066
   199
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   200
      }
wenzelm@43411
   201
    }
wenzelm@58748
   202
    (toks.toList, ctxt)
wenzelm@52066
   203
  }
wenzelm@55616
   204
wenzelm@55616
   205
wenzelm@58706
   206
  /* command spans */
wenzelm@57905
   207
wenzelm@57905
   208
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   209
  {
wenzelm@57905
   210
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   211
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   212
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   213
wenzelm@57905
   214
    def ship(span: List[Token])
wenzelm@57905
   215
    {
wenzelm@57905
   216
      val kind =
wenzelm@57910
   217
        if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
wenzelm@57910
   218
          val name = span.head.source
wenzelm@57911
   219
          val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
wenzelm@57910
   220
          Command_Span.Command_Span(name, pos)
wenzelm@57910
   221
        }
wenzelm@57905
   222
        else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@57905
   223
        else Command_Span.Malformed_Span
wenzelm@57905
   224
      result += Command_Span.Span(kind, span)
wenzelm@57905
   225
    }
wenzelm@57905
   226
wenzelm@57905
   227
    def flush()
wenzelm@57905
   228
    {
wenzelm@57905
   229
      if (!content.isEmpty) { ship(content.toList); content.clear }
wenzelm@57905
   230
      if (!improper.isEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   231
    }
wenzelm@57905
   232
wenzelm@57905
   233
    for (tok <- toks) {
wenzelm@57905
   234
      if (tok.is_command) { flush(); content += tok }
wenzelm@57905
   235
      else if (tok.is_improper) improper += tok
wenzelm@57905
   236
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   237
    }
wenzelm@57905
   238
    flush()
wenzelm@57905
   239
wenzelm@57905
   240
    result.toList
wenzelm@57905
   241
  }
wenzelm@57905
   242
wenzelm@57906
   243
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@57906
   244
    parse_spans(scan(input))
wenzelm@57906
   245
wenzelm@57905
   246
wenzelm@58706
   247
  /* overall document structure */
wenzelm@55616
   248
wenzelm@58706
   249
  def heading_level(command: Command): Option[Int] =
wenzelm@58706
   250
  {
wenzelm@58868
   251
    command.name match {
wenzelm@58868
   252
      case "chapter" => Some(0)
wenzelm@58868
   253
      case "section" | "header" => Some(1)
wenzelm@58868
   254
      case "subsection" => Some(2)
wenzelm@58868
   255
      case "subsubsection" => Some(3)
wenzelm@58868
   256
      case _ =>
wenzelm@58900
   257
        keywords.kind(command.name) match {
wenzelm@58868
   258
          case Some(kind) if Keyword.theory(kind) => Some(4)
wenzelm@58868
   259
          case _ => None
wenzelm@58868
   260
        }
wenzelm@58706
   261
    }
wenzelm@58706
   262
  }
wenzelm@58706
   263
wenzelm@58743
   264
  def parse_document(node_name: Document.Node.Name, text: CharSequence):
wenzelm@58743
   265
    List[Outer_Syntax.Document] =
wenzelm@58706
   266
  {
wenzelm@58706
   267
    /* stack operations */
wenzelm@58706
   268
wenzelm@58706
   269
    def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
wenzelm@58706
   270
      new mutable.ListBuffer[Outer_Syntax.Document]
wenzelm@58706
   271
wenzelm@58747
   272
    var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
wenzelm@58747
   273
      List((0, Command.empty, buffer()))
wenzelm@55616
   274
wenzelm@58706
   275
    @tailrec def close(level: Int => Boolean)
wenzelm@58706
   276
    {
wenzelm@58706
   277
      stack match {
wenzelm@58747
   278
        case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@58747
   279
          body2 += Outer_Syntax.Document_Block(command.name, command.source, body.toList)
wenzelm@58706
   280
          stack = stack.tail
wenzelm@58706
   281
          close(level)
wenzelm@58706
   282
        case _ =>
wenzelm@58706
   283
      }
wenzelm@58706
   284
    }
wenzelm@58706
   285
wenzelm@58743
   286
    def result(): List[Outer_Syntax.Document] =
wenzelm@58706
   287
    {
wenzelm@58706
   288
      close(_ => true)
wenzelm@58743
   289
      stack.head._3.toList
wenzelm@58706
   290
    }
wenzelm@58706
   291
wenzelm@58706
   292
    def add(command: Command)
wenzelm@58706
   293
    {
wenzelm@58706
   294
      heading_level(command) match {
wenzelm@58706
   295
        case Some(i) =>
wenzelm@58706
   296
          close(_ > i)
wenzelm@58747
   297
          stack = (i + 1, command, buffer()) :: stack
wenzelm@58706
   298
        case None =>
wenzelm@58706
   299
      }
wenzelm@58706
   300
      stack.head._3 += Outer_Syntax.Document_Atom(command)
wenzelm@58706
   301
    }
wenzelm@58706
   302
wenzelm@58706
   303
wenzelm@58706
   304
    /* result structure */
wenzelm@58706
   305
wenzelm@58706
   306
    val spans = parse_spans(text)
wenzelm@58706
   307
    spans.foreach(span => add(Command(Document_ID.none, node_name, Nil, span)))
wenzelm@58706
   308
    result()
wenzelm@55616
   309
  }
wenzelm@34166
   310
}