src/Pure/Isar/outer_lex.scala
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 34311 f0a6f02ad705
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm@34139
     1
/*  Title:      Pure/Isar/outer_lex.scala
wenzelm@34139
     2
    Author:     Makarius
wenzelm@34139
     3
wenzelm@34139
     4
Outer lexical syntax for Isabelle/Isar.
wenzelm@34139
     5
*/
wenzelm@34139
     6
wenzelm@34139
     7
package isabelle
wenzelm@34139
     8
wenzelm@34139
     9
wenzelm@34139
    10
object Outer_Lex
wenzelm@34139
    11
{
wenzelm@34157
    12
  /* tokens */
wenzelm@34139
    13
wenzelm@34157
    14
  object Token_Kind extends Enumeration
wenzelm@34139
    15
  {
wenzelm@34157
    16
    val COMMAND = Value("command")
wenzelm@34157
    17
    val KEYWORD = Value("keyword")
wenzelm@34157
    18
    val IDENT = Value("identifier")
wenzelm@34157
    19
    val LONG_IDENT = Value("long identifier")
wenzelm@34157
    20
    val SYM_IDENT = Value("symbolic identifier")
wenzelm@34157
    21
    val VAR = Value("schematic variable")
wenzelm@34157
    22
    val TYPE_IDENT = Value("type variable")
wenzelm@34157
    23
    val TYPE_VAR = Value("schematic type variable")
wenzelm@34157
    24
    val NAT = Value("number")
wenzelm@34157
    25
    val STRING = Value("string")
wenzelm@34157
    26
    val ALT_STRING = Value("back-quoted string")
wenzelm@34157
    27
    val VERBATIM = Value("verbatim text")
wenzelm@34157
    28
    val SPACE = Value("white space")
wenzelm@34157
    29
    val COMMENT = Value("comment text")
wenzelm@34157
    30
    val BAD_INPUT = Value("bad input")
wenzelm@34157
    31
    val UNPARSED = Value("unparsed input")
wenzelm@34139
    32
  }
wenzelm@34139
    33
wenzelm@34157
    34
  sealed case class Token(val kind: Token_Kind.Value, val source: String)
wenzelm@34139
    35
  {
wenzelm@34265
    36
    def is_command: Boolean = kind == Token_Kind.COMMAND
wenzelm@34157
    37
    def is_delimited: Boolean =
wenzelm@34157
    38
      kind == Token_Kind.STRING ||
wenzelm@34157
    39
      kind == Token_Kind.ALT_STRING ||
wenzelm@34157
    40
      kind == Token_Kind.VERBATIM ||
wenzelm@34157
    41
      kind == Token_Kind.COMMENT
wenzelm@34157
    42
    def is_name: Boolean =
wenzelm@34157
    43
      kind == Token_Kind.IDENT ||
wenzelm@34157
    44
      kind == Token_Kind.SYM_IDENT ||
wenzelm@34157
    45
      kind == Token_Kind.STRING ||
wenzelm@34157
    46
      kind == Token_Kind.NAT
wenzelm@34157
    47
    def is_xname: Boolean = is_name || kind == Token_Kind.LONG_IDENT
wenzelm@34157
    48
    def is_text: Boolean = is_xname || kind == Token_Kind.VERBATIM
wenzelm@34157
    49
    def is_space: Boolean = kind == Token_Kind.SPACE
wenzelm@34157
    50
    def is_comment: Boolean = kind == Token_Kind.COMMENT
wenzelm@34311
    51
    def is_ignored: Boolean = is_space || is_comment
wenzelm@34265
    52
    def is_unparsed: Boolean = kind == Token_Kind.UNPARSED
wenzelm@34139
    53
wenzelm@34157
    54
    def content: String =
wenzelm@34157
    55
      if (kind == Token_Kind.STRING) Scan.Lexicon.empty.quoted_content("\"", source)
wenzelm@34157
    56
      else if (kind == Token_Kind.ALT_STRING) Scan.Lexicon.empty.quoted_content("`", source)
wenzelm@34157
    57
      else if (kind == Token_Kind.VERBATIM) Scan.Lexicon.empty.verbatim_content(source)
wenzelm@34157
    58
      else if (kind == Token_Kind.COMMENT) Scan.Lexicon.empty.comment_content(source)
wenzelm@34157
    59
      else source
wenzelm@34157
    60
wenzelm@34157
    61
    def text: (String, String) =
wenzelm@34157
    62
      if (kind == Token_Kind.COMMAND && source == ";") ("terminator", "")
wenzelm@34157
    63
      else (kind.toString, source)
wenzelm@34139
    64
  }
wenzelm@34139
    65
wenzelm@34157
    66
wenzelm@34157
    67
  /* token reader */
wenzelm@34139
    68
wenzelm@34157
    69
  class Line_Position(val line: Int) extends scala.util.parsing.input.Position
wenzelm@34139
    70
  {
wenzelm@34157
    71
    def column = 0
wenzelm@34157
    72
    def lineContents = ""
wenzelm@34157
    73
    override def toString = line.toString
wenzelm@34157
    74
wenzelm@34157
    75
    def advance(token: Token): Line_Position =
wenzelm@34157
    76
    {
wenzelm@34157
    77
      var n = 0
wenzelm@34157
    78
      for (c <- token.content if c == '\n') n += 1
wenzelm@34157
    79
      if (n == 0) this else new Line_Position(line + n)
wenzelm@34157
    80
    }
wenzelm@34139
    81
  }
wenzelm@34139
    82
wenzelm@34157
    83
  abstract class Reader extends scala.util.parsing.input.Reader[Token]
wenzelm@34157
    84
wenzelm@34157
    85
  private class Token_Reader(tokens: List[Token], val pos: Line_Position) extends Reader
wenzelm@34139
    86
  {
wenzelm@34157
    87
    def first = tokens.head
wenzelm@34157
    88
    def rest = new Token_Reader(tokens.tail, pos.advance(first))
wenzelm@34157
    89
    def atEnd = tokens.isEmpty
wenzelm@34139
    90
  }
wenzelm@34139
    91
wenzelm@34157
    92
  def reader(tokens: List[Token]): Reader = new Token_Reader(tokens, new Line_Position(1))
wenzelm@34139
    93
}
wenzelm@34139
    94