-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip/Ignore/Optional parts #34
Comments
Found out by myself for the constructor part, using optional and acceptZero = true data class Function(val name: String, val arguments: List<Argument>)
data class Constructor(val arguments: List<Argument>)
object test : Grammar<Function>() {
private val lpar by literalToken("(")
private val rpar by literalToken(")")
private val set by literalToken("=")
private val coma by literalToken(", ")
private val variable by regexToken("\\w+")
private val string by regexToken("\"\\w+\"")
private val integer by regexToken("\\d+")
private val ws by regexToken("\\s+", ignore = true)
val assignmentParser by (variable and set and ( variable or integer or string )) use { Assignment(t1.text,
when (t3.type) {
variable -> t3.text
integer -> t3.text.toInt()
string -> t3.text
else -> throw IllegalStateException("unkown type ${t3.type}")
})}
val constructorParser by (lpar and separatedTerms(assignmentParser, coma, acceptZero = true) and rpar) use { Constructor(t2) }
private val functionParser by (variable and optional(constructorParser)) use { Function(t1.text, t2?.arguments ?: emptyList())}
override val rootParser by functionParser
}
fun main() {
println(test.parseToEnd("hello"))
println(test.parseToEnd("hello()"))
println(test.parseToEnd("hello(x=1)"))
println(test.parseToEnd("hello(x=pouet)"))
println(test.parseToEnd("hello(x=\"pouet\", y=2)"))
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I have some questions about regexTokens and not parsing some parts of an input.
See file at bottom for reference.
Ignore
In main() there is a line to parse :
But StartBonus may have a constructor like in entrance, which may
be empty, like in
Or it can have things inside, but I do not want to parse them. I want to ignore them.
But when I try to add a regexp like ([^)].*) it messes up with for example lpar matching.
regexTokens
I think it's related.
At start of input file there is :
0.5f regexp (not present in file) matches a lot of things.
So in the next line
will be matched by the 0.5f regexp
Sample (can be run) code :
The text was updated successfully, but these errors were encountered: