fix string escape handling
This commit is contained in:
@@ -11,6 +11,7 @@ import org.parboiled.matchers.Matcher
|
||||
import org.parboiled.matchers.TestMatcher
|
||||
import org.parboiled.parserunners.TracingParseRunner
|
||||
import org.parboiled.support.ParseTreeUtils
|
||||
import org.parboiled.support.StringVar
|
||||
|
||||
object FigParser {
|
||||
|
||||
@@ -359,18 +360,30 @@ open class FigPegParser: BaseParser<Any>() {
|
||||
// pushes FigString
|
||||
open fun StringLiteralRule( terminators:String? ): Rule {
|
||||
return FirstOf(
|
||||
Sequence(
|
||||
QuotedStringRule(),
|
||||
UnquotedStringRule(terminators)
|
||||
).suppressSubnodes()
|
||||
}
|
||||
|
||||
// pushes FigString
|
||||
open fun QuotedStringRule(): Rule {
|
||||
val text = StringVar()
|
||||
return Sequence(
|
||||
"\"",
|
||||
TextOrEmptyRule(charsRequiringEscape = "\""),
|
||||
push( FigString( match() ) ),
|
||||
TextOrEmptyRule(charsRequiringEscape = "\"",var_=text),
|
||||
push( FigString( text.get() ) ),
|
||||
"\""
|
||||
),
|
||||
Sequence(
|
||||
TextRule(charsRequiringEscape = "${WHITESPACE}${terminators?:""}"),
|
||||
push( FigString( match() ) ),
|
||||
)
|
||||
}
|
||||
|
||||
// pushes FigString
|
||||
open fun UnquotedStringRule(terminators:String?): Rule {
|
||||
val text = StringVar()
|
||||
return Sequence(
|
||||
TextRule(charsRequiringEscape = "${WHITESPACE}${terminators?:""}",var_=text),
|
||||
push( FigString( text.get() ) ),
|
||||
TestWhitespaceTerminatorOrEnd(terminators)
|
||||
)
|
||||
).suppressSubnodes()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -379,19 +392,21 @@ open class FigPegParser: BaseParser<Any>() {
|
||||
*
|
||||
* @param charsRequiringEscape
|
||||
*/
|
||||
open fun TextOrEmptyRule(charsRequiringEscape: String): Rule {
|
||||
open fun TextOrEmptyRule(charsRequiringEscape: String,var_:StringVar): Rule {
|
||||
return ZeroOrMore(
|
||||
FirstOf(
|
||||
Sequence(
|
||||
'\\',
|
||||
AnyOf(charsRequiringEscape + '\\')
|
||||
AnyOf(charsRequiringEscape + '\\'),
|
||||
var_.append(match())
|
||||
),
|
||||
OneOrMore(
|
||||
Sequence(
|
||||
TestNot(
|
||||
AnyOf(charsRequiringEscape + '\\')
|
||||
),
|
||||
ANY
|
||||
ANY,
|
||||
var_.append(match())
|
||||
)
|
||||
).suppressSubnodes()
|
||||
)
|
||||
@@ -404,17 +419,19 @@ open class FigPegParser: BaseParser<Any>() {
|
||||
*
|
||||
* @param charsRequiringEscape
|
||||
*/
|
||||
open fun TextRule(charsRequiringEscape: String): Rule {
|
||||
open fun TextRule(charsRequiringEscape: String,var_:StringVar): Rule {
|
||||
return OneOrMore(
|
||||
FirstOf(
|
||||
Sequence(
|
||||
'\\',
|
||||
AnyOf(charsRequiringEscape + '\\')
|
||||
AnyOf(charsRequiringEscape + '\\'),
|
||||
var_.append(match())
|
||||
),
|
||||
OneOrMore(
|
||||
Sequence(
|
||||
TestNot(AnyOf(charsRequiringEscape + '\\')),
|
||||
ANY
|
||||
ANY,
|
||||
var_.append(match())
|
||||
)
|
||||
).suppressSubnodes()
|
||||
)
|
||||
|
||||
@@ -67,6 +67,14 @@ class FigParserTest {
|
||||
))
|
||||
)
|
||||
|
||||
@Test
|
||||
fun testString() = test(
|
||||
"abc",
|
||||
FigList(mutableListOf(
|
||||
FigString("abc")
|
||||
))
|
||||
)
|
||||
|
||||
@Test
|
||||
fun testNumber() = test(
|
||||
"1",
|
||||
@@ -94,7 +102,7 @@ class FigParserTest {
|
||||
|
||||
@Test
|
||||
fun escapeQuotedString() = test(
|
||||
""""a\ b""",
|
||||
"\"a b\"",
|
||||
FigList(mutableListOf(
|
||||
FigString("a b")
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user