src/Pure/System/build.scala
author wenzelm
Wed, 18 Jul 2012 17:27:28 +0200
changeset 49354 62570361e608
parent 49352 9c7f8e5805b4
child 49355 6f4fc030882a
permissions -rw-r--r--
more errors;
     1 /*  Title:      Pure/System/build.scala
     2     Author:     Makarius
     3 
     4 Build and manage Isabelle sessions.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.io.File
    11 
    12 import scala.collection.mutable
    13 
    14 
    15 object Build
    16 {
    17   /* command line entry point */
    18 
    19   private object Bool
    20   {
    21     def unapply(s: String): Option[Boolean] =
    22       s match {
    23         case "true" => Some(true)
    24         case "false" => Some(false)
    25         case _ => None
    26       }
    27   }
    28 
    29   def main(args: Array[String])
    30   {
    31     def bad_args(): Nothing = error("Bad arguments: " + args.toString)
    32 
    33     val rc =
    34       try {
    35         args.toList match {
    36           case Bool(all_sessions) :: Bool(build_images) :: Bool(list_only) :: rest =>
    37             rest.indexWhere(_ == "\n") match {
    38               case -1 => bad_args()
    39               case i =>
    40                 val (options, rest1) = rest.splitAt(i)
    41                 val sessions = rest1.tail
    42                 build(all_sessions, build_images, list_only, options, sessions)
    43             }
    44           case _ => bad_args()
    45         }
    46       }
    47       catch {
    48         case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
    49       }
    50 
    51     sys.exit(rc)
    52   }
    53 
    54 
    55   /* build */
    56 
    57   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
    58     options: List[String], sessions: List[String]): Int =
    59   {
    60     println("options = " + options.toString)
    61     println("sessions = " + sessions.toString)
    62 
    63     find_sessions() foreach println
    64 
    65     0
    66   }
    67 
    68 
    69   /** session information **/
    70 
    71   type Options = List[(String, Option[String])]
    72 
    73   case class Session_Info(
    74     dir: Path,
    75     name: String,
    76     parent: String,
    77     description: String,
    78     options: Options,
    79     theories: List[(Options, String)],
    80     files: List[String])
    81 
    82   private val pure_info =
    83     Session_Info(Path.current, "Pure", "", "", Nil, List((Nil, "Pure")), Nil)
    84 
    85 
    86   /* parsing */
    87 
    88   val ROOT_NAME = "ROOT"
    89 
    90   private case class Session_Entry(
    91     name: String,
    92     reset: Boolean,
    93     path: Option[String],
    94     parent: String,
    95     description: String,
    96     options: Options,
    97     theories: List[(Options, List[String])],
    98     files: List[String])
    99 
   100   private object Parser extends Parse.Parser
   101   {
   102     val SESSION = "session"
   103     val IN = "in"
   104     val DESCRIPTION = "description"
   105     val OPTIONS = "options"
   106     val THEORIES = "theories"
   107     val FILES = "files"
   108 
   109     val syntax =
   110       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
   111         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
   112 
   113     val session_entry: Parser[Session_Entry] =
   114     {
   115       val session_name = atom("session name", _.is_name)
   116       val theory_name = atom("theory name", _.is_name)
   117 
   118       val option =
   119         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   120       val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   121 
   122       val theories =
   123         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   124           { case _ ~ (x ~ y) => (x, y) }
   125 
   126       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   127         (keyword("!") ^^^ true | success(false)) ~
   128         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
   129         (keyword("=") ~> session_name <~ keyword("+")) ~
   130         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   131         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   132         rep1(theories) ~
   133         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
   134           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h => Session_Entry(a, b, c, d, e, f, g, h) }
   135     }
   136 
   137     def parse_entries(root: File): List[Session_Entry] =
   138     {
   139       val toks = syntax.scan(Standard_System.read_file(root))
   140       parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
   141         case Success(result, _) => result
   142         case bad => error(bad.toString)
   143       }
   144     }
   145   }
   146 
   147 
   148   /* find session */
   149 
   150   def find_sessions(more_dirs: List[Path] = Nil): List[Session_Info] =
   151   {
   152     val infos = new mutable.ListBuffer[Session_Info]
   153     infos += pure_info
   154 
   155     for {
   156       dir <- Isabelle_System.components() ++ more_dirs
   157       root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
   158       if root.isFile
   159       entry <- Parser.parse_entries(root)
   160     }
   161     {
   162       try {
   163         val parent =
   164           infos.find(_.name == entry.parent) getOrElse
   165            error("Unknown parent session: " + quote(entry.parent))
   166         val full_name =
   167           if (entry.reset) entry.name
   168           else parent.name + "-" + entry.name
   169 
   170         if (entry.name == "") error("Bad session name")
   171         if (infos.exists(_.name == full_name))
   172           error("Duplicate session name: " + quote(full_name))
   173 
   174         val path =
   175           entry.path match {
   176             case Some(p) => Path.explode(p)
   177             case None => Path.basic(entry.name)
   178           }
   179         val thys = entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
   180         val info =
   181           Session_Info(dir + path, full_name, entry.parent, entry.description,
   182             entry.options, thys, entry.files)
   183 
   184         infos += info
   185       }
   186       catch {
   187         case ERROR(msg) =>
   188           error(msg + "\nThe error(s) above occurred in session entry " +
   189             quote(entry.name) + " (file " + quote(root.toString) + ")")
   190       }
   191     }
   192     infos.toList
   193   }
   194 }
   195