src/Pure/Isar/token.scala
author wenzelm
Tue Dec 09 21:14:11 2014 +0100 (2014-12-09)
changeset 59122 c1dbcde94cd2
parent 59083 88b0b1f28adc
child 59671 9715eb8e9408
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/Isar/token.scala
     2     Author:     Makarius
     3 
     4 Outer token syntax for Isabelle/Isar.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.collection.mutable
    11 import scala.util.parsing.input
    12 
    13 
    14 object Token
    15 {
    16   /* tokens */
    17 
    18   object Kind extends Enumeration
    19   {
    20     /*immediate source*/
    21     val COMMAND = Value("command")
    22     val KEYWORD = Value("keyword")
    23     val IDENT = Value("identifier")
    24     val LONG_IDENT = Value("long identifier")
    25     val SYM_IDENT = Value("symbolic identifier")
    26     val VAR = Value("schematic variable")
    27     val TYPE_IDENT = Value("type variable")
    28     val TYPE_VAR = Value("schematic type variable")
    29     val NAT = Value("natural number")
    30     val FLOAT = Value("floating-point number")
    31     val SPACE = Value("white space")
    32     /*delimited content*/
    33     val STRING = Value("string")
    34     val ALT_STRING = Value("back-quoted string")
    35     val VERBATIM = Value("verbatim text")
    36     val CARTOUCHE = Value("text cartouche")
    37     val COMMENT = Value("comment text")
    38     /*special content*/
    39     val ERROR = Value("bad input")
    40     val UNPARSED = Value("unparsed input")
    41   }
    42 
    43 
    44   /* parsers */
    45 
    46   object Parsers extends Parsers
    47 
    48   trait Parsers extends Scan.Parsers
    49   {
    50     private def delimited_token: Parser[Token] =
    51     {
    52       val string = quoted("\"") ^^ (x => Token(Token.Kind.STRING, x))
    53       val alt_string = quoted("`") ^^ (x => Token(Token.Kind.ALT_STRING, x))
    54       val verb = verbatim ^^ (x => Token(Token.Kind.VERBATIM, x))
    55       val cart = cartouche ^^ (x => Token(Token.Kind.CARTOUCHE, x))
    56       val cmt = comment ^^ (x => Token(Token.Kind.COMMENT, x))
    57 
    58       string | (alt_string | (verb | (cart | cmt)))
    59     }
    60 
    61     private def other_token(keywords: Keyword.Keywords): Parser[Token] =
    62     {
    63       val letdigs1 = many1(Symbol.is_letdig)
    64       val sub = one(s => s == Symbol.sub_decoded || s == "\\<^sub>")
    65       val id =
    66         one(Symbol.is_letter) ~
    67           (rep(letdigs1 | (sub ~ letdigs1 ^^ { case x ~ y => x + y })) ^^ (_.mkString)) ^^
    68         { case x ~ y => x + y }
    69 
    70       val nat = many1(Symbol.is_digit)
    71       val natdot = nat ~ "." ~ nat ^^ { case x ~ y ~ z => x + y + z }
    72       val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
    73 
    74       val ident = id ~ rep("." ~> id) ^^
    75         { case x ~ Nil => Token(Token.Kind.IDENT, x)
    76           case x ~ ys => Token(Token.Kind.LONG_IDENT, (x :: ys).mkString(".")) }
    77 
    78       val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.VAR, x + y) }
    79       val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token.Kind.TYPE_IDENT, x + y) }
    80       val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.TYPE_VAR, x + y) }
    81       val nat_ = nat ^^ (x => Token(Token.Kind.NAT, x))
    82       val float =
    83         ("-" ~ natdot ^^ { case x ~ y => x + y } | natdot) ^^ (x => Token(Token.Kind.FLOAT, x))
    84 
    85       val sym_ident =
    86         (many1(Symbol.is_symbolic_char) | one(sym => Symbol.is_symbolic(sym))) ^^
    87         (x => Token(Token.Kind.SYM_IDENT, x))
    88 
    89       val keyword =
    90         literal(keywords.minor) ^^ (x => Token(Token.Kind.KEYWORD, x)) |||
    91         literal(keywords.major) ^^ (x => Token(Token.Kind.COMMAND, x))
    92 
    93       val space = many1(Symbol.is_blank) ^^ (x => Token(Token.Kind.SPACE, x))
    94 
    95       val recover_delimited =
    96         (recover_quoted("\"") |
    97           (recover_quoted("`") |
    98             (recover_verbatim |
    99               (recover_cartouche | recover_comment)))) ^^ (x => Token(Token.Kind.ERROR, x))
   100 
   101       val bad = one(_ => true) ^^ (x => Token(Token.Kind.ERROR, x))
   102 
   103       space | (recover_delimited |
   104         (((ident | (var_ | (type_ident | (type_var | (float | (nat_ | sym_ident)))))) |||
   105           keyword) | bad))
   106     }
   107 
   108     def token(keywords: Keyword.Keywords): Parser[Token] =
   109       delimited_token | other_token(keywords)
   110 
   111     def token_line(keywords: Keyword.Keywords, ctxt: Scan.Line_Context)
   112       : Parser[(Token, Scan.Line_Context)] =
   113     {
   114       val string =
   115         quoted_line("\"", ctxt) ^^ { case (x, c) => (Token(Token.Kind.STRING, x), c) }
   116       val alt_string =
   117         quoted_line("`", ctxt) ^^ { case (x, c) => (Token(Token.Kind.ALT_STRING, x), c) }
   118       val verb = verbatim_line(ctxt) ^^ { case (x, c) => (Token(Token.Kind.VERBATIM, x), c) }
   119       val cart = cartouche_line(ctxt) ^^ { case (x, c) => (Token(Token.Kind.CARTOUCHE, x), c) }
   120       val cmt = comment_line(ctxt) ^^ { case (x, c) => (Token(Token.Kind.COMMENT, x), c) }
   121       val other = other_token(keywords) ^^ { case x => (x, Scan.Finished) }
   122 
   123       string | (alt_string | (verb | (cart | (cmt | other))))
   124     }
   125   }
   126 
   127 
   128   /* explode */
   129 
   130   def explode(keywords: Keyword.Keywords, inp: CharSequence): List[Token] =
   131   {
   132     val in: input.Reader[Char] = new input.CharSequenceReader(inp)
   133     Parsers.parseAll(Parsers.rep(Parsers.token(keywords)), in) match {
   134       case Parsers.Success(tokens, _) => tokens
   135       case _ => error("Unexpected failure of tokenizing input:\n" + inp.toString)
   136     }
   137   }
   138 
   139   def explode_line(keywords: Keyword.Keywords, inp: CharSequence, context: Scan.Line_Context)
   140     : (List[Token], Scan.Line_Context) =
   141   {
   142     var in: input.Reader[Char] = new input.CharSequenceReader(inp)
   143     val toks = new mutable.ListBuffer[Token]
   144     var ctxt = context
   145     while (!in.atEnd) {
   146       Parsers.parse(Parsers.token_line(keywords, ctxt), in) match {
   147         case Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   148         case Parsers.NoSuccess(_, rest) =>
   149           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   150       }
   151     }
   152     (toks.toList, ctxt)
   153   }
   154 
   155 
   156   /* token reader */
   157 
   158   object Pos
   159   {
   160     val none: Pos = new Pos(0, "")
   161   }
   162 
   163   final class Pos private[Token](val line: Int, val file: String)
   164     extends scala.util.parsing.input.Position
   165   {
   166     def column = 0
   167     def lineContents = ""
   168 
   169     def advance(token: Token): Pos =
   170     {
   171       var n = 0
   172       for (c <- token.content if c == '\n') n += 1
   173       if (n == 0) this else new Pos(line + n, file)
   174     }
   175 
   176     def position: Position.T = Position.Line_File(line, file)
   177     override def toString: String = Position.here_undelimited(position)
   178   }
   179 
   180   abstract class Reader extends scala.util.parsing.input.Reader[Token]
   181 
   182   private class Token_Reader(tokens: List[Token], val pos: Pos) extends Reader
   183   {
   184     def first = tokens.head
   185     def rest = new Token_Reader(tokens.tail, pos.advance(first))
   186     def atEnd = tokens.isEmpty
   187   }
   188 
   189   def reader(tokens: List[Token], file: String = ""): Reader =
   190     new Token_Reader(tokens, new Pos(1, file))
   191 }
   192 
   193 
   194 sealed case class Token(val kind: Token.Kind.Value, val source: String)
   195 {
   196   def is_command: Boolean = kind == Token.Kind.COMMAND
   197   def is_command_kind(keywords: Keyword.Keywords, pred: String => Boolean): Boolean =
   198     is_command &&
   199       (keywords.command_kind(source) match { case Some(k) => pred(k) case None => false })
   200   def is_keyword: Boolean = kind == Token.Kind.KEYWORD
   201   def is_delimiter: Boolean = is_keyword && !Symbol.is_ascii_identifier(source)
   202   def is_ident: Boolean = kind == Token.Kind.IDENT
   203   def is_sym_ident: Boolean = kind == Token.Kind.SYM_IDENT
   204   def is_string: Boolean = kind == Token.Kind.STRING
   205   def is_nat: Boolean = kind == Token.Kind.NAT
   206   def is_float: Boolean = kind == Token.Kind.FLOAT
   207   def is_name: Boolean =
   208     kind == Token.Kind.IDENT ||
   209     kind == Token.Kind.SYM_IDENT ||
   210     kind == Token.Kind.STRING ||
   211     kind == Token.Kind.NAT
   212   def is_xname: Boolean = is_name || kind == Token.Kind.LONG_IDENT
   213   def is_text: Boolean = is_xname || kind == Token.Kind.VERBATIM || kind == Token.Kind.CARTOUCHE
   214   def is_space: Boolean = kind == Token.Kind.SPACE
   215   def is_comment: Boolean = kind == Token.Kind.COMMENT
   216   def is_improper: Boolean = is_space || is_comment
   217   def is_proper: Boolean = !is_space && !is_comment
   218   def is_error: Boolean = kind == Token.Kind.ERROR
   219   def is_unparsed: Boolean = kind == Token.Kind.UNPARSED
   220 
   221   def is_unfinished: Boolean = is_error &&
   222    (source.startsWith("\"") ||
   223     source.startsWith("`") ||
   224     source.startsWith("{*") ||
   225     source.startsWith("(*") ||
   226     source.startsWith(Symbol.open) ||
   227     source.startsWith(Symbol.open_decoded))
   228 
   229   def is_begin: Boolean = is_keyword && source == "begin"
   230   def is_end: Boolean = is_command && source == "end"
   231 
   232   def is_begin_block: Boolean = is_command && source == "{"
   233   def is_end_block: Boolean = is_command && source == "}"
   234 
   235   def content: String =
   236     if (kind == Token.Kind.STRING) Scan.Parsers.quoted_content("\"", source)
   237     else if (kind == Token.Kind.ALT_STRING) Scan.Parsers.quoted_content("`", source)
   238     else if (kind == Token.Kind.VERBATIM) Scan.Parsers.verbatim_content(source)
   239     else if (kind == Token.Kind.CARTOUCHE) Scan.Parsers.cartouche_content(source)
   240     else if (kind == Token.Kind.COMMENT) Scan.Parsers.comment_content(source)
   241     else source
   242 }
   243