49 def parse_blocks( |
49 def parse_blocks( |
50 syntax: Outer_Syntax, |
50 syntax: Outer_Syntax, |
51 node_name: Document.Node.Name, |
51 node_name: Document.Node.Name, |
52 text: CharSequence): List[Document] = |
52 text: CharSequence): List[Document] = |
53 { |
53 { |
|
54 def is_plain_theory(command: Command): Boolean = |
|
55 is_theory_command(syntax.keywords, command.span.name) && |
|
56 !command.span.is_begin && !command.span.is_end |
|
57 |
|
58 |
54 /* stack operations */ |
59 /* stack operations */ |
55 |
60 |
56 def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document] |
61 def buffer(): mutable.ListBuffer[Document] = new mutable.ListBuffer[Document] |
57 |
62 |
58 var stack: List[(Command, mutable.ListBuffer[Document])] = |
63 var stack: List[(Command, mutable.ListBuffer[Document])] = |
59 List((Command.empty, buffer())) |
64 List((Command.empty, buffer())) |
|
65 |
|
66 def open(command: Command) { stack = (command, buffer()) :: stack } |
60 |
67 |
61 def close(): Boolean = |
68 def close(): Boolean = |
62 stack match { |
69 stack match { |
63 case (command, body) :: (_, body2) :: _ => |
70 case (command, body) :: (_, body2) :: _ => |
64 body2 += Block(command.span.name, command.source, body.toList) |
71 body2 += Block(command.span.name, command.source, body.toList) |
66 true |
73 true |
67 case _ => |
74 case _ => |
68 false |
75 false |
69 } |
76 } |
70 |
77 |
|
78 def flush() { if (is_plain_theory(stack.head._1)) close() } |
|
79 |
71 def result(): List[Document] = |
80 def result(): List[Document] = |
72 { |
81 { |
73 while (close()) { } |
82 while (close()) { } |
74 stack.head._2.toList |
83 stack.head._2.toList |
75 } |
84 } |
76 |
85 |
77 def add(command: Command) |
86 def add(command: Command) |
78 { |
87 { |
79 if (command.span.is_begin) stack = (command, buffer()) :: stack |
88 if (command.span.is_begin || is_plain_theory(command)) { flush(); open(command) } |
80 else if (command.span.is_end) close() |
89 else if (command.span.is_end) { flush(); close() } |
81 |
90 |
82 stack.head._2 += Atom(command) |
91 stack.head._2 += Atom(command) |
83 } |
92 } |
84 |
93 |
85 |
94 |