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 8902421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:36:33+00:00 2026-06-15T01:36:33+00:00

I have a file containing a text representation of an object. I have written

  • 0

I have a file containing a text representation of an object. I have written a combinator parser grammar that parses the text and returns the object. In the text, “#” is a comment delimiter: everything from that character to the end of the line is ignored. Blank lines are also ignored. I want to process text one line at a time, so that I can handle very large files.

I don’t want to clutter up my parser grammar with generic comment and blank line logic. I’d like to remove these as a preprocessing step. Converting the file to an iterator over line I can do something like this:

Source.fromFile("file.txt").getLines.map(_.replaceAll("#.*", "").trim).filter(!_.isEmpty)

How can I pass the output of an expression like that into a combinator parser? I can’t figure out how to create a Reader object out of a filtered expression like this. The Java FileReader interface doesn’t work that way.

Is there a way to do this, or should I put my comment and blank line logic in the parser grammar? If the latter, is there some util.parsing package that already does this for me?

  • 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-15T01:36:34+00:00Added an answer on June 15, 2026 at 1:36 am

    The simplest way to do this is to use the fromLines method on PagedSeq:

    import scala.collection.immutable.PagedSeq
    import scala.io.Source
    import scala.util.parsing.input.PagedSeqReader
    
    val lines = Source.fromFile("file.txt").getLines.map(
      _.replaceAll("#.*", "").trim
    ).filterNot(_.isEmpty)
    
    val reader = new PagedSeqReader(PagedSeq.fromLines(lines))
    

    And now you’ve got a scala.util.parsing.input.Reader that you can plug into your parser. This is essentially what happens when you parse a java.io.Reader, anyway—it immediately gets wrapped in a PagedSeqReader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have an XML file containing some scientific text that I want to display
I have a .html file containing text content like: <div> The study concludes that
I have a text file containing the output of a recursive directory listing that
I have a text file containing the row numbers of the rows that should
I have a javascript file that is reading a text file containing cartesian coordinates.
I have a text file containing strings to encrypt. These strings are indicated by
I have a text file containing 5 columns of data. The first column contains
I have a text file containing the data in following format 12345 Abdt3 hy45d
I have a text file containing unwanted null characters (ASCII NUL, \0 ). When

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.