src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Aug 04 10:55:51 2016 +0200 (2016-08-04)
changeset 63603 9d9ea2c6bc38
parent 63592 64db21931bcb
child 63604 d8de4f8b95eb
permissions -rw-r--r--
clarified modules;
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@58706
    47
  /* overall document structure */
wenzelm@58706
    48
wenzelm@58706
    49
  sealed abstract class Document { def length: Int }
wenzelm@58747
    50
  case class Document_Block(name: String, text: String, body: List[Document]) extends Document
wenzelm@58706
    51
  {
wenzelm@58706
    52
    val length: Int = (0 /: body)(_ + _.length)
wenzelm@58706
    53
  }
wenzelm@58747
    54
  case class Document_Atom(command: Command) extends Document
wenzelm@58706
    55
  {
wenzelm@58706
    56
    def length: Int = command.length
wenzelm@58706
    57
  }
wenzelm@43774
    58
}
wenzelm@43774
    59
wenzelm@46712
    60
final class Outer_Syntax private(
wenzelm@58900
    61
  val keywords: Keyword.Keywords = Keyword.Keywords.empty,
wenzelm@53280
    62
  val completion: Completion = Completion.empty,
wenzelm@55749
    63
  val language_context: Completion.Language_Context = Completion.Language_Context.outer,
wenzelm@63584
    64
  val has_tokens: Boolean = true)
wenzelm@34166
    65
{
wenzelm@58706
    66
  /** syntax content **/
wenzelm@58706
    67
wenzelm@58900
    68
  override def toString: String = keywords.toString
wenzelm@56393
    69
wenzelm@58695
    70
wenzelm@58695
    71
  /* add keywords */
wenzelm@58695
    72
wenzelm@63579
    73
  def + (name: String, kind: String = "", tags: List[String] = Nil): Outer_Syntax =
wenzelm@53280
    74
  {
wenzelm@63429
    75
    val keywords1 = keywords + (name, kind, tags)
wenzelm@53280
    76
    val completion1 =
wenzelm@63587
    77
      completion.add_keyword(name).
wenzelm@63587
    78
        add_abbrevs(
wenzelm@63587
    79
          (if (Keyword.theory_block.contains(kind)) List((name, name + "\nbegin\n\u0007\nend"))
wenzelm@63587
    80
           else Nil) :::
wenzelm@63587
    81
          (if (Completion.Word_Parsers.is_word(name)) List((name, name)) else Nil))
wenzelm@58900
    82
    new Outer_Syntax(keywords1, completion1, language_context, true)
wenzelm@53280
    83
  }
wenzelm@48706
    84
wenzelm@48873
    85
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    86
    (this /: keywords) {
wenzelm@63579
    87
      case (syntax, (name, ((kind, tags), _))) =>
wenzelm@63579
    88
        syntax + (Symbol.decode(name), kind, tags) + (Symbol.encode(name), kind, tags)
wenzelm@63579
    89
    }
wenzelm@63579
    90
wenzelm@63579
    91
  def add_abbrevs(abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
wenzelm@63579
    92
    if (abbrevs.isEmpty) this
wenzelm@63579
    93
    else {
wenzelm@63579
    94
      val completion1 =
wenzelm@63579
    95
        completion.add_abbrevs(
wenzelm@63579
    96
          (for ((a, b) <- abbrevs) yield {
wenzelm@63579
    97
            val a1 = Symbol.decode(a)
wenzelm@63579
    98
            val a2 = Symbol.encode(a)
wenzelm@63579
    99
            val b1 = Symbol.decode(b)
wenzelm@63579
   100
            List((a1, b1), (a2, b1))
wenzelm@63579
   101
          }).flatten)
wenzelm@63579
   102
      new Outer_Syntax(keywords, completion1, language_context, has_tokens)
wenzelm@46940
   103
    }
wenzelm@34166
   104
wenzelm@58695
   105
wenzelm@59073
   106
  /* merge */
wenzelm@59073
   107
wenzelm@63584
   108
  def ++ (other: Outer_Syntax): Outer_Syntax =
wenzelm@59073
   109
    if (this eq other) this
wenzelm@59073
   110
    else {
wenzelm@59077
   111
      val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
wenzelm@59077
   112
      val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
wenzelm@59077
   113
      if ((keywords eq keywords1) && (completion eq completion1)) this
wenzelm@59077
   114
      else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
wenzelm@59073
   115
    }
wenzelm@59073
   116
wenzelm@59073
   117
wenzelm@59735
   118
  /* load commands */
wenzelm@58900
   119
wenzelm@63441
   120
  def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
wenzelm@58900
   121
  def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
wenzelm@58900
   122
wenzelm@58900
   123
wenzelm@58706
   124
  /* language context */
wenzelm@34166
   125
wenzelm@58706
   126
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@58900
   127
    new Outer_Syntax(keywords, completion, context, has_tokens)
wenzelm@58706
   128
wenzelm@58706
   129
  def no_tokens: Outer_Syntax =
wenzelm@46969
   130
  {
wenzelm@58900
   131
    require(keywords.is_empty)
wenzelm@58706
   132
    new Outer_Syntax(
wenzelm@58706
   133
      completion = completion,
wenzelm@58706
   134
      language_context = language_context,
wenzelm@58706
   135
      has_tokens = false)
wenzelm@46969
   136
  }
wenzelm@40454
   137
wenzelm@58706
   138
wenzelm@40454
   139
wenzelm@58706
   140
  /** parsing **/
wenzelm@34166
   141
wenzelm@58706
   142
  /* command spans */
wenzelm@57905
   143
wenzelm@57905
   144
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   145
  {
wenzelm@57905
   146
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   147
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   148
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   149
wenzelm@57905
   150
    def ship(span: List[Token])
wenzelm@57905
   151
    {
wenzelm@57905
   152
      val kind =
wenzelm@59924
   153
        if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@59924
   154
        else if (span.exists(_.is_error)) Command_Span.Malformed_Span
wenzelm@59924
   155
        else
wenzelm@59924
   156
          span.find(_.is_command) match {
wenzelm@59924
   157
            case None => Command_Span.Malformed_Span
wenzelm@59924
   158
            case Some(cmd) =>
wenzelm@59924
   159
              val name = cmd.source
wenzelm@59924
   160
              val offset =
wenzelm@59924
   161
                (0 /: span.takeWhile(_ != cmd)) {
wenzelm@59924
   162
                  case (i, tok) => i + Symbol.iterator(tok.source).length }
wenzelm@59924
   163
              val end_offset = offset + Symbol.iterator(name).length
wenzelm@59924
   164
              val pos = Position.Range(Text.Range(offset, end_offset) + 1)
wenzelm@59924
   165
              Command_Span.Command_Span(name, pos)
wenzelm@59924
   166
          }
wenzelm@57905
   167
      result += Command_Span.Span(kind, span)
wenzelm@57905
   168
    }
wenzelm@57905
   169
wenzelm@57905
   170
    def flush()
wenzelm@57905
   171
    {
wenzelm@59319
   172
      if (content.nonEmpty) { ship(content.toList); content.clear }
wenzelm@59319
   173
      if (improper.nonEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   174
    }
wenzelm@57905
   175
wenzelm@57905
   176
    for (tok <- toks) {
wenzelm@59924
   177
      if (tok.is_improper) improper += tok
wenzelm@63441
   178
      else if (keywords.is_before_command(tok) ||
wenzelm@63441
   179
        tok.is_command &&
wenzelm@63441
   180
          (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
wenzelm@59924
   181
      { flush(); content += tok }
wenzelm@57905
   182
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   183
    }
wenzelm@57905
   184
    flush()
wenzelm@57905
   185
wenzelm@57905
   186
    result.toList
wenzelm@57905
   187
  }
wenzelm@57905
   188
wenzelm@57906
   189
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@59083
   190
    parse_spans(Token.explode(keywords, input))
wenzelm@57906
   191
wenzelm@57905
   192
wenzelm@58706
   193
  /* overall document structure */
wenzelm@55616
   194
wenzelm@58706
   195
  def heading_level(command: Command): Option[Int] =
wenzelm@58706
   196
  {
wenzelm@59735
   197
    val name = command.span.name
wenzelm@59735
   198
    name match {
wenzelm@59735
   199
      case Thy_Header.CHAPTER => Some(0)
wenzelm@62453
   200
      case Thy_Header.SECTION => Some(1)
wenzelm@59735
   201
      case Thy_Header.SUBSECTION => Some(2)
wenzelm@59735
   202
      case Thy_Header.SUBSUBSECTION => Some(3)
wenzelm@61463
   203
      case Thy_Header.PARAGRAPH => Some(4)
wenzelm@61463
   204
      case Thy_Header.SUBPARAGRAPH => Some(5)
wenzelm@58868
   205
      case _ =>
wenzelm@63441
   206
        keywords.kinds.get(name) match {
wenzelm@61463
   207
          case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
wenzelm@58868
   208
          case _ => None
wenzelm@58868
   209
        }
wenzelm@58706
   210
    }
wenzelm@58706
   211
  }
wenzelm@58706
   212
wenzelm@58743
   213
  def parse_document(node_name: Document.Node.Name, text: CharSequence):
wenzelm@58743
   214
    List[Outer_Syntax.Document] =
wenzelm@58706
   215
  {
wenzelm@58706
   216
    /* stack operations */
wenzelm@58706
   217
wenzelm@58706
   218
    def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
wenzelm@58706
   219
      new mutable.ListBuffer[Outer_Syntax.Document]
wenzelm@58706
   220
wenzelm@58747
   221
    var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
wenzelm@58747
   222
      List((0, Command.empty, buffer()))
wenzelm@55616
   223
wenzelm@58706
   224
    @tailrec def close(level: Int => Boolean)
wenzelm@58706
   225
    {
wenzelm@58706
   226
      stack match {
wenzelm@58747
   227
        case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@59735
   228
          body2 += Outer_Syntax.Document_Block(command.span.name, command.source, body.toList)
wenzelm@58706
   229
          stack = stack.tail
wenzelm@58706
   230
          close(level)
wenzelm@58706
   231
        case _ =>
wenzelm@58706
   232
      }
wenzelm@58706
   233
    }
wenzelm@58706
   234
wenzelm@58743
   235
    def result(): List[Outer_Syntax.Document] =
wenzelm@58706
   236
    {
wenzelm@58706
   237
      close(_ => true)
wenzelm@58743
   238
      stack.head._3.toList
wenzelm@58706
   239
    }
wenzelm@58706
   240
wenzelm@58706
   241
    def add(command: Command)
wenzelm@58706
   242
    {
wenzelm@58706
   243
      heading_level(command) match {
wenzelm@58706
   244
        case Some(i) =>
wenzelm@58706
   245
          close(_ > i)
wenzelm@58747
   246
          stack = (i + 1, command, buffer()) :: stack
wenzelm@58706
   247
        case None =>
wenzelm@58706
   248
      }
wenzelm@58706
   249
      stack.head._3 += Outer_Syntax.Document_Atom(command)
wenzelm@58706
   250
    }
wenzelm@58706
   251
wenzelm@58706
   252
wenzelm@58706
   253
    /* result structure */
wenzelm@58706
   254
wenzelm@58706
   255
    val spans = parse_spans(text)
wenzelm@59702
   256
    spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
wenzelm@58706
   257
    result()
wenzelm@55616
   258
  }
wenzelm@34166
   259
}