src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Feb 23 20:24:05 2012 +0100 (2012-02-23)
changeset 46626 a02115865bcc
parent 46624 dc4c72092088
child 46712 8650d9a95736
permissions -rw-r--r--
streamlined abstract datatype;
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@46626
    37
  def init(): Outer_Syntax = new Outer_Syntax()
wenzelm@43774
    38
}
wenzelm@43774
    39
wenzelm@46626
    40
class Outer_Syntax private(
wenzelm@46626
    41
  keywords: Map[String, String] = Map((";" -> Keyword.DIAG)),
wenzelm@46626
    42
  lexicon: Scan.Lexicon = Scan.Lexicon.empty,
wenzelm@46626
    43
  val completion: Completion = Completion.init())
wenzelm@34166
    44
{
wenzelm@38471
    45
  def keyword_kind(name: String): Option[String] = keywords.get(name)
wenzelm@38471
    46
wenzelm@40533
    47
  def + (name: String, kind: String, replace: String): Outer_Syntax =
wenzelm@46626
    48
    new Outer_Syntax(
wenzelm@46626
    49
      keywords + (name -> kind),
wenzelm@46626
    50
      lexicon + name,
wenzelm@46626
    51
      if (Keyword.control(kind)) completion else completion + (name, replace))
wenzelm@34166
    52
wenzelm@40533
    53
  def + (name: String, kind: String): Outer_Syntax = this + (name, kind, name)
wenzelm@40533
    54
wenzelm@36947
    55
  def + (name: String): Outer_Syntax = this + (name, Keyword.MINOR)
wenzelm@34166
    56
wenzelm@34166
    57
  def is_command(name: String): Boolean =
wenzelm@40458
    58
    keyword_kind(name) match {
wenzelm@36947
    59
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
    60
      case None => false
wenzelm@34166
    61
    }
wenzelm@34166
    62
wenzelm@40454
    63
  def heading_level(name: String): Option[Int] =
wenzelm@40454
    64
    name match {
wenzelm@40458
    65
      // FIXME avoid hard-wired info!?
wenzelm@40454
    66
      case "header" => Some(1)
wenzelm@40454
    67
      case "chapter" => Some(2)
wenzelm@40454
    68
      case "section" | "sect" => Some(3)
wenzelm@40454
    69
      case "subsection" | "subsect" => Some(4)
wenzelm@40454
    70
      case "subsubsection" | "subsubsect" => Some(5)
wenzelm@40458
    71
      case _ =>
wenzelm@40458
    72
        keyword_kind(name) match {
wenzelm@40458
    73
          case Some(kind) if Keyword.theory(kind) => Some(6)
wenzelm@40458
    74
          case _ => None
wenzelm@40458
    75
        }
wenzelm@40454
    76
    }
wenzelm@40454
    77
wenzelm@40454
    78
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
    79
    heading_level(command.name)
wenzelm@40454
    80
wenzelm@34166
    81
wenzelm@34166
    82
  /* tokenize */
wenzelm@34166
    83
wenzelm@36956
    84
  def scan(input: Reader[Char]): List[Token] =
wenzelm@34166
    85
  {
wenzelm@34166
    86
    import lexicon._
wenzelm@34166
    87
wenzelm@43695
    88
    parseAll(rep(token(is_command)), input) match {
wenzelm@34166
    89
      case Success(tokens, _) => tokens
wenzelm@34264
    90
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
    91
    }
wenzelm@34166
    92
  }
wenzelm@34166
    93
wenzelm@36956
    94
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
    95
    scan(new CharSequenceReader(input))
wenzelm@43411
    96
wenzelm@43411
    97
  def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
wenzelm@43411
    98
  {
wenzelm@43411
    99
    import lexicon._
wenzelm@43411
   100
wenzelm@43411
   101
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@43411
   102
    val toks = new mutable.ListBuffer[Token]
wenzelm@43411
   103
    var ctxt = context
wenzelm@43411
   104
    while (!in.atEnd) {
wenzelm@43695
   105
      parse(token_context(is_command, ctxt), in) match {
wenzelm@43411
   106
        case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@43411
   107
        case NoSuccess(_, rest) =>
wenzelm@43411
   108
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   109
      }
wenzelm@43411
   110
    }
wenzelm@43411
   111
    (toks.toList, ctxt)
wenzelm@43411
   112
  }
wenzelm@34166
   113
}