Skip to content

Commit

Permalink
Added range-based char class operations oneOf and noneOf (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 authored Jan 7, 2022
1 parent 2686a55 commit 2707b4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/scala/parsley/character.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import parsley.errors.combinator.ErrorMethods

import scala.annotation.switch
import scala.language.implicitConversions
import scala.collection.immutable.NumericRange

/** This module contains many parsers to do with reading one or more characters. Almost every parser will need something from this module.
* @since 2.2.0
Expand Down Expand Up @@ -47,6 +48,14 @@ object character
* Returns the parsed character. See also `satisfy`.*/
def oneOf(cs: Char*): Parsley[Char] = oneOf(cs.toSet)

/**`oneOf(cs)` succeeds if the current character is in the supplied sequence of characters `cs`.
* Returns the parsed character. See also `satisfy`.*/
def oneOf(cs: NumericRange[Char]): Parsley[Char] = oneOf(cs.toSet)

/**As the dual of `oneOf`, `noneOf(cs)` succeeds if the current character is not in the supplied
* sequence of characters `cs`. Returns the parsed character.*/
def noneOf(cs: NumericRange[Char]): Parsley[Char] = noneOf(cs.toSet)

/**As the dual of `oneOf`, `noneOf(cs)` succeeds if the current character is not in the supplied
* sequence of characters `cs`. Returns the parsed character.*/
def noneOf(cs: Char*): Parsley[Char] = noneOf(cs.toSet)
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/parsley/CharTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,27 @@ class CharTests extends ParsleyTest {

"oneOf" should "match any of the characters provided" in {
val p = character.oneOf('a', 'b', 'c')
val q = character.oneOf('a' to 'c')
p.parse("a") should not be a [Failure[_]]
p.parse("b") should not be a [Failure[_]]
p.parse("c") should not be a [Failure[_]]
p.parse("d") shouldBe a [Failure[_]]
q.parse("a") should not be a [Failure[_]]
q.parse("b") should not be a [Failure[_]]
q.parse("c") should not be a [Failure[_]]
q.parse("d") shouldBe a [Failure[_]]
}

"noneOf" should "match none of the characters provided" in {
val p = character.noneOf('a', 'b', 'c')
val q = character.noneOf('a' to 'c')
p.parse("a") shouldBe a [Failure[_]]
p.parse("b") shouldBe a [Failure[_]]
p.parse("c") shouldBe a [Failure[_]]
p.parse("d") should not be a [Failure[_]]
q.parse("a") shouldBe a [Failure[_]]
q.parse("b") shouldBe a [Failure[_]]
q.parse("c") shouldBe a [Failure[_]]
q.parse("d") should not be a [Failure[_]]
}
}

0 comments on commit 2707b4c

Please sign in to comment.