author | wenzelm |
Wed, 05 Nov 2014 21:59:21 +0100 | |
changeset 58907 | 0ee3563803c9 |
parent 58901 | 47809a811eba |
child 58938 | 0c45680b7d9d |
permissions | -rw-r--r-- |
34166 | 1 |
/* Title: Pure/Isar/outer_syntax.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Isabelle/Isar outer syntax. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import scala.util.parsing.input.{Reader, CharSequenceReader} |
|
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
11 |
import scala.collection.mutable |
58706 | 12 |
import scala.annotation.tailrec |
34166 | 13 |
|
14 |
||
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
15 |
object Outer_Syntax |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
16 |
{ |
58706 | 17 |
/* syntax */ |
18 |
||
19 |
val empty: Outer_Syntax = new Outer_Syntax() |
|
20 |
||
21 |
def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init()) |
|
22 |
||
23 |
||
24 |
/* string literals */ |
|
25 |
||
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
26 |
def quote_string(str: String): String = |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
27 |
{ |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
28 |
val result = new StringBuilder(str.length + 10) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
29 |
result += '"' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
30 |
for (s <- Symbol.iterator(str)) { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
31 |
if (s.length == 1) { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
32 |
val c = s(0) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
33 |
if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') { |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
34 |
result += '\\' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
35 |
if (c < 10) result += '0' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
36 |
if (c < 100) result += '0' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
37 |
result ++= (c.asInstanceOf[Int].toString) |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
38 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
39 |
else result += c |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
40 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
41 |
else result ++= s |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
42 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
43 |
result += '"' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
44 |
result.toString |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
45 |
} |
46626 | 46 |
|
58696 | 47 |
|
58697 | 48 |
/* line-oriented structure */ |
58696 | 49 |
|
58697 | 50 |
object Line_Structure |
58696 | 51 |
{ |
58700 | 52 |
val init = Line_Structure() |
58696 | 53 |
} |
54 |
||
58700 | 55 |
sealed case class Line_Structure( |
56 |
improper: Boolean = true, |
|
57 |
command: Boolean = false, |
|
58 |
depth: Int = 0, |
|
59 |
span_depth: Int = 0, |
|
60 |
after_span_depth: Int = 0) |
|
58706 | 61 |
|
62 |
||
63 |
/* overall document structure */ |
|
64 |
||
65 |
sealed abstract class Document { def length: Int } |
|
58747 | 66 |
case class Document_Block(name: String, text: String, body: List[Document]) extends Document |
58706 | 67 |
{ |
68 |
val length: Int = (0 /: body)(_ + _.length) |
|
69 |
} |
|
58747 | 70 |
case class Document_Atom(command: Command) extends Document |
58706 | 71 |
{ |
72 |
def length: Int = command.length |
|
73 |
} |
|
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
74 |
} |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43695
diff
changeset
|
75 |
|
46712 | 76 |
final class Outer_Syntax private( |
58900 | 77 |
val keywords: Keyword.Keywords = Keyword.Keywords.empty, |
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
78 |
val completion: Completion = Completion.empty, |
55749 | 79 |
val language_context: Completion.Language_Context = Completion.Language_Context.outer, |
56393
22f533e6a049
more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents:
56314
diff
changeset
|
80 |
val has_tokens: Boolean = true) extends Prover.Syntax |
34166 | 81 |
{ |
58706 | 82 |
/** syntax content **/ |
83 |
||
58900 | 84 |
override def toString: String = keywords.toString |
56393
22f533e6a049
more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents:
56314
diff
changeset
|
85 |
|
58695 | 86 |
|
87 |
/* add keywords */ |
|
88 |
||
58907 | 89 |
def + (name: String): Outer_Syntax = this + (name, None, None) |
90 |
def + (name: String, kind: String): Outer_Syntax = this + (name, Some((kind, Nil)), None) |
|
58901 | 91 |
def + (name: String, opt_kind: Option[(String, List[String])], replace: Option[String]) |
92 |
: Outer_Syntax = |
|
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
93 |
{ |
58901 | 94 |
val keywords1 = |
95 |
opt_kind match { |
|
96 |
case None => keywords + name |
|
97 |
case Some(kind) => keywords + (name, kind) |
|
98 |
} |
|
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
99 |
val completion1 = |
58853 | 100 |
if (replace == Some("")) completion |
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
101 |
else completion + (name, replace getOrElse name) |
58900 | 102 |
new Outer_Syntax(keywords1, completion1, language_context, true) |
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
103 |
} |
48706 | 104 |
|
48873 | 105 |
def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax = |
106 |
(this /: keywords) { |
|
58901 | 107 |
case (syntax, (name, opt_spec, replace)) => |
108 |
val opt_kind = opt_spec.map(_._1) |
|
50128
599c935aac82
alternative completion for outer syntax keywords;
wenzelm
parents:
48885
diff
changeset
|
109 |
syntax + |
58901 | 110 |
(Symbol.decode(name), opt_kind, replace) + |
111 |
(Symbol.encode(name), opt_kind, replace) |
|
46940 | 112 |
} |
34166 | 113 |
|
58695 | 114 |
|
58900 | 115 |
/* load commands */ |
116 |
||
117 |
def load_command(name: String): Option[List[String]] = keywords.load_command(name) |
|
118 |
def load_commands_in(text: String): Boolean = keywords.load_commands_in(text) |
|
119 |
||
120 |
||
58706 | 121 |
/* language context */ |
34166 | 122 |
|
58706 | 123 |
def set_language_context(context: Completion.Language_Context): Outer_Syntax = |
58900 | 124 |
new Outer_Syntax(keywords, completion, context, has_tokens) |
58706 | 125 |
|
126 |
def no_tokens: Outer_Syntax = |
|
46969 | 127 |
{ |
58900 | 128 |
require(keywords.is_empty) |
58706 | 129 |
new Outer_Syntax( |
130 |
completion = completion, |
|
131 |
language_context = language_context, |
|
132 |
has_tokens = false) |
|
46969 | 133 |
} |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
134 |
|
58706 | 135 |
|
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
136 |
|
58706 | 137 |
/** parsing **/ |
34166 | 138 |
|
58697 | 139 |
/* line-oriented structure */ |
58696 | 140 |
|
58700 | 141 |
def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure) |
142 |
: Outer_Syntax.Line_Structure = |
|
58696 | 143 |
{ |
58700 | 144 |
val improper1 = tokens.forall(_.is_improper) |
145 |
val command1 = tokens.exists(_.is_command) |
|
146 |
||
58696 | 147 |
val depth1 = |
58901 | 148 |
if (tokens.exists(tok => keywords.is_command_kind(tok, Keyword.theory))) 0 |
58700 | 149 |
else if (command1) struct.after_span_depth |
150 |
else struct.span_depth |
|
151 |
||
152 |
val (span_depth1, after_span_depth1) = |
|
153 |
((struct.span_depth, struct.after_span_depth) /: tokens) { |
|
58703 | 154 |
case ((x, y), tok) => |
155 |
if (tok.is_command) { |
|
58901 | 156 |
if (keywords.is_command_kind(tok, Keyword.theory_goal)) |
58900 | 157 |
(2, 1) |
58901 | 158 |
else if (keywords.is_command_kind(tok, Keyword.theory)) |
58900 | 159 |
(1, 0) |
58901 | 160 |
else if (keywords.is_command_kind(tok, Keyword.proof_goal) || tok.is_begin_block) |
58900 | 161 |
(y + 2, y + 1) |
58901 | 162 |
else if (keywords.is_command_kind(tok, Keyword.qed) || tok.is_end_block) |
58900 | 163 |
(y + 1, y - 1) |
58901 | 164 |
else if (keywords.is_command_kind(tok, Keyword.qed_global)) |
58900 | 165 |
(1, 0) |
58703 | 166 |
else (x, y) |
167 |
} |
|
168 |
else (x, y) |
|
58696 | 169 |
} |
58700 | 170 |
|
171 |
Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1) |
|
58696 | 172 |
} |
173 |
||
174 |
||
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
175 |
/* token language */ |
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
176 |
|
57907 | 177 |
def scan(input: CharSequence): List[Token] = |
52066
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
178 |
{ |
58503 | 179 |
val in: Reader[Char] = new CharSequenceReader(input) |
58900 | 180 |
Token.Parsers.parseAll(Token.Parsers.rep(Token.Parsers.token(keywords)), in) match { |
55494 | 181 |
case Token.Parsers.Success(tokens, _) => tokens |
57907 | 182 |
case _ => error("Unexpected failure of tokenizing input:\n" + input.toString) |
34166 | 183 |
} |
52066
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
184 |
} |
34166 | 185 |
|
58748 | 186 |
def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) = |
52066
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
187 |
{ |
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
188 |
var in: Reader[Char] = new CharSequenceReader(input) |
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
189 |
val toks = new mutable.ListBuffer[Token] |
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
190 |
var ctxt = context |
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
191 |
while (!in.atEnd) { |
58900 | 192 |
Token.Parsers.parse(Token.Parsers.token_line(keywords, ctxt), in) match { |
55494 | 193 |
case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest } |
194 |
case Token.Parsers.NoSuccess(_, rest) => |
|
52066
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
195 |
error("Unexpected failure of tokenizing input:\n" + rest.source.toString) |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
196 |
} |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
197 |
} |
58748 | 198 |
(toks.toList, ctxt) |
52066
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
199 |
} |
55616 | 200 |
|
201 |
||
58706 | 202 |
/* command spans */ |
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
203 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
204 |
def parse_spans(toks: List[Token]): List[Command_Span.Span] = |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
205 |
{ |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
206 |
val result = new mutable.ListBuffer[Command_Span.Span] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
207 |
val content = new mutable.ListBuffer[Token] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
208 |
val improper = new mutable.ListBuffer[Token] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
209 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
210 |
def ship(span: List[Token]) |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
211 |
{ |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
212 |
val kind = |
57910 | 213 |
if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) { |
214 |
val name = span.head.source |
|
57911
dcb758188aa6
clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents:
57910
diff
changeset
|
215 |
val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1) |
57910 | 216 |
Command_Span.Command_Span(name, pos) |
217 |
} |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
218 |
else if (span.forall(_.is_improper)) Command_Span.Ignored_Span |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
219 |
else Command_Span.Malformed_Span |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
220 |
result += Command_Span.Span(kind, span) |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
221 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
222 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
223 |
def flush() |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
224 |
{ |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
225 |
if (!content.isEmpty) { ship(content.toList); content.clear } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
226 |
if (!improper.isEmpty) { ship(improper.toList); improper.clear } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
227 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
228 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
229 |
for (tok <- toks) { |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
230 |
if (tok.is_command) { flush(); content += tok } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
231 |
else if (tok.is_improper) improper += tok |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
232 |
else { content ++= improper; improper.clear; content += tok } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
233 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
234 |
flush() |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
235 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
236 |
result.toList |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
237 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
238 |
|
57906 | 239 |
def parse_spans(input: CharSequence): List[Command_Span.Span] = |
240 |
parse_spans(scan(input)) |
|
241 |
||
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
242 |
|
58706 | 243 |
/* overall document structure */ |
55616 | 244 |
|
58706 | 245 |
def heading_level(command: Command): Option[Int] = |
246 |
{ |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
247 |
command.name match { |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
248 |
case "chapter" => Some(0) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
249 |
case "section" | "header" => Some(1) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
250 |
case "subsection" => Some(2) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
251 |
case "subsubsection" => Some(3) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
252 |
case _ => |
58901 | 253 |
keywords.command_kind(command.name) match { |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
254 |
case Some(kind) if Keyword.theory(kind) => Some(4) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
255 |
case _ => None |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
256 |
} |
58706 | 257 |
} |
258 |
} |
|
259 |
||
58743 | 260 |
def parse_document(node_name: Document.Node.Name, text: CharSequence): |
261 |
List[Outer_Syntax.Document] = |
|
58706 | 262 |
{ |
263 |
/* stack operations */ |
|
264 |
||
265 |
def buffer(): mutable.ListBuffer[Outer_Syntax.Document] = |
|
266 |
new mutable.ListBuffer[Outer_Syntax.Document] |
|
267 |
||
58747 | 268 |
var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] = |
269 |
List((0, Command.empty, buffer())) |
|
55616 | 270 |
|
58706 | 271 |
@tailrec def close(level: Int => Boolean) |
272 |
{ |
|
273 |
stack match { |
|
58747 | 274 |
case (lev, command, body) :: (_, _, body2) :: rest if level(lev) => |
275 |
body2 += Outer_Syntax.Document_Block(command.name, command.source, body.toList) |
|
58706 | 276 |
stack = stack.tail |
277 |
close(level) |
|
278 |
case _ => |
|
279 |
} |
|
280 |
} |
|
281 |
||
58743 | 282 |
def result(): List[Outer_Syntax.Document] = |
58706 | 283 |
{ |
284 |
close(_ => true) |
|
58743 | 285 |
stack.head._3.toList |
58706 | 286 |
} |
287 |
||
288 |
def add(command: Command) |
|
289 |
{ |
|
290 |
heading_level(command) match { |
|
291 |
case Some(i) => |
|
292 |
close(_ > i) |
|
58747 | 293 |
stack = (i + 1, command, buffer()) :: stack |
58706 | 294 |
case None => |
295 |
} |
|
296 |
stack.head._3 += Outer_Syntax.Document_Atom(command) |
|
297 |
} |
|
298 |
||
299 |
||
300 |
/* result structure */ |
|
301 |
||
302 |
val spans = parse_spans(text) |
|
303 |
spans.foreach(span => add(Command(Document_ID.none, node_name, Nil, span))) |
|
304 |
result() |
|
55616 | 305 |
} |
34166 | 306 |
} |