src/Pure/Isar/outer_syntax.scala
author wenzelm
Mon Nov 18 23:26:15 2013 +0100 (2013-11-18)
changeset 54513 5545aff878b1
parent 54462 c9bb76303348
child 55492 28d4db6c6e79
permissions -rw-r--r--
inline blobs into command, via SHA1 digest;
broadcast all blobs within edit, without storing the result;
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@43774
    40
}
wenzelm@43774
    41
wenzelm@46712
    42
final class Outer_Syntax private(
wenzelm@48864
    43
  keywords: Map[String, (String, List[String])] = Map.empty,
wenzelm@46626
    44
  lexicon: Scan.Lexicon = Scan.Lexicon.empty,
wenzelm@53280
    45
  val completion: Completion = Completion.empty,
wenzelm@53280
    46
  val has_tokens: Boolean = true)
wenzelm@34166
    47
{
wenzelm@48660
    48
  override def toString: String =
wenzelm@48864
    49
    (for ((name, (kind, files)) <- keywords) yield {
wenzelm@48660
    50
      if (kind == Keyword.MINOR) quote(name)
wenzelm@48864
    51
      else
wenzelm@48864
    52
        quote(name) + " :: " + quote(kind) +
wenzelm@48864
    53
        (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
wenzelm@48671
    54
    }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
wenzelm@48660
    55
wenzelm@48864
    56
  def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
wenzelm@48864
    57
  def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
wenzelm@38471
    58
wenzelm@54462
    59
  def thy_load(span: List[Token]): Option[List[String]] =
wenzelm@54462
    60
    keywords.get(Command.name(span)) match {
wenzelm@54513
    61
      case Some((Keyword.THY_LOAD, exts)) => Some(exts)
wenzelm@54462
    62
      case _ => None
wenzelm@54462
    63
    }
wenzelm@54462
    64
wenzelm@54513
    65
  val thy_load_commands: List[(String, List[String])] =
wenzelm@48885
    66
    (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
wenzelm@48872
    67
wenzelm@50128
    68
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@53280
    69
  {
wenzelm@53280
    70
    val keywords1 = keywords + (name -> kind)
wenzelm@53280
    71
    val lexicon1 = lexicon + name
wenzelm@53280
    72
    val completion1 =
wenzelm@50128
    73
      if (Keyword.control(kind._1) || replace == Some("")) completion
wenzelm@53280
    74
      else completion + (name, replace getOrElse name)
wenzelm@53280
    75
    new Outer_Syntax(keywords1, lexicon1, completion1, true)
wenzelm@53280
    76
  }
wenzelm@34166
    77
wenzelm@53280
    78
  def + (name: String, kind: (String, List[String])): Outer_Syntax =
wenzelm@53280
    79
    this + (name, kind, Some(name))
wenzelm@53280
    80
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@53280
    81
    this + (name, (kind, Nil), Some(name))
wenzelm@50128
    82
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
    83
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
    84
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
    85
wenzelm@48873
    86
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    87
    (this /: keywords) {
wenzelm@52439
    88
      case (syntax, (name, Some((kind, _)), replace)) =>
wenzelm@50128
    89
        syntax +
wenzelm@50128
    90
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
    91
          (Symbol.encode(name), kind, replace)
wenzelm@52439
    92
      case (syntax, (name, None, replace)) =>
wenzelm@50128
    93
        syntax +
wenzelm@50128
    94
          (Symbol.decode(name), replace) +
wenzelm@50128
    95
          (Symbol.encode(name), replace)
wenzelm@46940
    96
    }
wenzelm@34166
    97
wenzelm@34166
    98
  def is_command(name: String): Boolean =
wenzelm@40458
    99
    keyword_kind(name) match {
wenzelm@36947
   100
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
   101
      case None => false
wenzelm@34166
   102
    }
wenzelm@34166
   103
wenzelm@40454
   104
  def heading_level(name: String): Option[Int] =
wenzelm@46969
   105
  {
wenzelm@46969
   106
    keyword_kind(name) match {
wenzelm@46969
   107
      case _ if name == "header" => Some(0)
wenzelm@46969
   108
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
   109
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
   110
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
   111
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
   112
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
   113
      case _ => None
wenzelm@40454
   114
    }
wenzelm@46969
   115
  }
wenzelm@40454
   116
wenzelm@40454
   117
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
   118
    heading_level(command.name)
wenzelm@40454
   119
wenzelm@34166
   120
wenzelm@53280
   121
  /* token language */
wenzelm@53280
   122
wenzelm@53280
   123
  def no_tokens: Outer_Syntax =
wenzelm@53280
   124
  {
wenzelm@53280
   125
    require(keywords.isEmpty && lexicon.isEmpty)
wenzelm@53280
   126
    new Outer_Syntax(completion = completion, has_tokens = false)
wenzelm@53280
   127
  }
wenzelm@34166
   128
wenzelm@36956
   129
  def scan(input: Reader[Char]): List[Token] =
wenzelm@52066
   130
  {
wenzelm@52066
   131
    import lexicon._
wenzelm@34166
   132
wenzelm@52066
   133
    parseAll(rep(token(is_command)), input) match {
wenzelm@52066
   134
      case Success(tokens, _) => tokens
wenzelm@52066
   135
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
   136
    }
wenzelm@52066
   137
  }
wenzelm@34166
   138
wenzelm@36956
   139
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
   140
    scan(new CharSequenceReader(input))
wenzelm@43411
   141
wenzelm@43411
   142
  def scan_context(input: CharSequence, context: Scan.Context): (List[Token], Scan.Context) =
wenzelm@52066
   143
  {
wenzelm@52066
   144
    import lexicon._
wenzelm@43411
   145
wenzelm@52066
   146
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   147
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   148
    var ctxt = context
wenzelm@52066
   149
    while (!in.atEnd) {
wenzelm@52066
   150
      parse(token_context(is_command, ctxt), in) match {
wenzelm@52066
   151
        case Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@52066
   152
        case NoSuccess(_, rest) =>
wenzelm@52066
   153
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   154
      }
wenzelm@43411
   155
    }
wenzelm@52066
   156
    (toks.toList, ctxt)
wenzelm@52066
   157
  }
wenzelm@34166
   158
}