src/Pure/Isar/document_structure.scala
author wenzelm
Tue, 22 Jan 2019 19:36:17 +0100
changeset 69719 331ef175a112
parent 68845 3b2daa7bf9f4
child 69859 e18ba60a1cf8
permissions -rw-r--r--
Backed out changeset 1bc422c08209 -- obsolete in AFP/5d11846ac6ab;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Isar/document_structure.scala
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     3
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     4
Overall document structure.
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     5
*/
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     6
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     8
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
     9
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    10
import scala.collection.mutable
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    11
import scala.annotation.tailrec
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    12
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    13
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    14
object Document_Structure
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    15
{
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    16
  /** general structure **/
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    17
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    18
  sealed abstract class Document { def length: Int }
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    19
  case class Block(name: String, text: String, body: List[Document]) extends Document
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    20
  { val length: Int = (0 /: body)(_ + _.length) }
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    21
  case class Atom(length: Int) extends Document
63605
wenzelm
parents: 63604
diff changeset
    22
68840
51ab4c78235b tuned signature;
wenzelm
parents: 64610
diff changeset
    23
  private def is_theory_command(keywords: Keyword.Keywords, command: Command): Boolean =
68845
3b2daa7bf9f4 support Thy_Element in Scala, following ML version;
wenzelm
parents: 68840
diff changeset
    24
    command.span.is_kind(keywords,
3b2daa7bf9f4 support Thy_Element in Scala, following ML version;
wenzelm
parents: 68840
diff changeset
    25
      kind => Keyword.theory(kind) && !Keyword.theory_end(kind), false)
63605
wenzelm
parents: 63604
diff changeset
    26
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    27
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    28
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    29
  /** context blocks **/
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    30
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    31
  def parse_blocks(
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    32
    syntax: Outer_Syntax,
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    33
    node_name: Document.Node.Name,
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    34
    text: CharSequence): List[Document] =
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    35
  {
63607
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    36
    def is_plain_theory(command: Command): Boolean =
68840
51ab4c78235b tuned signature;
wenzelm
parents: 64610
diff changeset
    37
      is_theory_command(syntax.keywords, command) &&
63607
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    38
      !command.span.is_begin && !command.span.is_end
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    39
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    40
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    41
    /* stack operations */
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    42
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    43
    def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document]
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    44
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    45
    var stack: List[(Command, mutable.ListBuffer[Document])] =
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    46
      List((Command.empty, buffer()))
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    47
63607
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    48
    def open(command: Command) { stack = (command, buffer()) :: stack }
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    49
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    50
    def close(): Boolean =
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    51
      stack match {
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    52
        case (command, body) :: (_, body2) :: _ =>
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    53
          body2 += Block(command.span.name, command.source, body.toList)
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    54
          stack = stack.tail
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    55
          true
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    56
        case _ =>
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    57
          false
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    58
      }
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    59
63607
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    60
    def flush() { if (is_plain_theory(stack.head._1)) close() }
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    61
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    62
    def result(): List[Document] =
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    63
    {
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    64
      while (close()) { }
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    65
      stack.head._2.toList
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    66
    }
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    67
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    68
    def add(command: Command)
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    69
    {
63607
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    70
      if (command.span.is_begin || is_plain_theory(command)) { flush(); open(command) }
7246254d558f clarified handling of plain theory commands;
wenzelm
parents: 63606
diff changeset
    71
      else if (command.span.is_end) { flush(); close() }
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    72
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    73
      stack.head._2 += Atom(command.source.length)
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    74
    }
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    75
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    76
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    77
    /* result structure */
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    78
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    79
    val spans = syntax.parse_spans(text)
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    80
    spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    81
    result()
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    82
  }
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    83
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    84
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 63605
diff changeset
    85
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    86
  /** section headings **/
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    87
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    88
  trait Item
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    89
  {
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    90
    def name: String = ""
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    91
    def source: String = ""
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    92
    def heading_level: Option[Int] = None
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    93
  }
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    94
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    95
  object No_Item extends Item
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
    96
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    97
  class Sections(keywords: Keyword.Keywords)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    98
  {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
    99
    private def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document]
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   100
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   101
    private var stack: List[(Int, Item, mutable.ListBuffer[Document])] =
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   102
      List((0, No_Item, buffer()))
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   103
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   104
    @tailrec private def close(level: Int => Boolean)
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   105
    {
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   106
      stack match {
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   107
        case (lev, item, body) :: (_, _, body2) :: _ if level(lev) =>
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   108
          body2 += Block(item.name, item.source, body.toList)
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   109
          stack = stack.tail
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   110
          close(level)
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   111
        case _ =>
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   112
      }
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   113
    }
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   114
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   115
    def result(): List[Document] =
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   116
    {
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   117
      close(_ => true)
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   118
      stack.head._3.toList
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   119
    }
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   120
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   121
    def add(item: Item)
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   122
    {
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   123
      item.heading_level match {
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   124
        case Some(i) =>
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   125
          close(_ > i)
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   126
          stack = (i + 1, item, buffer()) :: stack
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   127
        case None =>
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   128
      }
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   129
      stack.head._3 += Atom(item.source.length)
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   130
    }
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   131
  }
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   132
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   133
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   134
  /* outer syntax sections */
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   135
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   136
  private class Command_Item(keywords: Keyword.Keywords, command: Command) extends Item
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   137
  {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   138
    override def name: String = command.span.name
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   139
    override def source: String = command.source
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   140
    override def heading_level: Option[Int] =
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   141
    {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   142
      name match {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   143
        case Thy_Header.CHAPTER => Some(0)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   144
        case Thy_Header.SECTION => Some(1)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   145
        case Thy_Header.SUBSECTION => Some(2)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   146
        case Thy_Header.SUBSUBSECTION => Some(3)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   147
        case Thy_Header.PARAGRAPH => Some(4)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   148
        case Thy_Header.SUBPARAGRAPH => Some(5)
68840
51ab4c78235b tuned signature;
wenzelm
parents: 64610
diff changeset
   149
        case _ if is_theory_command(keywords, command) => Some(6)
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   150
        case _ => None
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   151
      }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   152
    }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   153
  }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   154
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   155
  def parse_sections(
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   156
    syntax: Outer_Syntax,
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   157
    node_name: Document.Node.Name,
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   158
    text: CharSequence): List[Document] =
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   159
  {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   160
    val sections = new Sections(syntax.keywords)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   161
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   162
    for { span <- syntax.parse_spans(text) } {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   163
      sections.add(
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   164
        new Command_Item(syntax.keywords,
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   165
          Command(Document_ID.none, node_name, Command.no_blobs, span)))
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   166
    }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   167
    sections.result()
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   168
  }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   169
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   170
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   171
  /* ML sections */
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   172
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   173
  private class ML_Item(token: ML_Lex.Token, level: Option[Int]) extends Item
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   174
  {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   175
    override def source: String = token.source
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   176
    override def heading_level: Option[Int] = level
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   177
  }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   178
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   179
  def parse_ml_sections(SML: Boolean, text: CharSequence): List[Document] =
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   180
  {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   181
    val sections = new Sections(Keyword.Keywords.empty)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   182
    val nl = new ML_Item(ML_Lex.Token(ML_Lex.Kind.SPACE, "\n"), None)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   183
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   184
    var context: Scan.Line_Context = Scan.Finished
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   185
    for (line <- Library.separated_chunks(_ == '\n', text)) {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   186
      val (toks, next_context) = ML_Lex.tokenize_line(SML, line, context)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   187
      val level =
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   188
        toks.filterNot(_.is_space) match {
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   189
          case List(tok) if tok.is_comment =>
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   190
            val s = tok.source
64610
1b89608974e9 clarified modules;
wenzelm
parents: 63666
diff changeset
   191
            if (Codepoint.iterator(s).exists(c => Character.isLetter(c) || Character.isDigit(c)))
63666
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   192
            {
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   193
              if (s.startsWith("(**** ") && s.endsWith(" ****)")) Some(0)
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   194
              else if (s.startsWith("(*** ") && s.endsWith(" ***)")) Some(1)
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   195
              else if (s.startsWith("(** ") && s.endsWith(" **)")) Some(2)
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   196
              else if (s.startsWith("(* ") && s.endsWith(" *)")) Some(3)
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   197
              else None
ff6caffcaed4 suppress ASCII art;
wenzelm
parents: 63610
diff changeset
   198
            }
63610
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   199
            else None
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   200
          case _ => None
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   201
        }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   202
      if (level.isDefined && context == Scan.Finished && next_context == Scan.Finished)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   203
        toks.foreach(tok => sections.add(new ML_Item(tok, if (tok.is_comment) level else None)))
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   204
      else
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   205
        toks.foreach(tok => sections.add(new ML_Item(tok, None)))
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   206
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   207
      sections.add(nl)
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   208
      context = next_context
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   209
    }
4b40b8196dc7 Sidekick parser for isabelle-ml and sml mode;
wenzelm
parents: 63607
diff changeset
   210
    sections.result()
63604
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   211
  }
d8de4f8b95eb clarified modules;
wenzelm
parents:
diff changeset
   212
}