src/Pure/Isar/outer_syntax.scala
author wenzelm
Sun Mar 15 19:21:15 2015 +0100 (2015-03-15)
changeset 59702 58dfaa369c11
parent 59700 d887abcc7c24
child 59735 24bee1b11fce
permissions -rw-r--r--
hybrid use of command blobs: inlined errors and auxiliary files;
static check of theory imports;
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@43774
    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@58907
    88
  def + (name: String): Outer_Syntax = this + (name, None, None)
wenzelm@58907
    89
  def + (name: String, kind: String): Outer_Syntax = this + (name, Some((kind, Nil)), None)
wenzelm@58901
    90
  def + (name: String, opt_kind: Option[(String, List[String])], replace: Option[String])
wenzelm@58901
    91
    : Outer_Syntax =
wenzelm@53280
    92
  {
wenzelm@58901
    93
    val keywords1 =
wenzelm@58901
    94
      opt_kind match {
wenzelm@58901
    95
        case None => keywords + name
wenzelm@58901
    96
        case Some(kind) => keywords + (name, kind)
wenzelm@58901
    97
      }
wenzelm@53280
    98
    val completion1 =
wenzelm@58853
    99
      if (replace == Some("")) completion
wenzelm@53280
   100
      else completion + (name, replace getOrElse name)
wenzelm@58900
   101
    new Outer_Syntax(keywords1, completion1, language_context, true)
wenzelm@53280
   102
  }
wenzelm@48706
   103
wenzelm@48873
   104
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
   105
    (this /: keywords) {
wenzelm@58901
   106
      case (syntax, (name, opt_spec, replace)) =>
wenzelm@58901
   107
        val opt_kind = opt_spec.map(_._1)
wenzelm@50128
   108
        syntax +
wenzelm@58901
   109
          (Symbol.decode(name), opt_kind, replace) +
wenzelm@58901
   110
          (Symbol.encode(name), opt_kind, replace)
wenzelm@46940
   111
    }
wenzelm@34166
   112
wenzelm@58695
   113
wenzelm@59073
   114
  /* merge */
wenzelm@59073
   115
wenzelm@59077
   116
  def ++ (other: Prover.Syntax): Prover.Syntax =
wenzelm@59073
   117
    if (this eq other) this
wenzelm@59073
   118
    else {
wenzelm@59077
   119
      val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
wenzelm@59077
   120
      val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
wenzelm@59077
   121
      if ((keywords eq keywords1) && (completion eq completion1)) this
wenzelm@59077
   122
      else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
wenzelm@59073
   123
    }
wenzelm@59073
   124
wenzelm@59073
   125
wenzelm@59702
   126
  /* command categories */
wenzelm@59702
   127
wenzelm@59702
   128
  def is_theory_begin(name: String): Boolean = keywords.is_command_kind(name, Keyword.theory_begin)
wenzelm@58900
   129
wenzelm@58900
   130
  def load_command(name: String): Option[List[String]] = keywords.load_command(name)
wenzelm@58900
   131
  def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
wenzelm@58900
   132
wenzelm@58900
   133
wenzelm@58706
   134
  /* language context */
wenzelm@34166
   135
wenzelm@58706
   136
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@58900
   137
    new Outer_Syntax(keywords, completion, context, has_tokens)
wenzelm@58706
   138
wenzelm@58706
   139
  def no_tokens: Outer_Syntax =
wenzelm@46969
   140
  {
wenzelm@58900
   141
    require(keywords.is_empty)
wenzelm@58706
   142
    new Outer_Syntax(
wenzelm@58706
   143
      completion = completion,
wenzelm@58706
   144
      language_context = language_context,
wenzelm@58706
   145
      has_tokens = false)
wenzelm@46969
   146
  }
wenzelm@40454
   147
wenzelm@58706
   148
wenzelm@40454
   149
wenzelm@58706
   150
  /** parsing **/
wenzelm@34166
   151
wenzelm@58697
   152
  /* line-oriented structure */
wenzelm@58696
   153
wenzelm@58700
   154
  def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure)
wenzelm@58700
   155
    : Outer_Syntax.Line_Structure =
wenzelm@58696
   156
  {
wenzelm@58700
   157
    val improper1 = tokens.forall(_.is_improper)
wenzelm@58700
   158
    val command1 = tokens.exists(_.is_command)
wenzelm@58700
   159
wenzelm@58696
   160
    val depth1 =
wenzelm@59122
   161
      if (tokens.exists(tok => tok.is_command_kind(keywords, Keyword.theory))) 0
wenzelm@58700
   162
      else if (command1) struct.after_span_depth
wenzelm@58700
   163
      else struct.span_depth
wenzelm@58700
   164
wenzelm@58700
   165
    val (span_depth1, after_span_depth1) =
wenzelm@58700
   166
      ((struct.span_depth, struct.after_span_depth) /: tokens) {
wenzelm@58703
   167
        case ((x, y), tok) =>
wenzelm@58703
   168
          if (tok.is_command) {
wenzelm@59122
   169
            if (tok.is_command_kind(keywords, Keyword.theory_goal))
wenzelm@58900
   170
              (2, 1)
wenzelm@59122
   171
            else if (tok.is_command_kind(keywords, Keyword.theory))
wenzelm@58900
   172
              (1, 0)
wenzelm@59122
   173
            else if (tok.is_command_kind(keywords, Keyword.proof_goal) || tok.is_begin_block)
wenzelm@58900
   174
              (y + 2, y + 1)
wenzelm@59122
   175
            else if (tok.is_command_kind(keywords, Keyword.qed) || tok.is_end_block)
wenzelm@58900
   176
              (y + 1, y - 1)
wenzelm@59122
   177
            else if (tok.is_command_kind(keywords, Keyword.qed_global))
wenzelm@58900
   178
              (1, 0)
wenzelm@58703
   179
            else (x, y)
wenzelm@58703
   180
          }
wenzelm@58703
   181
          else (x, y)
wenzelm@58696
   182
      }
wenzelm@58700
   183
wenzelm@58700
   184
    Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
wenzelm@58696
   185
  }
wenzelm@58696
   186
wenzelm@58696
   187
wenzelm@58706
   188
  /* command spans */
wenzelm@57905
   189
wenzelm@57905
   190
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   191
  {
wenzelm@57905
   192
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   193
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   194
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   195
wenzelm@57905
   196
    def ship(span: List[Token])
wenzelm@57905
   197
    {
wenzelm@57905
   198
      val kind =
wenzelm@59319
   199
        if (span.nonEmpty && span.head.is_command && !span.exists(_.is_error)) {
wenzelm@57910
   200
          val name = span.head.source
wenzelm@57911
   201
          val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
wenzelm@57910
   202
          Command_Span.Command_Span(name, pos)
wenzelm@57910
   203
        }
wenzelm@57905
   204
        else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@57905
   205
        else Command_Span.Malformed_Span
wenzelm@57905
   206
      result += Command_Span.Span(kind, span)
wenzelm@57905
   207
    }
wenzelm@57905
   208
wenzelm@57905
   209
    def flush()
wenzelm@57905
   210
    {
wenzelm@59319
   211
      if (content.nonEmpty) { ship(content.toList); content.clear }
wenzelm@59319
   212
      if (improper.nonEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   213
    }
wenzelm@57905
   214
wenzelm@57905
   215
    for (tok <- toks) {
wenzelm@57905
   216
      if (tok.is_command) { flush(); content += tok }
wenzelm@57905
   217
      else if (tok.is_improper) improper += tok
wenzelm@57905
   218
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   219
    }
wenzelm@57905
   220
    flush()
wenzelm@57905
   221
wenzelm@57905
   222
    result.toList
wenzelm@57905
   223
  }
wenzelm@57905
   224
wenzelm@57906
   225
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@59083
   226
    parse_spans(Token.explode(keywords, input))
wenzelm@57906
   227
wenzelm@57905
   228
wenzelm@58706
   229
  /* overall document structure */
wenzelm@55616
   230
wenzelm@58706
   231
  def heading_level(command: Command): Option[Int] =
wenzelm@58706
   232
  {
wenzelm@58868
   233
    command.name match {
wenzelm@58868
   234
      case "chapter" => Some(0)
wenzelm@58868
   235
      case "section" | "header" => Some(1)
wenzelm@58868
   236
      case "subsection" => Some(2)
wenzelm@58868
   237
      case "subsubsection" => Some(3)
wenzelm@58868
   238
      case _ =>
wenzelm@58901
   239
        keywords.command_kind(command.name) match {
wenzelm@59700
   240
          case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(4)
wenzelm@58868
   241
          case _ => None
wenzelm@58868
   242
        }
wenzelm@58706
   243
    }
wenzelm@58706
   244
  }
wenzelm@58706
   245
wenzelm@58743
   246
  def parse_document(node_name: Document.Node.Name, text: CharSequence):
wenzelm@58743
   247
    List[Outer_Syntax.Document] =
wenzelm@58706
   248
  {
wenzelm@58706
   249
    /* stack operations */
wenzelm@58706
   250
wenzelm@58706
   251
    def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
wenzelm@58706
   252
      new mutable.ListBuffer[Outer_Syntax.Document]
wenzelm@58706
   253
wenzelm@58747
   254
    var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
wenzelm@58747
   255
      List((0, Command.empty, buffer()))
wenzelm@55616
   256
wenzelm@58706
   257
    @tailrec def close(level: Int => Boolean)
wenzelm@58706
   258
    {
wenzelm@58706
   259
      stack match {
wenzelm@58747
   260
        case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@58747
   261
          body2 += Outer_Syntax.Document_Block(command.name, command.source, body.toList)
wenzelm@58706
   262
          stack = stack.tail
wenzelm@58706
   263
          close(level)
wenzelm@58706
   264
        case _ =>
wenzelm@58706
   265
      }
wenzelm@58706
   266
    }
wenzelm@58706
   267
wenzelm@58743
   268
    def result(): List[Outer_Syntax.Document] =
wenzelm@58706
   269
    {
wenzelm@58706
   270
      close(_ => true)
wenzelm@58743
   271
      stack.head._3.toList
wenzelm@58706
   272
    }
wenzelm@58706
   273
wenzelm@58706
   274
    def add(command: Command)
wenzelm@58706
   275
    {
wenzelm@58706
   276
      heading_level(command) match {
wenzelm@58706
   277
        case Some(i) =>
wenzelm@58706
   278
          close(_ > i)
wenzelm@58747
   279
          stack = (i + 1, command, buffer()) :: stack
wenzelm@58706
   280
        case None =>
wenzelm@58706
   281
      }
wenzelm@58706
   282
      stack.head._3 += Outer_Syntax.Document_Atom(command)
wenzelm@58706
   283
    }
wenzelm@58706
   284
wenzelm@58706
   285
wenzelm@58706
   286
    /* result structure */
wenzelm@58706
   287
wenzelm@58706
   288
    val spans = parse_spans(text)
wenzelm@59702
   289
    spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
wenzelm@58706
   290
    result()
wenzelm@55616
   291
  }
wenzelm@34166
   292
}