src/Pure/Isar/outer_syntax.scala
author wenzelm
Mon Aug 11 22:29:48 2014 +0200 (2014-08-11)
changeset 57901 e1abca2527da
parent 56393 22f533e6a049
child 57905 c0c5652e796e
permissions -rw-r--r--
more explicit type Span in Scala, according to ML version;
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@55749
    46
  val language_context: Completion.Language_Context = Completion.Language_Context.outer,
wenzelm@56393
    47
  val has_tokens: Boolean = true) extends Prover.Syntax
wenzelm@34166
    48
{
wenzelm@48660
    49
  override def toString: String =
wenzelm@48864
    50
    (for ((name, (kind, files)) <- keywords) yield {
wenzelm@48660
    51
      if (kind == Keyword.MINOR) quote(name)
wenzelm@48864
    52
      else
wenzelm@48864
    53
        quote(name) + " :: " + quote(kind) +
wenzelm@48864
    54
        (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
wenzelm@48671
    55
    }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
wenzelm@48660
    56
wenzelm@48864
    57
  def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
wenzelm@48864
    58
  def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
wenzelm@38471
    59
wenzelm@57901
    60
  def load_command(name: String): Option[List[String]] =
wenzelm@57901
    61
    keywords.get(name) match {
wenzelm@54513
    62
      case Some((Keyword.THY_LOAD, exts)) => Some(exts)
wenzelm@54462
    63
      case _ => None
wenzelm@54462
    64
    }
wenzelm@54462
    65
wenzelm@56314
    66
  val load_commands: List[(String, List[String])] =
wenzelm@48885
    67
    (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
wenzelm@48872
    68
wenzelm@56393
    69
  def load_commands_in(text: String): Boolean =
wenzelm@56393
    70
    load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
wenzelm@56393
    71
wenzelm@50128
    72
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@53280
    73
  {
wenzelm@53280
    74
    val keywords1 = keywords + (name -> kind)
wenzelm@53280
    75
    val lexicon1 = lexicon + name
wenzelm@53280
    76
    val completion1 =
wenzelm@50128
    77
      if (Keyword.control(kind._1) || replace == Some("")) completion
wenzelm@53280
    78
      else completion + (name, replace getOrElse name)
wenzelm@55749
    79
    new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
wenzelm@53280
    80
  }
wenzelm@34166
    81
wenzelm@53280
    82
  def + (name: String, kind: (String, List[String])): Outer_Syntax =
wenzelm@53280
    83
    this + (name, kind, Some(name))
wenzelm@53280
    84
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@53280
    85
    this + (name, (kind, Nil), Some(name))
wenzelm@50128
    86
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
    87
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
    88
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
    89
wenzelm@48873
    90
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    91
    (this /: keywords) {
wenzelm@52439
    92
      case (syntax, (name, Some((kind, _)), replace)) =>
wenzelm@50128
    93
        syntax +
wenzelm@50128
    94
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
    95
          (Symbol.encode(name), kind, replace)
wenzelm@52439
    96
      case (syntax, (name, None, replace)) =>
wenzelm@50128
    97
        syntax +
wenzelm@50128
    98
          (Symbol.decode(name), replace) +
wenzelm@50128
    99
          (Symbol.encode(name), replace)
wenzelm@46940
   100
    }
wenzelm@34166
   101
wenzelm@34166
   102
  def is_command(name: String): Boolean =
wenzelm@40458
   103
    keyword_kind(name) match {
wenzelm@36947
   104
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
   105
      case None => false
wenzelm@34166
   106
    }
wenzelm@34166
   107
wenzelm@40454
   108
  def heading_level(name: String): Option[Int] =
wenzelm@46969
   109
  {
wenzelm@46969
   110
    keyword_kind(name) match {
wenzelm@46969
   111
      case _ if name == "header" => Some(0)
wenzelm@46969
   112
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
   113
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
   114
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
   115
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
   116
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
   117
      case _ => None
wenzelm@40454
   118
    }
wenzelm@46969
   119
  }
wenzelm@40454
   120
wenzelm@40454
   121
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
   122
    heading_level(command.name)
wenzelm@40454
   123
wenzelm@34166
   124
wenzelm@53280
   125
  /* token language */
wenzelm@53280
   126
wenzelm@36956
   127
  def scan(input: Reader[Char]): List[Token] =
wenzelm@52066
   128
  {
wenzelm@55616
   129
    Token.Parsers.parseAll(
wenzelm@55616
   130
        Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), input) match {
wenzelm@55494
   131
      case Token.Parsers.Success(tokens, _) => tokens
wenzelm@52066
   132
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
   133
    }
wenzelm@52066
   134
  }
wenzelm@34166
   135
wenzelm@36956
   136
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
   137
    scan(new CharSequenceReader(input))
wenzelm@43411
   138
wenzelm@55510
   139
  def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
wenzelm@52066
   140
  {
wenzelm@52066
   141
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   142
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   143
    var ctxt = context
wenzelm@52066
   144
    while (!in.atEnd) {
wenzelm@55510
   145
      Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
wenzelm@55494
   146
        case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@55494
   147
        case Token.Parsers.NoSuccess(_, rest) =>
wenzelm@52066
   148
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   149
      }
wenzelm@43411
   150
    }
wenzelm@52066
   151
    (toks.toList, ctxt)
wenzelm@52066
   152
  }
wenzelm@55616
   153
wenzelm@55616
   154
wenzelm@55616
   155
  /* language context */
wenzelm@55616
   156
wenzelm@55749
   157
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@55616
   158
    new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
wenzelm@55616
   159
wenzelm@55616
   160
  def no_tokens: Outer_Syntax =
wenzelm@55616
   161
  {
wenzelm@55616
   162
    require(keywords.isEmpty && lexicon.isEmpty)
wenzelm@55616
   163
    new Outer_Syntax(
wenzelm@55616
   164
      completion = completion,
wenzelm@55749
   165
      language_context = language_context,
wenzelm@55616
   166
      has_tokens = false)
wenzelm@55616
   167
  }
wenzelm@34166
   168
}