src/Pure/Isar/parse.scala
changeset 75393 87ebf5a50283
parent 74887 56247fdb8bbb
child 75405 b13ab7d11b90
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
     9 
     9 
    10 import scala.util.parsing.combinator.Parsers
    10 import scala.util.parsing.combinator.Parsers
    11 import scala.annotation.tailrec
    11 import scala.annotation.tailrec
    12 
    12 
    13 
    13 
    14 object Parse
    14 object Parse {
    15 {
       
    16   /* parsing tokens */
    15   /* parsing tokens */
    17 
    16 
    18   trait Parser extends Parsers
    17   trait Parser extends Parsers {
    19   {
       
    20     type Elem = Token
    18     type Elem = Token
    21 
    19 
    22     def filter_proper: Boolean = true
    20     def filter_proper: Boolean = true
    23 
    21 
    24     @tailrec private def proper(in: Input): Input =
    22     @tailrec private def proper(in: Input): Input =
    25       if (!filter_proper || in.atEnd || in.first.is_proper) in
    23       if (!filter_proper || in.atEnd || in.first.is_proper) in
    26       else proper(in.rest)
    24       else proper(in.rest)
    27 
    25 
    28     private def proper_position: Parser[Position.T] =
    26     private def proper_position: Parser[Position.T] =
    29       new Parser[Position.T] {
    27       new Parser[Position.T] {
    30         def apply(raw_input: Input) =
    28         def apply(raw_input: Input) = {
    31         {
       
    32           val in = proper(raw_input)
    29           val in = proper(raw_input)
    33           val pos =
    30           val pos =
    34             in.pos match {
    31             in.pos match {
    35               case pos: Token.Pos => pos
    32               case pos: Token.Pos => pos
    36               case _ => Token.Pos.none
    33               case _ => Token.Pos.none
    42     def position[A](parser: Parser[A]): Parser[(A, Position.T)] =
    39     def position[A](parser: Parser[A]): Parser[(A, Position.T)] =
    43       proper_position ~ parser ^^ { case x ~ y => (y, x) }
    40       proper_position ~ parser ^^ { case x ~ y => (y, x) }
    44 
    41 
    45     def token(s: String, pred: Elem => Boolean): Parser[Elem] =
    42     def token(s: String, pred: Elem => Boolean): Parser[Elem] =
    46       new Parser[Elem] {
    43       new Parser[Elem] {
    47         def apply(raw_input: Input) =
    44         def apply(raw_input: Input) = {
    48         {
       
    49           val in = proper(raw_input)
    45           val in = proper(raw_input)
    50           if (in.atEnd) Failure(s + " expected,\nbut end-of-input was found", in)
    46           if (in.atEnd) Failure(s + " expected,\nbut end-of-input was found", in)
    51           else {
    47           else {
    52             val token = in.first
    48             val token = in.first
    53             if (pred(token)) Success(token, proper(in.rest))
    49             if (pred(token)) Success(token, proper(in.rest))
    93 
    89 
    94     /* wrappers */
    90     /* wrappers */
    95 
    91 
    96     def parse[T](p: Parser[T], in: Token.Reader): ParseResult[T] = p(in)
    92     def parse[T](p: Parser[T], in: Token.Reader): ParseResult[T] = p(in)
    97 
    93 
    98     def parse_all[T](p: Parser[T], in: Token.Reader): ParseResult[T] =
    94     def parse_all[T](p: Parser[T], in: Token.Reader): ParseResult[T] = {
    99     {
       
   100       val result = parse(p, in)
    95       val result = parse(p, in)
   101       val rest = proper(result.next)
    96       val rest = proper(result.next)
   102       if (result.successful && !rest.atEnd) Error("bad input", rest)
    97       if (result.successful && !rest.atEnd) Error("bad input", rest)
   103       else result
    98       else result
   104     }
    99     }