author | wenzelm |
Tue, 18 Apr 2017 14:19:49 +0200 | |
changeset 65503 | a3fffad8f217 |
parent 65498 | 2af863e28204 |
child 65529 | 53fd6cf53ec2 |
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 |
||
65359 | 16 |
class Resources( |
65361 | 17 |
val session_base: Sessions.Base, |
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
18 |
val default_qualifier: String = Sessions.DRAFT, |
65359 | 19 |
val log: Logger = No_Logger) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
20 |
{ |
64839 | 21 |
val thy_info = new Thy_Info(this) |
22 |
||
65250 | 23 |
def thy_path(path: Path): Path = path.ext("thy") |
24 |
||
64839 | 25 |
|
44953 | 26 |
/* file-system operations */ |
27 |
||
28 |
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
|
29 |
(Path.explode(dir) + source_path).expand.implode |
44953 | 30 |
|
65488 | 31 |
def append(node_name: Document.Node.Name, source_path: Path): String = |
32 |
append(node_name.master_dir, source_path) |
|
33 |
||
46737 | 34 |
|
64657 | 35 |
/* source files of Isabelle/ML bootstrap */ |
36 |
||
37 |
def source_file(raw_name: String): Option[String] = |
|
38 |
{ |
|
39 |
if (Path.is_wellformed(raw_name)) { |
|
40 |
if (Path.is_valid(raw_name)) { |
|
41 |
def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None |
|
42 |
||
43 |
val path = Path.explode(raw_name) |
|
44 |
val path1 = |
|
45 |
if (path.is_absolute || path.is_current) check(path) |
|
46 |
else { |
|
47 |
check(Path.explode("~~/src/Pure") + path) orElse |
|
48 |
(if (Isabelle_System.getenv("ML_SOURCES") == "") None |
|
49 |
else check(Path.explode("$ML_SOURCES") + path)) |
|
50 |
} |
|
51 |
Some(File.platform_path(path1 getOrElse path)) |
|
52 |
} |
|
53 |
else None |
|
54 |
} |
|
55 |
else Some(raw_name) |
|
56 |
} |
|
57 |
||
58 |
||
46737 | 59 |
/* theory files */ |
60 |
||
63584
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
wenzelm
parents:
63579
diff
changeset
|
61 |
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
|
62 |
if (syntax.load_commands_in(text)) { |
57906 | 63 |
val spans = syntax.parse_spans(text) |
59689
7968c57ea240
simplified Command.resolve_files in ML, using blobs_index from Scala;
wenzelm
parents:
59683
diff
changeset
|
64 |
spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList |
56392 | 65 |
} |
66 |
else Nil |
|
48885 | 67 |
|
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
68 |
def theory_qualifier(name: Document.Node.Name): String = |
65457 | 69 |
session_base.global_theories.getOrElse(name.theory, Long_Name.qualifier(name.theory)) |
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
70 |
|
65476 | 71 |
def theory_name(qualifier: String, theory0: String): (Boolean, String) = |
65471
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
72 |
session_base.loaded_theories.get(theory0) match { |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
73 |
case Some(theory) => (true, theory) |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
74 |
case None => |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
75 |
val theory = |
65503 | 76 |
if (Long_Name.is_qualified(theory0) || session_base.global_theories.isDefinedAt(theory0)) |
77 |
theory0 |
|
65471
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
78 |
else Long_Name.qualify(qualifier, theory0) |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
79 |
(false, theory) |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
80 |
} |
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
81 |
|
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
82 |
def import_name(qualifier: String, dir: String, s: String): Document.Node.Name = |
65476 | 83 |
theory_name(qualifier, Thy_Header.import_name(s)) match { |
65472 | 84 |
case (true, theory) => Document.Node.Name.loaded_theory(theory) |
85 |
case (false, theory) => |
|
65498 | 86 |
session_base.known_theory(theory) match { |
65472 | 87 |
case Some(node_name) => node_name |
88 |
case None => |
|
89 |
val path = Path.explode(s) |
|
90 |
val node = append(dir, thy_path(path)) |
|
91 |
val master_dir = append(dir, path.dir) |
|
92 |
Document.Node.Name(node, master_dir, theory) |
|
93 |
} |
|
94 |
} |
|
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
|
95 |
|
65489 | 96 |
def import_name(node_name: Document.Node.Name, s: String): Document.Node.Name = |
97 |
import_name(theory_qualifier(node_name), node_name.master_dir, s) |
|
98 |
||
64656 | 99 |
def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A = |
100 |
{ |
|
65494 | 101 |
val path = File.check_file(Path.explode(name.node)) |
64656 | 102 |
val reader = Scan.byte_reader(path.file) |
103 |
try { f(reader) } finally { reader.close } |
|
104 |
} |
|
105 |
||
65359 | 106 |
def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char], |
107 |
start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header = |
|
46737 | 108 |
{ |
64826
c97296294f6d
clarified check_thy_reader: check node_name here;
wenzelm
parents:
64825
diff
changeset
|
109 |
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
|
110 |
try { |
64825 | 111 |
val header = Thy_Header.read(reader, start, strict).decode_symbols |
48885 | 112 |
|
65439 | 113 |
val base_name = node_name.theory_base_name |
59694 | 114 |
val (name, pos) = header.name |
115 |
if (base_name != name) |
|
59718 | 116 |
error("Bad theory name " + quote(name) + |
65250 | 117 |
" for file " + thy_path(Path.basic(base_name)) + Position.here(pos) + |
59718 | 118 |
Completion.report_names(pos, 1, List((base_name, ("theory", base_name))))) |
48885 | 119 |
|
65489 | 120 |
val imports = header.imports.map({ case (s, pos) => (import_name(node_name, s), pos) }) |
63579 | 121 |
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
|
122 |
} |
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
123 |
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
|
124 |
} |
57615
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
125 |
else Document.Node.no_header |
46737 | 126 |
} |
46748
8f3ae4d04a2d
refined node_header -- more direct buffer access (again);
wenzelm
parents:
46737
diff
changeset
|
127 |
|
65359 | 128 |
def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command, |
129 |
strict: Boolean = true): Document.Node.Header = |
|
130 |
with_thy_reader(name, check_thy_reader(name, _, start, strict)) |
|
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
131 |
|
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
132 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
133 |
/* special header */ |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
134 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
135 |
def special_header(name: Document.Node.Name): Option[Document.Node.Header] = |
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
136 |
{ |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
137 |
val imports = |
65489 | 138 |
if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP)) |
139 |
else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE)) |
|
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
140 |
else Nil |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
141 |
if (imports.isEmpty) None |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
142 |
else Some(Document.Node.Header(imports.map((_, Position.none)))) |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
143 |
} |
64673
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 |
|
64797 | 146 |
/* blobs */ |
147 |
||
148 |
def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] = |
|
149 |
(for { |
|
150 |
(node_name, node) <- nodes.iterator |
|
65361 | 151 |
if !session_base.loaded_theory(node_name) |
64797 | 152 |
cmd <- node.load_commands.iterator |
153 |
name <- cmd.blobs_undefined.iterator |
|
154 |
} yield name).toList |
|
155 |
||
156 |
||
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
157 |
/* document changes */ |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
158 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
159 |
def parse_change( |
56315 | 160 |
reparse_limit: Int, |
161 |
previous: Document.Version, |
|
162 |
doc_blobs: Document.Blobs, |
|
163 |
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
|
164 |
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
|
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 commit(change: Session.Change) { } |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
167 |
} |