Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:32:07+00:00 2026-06-01T09:32:07+00:00

I really like parser combinators but I’m not happy with the solution I’ve come

  • 0

I really like parser combinators but I’m not happy with the solution I’ve come up with to extract data when I don’t care about the text before the relevant text.

Consider this small parser to get monetary amounts:

import scala.util.parsing.combinator._

case class Amount(number: Double, currency: String)

object MyParser extends JavaTokenParsers {
  def number = floatingPointNumber ^^ (_.toDouble)
  def currency = """\w+""".r ^? ({
    case "USD" => "USD"
    case "EUR" => "EUR"
  }, "Unknown currency code: " + _)

  def amount = (number ~ currency) ^^ {
    case num ~ curr => Amount(num, curr)
  } | currency ~ number ^^ {
    case curr ~ num => Amount(num, curr)
  }

  def junk = """\S+""".r
  def amountNested: Parser[Any] = amount | junk ~> amountNested
}

As you can see, I can get Amounts back easily if I give the parser a string that begins with valid data:

scala> MyParser.parse(MyParser.amount, "101.41 EUR")
res7: MyParser.ParseResult[Amount] = [1.11] parsed: Amount(101.41,EUR)

scala> MyParser.parse(MyParser.amount, "EUR 102.13")
res8: MyParser.ParseResult[Amount] = [1.11] parsed: Amount(102.13,EUR)

However, it fails when there is non-matching text before it:

scala> MyParser.parse(MyParser.amount, "I have 101.41 EUR")
res9: MyParser.ParseResult[Amount] = 
[1.2] failure: Unknown currency code: I

I have 101.41 EUR
 ^

My solution is the amountNested parser, in which it recursively tries to find an Amount. This works but it gives a ParseResult[Any]:

scala> MyParser.parse(MyParser.amountNested, "I have 101.41 EUR")
res10: MyParser.ParseResult[Any] = [1.18] parsed: Amount(101.41,EUR)

This loss of type information (which can be ‘retrieved’ using pattern matching, of course) seems unfortunately because any success will contain an Amount.

Is there a way to keep searching my input ("I have 101.41 EUR") until I have a match or not but without having a Parser[Any]?

Looking at the ScalaDocs it seems like the * method on Parser might help but all I get are failures or infinite loops when I try things like:

def amount2 = ("""\S+""".r *) ~> amount
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:32:08+00:00Added an answer on June 1, 2026 at 9:32 am

    It typechecks all right if you declare your amountNested to be Parser[Amount] :

    def amountNested: Parser[Amount] = amount | junk ~> amountNested
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really like Xml for saving data, but when does sqlite/database become the better
I'd like to extract a date from any string entered. But not specific to
I really like DevX components, but they are pretty expensive, maybe anyone knows free
I really like how Consolas looks with ClearType enabled but unfortunately it makes everything
This seems like a really simple question; but I can't see how it's actually
I'm fairly new to Scala and while reading about parser combinators( The Magic Behind
I really like the fact that Microsoft has taken a commitment to bring MVC
I really like the way gmail has the archive system implemented. I can archive
I really like the format and type of links from RubyFlow for Ruby related
I really like the Mako templating system that's used in Pylons and a couple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.