author | wenzelm |
Thu, 27 Sep 2012 19:34:31 +0200 | |
changeset 49614 | 0009a6ebc83b |
parent 49527 | b96e4a39cc3e |
child 49645 | 5a0817ec0314 |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/PIDE/command.scala |
2 |
Author: Fabian Immler, TU Munich |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Prover commands with semantic state. |
|
6 |
*/ |
|
34407 | 7 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34865
diff
changeset
|
8 |
package isabelle |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
|
43520
cec9b95fa35d
explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents:
40572
diff
changeset
|
10 |
import java.lang.System |
34451 | 11 |
|
45644 | 12 |
import scala.collection.mutable |
38872 | 13 |
import scala.collection.immutable.SortedMap |
14 |
||
15 |
||
34637 | 16 |
object Command |
17 |
{ |
|
38361 | 18 |
/** accumulated results from prover **/ |
19 |
||
43714 | 20 |
sealed case class State( |
38361 | 21 |
val command: Command, |
38872 | 22 |
val status: List[Markup] = Nil, |
23 |
val results: SortedMap[Long, XML.Tree] = SortedMap.empty, |
|
24 |
val markup: Markup_Tree = Markup_Tree.empty) |
|
38361 | 25 |
{ |
49614 | 26 |
def markup_to_XML: XML.Body = markup.to_XML(command.source) |
27 |
||
28 |
||
46152
793cecd4ffc0
accumulate status as regular markup for command range;
wenzelm
parents:
45709
diff
changeset
|
29 |
/* accumulate content */ |
38361 | 30 |
|
46152
793cecd4ffc0
accumulate status as regular markup for command range;
wenzelm
parents:
45709
diff
changeset
|
31 |
private def add_status(st: Markup): State = copy(status = st :: status) |
793cecd4ffc0
accumulate status as regular markup for command range;
wenzelm
parents:
45709
diff
changeset
|
32 |
private def add_markup(m: Text.Markup): State = copy(markup = markup + m) |
38361 | 33 |
|
49527 | 34 |
def + (alt_id: Document.ID, message: XML.Elem): Command.State = |
38361 | 35 |
message match { |
45666 | 36 |
case XML.Elem(Markup(Isabelle_Markup.STATUS, _), msgs) => |
38714 | 37 |
(this /: msgs)((state, msg) => |
38 |
msg match { |
|
46152
793cecd4ffc0
accumulate status as regular markup for command range;
wenzelm
parents:
45709
diff
changeset
|
39 |
case elem @ XML.Elem(markup, Nil) => |
46910
3e068ef04b42
clarified command state -- markup within proper_range, excluding trailing whitespace;
wenzelm
parents:
46813
diff
changeset
|
40 |
state.add_status(markup).add_markup(Text.Info(command.proper_range, elem)) |
46152
793cecd4ffc0
accumulate status as regular markup for command range;
wenzelm
parents:
45709
diff
changeset
|
41 |
|
38714 | 42 |
case _ => System.err.println("Ignored status message: " + msg); state |
43 |
}) |
|
38581
d503a0912e14
simplified Command.status again, reverting most of e5eed57913d0 (note that more complex information can be represented with full markup reports);
wenzelm
parents:
38579
diff
changeset
|
44 |
|
45666 | 45 |
case XML.Elem(Markup(Isabelle_Markup.REPORT, _), msgs) => |
38572
0fe2c01ef7da
Command.State: accumulate markup reports uniformly;
wenzelm
parents:
38564
diff
changeset
|
46 |
(this /: msgs)((state, msg) => |
0fe2c01ef7da
Command.State: accumulate markup reports uniformly;
wenzelm
parents:
38564
diff
changeset
|
47 |
msg match { |
39173
ed3946086358
Command.State.accumulate: check actual source range;
wenzelm
parents:
39172
diff
changeset
|
48 |
case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args) |
49527 | 49 |
if (id == command.id || id == alt_id) && |
50 |
command.range.contains(command.decode(raw_range)) => |
|
39173
ed3946086358
Command.State.accumulate: check actual source range;
wenzelm
parents:
39172
diff
changeset
|
51 |
val range = command.decode(raw_range) |
38872 | 52 |
val props = Position.purge(atts) |
45455 | 53 |
val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args)) |
38723 | 54 |
state.add_markup(info) |
49526
6d1465c00f2e
more restrictive pattern, to avoid malformed positions intruding the command range (cf. d7a1973b063c);
wenzelm
parents:
49493
diff
changeset
|
55 |
case XML.Elem(Markup(name, atts), args) |
6d1465c00f2e
more restrictive pattern, to avoid malformed positions intruding the command range (cf. d7a1973b063c);
wenzelm
parents:
49493
diff
changeset
|
56 |
if !atts.exists({ case (a, _) => Isabelle_Markup.POSITION_PROPERTIES(a) }) => |
49037
d7a1973b063c
more markup for failed goal forks, reusing "bad";
wenzelm
parents:
48922
diff
changeset
|
57 |
val range = command.proper_range |
d7a1973b063c
more markup for failed goal forks, reusing "bad";
wenzelm
parents:
48922
diff
changeset
|
58 |
val props = Position.purge(atts) |
d7a1973b063c
more markup for failed goal forks, reusing "bad";
wenzelm
parents:
48922
diff
changeset
|
59 |
val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args)) |
d7a1973b063c
more markup for failed goal forks, reusing "bad";
wenzelm
parents:
48922
diff
changeset
|
60 |
state.add_markup(info) |
40572 | 61 |
case _ => |
62 |
// FIXME System.err.println("Ignored report message: " + msg) |
|
63 |
state |
|
38361 | 64 |
}) |
38872 | 65 |
case XML.Elem(Markup(name, atts), body) => |
66 |
atts match { |
|
45666 | 67 |
case Isabelle_Markup.Serial(i) => |
49493
db58490a68ac
more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents:
49466
diff
changeset
|
68 |
val message1 = XML.Elem(Markup(Isabelle_Markup.message(name), atts), body) |
db58490a68ac
more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents:
49466
diff
changeset
|
69 |
val message2 = XML.Elem(Markup(name, Position.purge(atts)), body) |
49418 | 70 |
|
49493
db58490a68ac
more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents:
49466
diff
changeset
|
71 |
val st0 = copy(results = results + (i -> message1)) |
39441
4110cc1b8f9f
allow embedded reports in regular prover messages, to avoid side-effects for errors for example;
wenzelm
parents:
39173
diff
changeset
|
72 |
val st1 = |
49493
db58490a68ac
more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents:
49466
diff
changeset
|
73 |
if (Protocol.is_tracing(message)) st0 |
39622
53365ba766ac
Command.accumulate: refrain from adding tracing messages to markup tree -- potential scalability problem;
wenzelm
parents:
39441
diff
changeset
|
74 |
else |
45709
87017fcbad83
clarified modules (again) -- NB: both Document and Protocol are specific to this particular prover;
wenzelm
parents:
45672
diff
changeset
|
75 |
(st0 /: Protocol.message_positions(command, message))( |
49493
db58490a68ac
more realistic sendback: pick exec_id from message position and text from buffer;
wenzelm
parents:
49466
diff
changeset
|
76 |
(st, range) => st.add_markup(Text.Info(range, message2))) |
49445
638cefe3ee99
earlier treatment of embedded report/no_report messages (see also 4110cc1b8f9f);
wenzelm
parents:
49418
diff
changeset
|
77 |
|
638cefe3ee99
earlier treatment of embedded report/no_report messages (see also 4110cc1b8f9f);
wenzelm
parents:
49418
diff
changeset
|
78 |
st1 |
38872 | 79 |
case _ => System.err.println("Ignored message without serial number: " + message); this |
80 |
} |
|
38361 | 81 |
} |
82 |
} |
|
38367 | 83 |
|
84 |
||
45644 | 85 |
/* make commands */ |
86 |
||
48745 | 87 |
type Span = List[Token] |
88 |
||
49414 | 89 |
def apply(id: Document.Command_ID, node_name: Document.Node.Name, |
90 |
span: Span, markup: Markup_Tree): Command = |
|
45644 | 91 |
{ |
92 |
val source: String = |
|
48745 | 93 |
span match { |
45644 | 94 |
case List(tok) => tok.source |
48745 | 95 |
case _ => span.map(_.source).mkString |
45644 | 96 |
} |
97 |
||
48745 | 98 |
val span1 = new mutable.ListBuffer[Token] |
45644 | 99 |
var i = 0 |
48745 | 100 |
for (Token(kind, s) <- span) { |
45644 | 101 |
val n = s.length |
102 |
val s1 = source.substring(i, i + n) |
|
48745 | 103 |
span1 += Token(kind, s1) |
45644 | 104 |
i += n |
105 |
} |
|
106 |
||
49414 | 107 |
new Command(id, node_name, span1.toList, source, markup) |
45644 | 108 |
} |
109 |
||
49414 | 110 |
val empty = Command(Document.no_id, Document.Node.Name.empty, Nil, Markup_Tree.empty) |
111 |
||
112 |
def unparsed(id: Document.Command_ID, source: String, markup: Markup_Tree): Command = |
|
113 |
Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), markup) |
|
114 |
||
115 |
def unparsed(source: String): Command = unparsed(Document.no_id, source, Markup_Tree.empty) |
|
44384 | 116 |
|
49414 | 117 |
def rich_text(id: Document.Command_ID, body: XML.Body): Command = |
118 |
{ |
|
49466 | 119 |
val text = XML.content(body) |
120 |
val markup = Markup_Tree.from_XML(body) |
|
49414 | 121 |
unparsed(id, text, markup) |
122 |
} |
|
49359
c1262d7389fb
refined output panel: more value-oriented approach to update and caret focus;
wenzelm
parents:
49037
diff
changeset
|
123 |
|
44384 | 124 |
|
125 |
/* perspective */ |
|
126 |
||
44474 | 127 |
object Perspective |
128 |
{ |
|
129 |
val empty: Perspective = Perspective(Nil) |
|
130 |
} |
|
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
131 |
|
44474 | 132 |
sealed case class Perspective(commands: List[Command]) // visible commands in canonical order |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
133 |
{ |
44474 | 134 |
def same(that: Perspective): Boolean = |
135 |
{ |
|
136 |
val cmds1 = this.commands |
|
137 |
val cmds2 = that.commands |
|
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
138 |
require(!cmds1.exists(_.is_undefined)) |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
139 |
require(!cmds2.exists(_.is_undefined)) |
44474 | 140 |
cmds1.length == cmds2.length && |
141 |
(cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id }) |
|
142 |
} |
|
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
143 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
144 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
145 |
|
38361 | 146 |
|
46712 | 147 |
final class Command private( |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37373
diff
changeset
|
148 |
val id: Document.Command_ID, |
44615 | 149 |
val node_name: Document.Node.Name, |
48745 | 150 |
val span: Command.Span, |
49414 | 151 |
val source: String, |
152 |
val init_markup: Markup_Tree) |
|
34451 | 153 |
{ |
34859 | 154 |
/* classification */ |
34500
384427c750c8
state_results: separate buffer for messages from running command;
wenzelm
parents:
34497
diff
changeset
|
155 |
|
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
156 |
def is_undefined: Boolean = id == Document.no_id |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
157 |
val is_unparsed: Boolean = span.exists(_.is_unparsed) |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
158 |
val is_unfinished: Boolean = span.exists(_.is_unfinished) |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44384
diff
changeset
|
159 |
|
48599 | 160 |
val is_ignored: Boolean = !span.exists(_.is_proper) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48745
diff
changeset
|
161 |
val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error)) |
47012
0e246130486b
clarified command span classification: strict Command.is_command, permissive Command.name;
wenzelm
parents:
46910
diff
changeset
|
162 |
def is_command: Boolean = !is_ignored && !is_malformed |
34859 | 163 |
|
47012
0e246130486b
clarified command span classification: strict Command.is_command, permissive Command.name;
wenzelm
parents:
46910
diff
changeset
|
164 |
def name: String = |
48718 | 165 |
span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" } |
47012
0e246130486b
clarified command span classification: strict Command.is_command, permissive Command.name;
wenzelm
parents:
46910
diff
changeset
|
166 |
|
37129 | 167 |
override def toString = |
37373
25078ba44436
tuned Command.toString -- preserving uniqueness allows the Scala toplevel to print Linear_Set[Command] results without crashing;
wenzelm
parents:
37197
diff
changeset
|
168 |
id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED") |
34495 | 169 |
|
34859 | 170 |
|
171 |
/* source text */ |
|
34451 | 172 |
|
46813 | 173 |
def length: Int = source.length |
174 |
val range: Text.Range = Text.Range(0, length) |
|
175 |
||
176 |
val proper_range: Text.Range = |
|
47459
373e456149cc
include trailing comments in proper_command range;
wenzelm
parents:
47012
diff
changeset
|
177 |
Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_space))(_ - _.source.length)) |
46813 | 178 |
|
38426 | 179 |
def source(range: Text.Range): String = source.substring(range.start, range.stop) |
38572
0fe2c01ef7da
Command.State: accumulate markup reports uniformly;
wenzelm
parents:
38564
diff
changeset
|
180 |
|
34859 | 181 |
lazy val symbol_index = new Symbol.Index(source) |
38579 | 182 |
def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i) |
183 |
def decode(r: Text.Range): Text.Range = symbol_index.decode(r) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
184 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
185 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
186 |
/* accumulated results */ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
187 |
|
49414 | 188 |
val init_state: Command.State = Command.State(this, markup = init_markup) |
34676
9e725d34df7b
Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents:
34675
diff
changeset
|
189 |
} |