summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGilles Dubochet <gilles.dubochet@epfl.ch>2007-09-19 12:53:49 +0000
committerGilles Dubochet <gilles.dubochet@epfl.ch>2007-09-19 12:53:49 +0000
commit151acf12ef82ed73dbb400536bb86b2cabdd9ac4 (patch)
treee76e680018e824a865c35b4fd7b178ba6ffae90d /src
parent87fed8f410f2941c2f41ba0a3133877aecfccb7a (diff)
downloadscala-151acf12ef82ed73dbb400536bb86b2cabdd9ac4.tar.gz
scala-151acf12ef82ed73dbb400536bb86b2cabdd9ac4.tar.bz2
scala-151acf12ef82ed73dbb400536bb86b2cabdd9ac4.zip
Added “alternative with longest match selection...
Added “alternative with longest match selection” parser to combinator library (requested for DBC2).
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/util/parsing/combinator/Parsers.scala52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala
index 06a2d7c72f..84415c0fc9 100644
--- a/src/library/scala/util/parsing/combinator/Parsers.scala
+++ b/src/library/scala/util/parsing/combinator/Parsers.scala
@@ -268,6 +268,32 @@ trait Parsers {
override def toString = "|"
}
+ /** A parser combinator for alternative with longest match composition
+ *
+ *<p>`p ||| q' succeeds if `p' succeeds or `q' succeeds
+ * If `p' and `q' both succeed, the parser that consumed the most
+ * characters accepts.</p>
+ *
+ * @param q a parser that accepts if p consumes less characters.
+ * @return a `Parser' that returns the result of the parser consuming the most characteres (out of `p' and `q').
+ */
+ def ||| [U >: T](q: => Parser[U]): Parser[U] = new Parser[U] {
+ def apply(in: Input) = {
+ val res1 = Parser.this(in)
+ val res2 = q(in)
+
+ (res1, res2) match {
+ case (s1 @ Success(_, next1), s2 @ Success(_, next2)) => if (next2.pos < next1.pos) s1 else s2
+ case (s1 @ Success(_, _), _) => s1
+ case (_, s2 @ Success(_, _)) => s2
+ case (e1 @ Error(_, _), _) => e1
+ case (f1 @ Failure(_, next1), f2 @ Failure(_, next2)) => if (next2.pos < next1.pos) f1 else f2
+ case (f1 @ Failure(_, next1), e2 @ Error(_, next2)) => if (next2.pos < next1.pos) f1 else e2
+ }
+ }
+ override def toString = "|||"
+ }
+
/** A parser combinator for function application
*
*<p>`p ^^ f' succeeds if `p' succeeds; it returns `f' applied to the result of `p'.</p>
@@ -467,6 +493,32 @@ trait Parsers {
override def toString = "|"
}
+ /** A parser combinator for alternative with longest match composition
+ *
+ *<p>`p ||| q' succeeds if `p' succeeds or `q' succeeds
+ * If `p' and `q' both succeed, the parser that consumed the most
+ * characters accepts.</p>
+ *
+ * @param q a parser that accepts if p consumes less characters.
+ * @return a `Parser' that returns the result of the parser consuming the most characteres (out of `p' and `q').
+ */
+ def ||| [Q <% UnitParser](q: => Q): UnitParser = new UnitParser {
+ def apply(in: Input) = {
+ val res1 = UnitParser.this(in)
+ val res2 = q(in)
+
+ (res1, res2) match {
+ case (s1 @ Success(_, next1), s2 @ Success(_, next2)) => if (next2.pos < next1.pos) s1 else s2
+ case (s1 @ Success(_, _), _) => s1
+ case (_, s2 @ Success(_, _)) => s2
+ case (e1 @ Error(_, _), _) => e1
+ case (f1 @ Failure(_, next1), f2 @ Failure(_, next2)) => if (next2.pos < next1.pos) f1 else f2
+ case (f1 @ Failure(_, next1), e2 @ Error(_, next2)) => if (next2.pos < next1.pos) f1 else e2
+ }
+ }
+ override def toString = "|||"
+ }
+
/** A parser combinator for function application
*