src/Pure/PIDE/resources.scala
author wenzelm
Tue Aug 12 00:08:32 2014 +0200 (2014-08-12)
changeset 57905 c0c5652e796e
parent 57615 df1b3452d71c
child 57906 020df63dd0a9
permissions -rw-r--r--
separate module Command_Span: mostly syntactic representation;
potentially prover-specific Output_Syntax.parse_spans;
     1 /*  Title:      Pure/PIDE/resources.scala
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.annotation.tailrec
    11 import scala.util.parsing.input.Reader
    12 
    13 import java.io.{File => JFile}
    14 
    15 
    16 object Resources
    17 {
    18   def thy_path(path: Path): Path = path.ext("thy")
    19 }
    20 
    21 
    22 class Resources(
    23   val loaded_theories: Set[String],
    24   val known_theories: Map[String, Document.Node.Name],
    25   val base_syntax: Prover.Syntax)
    26 {
    27   /* document node names */
    28 
    29   def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
    30   {
    31     val no_qualifier = "" // FIXME
    32     val path = raw_path.expand
    33     val node = path.implode
    34     val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
    35     val master_dir = if (theory == "") "" else path.dir.implode
    36     Document.Node.Name(node, master_dir, theory)
    37   }
    38 
    39 
    40   /* file-system operations */
    41 
    42   def append(dir: String, source_path: Path): String =
    43     (Path.explode(dir) + source_path).expand.implode
    44 
    45   def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    46   {
    47     val path = Path.explode(name.node)
    48     if (!path.is_file) error("No such file: " + path.toString)
    49 
    50     val reader = Scan.byte_reader(path.file)
    51     try { f(reader) } finally { reader.close }
    52   }
    53 
    54 
    55   /* theory files */
    56 
    57   def loaded_files(syntax: Prover.Syntax, text: String): List[String] =
    58     if (syntax.load_commands_in(text)) {
    59       val spans = syntax.parse_spans(syntax.scan(text))
    60       spans.iterator.map(Command_Span.span_files(syntax, _)).flatten.toList
    61     }
    62     else Nil
    63 
    64   private def dummy_name(theory: String): Document.Node.Name =
    65     Document.Node.Name(theory + ".thy", "", theory)
    66 
    67   def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
    68   {
    69     val no_qualifier = "" // FIXME
    70     val thy1 = Thy_Header.base_name(s)
    71     val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
    72     (known_theories.get(thy1) orElse
    73      known_theories.get(thy2) orElse
    74      known_theories.get(Long_Name.base_name(thy1))) match {
    75       case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
    76       case Some(name) => name
    77       case None =>
    78         val path = Path.explode(s)
    79         val theory = path.base.implode
    80         if (Long_Name.is_qualified(theory)) dummy_name(theory)
    81         else {
    82           val node = append(master.master_dir, Resources.thy_path(path))
    83           val master_dir = append(master.master_dir, path.dir)
    84           Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
    85         }
    86     }
    87   }
    88 
    89   def check_thy_reader(qualifier: String, name: Document.Node.Name, reader: Reader[Char])
    90     : Document.Node.Header =
    91   {
    92     if (reader.source.length > 0) {
    93       try {
    94         val header = Thy_Header.read(reader).decode_symbols
    95 
    96         val base_name = Long_Name.base_name(name.theory)
    97         val name1 = header.name
    98         if (base_name != name1)
    99           error("Bad file name " + Resources.thy_path(Path.basic(base_name)) +
   100             " for theory " + quote(name1))
   101 
   102         val imports = header.imports.map(import_name(qualifier, name, _))
   103         Document.Node.Header(imports, header.keywords, Nil)
   104       }
   105       catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
   106     }
   107     else Document.Node.no_header
   108   }
   109 
   110   def check_thy(qualifier: String, name: Document.Node.Name): Document.Node.Header =
   111     with_thy_reader(name, check_thy_reader(qualifier, name, _))
   112 
   113 
   114   /* document changes */
   115 
   116   def parse_change(
   117       reparse_limit: Int,
   118       previous: Document.Version,
   119       doc_blobs: Document.Blobs,
   120       edits: List[Document.Edit_Text]): Session.Change =
   121     Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
   122 
   123   def commit(change: Session.Change) { }
   124 
   125 
   126   /* prover process */
   127 
   128   def start_prover(receiver: Prover.Message => Unit, name: String, args: List[String]): Prover =
   129     new Isabelle_Process(receiver, args) with Protocol
   130 }
   131