author | wenzelm |
Wed, 15 Mar 2017 11:07:07 +0100 | |
changeset 65255 | d388e63a46fc |
parent 65250 | 13a6c81534a8 |
child 65355 | 403eabd73c9a |
permissions | -rw-r--r-- |
56208 | 1 |
/* Title: Pure/PIDE/resources.scala |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
3 |
|
56208 | 4 |
Resources for theories and auxiliary files. |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
6 |
|
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
8 |
|
44953 | 9 |
|
48885 | 10 |
import scala.annotation.tailrec |
56823
37be55461dbe
more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents:
56801
diff
changeset
|
11 |
import scala.util.parsing.input.Reader |
48885 | 12 |
|
48409 | 13 |
import java.io.{File => JFile} |
44953 | 14 |
|
15 |
||
64856 | 16 |
class Resources(val base: Sessions.Base, val log: Logger = No_Logger) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
17 |
{ |
64839 | 18 |
val thy_info = new Thy_Info(this) |
19 |
||
65250 | 20 |
def thy_path(path: Path): Path = path.ext("thy") |
21 |
||
64839 | 22 |
|
54514 | 23 |
/* document node names */ |
24 |
||
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
25 |
def node_name(qualifier: String, raw_path: Path): Document.Node.Name = |
54514 | 26 |
{ |
56913
df4cd6e1fdfa
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents:
56823
diff
changeset
|
27 |
val no_qualifier = "" // FIXME |
54514 | 28 |
val path = raw_path.expand |
29 |
val node = path.implode |
|
56913
df4cd6e1fdfa
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents:
56823
diff
changeset
|
30 |
val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse("")) |
54515 | 31 |
val master_dir = if (theory == "") "" else path.dir.implode |
32 |
Document.Node.Name(node, master_dir, theory) |
|
54514 | 33 |
} |
34 |
||
35 |
||
44953 | 36 |
/* file-system operations */ |
37 |
||
38 |
def append(dir: String, source_path: Path): String = |
|
48711
8d381fdef898
need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
wenzelm
parents:
48710
diff
changeset
|
39 |
(Path.explode(dir) + source_path).expand.implode |
44953 | 40 |
|
64654 | 41 |
def append_file(dir: String, raw_name: String): String = |
42 |
if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name)) |
|
43 |
else raw_name |
|
44 |
||
45 |
||
46737 | 46 |
|
64657 | 47 |
/* source files of Isabelle/ML bootstrap */ |
48 |
||
49 |
def source_file(raw_name: String): Option[String] = |
|
50 |
{ |
|
51 |
if (Path.is_wellformed(raw_name)) { |
|
52 |
if (Path.is_valid(raw_name)) { |
|
53 |
def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None |
|
54 |
||
55 |
val path = Path.explode(raw_name) |
|
56 |
val path1 = |
|
57 |
if (path.is_absolute || path.is_current) check(path) |
|
58 |
else { |
|
59 |
check(Path.explode("~~/src/Pure") + path) orElse |
|
60 |
(if (Isabelle_System.getenv("ML_SOURCES") == "") None |
|
61 |
else check(Path.explode("$ML_SOURCES") + path)) |
|
62 |
} |
|
63 |
Some(File.platform_path(path1 getOrElse path)) |
|
64 |
} |
|
65 |
else None |
|
66 |
} |
|
67 |
else Some(raw_name) |
|
68 |
} |
|
69 |
||
70 |
||
46737 | 71 |
/* theory files */ |
72 |
||
63584
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
wenzelm
parents:
63579
diff
changeset
|
73 |
def loaded_files(syntax: Outer_Syntax, text: String): List[String] = |
56393
22f533e6a049
more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents:
56392
diff
changeset
|
74 |
if (syntax.load_commands_in(text)) { |
57906 | 75 |
val spans = syntax.parse_spans(text) |
59689
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
wenzelm
parents:
59683
diff
changeset
|
76 |
spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList |
56392 | 77 |
} |
78 |
else Nil |
|
48885 | 79 |
|
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
80 |
private def dummy_name(theory: String): Document.Node.Name = |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
81 |
Document.Node.Name(theory + ".thy", "", theory) |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
82 |
|
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
83 |
def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name = |
51634
553953ad8165
more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents:
51293
diff
changeset
|
84 |
{ |
56913
df4cd6e1fdfa
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents:
56823
diff
changeset
|
85 |
val no_qualifier = "" // FIXME |
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
86 |
val thy1 = Thy_Header.base_name(s) |
56913
df4cd6e1fdfa
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents:
56823
diff
changeset
|
87 |
val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1) |
64854 | 88 |
(base.known_theories.get(thy1) orElse |
89 |
base.known_theories.get(thy2) orElse |
|
90 |
base.known_theories.get(Long_Name.base_name(thy1))) match { |
|
91 |
case Some(name) if base.loaded_theories(name.theory) => dummy_name(name.theory) |
|
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
92 |
case Some(name) => name |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
93 |
case None => |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
94 |
val path = Path.explode(s) |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
95 |
val theory = path.base.implode |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
96 |
if (Long_Name.is_qualified(theory)) dummy_name(theory) |
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
97 |
else { |
65250 | 98 |
val node = append(master.master_dir, thy_path(path)) |
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
99 |
val master_dir = append(master.master_dir, path.dir) |
56913
df4cd6e1fdfa
no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents:
56823
diff
changeset
|
100 |
Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory)) |
56801
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents:
56393
diff
changeset
|
101 |
} |
51634
553953ad8165
more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents:
51293
diff
changeset
|
102 |
} |
553953ad8165
more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents:
51293
diff
changeset
|
103 |
} |
553953ad8165
more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents:
51293
diff
changeset
|
104 |
|
64656 | 105 |
def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A = |
106 |
{ |
|
107 |
val path = Path.explode(name.node) |
|
108 |
if (!path.is_file) error("No such file: " + path.toString) |
|
109 |
||
110 |
val reader = Scan.byte_reader(path.file) |
|
111 |
try { f(reader) } finally { reader.close } |
|
112 |
} |
|
113 |
||
59705 | 114 |
def check_thy_reader(qualifier: String, node_name: Document.Node.Name, |
64825 | 115 |
reader: Reader[Char], start: Token.Pos = Token.Pos.command, strict: Boolean = true) |
116 |
: Document.Node.Header = |
|
46737 | 117 |
{ |
64826
c97296294f6d
clarified check_thy_reader: check node_name here;
wenzelm
parents:
64825
diff
changeset
|
118 |
if (node_name.is_theory && reader.source.length > 0) { |
57615
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
119 |
try { |
64825 | 120 |
val header = Thy_Header.read(reader, start, strict).decode_symbols |
48885 | 121 |
|
59694 | 122 |
val base_name = Long_Name.base_name(node_name.theory) |
123 |
val (name, pos) = header.name |
|
124 |
if (base_name != name) |
|
59718 | 125 |
error("Bad theory name " + quote(name) + |
65250 | 126 |
" for file " + thy_path(Path.basic(base_name)) + Position.here(pos) + |
59718 | 127 |
Completion.report_names(pos, 1, List((base_name, ("theory", base_name))))) |
48885 | 128 |
|
59694 | 129 |
val imports = |
59695 | 130 |
header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) }) |
63579 | 131 |
Document.Node.Header(imports, header.keywords, header.abbrevs) |
57615
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
132 |
} |
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
133 |
catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) } |
50414
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50291
diff
changeset
|
134 |
} |
57615
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
135 |
else Document.Node.no_header |
46737 | 136 |
} |
46748
8f3ae4d04a2d
refined node_header -- more direct buffer access (again);
wenzelm
parents:
46737
diff
changeset
|
137 |
|
64825 | 138 |
def check_thy(qualifier: String, name: Document.Node.Name, |
139 |
start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header = |
|
140 |
with_thy_reader(name, check_thy_reader(qualifier, name, _, start, strict)) |
|
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
141 |
|
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
142 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
143 |
/* special header */ |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
144 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
145 |
def special_header(name: Document.Node.Name): Option[Document.Node.Header] = |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
146 |
if (Thy_Header.is_ml_root(name.theory)) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
147 |
Some(Document.Node.Header(List((import_name("", name, Thy_Header.ML_BOOTSTRAP), Position.none)))) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
148 |
else if (Thy_Header.is_bootstrap(name.theory)) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
149 |
Some(Document.Node.Header(List((import_name("", name, Thy_Header.PURE), Position.none)))) |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
150 |
else None |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
151 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
152 |
|
64797 | 153 |
/* blobs */ |
154 |
||
155 |
def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] = |
|
156 |
(for { |
|
157 |
(node_name, node) <- nodes.iterator |
|
64854 | 158 |
if !base.loaded_theories(node_name.theory) |
64797 | 159 |
cmd <- node.load_commands.iterator |
160 |
name <- cmd.blobs_undefined.iterator |
|
161 |
} yield name).toList |
|
162 |
||
163 |
||
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
164 |
/* document changes */ |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
165 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
166 |
def parse_change( |
56315 | 167 |
reparse_limit: Int, |
168 |
previous: Document.Version, |
|
169 |
doc_blobs: Document.Blobs, |
|
170 |
edits: List[Document.Edit_Text]): Session.Change = |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
171 |
Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits) |
55134
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54521
diff
changeset
|
172 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
173 |
def commit(change: Session.Change) { } |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
174 |
} |