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