summaryrefslogtreecommitdiff
path: root/sources/examples
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2004-01-19 14:57:11 +0000
committerMartin Odersky <odersky@gmail.com>2004-01-19 14:57:11 +0000
commitc72f7b292fb5976c656ee666044e0a9b6ede1459 (patch)
treedd5bd1232b33fe34dd7c681a906aaebb287662c2 /sources/examples
parent4e215f67917c35fadcd9a74390842634f2b103d0 (diff)
downloadscala-c72f7b292fb5976c656ee666044e0a9b6ede1459.tar.gz
scala-c72f7b292fb5976c656ee666044e0a9b6ede1459.tar.bz2
scala-c72f7b292fb5976c656ee666044e0a9b6ede1459.zip
*** empty log message ***
Diffstat (limited to 'sources/examples')
-rw-r--r--sources/examples/parsers.scala28
1 files changed, 26 insertions, 2 deletions
diff --git a/sources/examples/parsers.scala b/sources/examples/parsers.scala
index bef1e960cb..2ef0d778b9 100644
--- a/sources/examples/parsers.scala
+++ b/sources/examples/parsers.scala
@@ -58,6 +58,16 @@ abstract class ListParsers extends Parsers {
def expr : Parser = ident ||| number ||| list;
}
+abstract class ExprParsers extends Parsers {
+ def chr(p: char => boolean): Parser;
+ def chr(c: char): Parser = chr(d: char => d == c);
+
+ def digit : Parser = chr(Character.isDigit);
+ def number : Parser = digit &&& rep(digit);
+ def summand : Parser = number ||| chr('(') &&& expr &&& chr(')');
+ def expr : Parser = summand &&& rep(chr('+') &&& summand)
+}
+
class ParseString(s: String) extends Parsers {
type intype = int;
val input = 0;
@@ -68,12 +78,26 @@ class ParseString(s: String) extends Parsers {
}
}
-object Test {
+object TestList {
def main(args: Array[String]): unit =
if (args.length == 1) {
val ps = new ListParsers with ParseString(args(0));
- ps.exprs(input) match {
+ ps.expr(ps.input) match {
+ case Some(n) =>
+ System.out.println("parsed: " + args(0).substring(0, n));
+ case None =>
+ System.out.println("nothing parsed");
+ }
+ } else System.out.println("usage: java examples.Test <expr-string>");
+}
+
+object TestExpr {
+
+ def main(args: Array[String]): unit =
+ if (args.length == 1) {
+ val ps = new ExprParsers with ParseString(args(0));
+ ps.expr(ps.input) match {
case Some(n) =>
System.out.println("parsed: " + args(0).substring(0, n));
case None =>