src/Pure/Isar/outer_parse.scala
author wenzelm
Tue Dec 22 21:47:27 2009 +0100 (2009-12-22 ago)
changeset 34168 18843829c7f2
parent 34161 4c845a8f1357
child 34266 bfe8d6998734
permissions -rw-r--r--
clarified atom parser: return content;
added tags parser;
wenzelm@34159
     1
/*  Title:      Pure/Isar/outer_parse.scala
wenzelm@34159
     2
    Author:     Makarius
wenzelm@34159
     3
wenzelm@34159
     4
Generic parsers for Isabelle/Isar outer syntax.
wenzelm@34159
     5
*/
wenzelm@34159
     6
wenzelm@34159
     7
package isabelle
wenzelm@34159
     8
wenzelm@34159
     9
import scala.util.parsing.combinator.Parsers
wenzelm@34159
    10
wenzelm@34159
    11
wenzelm@34159
    12
object Outer_Parse
wenzelm@34159
    13
{
wenzelm@34161
    14
  /* parsing tokens */
wenzelm@34161
    15
wenzelm@34159
    16
  trait Parser extends Parsers
wenzelm@34159
    17
  {
wenzelm@34159
    18
    type Elem = Outer_Lex.Token
wenzelm@34159
    19
wenzelm@34161
    20
    private def proper(in: Input): Input =
wenzelm@34161
    21
      if (in.atEnd || in.first.is_proper) in
wenzelm@34161
    22
      else proper(in.rest)
wenzelm@34159
    23
wenzelm@34159
    24
    def token(s: String, pred: Elem => Boolean): Parser[Elem] = new Parser[Elem]
wenzelm@34159
    25
    {
wenzelm@34161
    26
      def apply(raw_input: Input) =
wenzelm@34159
    27
      {
wenzelm@34161
    28
        val in = proper(raw_input)
wenzelm@34159
    29
        if (in.atEnd) Failure(s + " expected (past end-of-file!)", in)
wenzelm@34159
    30
        else {
wenzelm@34159
    31
          val token = in.first
wenzelm@34161
    32
          if (pred(token)) Success(token, proper(in.rest))
wenzelm@34159
    33
          else
wenzelm@34159
    34
            token.text match {
wenzelm@34159
    35
              case (txt, "") =>
wenzelm@34159
    36
                Failure(s + " expected,\nbut " + txt + " was found", in)
wenzelm@34159
    37
              case (txt1, txt2) =>
wenzelm@34159
    38
                Failure(s + " expected,\nbut " + txt1 + " was found:\n" + txt2, in)
wenzelm@34159
    39
            }
wenzelm@34159
    40
        }
wenzelm@34159
    41
      }
wenzelm@34159
    42
    }
wenzelm@34159
    43
wenzelm@34168
    44
    def atom(s: String, pred: Elem => Boolean): Parser[String] =
wenzelm@34168
    45
      token(s, pred) ^^ (_.content)
wenzelm@34168
    46
wenzelm@34159
    47
    def not_eof: Parser[Elem] = token("input token", _ => true)
wenzelm@34159
    48
wenzelm@34168
    49
    def keyword(name: String): Parser[String] =
wenzelm@34168
    50
      atom(Outer_Lex.Token_Kind.KEYWORD.toString + " \"" + name + "\"",
wenzelm@34159
    51
        tok => tok.kind == Outer_Lex.Token_Kind.KEYWORD && tok.content == name)
wenzelm@34159
    52
wenzelm@34168
    53
    def name: Parser[String] = atom("name declaration", _.is_name)
wenzelm@34168
    54
    def xname: Parser[String] = atom("name reference", _.is_xname)
wenzelm@34168
    55
    def text: Parser[String] = atom("text", _.is_text)
wenzelm@34168
    56
    def ML_source: Parser[String] = atom("ML source", _.is_text)
wenzelm@34168
    57
    def doc_source: Parser[String] = atom("document source", _.is_text)
wenzelm@34168
    58
    def path: Parser[String] = atom("file name/path specification", _.is_name)
wenzelm@34168
    59
wenzelm@34168
    60
    private def tag_name: Parser[String] =
wenzelm@34168
    61
      atom("tag name", tok =>
wenzelm@34168
    62
          tok.kind == Outer_Lex.Token_Kind.IDENT ||
wenzelm@34168
    63
          tok.kind == Outer_Lex.Token_Kind.STRING)
wenzelm@34168
    64
wenzelm@34168
    65
    def tags: Parser[List[String]] = rep(keyword("%") ~> tag_name)
wenzelm@34161
    66
wenzelm@34161
    67
wenzelm@34161
    68
    /* wrappers */
wenzelm@34161
    69
wenzelm@34161
    70
    def parse[T](p: Parser[T], in: Outer_Lex.Reader): ParseResult[T] = p(in)
wenzelm@34161
    71
    def parse_all[T](p: Parser[T], in: Outer_Lex.Reader): ParseResult[T] = parse(phrase(p), in)
wenzelm@34159
    72
  }
wenzelm@34159
    73
}
wenzelm@34159
    74