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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:21:41+00:00 2026-06-17T05:21:41+00:00

I’m trying to use regex in my StandardTokenParsers based parser. For that, I’ve subclassed

  • 0

I’m trying to use regex in my StandardTokenParsers based parser. For that, I’ve subclassed StdLexical as follows:

class CustomLexical extends StdLexical{
  def regex(r: Regex): Parser[String] = new Parser[String] {
    def apply(in:Input) = r.findPrefixMatchOf(in.source.subSequence(in.offset, in.source.length)) match {
      case Some(matched) => Success(in.source.subSequence(in.offset, in.offset + matched.end).toString,
        in.drop(matched.end))
      case None => Failure("string matching regex `" + r + "' expected but " + in.first + " found", in)
    }
  }

  override def token: Parser[Token] =
    (   regex("[a-zA-Z]:\\\\[\\w\\\\?]* | /[\\w/]*".r)     ^^ { StringLit(_) }
      | identChar ~ rep( identChar | digit )               ^^ { case first ~ rest => processIdent(first :: rest mkString "") }
      | ...

But I’m a little confused on how I would define a Parser that takes advantage of this. I have a parser defined as:

def mTargetFolder: Parser[String] = "TargetFolder" ~> "=" ~> mFilePath

which should be used to identify valid file paths. I tried then:

def mFilePath: Parser[String] = "[a-zA-Z]:\\\\[\\w\\\\?]* | /[\\w/]*".r

But this is obviously not right. I get an error:

scala: type mismatch;
 found   : scala.util.matching.Regex
 required: McfpDSL.this.Parser[String]
    def mFilePath: Parser[String] = "[a-zA-Z]:\\\\[\\w\\\\?]* | /[\\w/]*".r
                                                                          ^

What is the proper way of using the extension made on my StdLexical subclass?

  • 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-17T05:21:42+00:00Added an answer on June 17, 2026 at 5:21 am

    If you really want to use token based parsing, and reuse StdLexical, I would advise to update the syntax for “TargetFolder” so that the value after the equal sign is a proper string literal. Or in other words, make it so the path should be enclosed with quotes. From that point you don’t need to extends StdLexical anymore.

    Then comes the problem of converting a regexp to a parser. Scala already has RegexParsers for this (which implicitly converts a regexp to a Parser[String]), but unfortunately that’s not what you want here because it works on streams of Char (type Elem = Char in RegexParsers) while you are working on a sttream of tokens.
    So we will indeed have to define our own conversion from Regex to Parser[String] (but at the syntactic level rather than lexical level, or in other words in the token parser).

    import scala.util.parsing.combinator.syntactical._
    import scala.util.matching.Regex
    import scala.util.parsing.input._
    
    object MyParser extends StandardTokenParsers {
      import lexical.StringLit
      def regexStringLit(r: Regex): Parser[String] = acceptMatch( 
        "string literal matching regex " + r, 
        { case StringLit( s ) if r.unapplySeq(s).isDefined => s }
      )
      lexical.delimiters += "="
      lexical.reserved += "TargetFolder"
      lazy val mTargetFolder: Parser[String] = "TargetFolder" ~> "=" ~> mFilePath
      lazy val mFilePath: Parser[String] = regexStringLit("([a-zA-Z]:\\\\[\\w\\\\?]*)|(/[\\w/]*)".r)  
      def parseTargetFolder( s: String ) = { mTargetFolder( new lexical.Scanner( s ) ) }
    }
    

    Example:

    scala> MyParser.parseTargetFolder("""TargetFolder = "c:\Dir1\Dir2" """)
    res12: MyParser.ParseResult[String] = [1.31] parsed: c:\Dir1\Dir2
    
    scala> MyParser.parseTargetFolder("""TargetFolder = "/Dir1/Dir2" """)
    res13: MyParser.ParseResult[String] = [1.29] parsed: /Dir1/Dir2
    
    scala> MyParser.parseTargetFolder("""TargetFolder = "Hello world" """)
    res14: MyParser.ParseResult[String] =
    [1.16] failure: identifier matching regex ([a-zA-Z]:\\[\w\\?]*)|(/[\w/]*) expected
    TargetFolder = "Hello world"
               ^
    

    Note that also fixed your “target folder” regexp here, you had missing parens around the two alternative, plus unneeded spaces.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to understand how to use SyndicationItem to display feed which is
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.