src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 07 13:21:29 2012 +0200 (2012-08-07)
changeset 48706 e2b512024eab
parent 48671 951bc4c3ee17
child 48707 ba531af91148
permissions -rw-r--r--
tuned signature;
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@46941
    38
  def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
wenzelm@43774
    39
}
wenzelm@43774
    40
wenzelm@46712
    41
final class Outer_Syntax private(
wenzelm@47469
    42
  keywords: Map[String, String] = Map.empty,
wenzelm@46626
    43
  lexicon: Scan.Lexicon = Scan.Lexicon.empty,
wenzelm@46941
    44
  val completion: Completion = Completion.empty)
wenzelm@34166
    45
{
wenzelm@48660
    46
  override def toString: String =
wenzelm@48660
    47
    (for ((name, kind) <- keywords) yield {
wenzelm@48660
    48
      if (kind == Keyword.MINOR) quote(name)
wenzelm@48660
    49
      else quote(name) + " :: " + quote(kind)
wenzelm@48671
    50
    }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
wenzelm@48660
    51
wenzelm@38471
    52
  def keyword_kind(name: String): Option[String] = keywords.get(name)
wenzelm@38471
    53
wenzelm@40533
    54
  def + (name: String, kind: String, replace: String): Outer_Syntax =
wenzelm@46626
    55
    new Outer_Syntax(
wenzelm@46626
    56
      keywords + (name -> kind),
wenzelm@46626
    57
      lexicon + name,
wenzelm@46626
    58
      if (Keyword.control(kind)) completion else completion + (name, replace))
wenzelm@34166
    59
wenzelm@40533
    60
  def + (name: String, kind: String): Outer_Syntax = this + (name, kind, name)
wenzelm@36947
    61
  def + (name: String): Outer_Syntax = this + (name, Keyword.MINOR)
wenzelm@48706
    62
wenzelm@48706
    63
  def add_keywords(header: Document.Node_Header): Outer_Syntax =
wenzelm@48706
    64
    header match {
wenzelm@48706
    65
      case Exn.Res(deps) =>
wenzelm@48706
    66
        (this /: deps.keywords) {
wenzelm@48706
    67
          case (syntax, ((name, Some((kind, _))))) => syntax + (name, kind)
wenzelm@48706
    68
          case (syntax, ((name, None))) => syntax + name
wenzelm@48706
    69
        }
wenzelm@48706
    70
      case Exn.Exn(_) => this
wenzelm@46940
    71
    }
wenzelm@34166
    72
wenzelm@34166
    73
  def is_command(name: String): Boolean =
wenzelm@40458
    74
    keyword_kind(name) match {
wenzelm@36947
    75
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
    76
      case None => false
wenzelm@34166
    77
    }
wenzelm@34166
    78
wenzelm@40454
    79
  def heading_level(name: String): Option[Int] =
wenzelm@46969
    80
  {
wenzelm@46969
    81
    keyword_kind(name) match {
wenzelm@46969
    82
      case _ if name == "header" => Some(0)
wenzelm@46969
    83
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
    84
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
    85
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
    86
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
    87
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
    88
      case _ => None
wenzelm@40454
    89
    }
wenzelm@46969
    90
  }
wenzelm@40454
    91
wenzelm@40454
    92
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
    93
    heading_level(command.name)
wenzelm@40454
    94
wenzelm@34166
    95
wenzelm@34166
    96
  /* tokenize */
wenzelm@34166
    97
wenzelm@36956
    98
  def scan(input: Reader[Char]): List[Token] =
wenzelm@34166
    99
  {
wenzelm@34166
   100
    import lexicon._
wenzelm@34166
   101
wenzelm@43695
   102
    parseAll(rep(token(is_command)), input) match {
wenzelm@34166
   103
      case Success(tokens, _) => tokens
wenzelm@34264
   104
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
   105
    }
wenzelm@34166
   106
  }
wenzelm@34166
   107
wenzelm@36956
   108
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
   109
    scan(new CharSequenceReader(input))
wenzelm@43411
   110
wenzelm@43411
   111
  def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
wenzelm@43411
   112
  {
wenzelm@43411
   113
    import lexicon._
wenzelm@43411
   114
wenzelm@43411
   115
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@43411
   116
    val toks = new mutable.ListBuffer[Token]
wenzelm@43411
   117
    var ctxt = context
wenzelm@43411
   118
    while (!in.atEnd) {
wenzelm@43695
   119
      parse(token_context(is_command, ctxt), in) match {
wenzelm@43411
   120
        case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@43411
   121
        case NoSuccess(_, rest) =>
wenzelm@43411
   122
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   123
      }
wenzelm@43411
   124
    }
wenzelm@43411
   125
    (toks.toList, ctxt)
wenzelm@43411
   126
  }
wenzelm@34166
   127
}