diff options
Diffstat (limited to 'docs/examples/Parsers.scala')
-rw-r--r-- | docs/examples/Parsers.scala | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/docs/examples/Parsers.scala b/docs/examples/Parsers.scala index 10512ab2fd..3d83d05fb9 100644 --- a/docs/examples/Parsers.scala +++ b/docs/examples/Parsers.scala @@ -1,8 +1,8 @@ -package examples; +package examples abstract class Parsers { - type inputType; + type inputType trait Parser[a] { @@ -33,13 +33,13 @@ abstract class Parsers { def ||| (p: => Parser[a]) = new Parser[a] { def apply(in: inputType): Result = Parser.this.apply(in) match { - case None => p(in) - case s => s + case None => p(in) + case s => s } } def &&& [b](p: => Parser[b]): Parser[b] = - for (val _ <- this; val x <- p) yield x; + for (val _ <- this; val x <- p) yield x } def succeed[a](x: a) = new Parser[a] { @@ -47,13 +47,13 @@ abstract class Parsers { } def rep[a](p: Parser[a]): Parser[List[a]] = - rep1(p) ||| succeed(List()); + rep1(p) ||| succeed(List()) def rep1[a](p: Parser[a]): Parser[List[a]] = - for (val x <- p; val xs <- rep(p)) yield x :: xs; + for (val x <- p; val xs <- rep(p)) yield x :: xs def opt[a](p: Parser[a]): Parser[List[a]] = - (for (val x <- p) yield List(x)) ||| succeed(List()); + (for (val x <- p) yield List(x)) ||| succeed(List()) } class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] { @@ -71,9 +71,9 @@ class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] i < delimiters.length() } - def hasNext: boolean = ch != EOI; + def hasNext: boolean = ch != EOI - private val buf = new StringBuffer; + private val buf = new StringBuffer def next: String = { while (ch <= ' ' && ch != EOI) nextChar(); @@ -81,8 +81,8 @@ class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] else { if (isDelimiter(ch)) ch.toString() else { - buf.setLength(0); buf append ch; - while (ch > ' ' && ch != EOI && !isDelimiter(ch)) { + buf.setLength(0); buf append ch + while (ch > ' ' && ch != EOI && !isDelimiter(ch)) { buf append ch; nextChar(); } buf.toString() @@ -91,18 +91,18 @@ class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] } } -abstract class TokenParsers extends Parsers { - type inputType = Stream[String]; +mixin class TokenParsers extends Parsers { + type inputType = Stream[String] def nextToken() = new Parser[String] { def apply(in: inputType): Result = - if (in.isEmpty) None else Some(Pair(in.head, in.tail)); + if (in.isEmpty) None else Some(Pair(in.head, in.tail)) } } -abstract class CharParsers extends Parsers { - def any: Parser[char]; +mixin class CharParsers extends Parsers { + def any: Parser[char] def chr(ch: char) = - for (val c <- any; c == ch) yield c; + for (val c <- any; c == ch) yield c def chr(p: char => boolean) = - for (val c <- any; p(c)) yield c; + for (val c <- any; p(c)) yield c } |