63604
|
1 |
/* Title: Pure/Isar/document_structure.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Overall document structure.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
import scala.collection.mutable
|
|
11 |
import scala.annotation.tailrec
|
|
12 |
|
|
13 |
|
|
14 |
object Document_Structure
|
|
15 |
{
|
63605
|
16 |
/* general structure */
|
63604
|
17 |
|
|
18 |
sealed abstract class Document { def length: Int }
|
|
19 |
case class Block(name: String, text: String, body: List[Document]) extends Document
|
|
20 |
{ val length: Int = (0 /: body)(_ + _.length) }
|
|
21 |
case class Atom(command: Command) extends Document
|
|
22 |
{ def length: Int = command.length }
|
|
23 |
|
63605
|
24 |
|
|
25 |
/* section headings etc. */
|
|
26 |
|
63604
|
27 |
def heading_level(keywords: Keyword.Keywords, command: Command): Option[Int] =
|
|
28 |
{
|
|
29 |
val name = command.span.name
|
|
30 |
name match {
|
|
31 |
case Thy_Header.CHAPTER => Some(0)
|
|
32 |
case Thy_Header.SECTION => Some(1)
|
|
33 |
case Thy_Header.SUBSECTION => Some(2)
|
|
34 |
case Thy_Header.SUBSUBSECTION => Some(3)
|
|
35 |
case Thy_Header.PARAGRAPH => Some(4)
|
|
36 |
case Thy_Header.SUBPARAGRAPH => Some(5)
|
|
37 |
case _ =>
|
|
38 |
keywords.kinds.get(name) match {
|
|
39 |
case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
|
|
40 |
case _ => None
|
|
41 |
}
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
63605
|
45 |
def parse_sections(
|
63604
|
46 |
syntax: Outer_Syntax,
|
|
47 |
node_name: Document.Node.Name,
|
|
48 |
text: CharSequence): List[Document] =
|
|
49 |
{
|
|
50 |
/* stack operations */
|
|
51 |
|
|
52 |
def buffer(): mutable.ListBuffer[Document] =
|
|
53 |
new mutable.ListBuffer[Document]
|
|
54 |
|
|
55 |
var stack: List[(Int, Command, mutable.ListBuffer[Document])] =
|
|
56 |
List((0, Command.empty, buffer()))
|
|
57 |
|
|
58 |
@tailrec def close(level: Int => Boolean)
|
|
59 |
{
|
|
60 |
stack match {
|
|
61 |
case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
|
|
62 |
body2 += Block(command.span.name, command.source, body.toList)
|
|
63 |
stack = stack.tail
|
|
64 |
close(level)
|
|
65 |
case _ =>
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
def result(): List[Document] =
|
|
70 |
{
|
|
71 |
close(_ => true)
|
|
72 |
stack.head._3.toList
|
|
73 |
}
|
|
74 |
|
|
75 |
def add(command: Command)
|
|
76 |
{
|
|
77 |
heading_level(syntax.keywords, command) match {
|
|
78 |
case Some(i) =>
|
|
79 |
close(_ > i)
|
|
80 |
stack = (i + 1, command, buffer()) :: stack
|
|
81 |
case None =>
|
|
82 |
}
|
|
83 |
stack.head._3 += Atom(command)
|
|
84 |
}
|
|
85 |
|
|
86 |
|
|
87 |
/* result structure */
|
|
88 |
|
|
89 |
val spans = syntax.parse_spans(text)
|
|
90 |
spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
|
|
91 |
result()
|
|
92 |
}
|
|
93 |
}
|