src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Feb 20 13:23:49 2014 +0100 (2014-02-20)
changeset 55616 25a7a998852a
parent 55510 1585a65aad64
child 55666 cc350eb1087e
permissions -rw-r--r--
default completion context via outer syntax;
no symbol completion for ML files;
tuned;
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@55616
    46
  val completion_context: Completion.Context = Completion.Context.default,
wenzelm@53280
    47
  val has_tokens: Boolean = true)
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@54462
    60
  def thy_load(span: List[Token]): Option[List[String]] =
wenzelm@54462
    61
    keywords.get(Command.name(span)) match {
wenzelm@54513
    62
      case Some((Keyword.THY_LOAD, exts)) => Some(exts)
wenzelm@54462
    63
      case _ => None
wenzelm@54462
    64
    }
wenzelm@54462
    65
wenzelm@54513
    66
  val thy_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@50128
    69
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@53280
    70
  {
wenzelm@53280
    71
    val keywords1 = keywords + (name -> kind)
wenzelm@53280
    72
    val lexicon1 = lexicon + name
wenzelm@53280
    73
    val completion1 =
wenzelm@50128
    74
      if (Keyword.control(kind._1) || replace == Some("")) completion
wenzelm@53280
    75
      else completion + (name, replace getOrElse name)
wenzelm@55616
    76
    new Outer_Syntax(keywords1, lexicon1, completion1, completion_context, true)
wenzelm@53280
    77
  }
wenzelm@34166
    78
wenzelm@53280
    79
  def + (name: String, kind: (String, List[String])): Outer_Syntax =
wenzelm@53280
    80
    this + (name, kind, Some(name))
wenzelm@53280
    81
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@53280
    82
    this + (name, (kind, Nil), Some(name))
wenzelm@50128
    83
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
    84
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
    85
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
    86
wenzelm@48873
    87
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
    88
    (this /: keywords) {
wenzelm@52439
    89
      case (syntax, (name, Some((kind, _)), replace)) =>
wenzelm@50128
    90
        syntax +
wenzelm@50128
    91
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
    92
          (Symbol.encode(name), kind, replace)
wenzelm@52439
    93
      case (syntax, (name, None, replace)) =>
wenzelm@50128
    94
        syntax +
wenzelm@50128
    95
          (Symbol.decode(name), replace) +
wenzelm@50128
    96
          (Symbol.encode(name), replace)
wenzelm@46940
    97
    }
wenzelm@34166
    98
wenzelm@34166
    99
  def is_command(name: String): Boolean =
wenzelm@40458
   100
    keyword_kind(name) match {
wenzelm@36947
   101
      case Some(kind) => kind != Keyword.MINOR
wenzelm@34166
   102
      case None => false
wenzelm@34166
   103
    }
wenzelm@34166
   104
wenzelm@40454
   105
  def heading_level(name: String): Option[Int] =
wenzelm@46969
   106
  {
wenzelm@46969
   107
    keyword_kind(name) match {
wenzelm@46969
   108
      case _ if name == "header" => Some(0)
wenzelm@46969
   109
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
   110
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
   111
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
   112
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
   113
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
   114
      case _ => None
wenzelm@40454
   115
    }
wenzelm@46969
   116
  }
wenzelm@40454
   117
wenzelm@40454
   118
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
   119
    heading_level(command.name)
wenzelm@40454
   120
wenzelm@34166
   121
wenzelm@53280
   122
  /* token language */
wenzelm@53280
   123
wenzelm@36956
   124
  def scan(input: Reader[Char]): List[Token] =
wenzelm@52066
   125
  {
wenzelm@55616
   126
    Token.Parsers.parseAll(
wenzelm@55616
   127
        Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), input) match {
wenzelm@55494
   128
      case Token.Parsers.Success(tokens, _) => tokens
wenzelm@52066
   129
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
   130
    }
wenzelm@52066
   131
  }
wenzelm@34166
   132
wenzelm@36956
   133
  def scan(input: CharSequence): List[Token] =
wenzelm@34166
   134
    scan(new CharSequenceReader(input))
wenzelm@43411
   135
wenzelm@55510
   136
  def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
wenzelm@52066
   137
  {
wenzelm@52066
   138
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   139
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   140
    var ctxt = context
wenzelm@52066
   141
    while (!in.atEnd) {
wenzelm@55510
   142
      Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
wenzelm@55494
   143
        case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@55494
   144
        case Token.Parsers.NoSuccess(_, rest) =>
wenzelm@52066
   145
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   146
      }
wenzelm@43411
   147
    }
wenzelm@52066
   148
    (toks.toList, ctxt)
wenzelm@52066
   149
  }
wenzelm@55616
   150
wenzelm@55616
   151
wenzelm@55616
   152
  /* language context */
wenzelm@55616
   153
wenzelm@55616
   154
  def set_completion_context(context: Completion.Context): Outer_Syntax =
wenzelm@55616
   155
    new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
wenzelm@55616
   156
wenzelm@55616
   157
  def no_tokens: Outer_Syntax =
wenzelm@55616
   158
  {
wenzelm@55616
   159
    require(keywords.isEmpty && lexicon.isEmpty)
wenzelm@55616
   160
    new Outer_Syntax(
wenzelm@55616
   161
      completion = completion,
wenzelm@55616
   162
      completion_context = completion_context,
wenzelm@55616
   163
      has_tokens = false)
wenzelm@55616
   164
  }
wenzelm@34166
   165
}