src/Pure/System/build.scala
author wenzelm
Wed Jul 18 13:43:36 2012 +0200 (2012-07-18)
changeset 48334 8dff9933e72a
parent 48280 7d86239986c2
child 48335 2f923e994056
permissions -rw-r--r--
added parser for Session_Info;
     1 /*  Title:      Pure/System/build.scala
     2     Author:     Makarius
     3 
     4 Build and manage Isabelle sessions.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Build
    11 {
    12   /* command line entry point */
    13 
    14   private object Bool
    15   {
    16     def unapply(s: String): Option[Boolean] =
    17       s match {
    18         case "true" => Some(true)
    19         case "false" => Some(false)
    20         case _ => None
    21       }
    22   }
    23 
    24   def main(args: Array[String])
    25   {
    26     def bad_args()
    27     {
    28       java.lang.System.err.println("Bad arguments: " + args.toString)
    29       sys.exit(2)
    30     }
    31 
    32     args.toList match {
    33       case Bool(all_sessions) :: Bool(build_images) :: Bool(list_only) :: rest =>
    34         rest.indexWhere(_ == "\n") match {
    35           case -1 => bad_args()
    36           case i =>
    37             val (options, rest1) = rest.splitAt(i)
    38             val sessions = rest1.tail
    39             val rc = build(all_sessions, build_images, list_only, options, sessions)
    40             sys.exit(rc)
    41         }
    42       case _ => bad_args()
    43     }
    44   }
    45 
    46 
    47   /* build */
    48 
    49   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
    50     options: List[String], sessions: List[String]): Int =
    51   {
    52     val rc = 1
    53 
    54     println("options = " + options.toString)
    55     println("sessions = " + sessions.toString)
    56 
    57     rc
    58   }
    59 
    60 
    61   /* session information */
    62 
    63   val ROOT_NAME = "ROOT"
    64 
    65   type Options = List[(String, Option[String])]
    66 
    67   case class Session_Info(
    68     dir: Path,
    69     name: String,
    70     path: String,
    71     parent: String,
    72     alt_name: Option[String],
    73     description: String,
    74     options: Options,
    75     theories: List[(Options, String)],
    76     files: List[String])
    77 
    78   private object Parser extends Parse.Parser
    79   {
    80     val SESSION = "session"
    81     val IN = "in"
    82     val NAME = "name"
    83     val DESCRIPTION = "description"
    84     val OPTIONS = "options"
    85     val THEORIES = "theories"
    86     val FILES = "files"
    87 
    88     val syntax =
    89       Outer_Syntax.empty + "(" + ")" + "+" + "," + "=" + "[" + "]" +
    90         SESSION + IN + NAME + DESCRIPTION + OPTIONS + THEORIES + FILES
    91 
    92     val session_info: Parser[Session_Info] =
    93     {
    94       val session_name = atom("session name", _.is_name)
    95       val theory_name = atom("theory name", _.is_name)
    96       val file_name = atom("file name", _.is_name)
    97 
    98       val option =
    99         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   100       val options: Parser[Options] =
   101         keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   102 
   103       val theories =
   104         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   105           { case _ ~ (x ~ y) => (x, y) }
   106 
   107       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   108         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
   109         (keyword("=") ~> session_name <~ keyword("+")) ~
   110         (opt(keyword(NAME) ~! session_name ^^ { case _ ~ x => x })) ~
   111         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   112         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   113         rep(theories) ~
   114         (keyword(FILES) ~! rep1(file_name) ^^ { case _ ~ x => x } | success(Nil)) ^^
   115           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h =>
   116             Session_Info(Path.current, a, b getOrElse a, c, d, e, f,
   117               g.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, h) }
   118     }
   119 
   120     def parse_entries(source: CharSequence): List[Session_Info] =
   121     {
   122       val in = Token.reader(syntax.scan(source))
   123       parse_all(rep(session_info), in) match {
   124         case Success(result, _) => result
   125         case bad => error(bad.toString)
   126       }
   127     }
   128   }
   129 
   130   def find_sessions(): List[Session_Info] =
   131   {
   132     for {
   133       dir <- Isabelle_System.components()
   134       root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
   135       if root.isFile
   136       entry <- Parser.parse_entries(Standard_System.read_file(root))
   137     } yield entry.copy(dir = dir)
   138   }
   139 }
   140