src/Pure/Isar/outer_syntax.scala
author wenzelm
Fri Mar 16 21:20:23 2012 +0100 (2012-03-16)
changeset 46969 481b7d9ad6fe
parent 46941 c0f776b661fa
child 47469 ba7fe841c885
permissions -rw-r--r--
more abstract heading level;
     1 /*  Title:      Pure/Isar/outer_syntax.scala
     2     Author:     Makarius
     3 
     4 Isabelle/Isar outer syntax.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.util.parsing.input.{Reader, CharSequenceReader}
    11 import scala.collection.mutable
    12 
    13 
    14 object Outer_Syntax
    15 {
    16   def quote_string(str: String): String =
    17   {
    18     val result = new StringBuilder(str.length + 10)
    19     result += '"'
    20     for (s <- Symbol.iterator(str)) {
    21       if (s.length == 1) {
    22         val c = s(0)
    23         if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
    24           result += '\\'
    25           if (c < 10) result += '0'
    26           if (c < 100) result += '0'
    27           result ++= (c.asInstanceOf[Int].toString)
    28         }
    29         else result += c
    30       }
    31       else result ++= s
    32     }
    33     result += '"'
    34     result.toString
    35   }
    36 
    37   type Decl = (String, Option[(String, List[String])])
    38 
    39   val empty: Outer_Syntax = new Outer_Syntax()
    40   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    41 }
    42 
    43 final class Outer_Syntax private(
    44   keywords: Map[String, String] = Map((";" -> Keyword.DIAG)),
    45   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    46   val completion: Completion = Completion.empty)
    47 {
    48   def keyword_kind(name: String): Option[String] = keywords.get(name)
    49 
    50   def + (name: String, kind: String, replace: String): Outer_Syntax =
    51     new Outer_Syntax(
    52       keywords + (name -> kind),
    53       lexicon + name,
    54       if (Keyword.control(kind)) completion else completion + (name, replace))
    55 
    56   def + (name: String, kind: String): Outer_Syntax = this + (name, kind, name)
    57   def + (name: String): Outer_Syntax = this + (name, Keyword.MINOR)
    58   def + (decl: Outer_Syntax.Decl): Outer_Syntax =
    59     decl match {
    60       case ((name, Some((kind, _)))) => this + (name, kind)
    61       case ((name, None)) => this + name
    62     }
    63 
    64   def is_command(name: String): Boolean =
    65     keyword_kind(name) match {
    66       case Some(kind) => kind != Keyword.MINOR
    67       case None => false
    68     }
    69 
    70   def heading_level(name: String): Option[Int] =
    71   {
    72     keyword_kind(name) match {
    73       case _ if name == "header" => Some(0)
    74       case Some(Keyword.THY_HEADING1) => Some(1)
    75       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
    76       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
    77       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
    78       case Some(kind) if Keyword.theory(kind) => Some(5)
    79       case _ => None
    80     }
    81   }
    82 
    83   def heading_level(command: Command): Option[Int] =
    84     heading_level(command.name)
    85 
    86 
    87   /* tokenize */
    88 
    89   def scan(input: Reader[Char]): List[Token] =
    90   {
    91     import lexicon._
    92 
    93     parseAll(rep(token(is_command)), input) match {
    94       case Success(tokens, _) => tokens
    95       case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
    96     }
    97   }
    98 
    99   def scan(input: CharSequence): List[Token] =
   100     scan(new CharSequenceReader(input))
   101 
   102   def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
   103   {
   104     import lexicon._
   105 
   106     var in: Reader[Char] = new CharSequenceReader(input)
   107     val toks = new mutable.ListBuffer[Token]
   108     var ctxt = context
   109     while (!in.atEnd) {
   110       parse(token_context(is_command, ctxt), in) match {
   111         case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   112         case NoSuccess(_, rest) =>
   113           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   114       }
   115     }
   116     (toks.toList, ctxt)
   117   }
   118 }