You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 :
version: 0.5f
0.5f regexp (not present in file) matches a lot of things.
So in the next line
alphabetFile: missionAlphabet.xpr
will be matched by the 0.5f regexp
Sample (can be run) code :
importcom.github.h0tk3y.betterParse.combinators.*importcom.github.h0tk3y.betterParse.grammar.Grammarimportcom.github.h0tk3y.betterParse.grammar.parseToEndimportcom.github.h0tk3y.betterParse.grammar.parserimportcom.github.h0tk3y.betterParse.lexer.literalTokenimportcom.github.h0tk3y.betterParse.lexer.regexTokeninterfaceLinedata classVersion(valvalue:String) : Line
data classAlphabetFile(valfile:String) : Line
sealedclassExpressiondata classTileMap(valwidth:Int, valheight:Int) : Expression()
data classGraph(valsymbols:List<Symbol>) : Expression()
sealedclassSymboldata classNode(valid:Int, valname:String, valconstructor: Constructor) : Symbol()
//data class Edge(val id: String, val name: String, val source: String, val target: String) : Symbol// Assignment or BooleanExpressionsealedclassArgumentdata classAssignment(valvariable:String, valvalue:Any) : Argument()
data classConstructor(valarguments:List<Argument>)
data classStart(valexpression:Expression) : Line
data classRule(valname:String, valtype:Expression) : Line
data classGGrammar(
valversion:Version,
valalphabetFile:AlphabetFile,
valstart:Start,
valrule:List<Rule>) {
overridefuntoString(): String {
returnlistOf(version, alphabetFile, start, rule.joinToString("\n")).joinToString("\n")
}
}
object GrammarParser2 : Grammar<GGrammar>() {
// literal firstprivateval versionLiteral by literalToken("version: ")
privateval version by literalToken("0.5f") // badprivateval alphabetFileLiteral by literalToken("alphabetFile: ")
privateval alphabet by literalToken("missionAlphabet.xpr") // badprivateval startLiteral by literalToken("start: ")
privateval graphLiteral by literalToken("GRAPH")
privateval tileMapLiteral by literalToken("TILEMAP")
privateval rule by literalToken("rule: ")
privateval colon by literalToken(":")
privateval lpar by literalToken("(")
privateval rpar by literalToken(")")
privateval set by literalToken("=")
privateval coma by literalToken(", ")
// main grammar parsersval versionParser by (versionLiteral and parser(this::version)) use { Version(t2.text) }
val alphabetParser by (alphabetFileLiteral and parser(this::alphabet)) use { AlphabetFile(t2.text) }
val tileMapParser by (tileMapLiteral and-parser(this::ws) and parser(this::integer) and-parser(this::ws) and parser(this::integer)) use { TileMap(t2.text.toInt(), t3.text.toInt()) }
val assignmentParser by (parser(this::word) and-set and (parser(this::word) or parser(this::integer))) use { Assignment(t1.text, t2.text) }
val constructorParser by (lpar and separatedTerms(assignmentParser, coma) and rpar) use { Constructor(t2) }
val nodeParser by (parser(this::integer) and-colon and parser(this::word) and constructorParser) use { Node(t1.text.toInt(), t2.text, t3 ) }
val graphParser by (graphLiteral and-parser(this::ws) and nodeParser) use { Graph(listOf(t2)) }
val expressionParser = (tileMapParser or graphParser)
val startParser by (startLiteral and expressionParser ) map { Start(it.t2) }
alphabetFile: missionAlphabet.xpr
val ruleParser by separatedTerms((-rule and parser(this::word) and set and expressionParser) use { Rule(t1.text, t3) }, parser(this::NEWLINE))
// regex lastprivatevalNEWLINE by regexToken("\n")
privateval integer by regexToken("\\d+")
privateval word by regexToken("\\w+")
privateval ws by regexToken("\\s+", ignore =true)
overrideval rootParser by (
versionParser *-NEWLINE*
alphabetParser *-NEWLINE*
startParser *-NEWLINE*
ruleParser
).map {
GGrammar(it.t1, it.t2, it.t3, it.t4)
}
}
funmain() {
val text =""" version: 0.5f alphabetFile: missionAlphabet.xpr start: GRAPH 0:Start(var=12, y=20) rule: StartBonus = GRAPH 1:entrance(x=2, y=3)""".trimIndent()
println(GrammarParser2.parseToEnd(text))
}
The text was updated successfully, but these errors were encountered:
Found out by myself for the constructor part, using optional and acceptZero = true
data classFunction(valname:String, valarguments:List<Argument>)
data classConstructor(valarguments:List<Argument>)
object test : Grammar<Function>() {
privateval lpar by literalToken("(")
privateval rpar by literalToken(")")
privateval set by literalToken("=")
privateval coma by literalToken(", ")
privateval variable by regexToken("\\w+")
privateval string by regexToken("\"\\w+\"")
privateval integer by regexToken("\\d+")
privateval 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->throwIllegalStateException("unkown type ${t3.type}")
})}
val constructorParser by (lpar and separatedTerms(assignmentParser, coma, acceptZero =true) and rpar) use { Constructor(t2) }
privateval functionParser by (variable and optional(constructorParser)) use { Function(t1.text, t2?.arguments ?: emptyList())}
overrideval rootParser by functionParser
}
funmain() {
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)"))
}
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: