| author | wenzelm |
| Sun, 03 Mar 2019 20:13:25 +0100 | |
| changeset 69859 | e18ba60a1cf8 |
| parent 68845 | 3b2daa7bf9f4 |
| child 69860 | b58a575d211e |
| permissions | -rw-r--r-- |
| 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 |
{
|
|
| 63610 | 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) }
|
|
| 63610 | 21 |
case class Atom(length: Int) extends Document |
| 63605 | 22 |
|
| 69859 | 23 |
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 | 26 |
|
| 69859 | 27 |
def heading_level(keywords: Keyword.Keywords, command: Command): Option[Int] = |
28 |
{
|
|
29 |
command.span.name match {
|
|
30 |
case Thy_Header.CHAPTER => Some(0) |
|
31 |
case Thy_Header.SECTION => Some(1) |
|
32 |
case Thy_Header.SUBSECTION => Some(2) |
|
33 |
case Thy_Header.SUBSUBSECTION => Some(3) |
|
34 |
case Thy_Header.PARAGRAPH => Some(4) |
|
35 |
case Thy_Header.SUBPARAGRAPH => Some(5) |
|
36 |
case _ if is_theory_command(keywords, command) => Some(6) |
|
37 |
case _ => None |
|
38 |
} |
|
39 |
} |
|
40 |
||
| 63604 | 41 |
|
| 63606 | 42 |
|
| 63610 | 43 |
/** context blocks **/ |
| 63606 | 44 |
|
45 |
def parse_blocks( |
|
46 |
syntax: Outer_Syntax, |
|
47 |
node_name: Document.Node.Name, |
|
48 |
text: CharSequence): List[Document] = |
|
49 |
{
|
|
| 63607 | 50 |
def is_plain_theory(command: Command): Boolean = |
| 68840 | 51 |
is_theory_command(syntax.keywords, command) && |
| 63607 | 52 |
!command.span.is_begin && !command.span.is_end |
53 |
||
54 |
||
| 63606 | 55 |
/* stack operations */ |
56 |
||
57 |
def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document] |
|
58 |
||
59 |
var stack: List[(Command, mutable.ListBuffer[Document])] = |
|
60 |
List((Command.empty, buffer())) |
|
61 |
||
| 63607 | 62 |
def open(command: Command) { stack = (command, buffer()) :: stack }
|
63 |
||
| 63606 | 64 |
def close(): Boolean = |
65 |
stack match {
|
|
66 |
case (command, body) :: (_, body2) :: _ => |
|
67 |
body2 += Block(command.span.name, command.source, body.toList) |
|
68 |
stack = stack.tail |
|
69 |
true |
|
70 |
case _ => |
|
71 |
false |
|
72 |
} |
|
73 |
||
| 63607 | 74 |
def flush() { if (is_plain_theory(stack.head._1)) close() }
|
75 |
||
| 63606 | 76 |
def result(): List[Document] = |
77 |
{
|
|
78 |
while (close()) { }
|
|
79 |
stack.head._2.toList |
|
80 |
} |
|
81 |
||
82 |
def add(command: Command) |
|
83 |
{
|
|
| 63607 | 84 |
if (command.span.is_begin || is_plain_theory(command)) { flush(); open(command) }
|
85 |
else if (command.span.is_end) { flush(); close() }
|
|
| 63606 | 86 |
|
| 63610 | 87 |
stack.head._2 += Atom(command.source.length) |
| 63606 | 88 |
} |
89 |
||
90 |
||
91 |
/* result structure */ |
|
92 |
||
93 |
val spans = syntax.parse_spans(text) |
|
94 |
spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span))) |
|
95 |
result() |
|
96 |
} |
|
97 |
||
98 |
||
99 |
||
| 63610 | 100 |
/** section headings **/ |
101 |
||
102 |
trait Item |
|
| 63604 | 103 |
{
|
| 63610 | 104 |
def name: String = "" |
105 |
def source: String = "" |
|
106 |
def heading_level: Option[Int] = None |
|
107 |
} |
|
| 63604 | 108 |
|
| 63610 | 109 |
object No_Item extends Item |
| 63604 | 110 |
|
| 63610 | 111 |
class Sections(keywords: Keyword.Keywords) |
112 |
{
|
|
113 |
private def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document] |
|
| 63604 | 114 |
|
| 63610 | 115 |
private var stack: List[(Int, Item, mutable.ListBuffer[Document])] = |
116 |
List((0, No_Item, buffer())) |
|
117 |
||
118 |
@tailrec private def close(level: Int => Boolean) |
|
| 63604 | 119 |
{
|
120 |
stack match {
|
|
| 63610 | 121 |
case (lev, item, body) :: (_, _, body2) :: _ if level(lev) => |
122 |
body2 += Block(item.name, item.source, body.toList) |
|
| 63604 | 123 |
stack = stack.tail |
124 |
close(level) |
|
125 |
case _ => |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
def result(): List[Document] = |
|
130 |
{
|
|
131 |
close(_ => true) |
|
132 |
stack.head._3.toList |
|
133 |
} |
|
134 |
||
| 63610 | 135 |
def add(item: Item) |
| 63604 | 136 |
{
|
| 63610 | 137 |
item.heading_level match {
|
| 63604 | 138 |
case Some(i) => |
139 |
close(_ > i) |
|
| 63610 | 140 |
stack = (i + 1, item, buffer()) :: stack |
| 63604 | 141 |
case None => |
142 |
} |
|
| 63610 | 143 |
stack.head._3 += Atom(item.source.length) |
| 63604 | 144 |
} |
| 63610 | 145 |
} |
| 63604 | 146 |
|
147 |
||
| 63610 | 148 |
/* outer syntax sections */ |
149 |
||
150 |
private class Command_Item(keywords: Keyword.Keywords, command: Command) extends Item |
|
151 |
{
|
|
152 |
override def name: String = command.span.name |
|
153 |
override def source: String = command.source |
|
| 69859 | 154 |
override def heading_level: Option[Int] = Document_Structure.heading_level(keywords, command) |
| 63610 | 155 |
} |
156 |
||
157 |
def parse_sections( |
|
158 |
syntax: Outer_Syntax, |
|
159 |
node_name: Document.Node.Name, |
|
160 |
text: CharSequence): List[Document] = |
|
161 |
{
|
|
162 |
val sections = new Sections(syntax.keywords) |
|
163 |
||
164 |
for { span <- syntax.parse_spans(text) } {
|
|
165 |
sections.add( |
|
166 |
new Command_Item(syntax.keywords, |
|
167 |
Command(Document_ID.none, node_name, Command.no_blobs, span))) |
|
168 |
} |
|
169 |
sections.result() |
|
170 |
} |
|
171 |
||
| 63604 | 172 |
|
| 63610 | 173 |
/* ML sections */ |
174 |
||
175 |
private class ML_Item(token: ML_Lex.Token, level: Option[Int]) extends Item |
|
176 |
{
|
|
177 |
override def source: String = token.source |
|
178 |
override def heading_level: Option[Int] = level |
|
179 |
} |
|
180 |
||
181 |
def parse_ml_sections(SML: Boolean, text: CharSequence): List[Document] = |
|
182 |
{
|
|
183 |
val sections = new Sections(Keyword.Keywords.empty) |
|
184 |
val nl = new ML_Item(ML_Lex.Token(ML_Lex.Kind.SPACE, "\n"), None) |
|
185 |
||
186 |
var context: Scan.Line_Context = Scan.Finished |
|
187 |
for (line <- Library.separated_chunks(_ == '\n', text)) {
|
|
188 |
val (toks, next_context) = ML_Lex.tokenize_line(SML, line, context) |
|
189 |
val level = |
|
190 |
toks.filterNot(_.is_space) match {
|
|
191 |
case List(tok) if tok.is_comment => |
|
192 |
val s = tok.source |
|
| 64610 | 193 |
if (Codepoint.iterator(s).exists(c => Character.isLetter(c) || Character.isDigit(c))) |
| 63666 | 194 |
{
|
195 |
if (s.startsWith("(**** ") && s.endsWith(" ****)")) Some(0)
|
|
196 |
else if (s.startsWith("(*** ") && s.endsWith(" ***)")) Some(1)
|
|
197 |
else if (s.startsWith("(** ") && s.endsWith(" **)")) Some(2)
|
|
198 |
else if (s.startsWith("(* ") && s.endsWith(" *)")) Some(3)
|
|
199 |
else None |
|
200 |
} |
|
| 63610 | 201 |
else None |
202 |
case _ => None |
|
203 |
} |
|
204 |
if (level.isDefined && context == Scan.Finished && next_context == Scan.Finished) |
|
205 |
toks.foreach(tok => sections.add(new ML_Item(tok, if (tok.is_comment) level else None))) |
|
206 |
else |
|
207 |
toks.foreach(tok => sections.add(new ML_Item(tok, None))) |
|
208 |
||
209 |
sections.add(nl) |
|
210 |
context = next_context |
|
211 |
} |
|
212 |
sections.result() |
|
| 63604 | 213 |
} |
214 |
} |