Last active
February 3, 2026 20:19
-
-
Save dacr/6d9b015b56a584e35beab6df22f57a43 to your computer and use it in GitHub Desktop.
cats-parse graph query language experiment - hello world 4 / published by https://github.com/dacr/code-examples-manager #fc484393-ce97-44b6-bf6c-8f8addbf6a41/b01a03190dcf4ae4e4f6ee9f7fe8dc02489d6fc5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : cats-parse graph query language experiment - hello world 4 | |
| // keywords : scala, typelevel, cats-parse, hello-world, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : fc484393-ce97-44b6-bf6c-8f8addbf6a41 | |
| // created-on : 2025-03-13T14:22:01+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 whitespaces = Parser.charIn(" \t\n\r").void.rep.void | |
| extension [A](p: Parser[A]) { | |
| def token: Parser[A] = p <* (whitespaces|Parser.end) | |
| } | |
| val helloToken = Parser.ignoreCase("hello").token | |
| val helloParser: Parser0[Option[Who]] = for { | |
| _ <- helloToken | |
| name <- alpha.rep.string.? <* whitespaces.? | |
| } yield name.map(Who.apply) | |
| val results = List( | |
| "hello david", | |
| "hello joe ", | |
| "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