feat: add support for escaped characters in RegexParser and corresponding tests
This commit is contained in:
parent
7ae97961b2
commit
60ec5916d0
3 changed files with 30 additions and 2 deletions
|
@ -8,10 +8,14 @@ import com.github.h0tk3y.betterParse.lexer.regexToken
|
|||
import com.github.h0tk3y.betterParse.parser.Parser
|
||||
|
||||
class RegexParser : Grammar<RegexItem>() {
|
||||
// val bracketContent by regexToken("[^\\]]*")
|
||||
val escapedCharacter by regexToken("\\\\[+*?.()|\\[\\]]")
|
||||
val postfixOperator by regexToken("[+*?]")
|
||||
val alternationSymbol by literalToken("|")
|
||||
val openParenSymbol by literalToken("(")
|
||||
val closeParenSymbol by literalToken(")")
|
||||
val bracketOpen by literalToken("[")
|
||||
val bracketClose by literalToken("]")
|
||||
|
||||
val dot by literalToken(".")
|
||||
val charToken by regexToken("[a-zA-Z0-9]")
|
||||
|
@ -21,6 +25,7 @@ class RegexParser : Grammar<RegexItem>() {
|
|||
val item: Parser<RegexItem> by
|
||||
char or
|
||||
(dot asJust DotItem()) or
|
||||
(escapedCharacter map { CharItem(it.text.substring(1)) }) or
|
||||
(skip(openParenSymbol) and (parser(::rootParser)) and skip(closeParenSymbol))
|
||||
|
||||
val term: Parser<RegexItem> by
|
||||
|
|
|
@ -66,7 +66,21 @@ class AndThenItem(val left: RegexItem, val right: RegexItem) : RegexItem {
|
|||
}
|
||||
|
||||
class CharItem(val value: String) : RegexItem {
|
||||
override fun toString(): String = value
|
||||
override fun toString(): String =
|
||||
// escape 특수 문자를 처리하여 출력
|
||||
when (value) {
|
||||
"+" -> "\\+"
|
||||
"*" -> "\\*"
|
||||
"?" -> "\\?"
|
||||
"." -> "\\."
|
||||
"(" -> "\\("
|
||||
")" -> "\\)"
|
||||
"|" -> "\\|"
|
||||
"[" -> "\\["
|
||||
"]" -> "\\]"
|
||||
else -> value // 일반 문자 그대로 반환
|
||||
}
|
||||
|
||||
override fun findMatch(str: String): AvailableState {
|
||||
return when {
|
||||
// 첫번째 문자가 value와 일치하는지 확인
|
||||
|
|
|
@ -107,4 +107,13 @@ class ParserTest {
|
|||
assert(result.match("ba").isSuccess)
|
||||
assert(result.match("bca").isSuccess)
|
||||
}
|
||||
@Test
|
||||
fun testEscapedCharacter() {
|
||||
val input = "\\+"
|
||||
val parser = RegexParser()
|
||||
val result = parser.parseToEnd(input)
|
||||
assertEquals("\\+", result.toString())
|
||||
assert(result.match("+").isSuccess)
|
||||
assert(!result.match("a").isSuccess)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue