Skip to content

Commit

Permalink
feat(parser): Change UnicodeCharClass.propValue to type `Option[Str…
Browse files Browse the repository at this point in the history
…ing]`
  • Loading branch information
nhaajt committed Dec 11, 2023
1 parent d324e09 commit be7f1b7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ case class UnicodeCharClass(
property: String,
override val location: Location,
isPositive: Boolean = true,
propValue: String = ""
propValue: Option[String] = None
) extends Leaf(
if (propValue.isEmpty) property else "%s=%s".format(property, propValue),
propValue match {
case Some(value) => s"$property=$value"
case None => property
},
location,
if (isPositive) """\p{""" else """\P{""",
"}"
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/weaponregex/parser/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ abstract class Parser(val pattern: String) {
(CharIn("a-z", "A-Z") ~ CharIn("a-z", "A-Z", "0-9", "_").rep).! ~ "=" ~
(CharIn("a-z", "A-Z") ~ CharIn("a-z", "A-Z", "0-9", "_").rep).! ~ "}"
)
.map { case (loc, (p, property, propValue)) => UnicodeCharClass(property, loc, p == "p", propValue) }
.map { case (loc, (p, property, propValue)) => UnicodeCharClass(property, loc, p == "p", Some(propValue)) }

/** Parse a unicode character class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PredefinedCharClassNodeTest extends munit.FunSuite {
}

test("UnicodeCharClass with property and value build") {
val node1 = UnicodeCharClass("hello_World_0123", LOCATION, propValue = "Goodbye_world_321")
val node1 = UnicodeCharClass("hello_World_0123", LOCATION, propValue = Some("Goodbye_world_321"))
assertEquals(node1.build, """\p{hello_World_0123=Goodbye_world_321}""")
}

Expand All @@ -30,7 +30,8 @@ class PredefinedCharClassNodeTest extends munit.FunSuite {
}

test("UnicodeCharClass with lone property build negated") {
val node1 = UnicodeCharClass("hello_World_0123", LOCATION, isPositive = false, propValue = "Goodbye_world_321")
val node1 =
UnicodeCharClass("hello_World_0123", LOCATION, isPositive = false, propValue = Some("Goodbye_world_321"))
assertEquals(node1.build, """\P{hello_World_0123=Goodbye_world_321}""")
}
}
16 changes: 8 additions & 8 deletions core/src/test/scala/weaponregex/parser/ParserJSTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ class ParserJSTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, Some(flag), parserFlavor).getOrFail.to[CharacterClass]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Alpha", _, true, "") => true
case UnicodeCharClass("Alpha", _, true, None) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "") => true
case UnicodeCharClass("hello_World_0123", _, false, None) => true
case _ => false
})

Expand All @@ -185,11 +185,11 @@ class ParserJSTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, Some(flag), parserFlavor).getOrFail.to[CharacterClass]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Script_Extensions", _, true, "Latin") => true
case UnicodeCharClass("Script_Extensions", _, true, Some("Latin")) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "Goodbye_world_321") => true
case UnicodeCharClass("hello_World_0123", _, false, Some("Goodbye_world_321")) => true
case _ => false
})

Expand All @@ -204,11 +204,11 @@ class ParserJSTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, Some(flag), parserFlavor).getOrFail.to[Concat]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Alpha", _, true, "") => true
case UnicodeCharClass("Alpha", _, true, None) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "") => true
case UnicodeCharClass("hello_World_0123", _, false, None) => true
case _ => false
})

Expand All @@ -223,11 +223,11 @@ class ParserJSTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, Some(flag), parserFlavor).getOrFail.to[Concat]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Script_Extensions", _, true, "Latin") => true
case UnicodeCharClass("Script_Extensions", _, true, Some("Latin")) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "Goodbye_world_321") => true
case UnicodeCharClass("hello_World_0123", _, false, Some("Goodbye_world_321")) => true
case _ => false
})

Expand Down
16 changes: 8 additions & 8 deletions core/src/test/scala/weaponregex/parser/ParserJVMTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ class ParserJVMTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, parserFlavor).getOrFail.to[CharacterClass]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Alpha", _, true, "") => true
case UnicodeCharClass("Alpha", _, true, None) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "") => true
case UnicodeCharClass("hello_World_0123", _, false, None) => true
case _ => false
})

Expand All @@ -152,11 +152,11 @@ class ParserJVMTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, parserFlavor).getOrFail.to[CharacterClass]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Script_Extensions", _, true, "Latin") => true
case UnicodeCharClass("Script_Extensions", _, true, Some("Latin")) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "Goodbye_world_321") => true
case UnicodeCharClass("hello_World_0123", _, false, Some("Goodbye_world_321")) => true
case _ => false
})

Expand All @@ -168,11 +168,11 @@ class ParserJVMTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, parserFlavor).getOrFail.to[Concat]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Alpha", _, true, "") => true
case UnicodeCharClass("Alpha", _, true, None) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "") => true
case UnicodeCharClass("hello_World_0123", _, false, None) => true
case _ => false
})

Expand All @@ -184,11 +184,11 @@ class ParserJVMTest extends munit.FunSuite with ParserTest {
val parsedTree = Parser(pattern, parserFlavor).getOrFail.to[Concat]

assert(clue(parsedTree.children.head) match {
case UnicodeCharClass("Script_Extensions", _, true, "Latin") => true
case UnicodeCharClass("Script_Extensions", _, true, Some("Latin")) => true
case _ => false
})
assert(clue(parsedTree.children.last) match {
case UnicodeCharClass("hello_World_0123", _, false, "Goodbye_world_321") => true
case UnicodeCharClass("hello_World_0123", _, false, Some("Goodbye_world_321")) => true
case _ => false
})

Expand Down

0 comments on commit be7f1b7

Please sign in to comment.