Skip to content

Instantly share code, notes, and snippets.

@dacr
Created March 28, 2025 09:01
Show Gist options
  • Save dacr/b0db75bc4382f3c71263ff83a7131612 to your computer and use it in GitHub Desktop.
Save dacr/b0db75bc4382f3c71263ff83a7131612 to your computer and use it in GitHub Desktop.
cats-parse graph query language experiment - hello world 2 / published by https://github.com/dacr/code-examples-manager #896d5781-e750-48af-802b-6fd6a109ecb5/d56fa8c55e79249316651fc657757db5967773f5
// summary : cats-parse graph query language experiment - hello world 2
// keywords : scala, typelevel, cats-parse, hello-world, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 896d5781-e750-48af-802b-6fd6a109ecb5
// created-on : 2025-03-13T14:11:45+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.6.4"
//> using dep "org.typelevel::cats-parse:1.1.0"
//> using dep "com.lihaoyi::pprint:0.9.0"
// ---------------------
import cats.parse.{Parser0,Parser}
import cats.parse.Rfc5234.{lwsp, alpha}
case class Who(name: String)
val helloToken = Parser.ignoreCase("hello").surroundedBy(lwsp)
val helloParser: Parser0[Option[Who]] =
(lwsp *> helloToken *> lwsp *> alpha.rep.string.? <* lwsp)
.map(name => name.map(Who.apply))
val results = List(
"hello david",
" hello joe ",
" hello ",
"hello"
).map(s => helloParser.parseAll(s))
pprint.pprintln(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment