Wondering if there is a better way to represent the following #235
-
After hitting a stackoverflow trying to use Is there a better way to represent this? //> using dep com.github.j-mie6::parsley:4.5.2
//> using dep "com.lihaoyi::pprint:0.9.0"
import parsley.Parsley.{atomic, many, some}
import parsley.character.{endOfLine, letter, hexDigit, oneOf}
import parsley.syntax.character.{charLift, stringLift}
import parsley.character
import parsley.expr.chain
// LANGTAG ::= '@' [a-zA-Z]+ ('-' [a-zA-Z0-9]+)*
val asciiLowerChar = oneOf('a' to 'z')
val asciiUpperChar = oneOf('A' to 'Z')
val asciiChar = asciiLowerChar | asciiUpperChar
val digit = oneOf('0' to '9')
val alphaNumeric = asciiChar | digit
val start = atomic('@' ~> some(asciiChar).map(_.mkString("@", "", "")))
lazy val tail = {
val identifier = some(alphaNumeric).map(_.mkString)
val rest = chain.left1(identifier, '-' as (_ + '-' + _))
rest
}
val langtag = (start <~> ('-' *> tail)).map(_ + '-' + _) | start
pprint.pprintln(start.parse("@en"))
pprint.pprintln(tail.parse("GB-01-oxendict-1997"))
pprint.pprintln(langtag.parse("@en-GB01-oxendict-1997")) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
You were hitting a stack overflow with as an aside, your |
Beta Was this translation helpful? Give feedback.
-
Original was: val LANGTAG = {
val asciiLowerChar = oneOf('a' to 'z')
val asciiUpperChar = oneOf('A' to 'Z')
val asciiChar = asciiLowerChar | asciiUpperChar
val digit = oneOf('0' to '9')
val start = '@' ~> some(asciiChar).map(_.mkString("@", "", ""))
val tail =
many('-' ~> some(asciiChar).map(_.mkString("-", "", "")))
.foldLeft("")(_ + _)
val langtag = (start <~> tail).map(_ + _)
langtag
} |
Beta Was this translation helpful? Give feedback.
-
Using 0.4.5.2 |
Beta Was this translation helpful? Give feedback.
Ahhhh wait no, I see exactly what it is, the debugger was reporting something a bit unusually (good test case for the traces!)
you are using the
.foldLeft
combinator on amany
, whereas you meant.map(_.foldLeft...)
on the list.many
can succeed having not consumed input, which makesfoldLeft
loop forever (I should have figured this out when I saw the original parent name wasinfix.left1
, which is the combinator used to implementfoldLeft
).again, what I'd do for this is to write: