src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Oct 16 12:24:19 2014 +0200 (2014-10-16)
changeset 58695 91839729224e
parent 58694 983e98da2a42
child 58696 6b7445774ce3
permissions -rw-r--r--
tuned comments;
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@58695
    57
wenzelm@58695
    58
  /* keyword kind */
wenzelm@58695
    59
wenzelm@48864
    60
  def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
wenzelm@48864
    61
  def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
wenzelm@38471
    62
wenzelm@58695
    63
  def is_command(name: String): Boolean =
wenzelm@58695
    64
    keyword_kind(name) match {
wenzelm@58695
    65
      case Some(kind) => kind != Keyword.MINOR
wenzelm@58695
    66
      case None => false
wenzelm@58695
    67
    }
wenzelm@58695
    68
wenzelm@58695
    69
wenzelm@58695
    70
  /* load commands */
wenzelm@58695
    71
wenzelm@57901
    72
  def load_command(name: String): Option[List[String]] =
wenzelm@57901
    73
    keywords.get(name) match {
wenzelm@54513
    74
      case Some((Keyword.THY_LOAD, exts)) => Some(exts)
wenzelm@54462
    75
      case _ => None
wenzelm@54462
    76
    }
wenzelm@54462
    77
wenzelm@56314
    78
  val load_commands: List[(String, List[String])] =
wenzelm@48885
    79
    (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
wenzelm@48872
    80
wenzelm@56393
    81
  def load_commands_in(text: String): Boolean =
wenzelm@56393
    82
    load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
wenzelm@56393
    83
wenzelm@58695
    84
wenzelm@58695
    85
  /* add keywords */
wenzelm@58695
    86
wenzelm@50128
    87
  def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
wenzelm@53280
    88
  {
wenzelm@53280
    89
    val keywords1 = keywords + (name -> kind)
wenzelm@53280
    90
    val lexicon1 = lexicon + name
wenzelm@53280
    91
    val completion1 =
wenzelm@50128
    92
      if (Keyword.control(kind._1) || replace == Some("")) completion
wenzelm@53280
    93
      else completion + (name, replace getOrElse name)
wenzelm@55749
    94
    new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
wenzelm@53280
    95
  }
wenzelm@34166
    96
wenzelm@53280
    97
  def + (name: String, kind: (String, List[String])): Outer_Syntax =
wenzelm@53280
    98
    this + (name, kind, Some(name))
wenzelm@53280
    99
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@53280
   100
    this + (name, (kind, Nil), Some(name))
wenzelm@50128
   101
  def + (name: String, replace: Option[String]): Outer_Syntax =
wenzelm@50128
   102
    this + (name, (Keyword.MINOR, Nil), replace)
wenzelm@50128
   103
  def + (name: String): Outer_Syntax = this + (name, None)
wenzelm@48706
   104
wenzelm@48873
   105
  def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
wenzelm@48873
   106
    (this /: keywords) {
wenzelm@52439
   107
      case (syntax, (name, Some((kind, _)), replace)) =>
wenzelm@50128
   108
        syntax +
wenzelm@50128
   109
          (Symbol.decode(name), kind, replace) +
wenzelm@50128
   110
          (Symbol.encode(name), kind, replace)
wenzelm@52439
   111
      case (syntax, (name, None, replace)) =>
wenzelm@50128
   112
        syntax +
wenzelm@50128
   113
          (Symbol.decode(name), replace) +
wenzelm@50128
   114
          (Symbol.encode(name), replace)
wenzelm@46940
   115
    }
wenzelm@34166
   116
wenzelm@58695
   117
wenzelm@58695
   118
  /* document headings */
wenzelm@34166
   119
wenzelm@40454
   120
  def heading_level(name: String): Option[Int] =
wenzelm@46969
   121
  {
wenzelm@46969
   122
    keyword_kind(name) match {
wenzelm@46969
   123
      case _ if name == "header" => Some(0)
wenzelm@46969
   124
      case Some(Keyword.THY_HEADING1) => Some(1)
wenzelm@46969
   125
      case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
wenzelm@46969
   126
      case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
wenzelm@46969
   127
      case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
wenzelm@46969
   128
      case Some(kind) if Keyword.theory(kind) => Some(5)
wenzelm@46969
   129
      case _ => None
wenzelm@40454
   130
    }
wenzelm@46969
   131
  }
wenzelm@40454
   132
wenzelm@40454
   133
  def heading_level(command: Command): Option[Int] =
wenzelm@40454
   134
    heading_level(command.name)
wenzelm@40454
   135
wenzelm@34166
   136
wenzelm@53280
   137
  /* token language */
wenzelm@53280
   138
wenzelm@57907
   139
  def scan(input: CharSequence): List[Token] =
wenzelm@52066
   140
  {
wenzelm@58503
   141
    val in: Reader[Char] = new CharSequenceReader(input)
wenzelm@55616
   142
    Token.Parsers.parseAll(
wenzelm@57907
   143
        Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), in) match {
wenzelm@55494
   144
      case Token.Parsers.Success(tokens, _) => tokens
wenzelm@57907
   145
      case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
wenzelm@34166
   146
    }
wenzelm@52066
   147
  }
wenzelm@34166
   148
wenzelm@58694
   149
  def scan_line(input: CharSequence, context: Scan.Line_Context, depth: Int)
wenzelm@58694
   150
    : (List[Token], Scan.Line_Context, Int) =
wenzelm@52066
   151
  {
wenzelm@52066
   152
    var in: Reader[Char] = new CharSequenceReader(input)
wenzelm@52066
   153
    val toks = new mutable.ListBuffer[Token]
wenzelm@52066
   154
    var ctxt = context
wenzelm@52066
   155
    while (!in.atEnd) {
wenzelm@55510
   156
      Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
wenzelm@55494
   157
        case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
wenzelm@55494
   158
        case Token.Parsers.NoSuccess(_, rest) =>
wenzelm@52066
   159
          error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
wenzelm@43411
   160
      }
wenzelm@43411
   161
    }
wenzelm@58694
   162
wenzelm@58694
   163
    val depth1 = depth // FIXME
wenzelm@58694
   164
    (toks.toList, ctxt, depth1)
wenzelm@52066
   165
  }
wenzelm@55616
   166
wenzelm@55616
   167
wenzelm@57905
   168
  /* parse_spans */
wenzelm@57905
   169
wenzelm@57905
   170
  def parse_spans(toks: List[Token]): List[Command_Span.Span] =
wenzelm@57905
   171
  {
wenzelm@57905
   172
    val result = new mutable.ListBuffer[Command_Span.Span]
wenzelm@57905
   173
    val content = new mutable.ListBuffer[Token]
wenzelm@57905
   174
    val improper = new mutable.ListBuffer[Token]
wenzelm@57905
   175
wenzelm@57905
   176
    def ship(span: List[Token])
wenzelm@57905
   177
    {
wenzelm@57905
   178
      val kind =
wenzelm@57910
   179
        if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
wenzelm@57910
   180
          val name = span.head.source
wenzelm@57911
   181
          val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
wenzelm@57910
   182
          Command_Span.Command_Span(name, pos)
wenzelm@57910
   183
        }
wenzelm@57905
   184
        else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
wenzelm@57905
   185
        else Command_Span.Malformed_Span
wenzelm@57905
   186
      result += Command_Span.Span(kind, span)
wenzelm@57905
   187
    }
wenzelm@57905
   188
wenzelm@57905
   189
    def flush()
wenzelm@57905
   190
    {
wenzelm@57905
   191
      if (!content.isEmpty) { ship(content.toList); content.clear }
wenzelm@57905
   192
      if (!improper.isEmpty) { ship(improper.toList); improper.clear }
wenzelm@57905
   193
    }
wenzelm@57905
   194
wenzelm@57905
   195
    for (tok <- toks) {
wenzelm@57905
   196
      if (tok.is_command) { flush(); content += tok }
wenzelm@57905
   197
      else if (tok.is_improper) improper += tok
wenzelm@57905
   198
      else { content ++= improper; improper.clear; content += tok }
wenzelm@57905
   199
    }
wenzelm@57905
   200
    flush()
wenzelm@57905
   201
wenzelm@57905
   202
    result.toList
wenzelm@57905
   203
  }
wenzelm@57905
   204
wenzelm@57906
   205
  def parse_spans(input: CharSequence): List[Command_Span.Span] =
wenzelm@57906
   206
    parse_spans(scan(input))
wenzelm@57906
   207
wenzelm@57905
   208
wenzelm@55616
   209
  /* language context */
wenzelm@55616
   210
wenzelm@55749
   211
  def set_language_context(context: Completion.Language_Context): Outer_Syntax =
wenzelm@55616
   212
    new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
wenzelm@55616
   213
wenzelm@55616
   214
  def no_tokens: Outer_Syntax =
wenzelm@55616
   215
  {
wenzelm@55616
   216
    require(keywords.isEmpty && lexicon.isEmpty)
wenzelm@55616
   217
    new Outer_Syntax(
wenzelm@55616
   218
      completion = completion,
wenzelm@55749
   219
      language_context = language_context,
wenzelm@55616
   220
      has_tokens = false)
wenzelm@55616
   221
  }
wenzelm@34166
   222
}