author | wenzelm |
Mon, 01 Dec 2014 15:21:49 +0100 | |
changeset 59073 | dcecfcc56dce |
parent 58938 | 0c45680b7d9d |
child 59077 | 7e0d3da6e6d8 |
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 |
|
59073 | 115 |
/* merge */ |
116 |
||
117 |
def ++ (other: Outer_Syntax): Outer_Syntax = |
|
118 |
if (this eq other) this |
|
119 |
else { |
|
120 |
val keywords1 = keywords ++ other.keywords |
|
121 |
val completion1 = completion ++ other.completion |
|
122 |
new Outer_Syntax(keywords1, completion1, language_context, has_tokens) |
|
123 |
} |
|
124 |
||
125 |
||
58900 | 126 |
/* load commands */ |
127 |
||
128 |
def load_command(name: String): Option[List[String]] = keywords.load_command(name) |
|
129 |
def load_commands_in(text: String): Boolean = keywords.load_commands_in(text) |
|
130 |
||
131 |
||
58706 | 132 |
/* language context */ |
34166 | 133 |
|
58706 | 134 |
def set_language_context(context: Completion.Language_Context): Outer_Syntax = |
58900 | 135 |
new Outer_Syntax(keywords, completion, context, has_tokens) |
58706 | 136 |
|
137 |
def no_tokens: Outer_Syntax = |
|
46969 | 138 |
{ |
58900 | 139 |
require(keywords.is_empty) |
58706 | 140 |
new Outer_Syntax( |
141 |
completion = completion, |
|
142 |
language_context = language_context, |
|
143 |
has_tokens = false) |
|
46969 | 144 |
} |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
145 |
|
58706 | 146 |
|
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38471
diff
changeset
|
147 |
|
58706 | 148 |
/** parsing **/ |
34166 | 149 |
|
58697 | 150 |
/* line-oriented structure */ |
58696 | 151 |
|
58700 | 152 |
def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure) |
153 |
: Outer_Syntax.Line_Structure = |
|
58696 | 154 |
{ |
58700 | 155 |
val improper1 = tokens.forall(_.is_improper) |
156 |
val command1 = tokens.exists(_.is_command) |
|
157 |
||
58696 | 158 |
val depth1 = |
58901 | 159 |
if (tokens.exists(tok => keywords.is_command_kind(tok, Keyword.theory))) 0 |
58700 | 160 |
else if (command1) struct.after_span_depth |
161 |
else struct.span_depth |
|
162 |
||
163 |
val (span_depth1, after_span_depth1) = |
|
164 |
((struct.span_depth, struct.after_span_depth) /: tokens) { |
|
58703 | 165 |
case ((x, y), tok) => |
166 |
if (tok.is_command) { |
|
58901 | 167 |
if (keywords.is_command_kind(tok, Keyword.theory_goal)) |
58900 | 168 |
(2, 1) |
58901 | 169 |
else if (keywords.is_command_kind(tok, Keyword.theory)) |
58900 | 170 |
(1, 0) |
58901 | 171 |
else if (keywords.is_command_kind(tok, Keyword.proof_goal) || tok.is_begin_block) |
58900 | 172 |
(y + 2, y + 1) |
58901 | 173 |
else if (keywords.is_command_kind(tok, Keyword.qed) || tok.is_end_block) |
58900 | 174 |
(y + 1, y - 1) |
58901 | 175 |
else if (keywords.is_command_kind(tok, Keyword.qed_global)) |
58900 | 176 |
(1, 0) |
58703 | 177 |
else (x, y) |
178 |
} |
|
179 |
else (x, y) |
|
58696 | 180 |
} |
58700 | 181 |
|
182 |
Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1) |
|
58696 | 183 |
} |
184 |
||
185 |
||
53280
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
186 |
/* token language */ |
c63a016805b9
explicit indication of outer syntax with no tokens;
wenzelm
parents:
52439
diff
changeset
|
187 |
|
57907 | 188 |
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
|
189 |
{ |
58503 | 190 |
val in: Reader[Char] = new CharSequenceReader(input) |
58900 | 191 |
Token.Parsers.parseAll(Token.Parsers.rep(Token.Parsers.token(keywords)), in) match { |
55494 | 192 |
case Token.Parsers.Success(tokens, _) => tokens |
57907 | 193 |
case _ => error("Unexpected failure of tokenizing input:\n" + input.toString) |
34166 | 194 |
} |
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 |
} |
34166 | 196 |
|
58748 | 197 |
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
|
198 |
{ |
83b7b88770c9
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm
parents:
50428
diff
changeset
|
199 |
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
|
200 |
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
|
201 |
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
|
202 |
while (!in.atEnd) { |
58900 | 203 |
Token.Parsers.parse(Token.Parsers.token_line(keywords, ctxt), in) match { |
55494 | 204 |
case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest } |
205 |
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
|
206 |
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
|
207 |
} |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
40533
diff
changeset
|
208 |
} |
58748 | 209 |
(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
|
210 |
} |
55616 | 211 |
|
212 |
||
58706 | 213 |
/* command spans */ |
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
214 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
215 |
def parse_spans(toks: List[Token]): List[Command_Span.Span] = |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
216 |
{ |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
217 |
val result = new mutable.ListBuffer[Command_Span.Span] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
218 |
val content = new mutable.ListBuffer[Token] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
219 |
val improper = new mutable.ListBuffer[Token] |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
220 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
221 |
def ship(span: List[Token]) |
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 |
val kind = |
57910 | 224 |
if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) { |
225 |
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
|
226 |
val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1) |
57910 | 227 |
Command_Span.Command_Span(name, pos) |
228 |
} |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
229 |
else if (span.forall(_.is_improper)) Command_Span.Ignored_Span |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
230 |
else Command_Span.Malformed_Span |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
231 |
result += Command_Span.Span(kind, span) |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
232 |
} |
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 |
def 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 |
if (!content.isEmpty) { ship(content.toList); content.clear } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
237 |
if (!improper.isEmpty) { ship(improper.toList); improper.clear } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
238 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
239 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
240 |
for (tok <- toks) { |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
241 |
if (tok.is_command) { flush(); content += tok } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
242 |
else if (tok.is_improper) improper += tok |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
243 |
else { content ++= improper; improper.clear; content += tok } |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
244 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
245 |
flush() |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
246 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
247 |
result.toList |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
248 |
} |
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
249 |
|
57906 | 250 |
def parse_spans(input: CharSequence): List[Command_Span.Span] = |
251 |
parse_spans(scan(input)) |
|
252 |
||
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57901
diff
changeset
|
253 |
|
58706 | 254 |
/* overall document structure */ |
55616 | 255 |
|
58706 | 256 |
def heading_level(command: Command): Option[Int] = |
257 |
{ |
|
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
258 |
command.name match { |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
259 |
case "chapter" => Some(0) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
260 |
case "section" | "header" => Some(1) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
261 |
case "subsection" => Some(2) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
262 |
case "subsubsection" => Some(3) |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
263 |
case _ => |
58901 | 264 |
keywords.command_kind(command.name) match { |
58938 | 265 |
case Some(kind) if Keyword.theory(kind) && kind != Keyword.THY_END => Some(4) |
58868
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
266 |
case _ => None |
c5e1cce7ace3
uniform heading commands work in any context, even in theory header;
wenzelm
parents:
58853
diff
changeset
|
267 |
} |
58706 | 268 |
} |
269 |
} |
|
270 |
||
58743 | 271 |
def parse_document(node_name: Document.Node.Name, text: CharSequence): |
272 |
List[Outer_Syntax.Document] = |
|
58706 | 273 |
{ |
274 |
/* stack operations */ |
|
275 |
||
276 |
def buffer(): mutable.ListBuffer[Outer_Syntax.Document] = |
|
277 |
new mutable.ListBuffer[Outer_Syntax.Document] |
|
278 |
||
58747 | 279 |
var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] = |
280 |
List((0, Command.empty, buffer())) |
|
55616 | 281 |
|
58706 | 282 |
@tailrec def close(level: Int => Boolean) |
283 |
{ |
|
284 |
stack match { |
|
58747 | 285 |
case (lev, command, body) :: (_, _, body2) :: rest if level(lev) => |
286 |
body2 += Outer_Syntax.Document_Block(command.name, command.source, body.toList) |
|
58706 | 287 |
stack = stack.tail |
288 |
close(level) |
|
289 |
case _ => |
|
290 |
} |
|
291 |
} |
|
292 |
||
58743 | 293 |
def result(): List[Outer_Syntax.Document] = |
58706 | 294 |
{ |
295 |
close(_ => true) |
|
58743 | 296 |
stack.head._3.toList |
58706 | 297 |
} |
298 |
||
299 |
def add(command: Command) |
|
300 |
{ |
|
301 |
heading_level(command) match { |
|
302 |
case Some(i) => |
|
303 |
close(_ > i) |
|
58747 | 304 |
stack = (i + 1, command, buffer()) :: stack |
58706 | 305 |
case None => |
306 |
} |
|
307 |
stack.head._3 += Outer_Syntax.Document_Atom(command) |
|
308 |
} |
|
309 |
||
310 |
||
311 |
/* result structure */ |
|
312 |
||
313 |
val spans = parse_spans(text) |
|
314 |
spans.foreach(span => add(Command(Document_ID.none, node_name, Nil, span))) |
|
315 |
result() |
|
55616 | 316 |
} |
34166 | 317 |
} |