src/Pure/Isar/token.scala
author wenzelm
Sat Mar 14 19:51:36 2015 +0100 (2015-03-14)
changeset 59695 a03e0561bdbf
parent 59671 9715eb8e9408
child 59696 f505fee04400
permissions -rw-r--r--
clarified positions of theory imports;
     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()
   161   }
   162 
   163   final class Pos private[Token](
   164       val line: Int = 0,
   165       val offset: Symbol.Offset = 0,
   166       val file: String = "",
   167       val id: Document_ID.Generic = Document_ID.none)
   168     extends scala.util.parsing.input.Position
   169   {
   170     def column = 0
   171     def lineContents = ""
   172 
   173     def advance(token: Token): Pos =
   174     {
   175       var line1 = line
   176       var offset1 = offset
   177       for (s <- Symbol.iterator(token.source)) {
   178         if (line1 > 0 && Symbol.is_newline(s)) line1 += 1
   179         if (offset1 > 0) offset1 += 1
   180       }
   181       if (line1 == line && offset1 == offset) this
   182       else new Pos(line1, offset1, file, id)
   183     }
   184 
   185     private def position(end_offset: Symbol.Offset): Position.T =
   186       (if (line > 0) Position.Line(line) else Nil) :::
   187       (if (offset > 0) Position.Offset(offset) else Nil) :::
   188       (if (end_offset > 0) Position.End_Offset(end_offset) else Nil) :::
   189       (if (file != "") Position.File(file) else Nil) :::
   190       (if (id != Document_ID.none) Position.Id(id) else Nil)
   191 
   192     def position(): Position.T = position(0)
   193     def position(token: Token): Position.T = position(advance(token).offset)
   194 
   195     override def toString: String = Position.here_undelimited(position())
   196   }
   197 
   198   abstract class Reader extends scala.util.parsing.input.Reader[Token]
   199 
   200   private class Token_Reader(tokens: List[Token], val pos: Pos) extends Reader
   201   {
   202     def first = tokens.head
   203     def rest = new Token_Reader(tokens.tail, pos.advance(first))
   204     def atEnd = tokens.isEmpty
   205   }
   206 
   207   def reader(tokens: List[Token], file: String, id: Document_ID.Generic = Document_ID.none)
   208     : Reader = new Token_Reader(tokens, new Pos(1, 1, file, id))
   209 }
   210 
   211 
   212 sealed case class Token(val kind: Token.Kind.Value, val source: String)
   213 {
   214   def is_command: Boolean = kind == Token.Kind.COMMAND
   215   def is_command_kind(keywords: Keyword.Keywords, pred: String => Boolean): Boolean =
   216     is_command &&
   217       (keywords.command_kind(source) match { case Some(k) => pred(k) case None => false })
   218   def is_keyword: Boolean = kind == Token.Kind.KEYWORD
   219   def is_delimiter: Boolean = is_keyword && !Symbol.is_ascii_identifier(source)
   220   def is_ident: Boolean = kind == Token.Kind.IDENT
   221   def is_sym_ident: Boolean = kind == Token.Kind.SYM_IDENT
   222   def is_string: Boolean = kind == Token.Kind.STRING
   223   def is_nat: Boolean = kind == Token.Kind.NAT
   224   def is_float: Boolean = kind == Token.Kind.FLOAT
   225   def is_name: Boolean =
   226     kind == Token.Kind.IDENT ||
   227     kind == Token.Kind.SYM_IDENT ||
   228     kind == Token.Kind.STRING ||
   229     kind == Token.Kind.NAT
   230   def is_xname: Boolean = is_name || kind == Token.Kind.LONG_IDENT
   231   def is_text: Boolean = is_xname || kind == Token.Kind.VERBATIM || kind == Token.Kind.CARTOUCHE
   232   def is_space: Boolean = kind == Token.Kind.SPACE
   233   def is_comment: Boolean = kind == Token.Kind.COMMENT
   234   def is_improper: Boolean = is_space || is_comment
   235   def is_proper: Boolean = !is_space && !is_comment
   236   def is_error: Boolean = kind == Token.Kind.ERROR
   237   def is_unparsed: Boolean = kind == Token.Kind.UNPARSED
   238 
   239   def is_unfinished: Boolean = is_error &&
   240    (source.startsWith("\"") ||
   241     source.startsWith("`") ||
   242     source.startsWith("{*") ||
   243     source.startsWith("(*") ||
   244     source.startsWith(Symbol.open) ||
   245     source.startsWith(Symbol.open_decoded))
   246 
   247   def is_begin: Boolean = is_keyword && source == "begin"
   248   def is_end: Boolean = is_command && source == "end"
   249 
   250   def is_begin_block: Boolean = is_command && source == "{"
   251   def is_end_block: Boolean = is_command && source == "}"
   252 
   253   def content: String =
   254     if (kind == Token.Kind.STRING) Scan.Parsers.quoted_content("\"", source)
   255     else if (kind == Token.Kind.ALT_STRING) Scan.Parsers.quoted_content("`", source)
   256     else if (kind == Token.Kind.VERBATIM) Scan.Parsers.verbatim_content(source)
   257     else if (kind == Token.Kind.CARTOUCHE) Scan.Parsers.cartouche_content(source)
   258     else if (kind == Token.Kind.COMMENT) Scan.Parsers.comment_content(source)
   259     else source
   260 }
   261