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
Let's say I have a String containing numbers that I want to convert into a data class. The fields are of fixed width. How should I go about building a grammar that can achieve this without having the different tokens cause issues with each other?
At the moment I'm seeing a MismatchedToken exception.
Reproducible sample case
data classLine(
valfieldA:Int,
valfieldB:Int,
valfieldC:Int
)
object LineGrammar : Grammar<Line>() {
privateval fieldA by token("[0-9]{2}")
privateval fieldB by token("[0-9]{5}")
privateval fieldC by token("[0-9]{3}")
privateval line =
fieldA and fieldB and fieldC
overrideval rootParser:Parser<Line> =
line.map { (a, b, c) ->Line(
fieldA = a.text.toInt(),
fieldB = b.text.toInt(),
fieldC = c.text.toInt()
)
}
}
val line ="1234567891"val result =LineGrammar.parseToEnd(line)
// result should be Line(fieldA = 12, fieldB = 34567, fieldC = 891)//// Instead seeing the following exception// Could not parse input: MismatchedToken(expected=fieldB ([0-9]{5}), found=fieldA for "34" at 2 (1:3))// com.github.h0tk3y.betterParse.parser.ParseException: Could not parse input: MismatchedToken(expected=fieldB ([0-9]{5}), found=fieldA for "34" at 2 (1:3))// at com.github.h0tk3y.betterParse.parser.ParserKt.toParsedOrThrow(Parser.kt:70)// at com.github.h0tk3y.betterParse.parser.ParserKt.parseToEnd(Parser.kt:30)// at com.github.h0tk3y.betterParse.grammar.GrammarKt.parseToEnd(Grammar.kt:69)
The text was updated successfully, but these errors were encountered:
Let's say I have a
String
containing numbers that I want to convert into adata class
. The fields are of fixed width. How should I go about building a grammar that can achieve this without having the different tokens cause issues with each other?At the moment I'm seeing a
MismatchedToken
exception.Reproducible sample case
The text was updated successfully, but these errors were encountered: