src/Pure/Isar/outer_syntax.scala
author wenzelm
Sat May 18 13:00:05 2013 +0200 (2013-05-18)
changeset 52066 83b7b88770c9
parent 50428 7a78a74139f5
child 52439 4cf3f6153eb8
permissions -rw-r--r--
discontinued odd workaround for scala-2.9.2, which is hopefully obsolete in scala-2.10.x;
wenzelm@34166
     1
/*  Title:      Pure/Isar/outer_syntax.scala
wenzelm@34166
     2
    Author:     Makarius
wenzelm@34166
     3
wenzelm@34166
     4
Isabelle/Isar outer syntax.
wenzelm@34166
     5
*/
wenzelm@34166
     6
wenzelm@34166
     7
package isabelle
wenzelm@34166
     8
wenzelm@34166
     9
wenzelm@34166
    10
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@43411
    11
import scala.collection.mutable
wenzelm@34166
    12
wenzelm@34166
    13
wenzelm@43774
    14
object Outer_Syntax
wenzelm@43774
    15
{
wenzelm@43774
    16
  def quote_string(str: String): String =
wenzelm@43774
    17
  {
wenzelm@43774
    18
    val result = new StringBuilder(str.length + 10)
wenzelm@43774
    19
    result += '"'
wenzelm@43774
    20
    for (s <- Symbol.iterator(str)) {
wenzelm@43774
    21
      if (s.length == 1) {
wenzelm@43774
    22
        val c = s(0)
wenzelm@43774
    23
        if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
wenzelm@43774
    24
          result += '\\'
wenzelm@43774
    25
          if (c < 10) result += '0'
wenzelm@43774
    26
          if (c < 100) result += '0'
wenzelm@43774
    27
          result ++= (c.asInstanceOf[Int].toString)
wenzelm@43774
    28
        }
wenzelm@43774
    29
        else result += c
wenzelm@43774
    30
      }
wenzelm@43774
    31
      else result ++= s
wenzelm@43774
    32
    }
wenzelm@43774
    33
    result += '"'
wenzelm@43774
    34
    result.toString
wenzelm@43774
    35
  }
wenzelm@46626
    36
wenzelm@46941
    37
  val empty: Outer_Syntax = new Outer_Syntax()
wenzelm@48870
    38
wenzelm@46941
    39
  def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
wenzelm@48870
    40
wenzelm@48870
    41
  def init_pure(): Outer_Syntax =
wenzelm@48870
    42
    init() + ("theory", Keyword.THY_BEGIN) + ("ML_file", Keyword.THY_LOAD)
wenzelm@43774
    43
}
wenzelm@43774
    44
wenzelm@46712
    45
final class Outer_Syntax private(
wenzelm@48864
    46
  keywords: Map[String, (String, List[String])] = Map.empty,
wenzelm@46626
    47
  lexicon: Scan.Lexicon = Scan.Lexicon.empty,
wenzelm@46941
    48
  val completion: Completion = Completion.empty)
wenzelm@34166
    49
{
wenzelm@48660
    50
  override def toString: String =
wenzelm@48864
    51
    (for ((name, (kind, files)) <- keywords) yield {
wenzelm@48660
    52
      if (kind == Keyword.MINOR) quote(name)
wenzelm@48864
    53
      else
wenzelm@48864
    54
        quote(name) + " :: " + quote(kind) +
wenzelm@48864
    55
        (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
wenzelm@48671
    56
    }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
wenzelm@48660
    57
wenzelm@48864
    58
  def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
wenzelm@48864
    59
  def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
wenzelm@38471
    60
wenzelm@48885
    61
  def thy_load_commands: List[(String, List[String])] =
wenzelm@48885
    62
    (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
wenzelm@48872
    63
wenzelm@50128
    64
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@46626
    65
    new Outer_Syntax(
wenzelm@46626
    66
      keywords + (name -> kind),
wenzelm@46626
    67
      lexicon + name,
wenzelm@50128
    68
      if (Keyword.control(kind._1) || replace == Some("")) completion
wenzelm@50128
    69
      else completion + (name, replace getOrElse name))
wenzelm@34166
    70
wenzelm@50128
    71
  def + (name: String, kind: (String, List[String])): Outer_Syntax = this + (name, kind, Some(name))
wenzelm@50128
    72
  def + (name: String, kind: String): Outer_Syntax = this + (name, (kind, Nil), Some(name))
wenzelm@50128
    73
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
    74
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
    75
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
    76
wenzelm@48873
    77
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    78
    (this /: keywords) {
wenzelm@50128
    79
      case (syntax, ((name, Some((kind, _)), replace))) =>
wenzelm@50128
    80
        syntax +
wenzelm@50128
    81
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
    82
          (Symbol.encode(name), kind, replace)
wenzelm@50128
    83
      case (syntax, ((name, None, replace))) =>
wenzelm@50128
    84
        syntax +
wenzelm@50128
    85
          (Symbol.decode(name), replace) +
wenzelm@50128
    86
          (Symbol.encode(name), replace)
wenzelm@46940
    87
    }
wenzelm@34166
    88
wenzelm@34166
    89
  def is_command(name: String): Boolean =
wenzelm@40458
    90
    keyword_kind(name) match {
wenzelm@36947
    91
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
    92
      case None => false
wenzelm@34166
    93
    }
wenzelm@34166
    94
wenzelm@40454
    95
  def heading_level(name: String): Option[Int] =
wenzelm@46969
    96
  {
wenzelm@46969
    97
    keyword_kind(name) match {
wenzelm@46969
    98
      case _ if name == "header" => Some(0)
wenzelm@46969
    99
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
   100
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
   101
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
   102
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
   103
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
   104
      case _ => None
wenzelm@40454
   105
    }
wenzelm@46969
   106
  }
wenzelm@40454
   107
wenzelm@40454
   108
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
   109
    heading_level(command.name)
wenzelm@40454
   110
wenzelm@34166
   111
wenzelm@34166
   112
  /* tokenize */
wenzelm@34166
   113
wenzelm@36956
   114
  def scan(input: Reader[Char]): List[Token] =
wenzelm@52066
   115
  {
wenzelm@52066
   116
    import lexicon._
wenzelm@34166
   117
wenzelm@52066
   118
    parseAll(rep(token(is_command)), input) match {
wenzelm@52066
   119
      case Success(tokens, _) => tokens
wenzelm@52066
   120
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
   121
    }
wenzelm@52066
   122
  }
wenzelm@34166
   123
wenzelm@36956
   124
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
   125
    scan(new CharSequenceReader(input))
wenzelm@43411
   126
wenzelm@43411
   127
  def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
wenzelm@52066
   128
  {
wenzelm@52066
   129
    import lexicon._
wenzelm@43411
   130
wenzelm@52066
   131
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   132
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   133
    var ctxt = context
wenzelm@52066
   134
    while (!in.atEnd) {
wenzelm@52066
   135
      parse(token_context(is_command, ctxt), in) match {
wenzelm@52066
   136
        case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@52066
   137
        case NoSuccess(_, rest) =>
wenzelm@52066
   138
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   139
      }
wenzelm@43411
   140
    }
wenzelm@52066
   141
    (toks.toList, ctxt)
wenzelm@52066
   142
  }
wenzelm@34166
   143
}