author | wenzelm |
Wed, 07 Nov 2018 21:42:16 +0100 | |
changeset 69255 | 800b1ce96fce |
parent 69008 | d55783ea6cf6 |
child 69393 | ed0824ef337e |
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, |
65359 | 18 |
val log: Logger = No_Logger) |
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
19 |
{ |
66959 | 20 |
resources => |
64839 | 21 |
|
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
22 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
23 |
/* file formats */ |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
24 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
25 |
val file_formats: File_Format.Environment = File_Format.environment() |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
26 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
27 |
def make_theory_name(name: Document.Node.Name): Option[Document.Node.Name] = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
28 |
file_formats.get(name).flatMap(_.make_theory_name(resources, name)) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
29 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
30 |
def make_theory_content(thy_name: Document.Node.Name): Option[String] = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
31 |
file_formats.get_theory(thy_name).flatMap(_.make_theory_content(resources, thy_name)) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
32 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
33 |
def make_preview(snapshot: Document.Snapshot): Option[Present.Preview] = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
34 |
file_formats.get(snapshot.node_name).flatMap(_.make_preview(snapshot)) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
35 |
|
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
36 |
def is_hidden(name: Document.Node.Name): Boolean = |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
37 |
!name.is_theory || name.theory == Sessions.root_name || file_formats.is_theory(name) |
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
69008
diff
changeset
|
38 |
|
65250 | 39 |
def thy_path(path: Path): Path = path.ext("thy") |
40 |
||
64839 | 41 |
|
44953 | 42 |
/* file-system operations */ |
43 |
||
44 |
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
|
45 |
(Path.explode(dir) + source_path).expand.implode |
44953 | 46 |
|
65488 | 47 |
def append(node_name: Document.Node.Name, source_path: Path): String = |
48 |
append(node_name.master_dir, source_path) |
|
49 |
||
46737 | 50 |
|
64657 | 51 |
/* source files of Isabelle/ML bootstrap */ |
52 |
||
53 |
def source_file(raw_name: String): Option[String] = |
|
54 |
{ |
|
55 |
if (Path.is_wellformed(raw_name)) { |
|
56 |
if (Path.is_valid(raw_name)) { |
|
57 |
def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None |
|
58 |
||
59 |
val path = Path.explode(raw_name) |
|
60 |
val path1 = |
|
61 |
if (path.is_absolute || path.is_current) check(path) |
|
62 |
else { |
|
63 |
check(Path.explode("~~/src/Pure") + path) orElse |
|
64 |
(if (Isabelle_System.getenv("ML_SOURCES") == "") None |
|
65 |
else check(Path.explode("$ML_SOURCES") + path)) |
|
66 |
} |
|
67 |
Some(File.platform_path(path1 getOrElse path)) |
|
68 |
} |
|
69 |
else None |
|
70 |
} |
|
71 |
else Some(raw_name) |
|
72 |
} |
|
73 |
||
74 |
||
46737 | 75 |
/* theory files */ |
76 |
||
66698
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
77 |
def loaded_files(syntax: Outer_Syntax, name: Document.Node.Name): () => List[Path] = |
66695 | 78 |
{ |
66918
ec2b50aeb0dd
more robust treatment of UTF8 in raw byte sources;
wenzelm
parents:
66917
diff
changeset
|
79 |
val (is_utf8, raw_text) = |
ec2b50aeb0dd
more robust treatment of UTF8 in raw byte sources;
wenzelm
parents:
66917
diff
changeset
|
80 |
with_thy_reader(name, reader => (Scan.reader_is_utf8(reader), reader.source.toString)) |
66698
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
81 |
() => { |
66917
fcf84cd6c94f
clarified check, assuming that load commands are plain ASCII;
wenzelm
parents:
66850
diff
changeset
|
82 |
if (syntax.load_commands_in(raw_text)) { |
66918
ec2b50aeb0dd
more robust treatment of UTF8 in raw byte sources;
wenzelm
parents:
66917
diff
changeset
|
83 |
val text = Symbol.decode(Scan.reader_decode_utf8(is_utf8, raw_text)) |
66698
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
84 |
val spans = syntax.parse_spans(text) |
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
85 |
val dir = Path.explode(name.master_dir) |
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
86 |
spans.iterator.map(Command.span_files(syntax, _)._1).flatten. |
66700 | 87 |
map(a => (dir + Path.explode(a)).expand).toList |
66698
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
88 |
} |
5b9dc3f7bcde
prefer sequential file-system access, but parallel parse;
wenzelm
parents:
66697
diff
changeset
|
89 |
else Nil |
66696
8f863dae78a0
clarified pure_files, based on uniform loaded_files;
wenzelm
parents:
66695
diff
changeset
|
90 |
} |
66695 | 91 |
} |
92 |
||
66701 | 93 |
def pure_files(syntax: Outer_Syntax, dir: Path): List[Path] = |
94 |
{ |
|
95 |
val roots = |
|
96 |
for { (name, _) <- Thy_Header.ml_roots } |
|
97 |
yield (dir + Path.explode(name)).expand |
|
98 |
val files = |
|
99 |
for { |
|
100 |
(path, (_, theory)) <- roots zip Thy_Header.ml_roots |
|
101 |
file <- loaded_files(syntax, Document.Node.Name(path.implode, path.dir.implode, theory))() |
|
102 |
} yield file |
|
103 |
roots ::: files |
|
104 |
} |
|
66696
8f863dae78a0
clarified pure_files, based on uniform loaded_files;
wenzelm
parents:
66695
diff
changeset
|
105 |
|
67104
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
106 |
def theory_name(qualifier: String, theory: String): String = |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
107 |
if (Long_Name.is_qualified(theory) || session_base.global_theories.isDefinedAt(theory)) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
108 |
theory |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
109 |
else Long_Name.qualify(qualifier, theory) |
65471
05e5bffcf1d8
clarified loaded_theories: map to qualified theory name;
wenzelm
parents:
65467
diff
changeset
|
110 |
|
65445
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
111 |
def import_name(qualifier: String, dir: String, s: String): Document.Node.Name = |
67104
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
112 |
{ |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
113 |
val theory = theory_name(qualifier, Thy_Header.import_name(s)) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
114 |
if (session_base.loaded_theory(theory)) Document.Node.Name.loaded_theory(theory) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
115 |
else { |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
116 |
session_base.known_theory(theory) match { |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
117 |
case Some(node_name) => node_name |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
118 |
case None => |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
119 |
if (Thy_Header.is_base_name(s) && Long_Name.is_qualified(s)) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
120 |
Document.Node.Name.loaded_theory(theory) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
121 |
else { |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
122 |
val path = Path.explode(s) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
123 |
val node = append(dir, thy_path(path)) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
124 |
val master_dir = append(dir, path.dir) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
125 |
Document.Node.Name(node, master_dir, theory) |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
126 |
} |
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
127 |
} |
65472 | 128 |
} |
67104
a2fa0c6a7aff
clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents:
67060
diff
changeset
|
129 |
} |
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
|
130 |
|
65529 | 131 |
def import_name(name: Document.Node.Name, s: String): Document.Node.Name = |
66966 | 132 |
import_name(session_base.theory_qualifier(name), name.master_dir, s) |
65489 | 133 |
|
66966 | 134 |
def standard_import(base: Sessions.Base, qualifier: String, dir: String, s: String): String = |
66767 | 135 |
{ |
136 |
val name = import_name(qualifier, dir, s) |
|
137 |
val s1 = |
|
138 |
if (session_base.loaded_theory(name)) name.theory |
|
139 |
else { |
|
66850 | 140 |
(try { Some(name.path) } catch { case ERROR(_) => None }) match { |
141 |
case None => s |
|
142 |
case Some(path) => |
|
143 |
session_base.known.get_file(path.file) match { |
|
66966 | 144 |
case Some(name1) if base.theory_qualifier(name1) != qualifier => |
66850 | 145 |
name1.theory |
146 |
case Some(name1) if Thy_Header.is_base_name(s) => |
|
147 |
name1.theory_base_name |
|
148 |
case _ => s |
|
149 |
} |
|
66767 | 150 |
} |
151 |
} |
|
152 |
val name2 = import_name(qualifier, dir, s1) |
|
153 |
if (name.node == name2.node) s1 else s |
|
154 |
} |
|
155 |
||
64656 | 156 |
def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A = |
157 |
{ |
|
66771
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
158 |
val path = name.path |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
159 |
if (path.is_file) { |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
160 |
val reader = Scan.byte_reader(path.file) |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
161 |
try { f(reader) } finally { reader.close } |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
162 |
} |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
163 |
else if (name.node == name.theory) |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
164 |
error("Cannot load theory " + quote(name.theory)) |
925d10a7a610
clarified error for bad session-qualified imports;
wenzelm
parents:
66767
diff
changeset
|
165 |
else error ("Cannot load theory file " + path) |
64656 | 166 |
} |
167 |
||
65359 | 168 |
def check_thy_reader(node_name: Document.Node.Name, reader: Reader[Char], |
169 |
start: Token.Pos = Token.Pos.command, strict: Boolean = true): Document.Node.Header = |
|
46737 | 170 |
{ |
64826
c97296294f6d
clarified check_thy_reader: check node_name here;
wenzelm
parents:
64825
diff
changeset
|
171 |
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
|
172 |
try { |
66713 | 173 |
val header = Thy_Header.read(reader, start, strict) |
48885 | 174 |
|
65439 | 175 |
val base_name = node_name.theory_base_name |
59694 | 176 |
val (name, pos) = header.name |
177 |
if (base_name != name) |
|
59718 | 178 |
error("Bad theory name " + quote(name) + |
65250 | 179 |
" for file " + thy_path(Path.basic(base_name)) + Position.here(pos) + |
66766 | 180 |
Completion.report_theories(pos, List(base_name))) |
48885 | 181 |
|
67296
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
182 |
val imports = |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
183 |
header.imports.map({ case (s, pos) => |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
184 |
val name = import_name(node_name, s) |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
185 |
if (Sessions.exclude_theory(name.theory_base_name)) |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
186 |
error("Bad theory name " + quote(name.theory_base_name) + Position.here(pos)) |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
187 |
(name, pos) |
888aa91f0556
clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents:
67294
diff
changeset
|
188 |
}) |
63579 | 189 |
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
|
190 |
} |
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
191 |
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
|
192 |
} |
57615
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents:
56913
diff
changeset
|
193 |
else Document.Node.no_header |
46737 | 194 |
} |
46748
8f3ae4d04a2d
refined node_header -- more direct buffer access (again);
wenzelm
parents:
46737
diff
changeset
|
195 |
|
65359 | 196 |
def check_thy(name: Document.Node.Name, start: Token.Pos = Token.Pos.command, |
197 |
strict: Boolean = true): Document.Node.Header = |
|
198 |
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
|
199 |
|
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
200 |
|
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
201 |
/* special header */ |
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
202 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
203 |
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
|
204 |
{ |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
205 |
val imports = |
67215 | 206 |
if (name.theory == Sessions.root_name) List(import_name(name, Sessions.theory_name)) |
207 |
else if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP)) |
|
65489 | 208 |
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
|
209 |
else Nil |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
210 |
if (imports.isEmpty) None |
e9e7f5f5794c
more qualifier treatment, but in the end it is still ignored;
wenzelm
parents:
65441
diff
changeset
|
211 |
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
|
212 |
} |
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
213 |
|
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64657
diff
changeset
|
214 |
|
64797 | 215 |
/* blobs */ |
216 |
||
217 |
def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] = |
|
218 |
(for { |
|
219 |
(node_name, node) <- nodes.iterator |
|
65361 | 220 |
if !session_base.loaded_theory(node_name) |
64797 | 221 |
cmd <- node.load_commands.iterator |
222 |
name <- cmd.blobs_undefined.iterator |
|
223 |
} yield name).toList |
|
224 |
||
225 |
||
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
226 |
/* document changes */ |
50566
b43c4f660320
tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents:
50415
diff
changeset
|
227 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
228 |
def parse_change( |
56315 | 229 |
reparse_limit: Int, |
230 |
previous: Document.Version, |
|
231 |
doc_blobs: Document.Blobs, |
|
68336
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents:
68317
diff
changeset
|
232 |
edits: List[Document.Edit_Text], |
68381
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
wenzelm
parents:
68336
diff
changeset
|
233 |
consolidate: List[Document.Node.Name]): Session.Change = |
68336
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents:
68317
diff
changeset
|
234 |
Thy_Syntax.parse_change(resources, reparse_limit, previous, doc_blobs, edits, consolidate) |
55134
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
54521
diff
changeset
|
235 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
236 |
def commit(change: Session.Change) { } |
66959 | 237 |
|
238 |
||
239 |
/* theory and file dependencies */ |
|
240 |
||
69007 | 241 |
def dependencies( |
242 |
thys: List[(Document.Node.Name, Position.T)], |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
243 |
progress: Progress = No_Progress): Dependencies[Unit] = |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
244 |
Dependencies.empty[Unit].require_thys((), thys, progress = progress) |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
245 |
|
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
246 |
def session_dependencies(info: Sessions.Info, progress: Progress = No_Progress) |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
247 |
: Dependencies[Options] = |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
248 |
{ |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
249 |
val qualifier = info.name |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
250 |
val dir = info.dir.implode |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
251 |
|
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
252 |
(Dependencies.empty[Options] /: info.theories)({ case (dependencies, (options, thys)) => |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
253 |
dependencies.require_thys(options, |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
254 |
for { (thy, pos) <- thys } yield (import_name(qualifier, dir, thy), pos), |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
255 |
progress = progress) |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
256 |
}) |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
257 |
} |
69007 | 258 |
|
66959 | 259 |
object Dependencies |
260 |
{ |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
261 |
def empty[A]: Dependencies[A] = new Dependencies[A](Nil, Map.empty) |
69007 | 262 |
|
263 |
private def show_path(names: List[Document.Node.Name]): String = |
|
264 |
names.map(name => quote(name.theory)).mkString(" via ") |
|
265 |
||
266 |
private def cycle_msg(names: List[Document.Node.Name]): String = |
|
267 |
"Cyclic dependency of " + show_path(names) |
|
268 |
||
269 |
private def required_by(initiators: List[Document.Node.Name]): String = |
|
270 |
if (initiators.isEmpty) "" |
|
271 |
else "\n(required by " + show_path(initiators.reverse) + ")" |
|
66959 | 272 |
} |
273 |
||
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
274 |
final class Dependencies[A] private( |
66959 | 275 |
rev_entries: List[Document.Node.Entry], |
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
276 |
seen: Map[Document.Node.Name, A]) |
66959 | 277 |
{ |
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
278 |
private def cons(entry: Document.Node.Entry): Dependencies[A] = |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
279 |
new Dependencies[A](entry :: rev_entries, seen) |
66959 | 280 |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
281 |
def require_thy(adjunct: A, |
69007 | 282 |
thy: (Document.Node.Name, Position.T), |
283 |
initiators: List[Document.Node.Name] = Nil, |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
284 |
progress: Progress = No_Progress): Dependencies[A] = |
69007 | 285 |
{ |
286 |
val (name, pos) = thy |
|
287 |
||
288 |
def message: String = |
|
289 |
"The error(s) above occurred for theory " + quote(name.theory) + |
|
290 |
Dependencies.required_by(initiators) + Position.here(pos) |
|
291 |
||
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
292 |
if (seen.isDefinedAt(name)) this |
69007 | 293 |
else { |
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
294 |
val dependencies1 = new Dependencies[A](rev_entries, seen + (name -> adjunct)) |
69007 | 295 |
if (session_base.loaded_theory(name)) dependencies1 |
296 |
else { |
|
297 |
try { |
|
298 |
if (initiators.contains(name)) error(Dependencies.cycle_msg(initiators)) |
|
299 |
||
300 |
progress.expose_interrupt() |
|
301 |
val header = |
|
302 |
try { check_thy(name, Token.Pos.file(name.node)).cat_errors(message) } |
|
303 |
catch { case ERROR(msg) => cat_error(msg, message) } |
|
304 |
val entry = Document.Node.Entry(name, header) |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
305 |
dependencies1.require_thys(adjunct, header.imports, |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
306 |
initiators = name :: initiators, progress = progress).cons(entry) |
69007 | 307 |
} |
308 |
catch { |
|
309 |
case e: Throwable => |
|
310 |
dependencies1.cons(Document.Node.Entry(name, Document.Node.bad_header(Exn.message(e)))) |
|
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
||
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
316 |
def require_thys(adjunct: A, |
69007 | 317 |
thys: List[(Document.Node.Name, Position.T)], |
318 |
progress: Progress = No_Progress, |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
319 |
initiators: List[Document.Node.Name] = Nil): Dependencies[A] = |
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
320 |
(this /: thys)(_.require_thy(adjunct, _, progress = progress, initiators = initiators)) |
66959 | 321 |
|
322 |
def entries: List[Document.Node.Entry] = rev_entries.reverse |
|
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
323 |
|
67059 | 324 |
def theories: List[Document.Node.Name] = entries.map(_.name) |
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
325 |
def adjunct_theories: List[(A, Document.Node.Name)] = theories.map(name => (seen(name), name)) |
66959 | 326 |
|
327 |
def errors: List[String] = entries.flatMap(_.header.errors) |
|
328 |
||
69008
d55783ea6cf6
more detailed session dependencies, with conditions for theories;
wenzelm
parents:
69007
diff
changeset
|
329 |
def check_errors: Dependencies[A] = |
67056 | 330 |
errors match { |
331 |
case Nil => this |
|
332 |
case errs => error(cat_lines(errs)) |
|
333 |
} |
|
334 |
||
66959 | 335 |
lazy val loaded_theories: Graph[String, Outer_Syntax] = |
336 |
(session_base.loaded_theories /: entries)({ case (graph, entry) => |
|
337 |
val name = entry.name.theory |
|
338 |
val imports = entry.header.imports.map(p => p._1.theory) |
|
339 |
||
340 |
val graph1 = (graph /: (name :: imports))(_.default_node(_, Outer_Syntax.empty)) |
|
341 |
val graph2 = (graph1 /: imports)(_.add_edge(_, name)) |
|
342 |
||
343 |
val syntax0 = if (name == Thy_Header.PURE) List(Thy_Header.bootstrap_syntax) else Nil |
|
344 |
val syntax1 = (name :: graph2.imm_preds(name).toList).map(graph2.get_node(_)) |
|
345 |
val syntax = Outer_Syntax.merge(syntax0 ::: syntax1) + entry.header |
|
346 |
||
347 |
graph2.map_node(name, _ => syntax) |
|
348 |
}) |
|
349 |
||
350 |
def loaded_files: List[(String, List[Path])] = |
|
351 |
{ |
|
67059 | 352 |
theories.map(_.theory) zip |
66959 | 353 |
Par_List.map((e: () => List[Path]) => e(), |
67059 | 354 |
theories.map(name => resources.loaded_files(loaded_theories.get_node(name.theory), name))) |
66959 | 355 |
} |
356 |
||
357 |
def imported_files: List[Path] = |
|
358 |
{ |
|
359 |
val base_theories = |
|
67059 | 360 |
loaded_theories.all_preds(theories.map(_.theory)). |
66959 | 361 |
filter(session_base.loaded_theories.defined(_)) |
362 |
||
68306
d575281e18d0
clarified signature: Known.theories retains Document.Node.Entry (with header);
wenzelm
parents:
67881
diff
changeset
|
363 |
base_theories.map(theory => session_base.known.theories(theory).name.path) ::: |
66959 | 364 |
base_theories.flatMap(session_base.known.loaded_files(_)) |
365 |
} |
|
366 |
||
367 |
lazy val overall_syntax: Outer_Syntax = |
|
368 |
Outer_Syntax.merge(loaded_theories.maximals.map(loaded_theories.get_node(_))) |
|
369 |
||
370 |
override def toString: String = entries.toString |
|
371 |
} |
|
43651
511df47bcadc
some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff
changeset
|
372 |
} |