mutable input in pb2

40 views
Skip to first unread message

pa...@blackopsdev.com

unread,
Aug 2, 2015, 10:21:58 AM8/2/15
to parboiled2.org User List
I'm migrating a dynamic parser (the rule is generated dynamically) from pb1 to pb2.
It could be great to have a mutable ParserInput.
So, parser looks like:

class MyParser(format: Format) {
  def MainRule = ??? // genarating a rule based on the format

  def parse(input: String): ParsedDataStructure = {
    setInput(input) // mutation
    //parsing
    ....
  }
}

So it's created once, and pares multiple times.
As far as I understand, in pb2 each parsing procedure requires parser creation.
It's costly for my case. Is there any kind of work around for this problem?
Because (even when I'm trying to mutate the input) parser stops working since the 2nd line.

Thanks, Paul.

Mathias Doenitz

unread,
Aug 2, 2015, 2:05:49 PM8/2/15
to parboil...@googlegroups.com
Pavel,

one thing to remember:
pb2 generates the parser rules *at compile-time*!
So you cannot easily generate rules dynamically at runtime, unless you are willing to run actually run scalac at runtime and dynamically load the generated classes!

In pb1 rules are generated at runtime, so dynamic creation is not an issue.

Cheers,
Mathias

---
mat...@parboiled.org
http://www.parboiled.org
> --
> You received this message because you are subscribed to the Google Groups "parboiled2.org User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to parboiled-use...@googlegroups.com.
> Visit this group at http://groups.google.com/group/parboiled-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/db3f02a5-40c9-43d1-a566-60163bcf86ec%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

pa...@blackopsdev.com

unread,
Aug 2, 2015, 2:54:22 PM8/2/15
to parboiled2.org User List
Thank you for reply.
Well, actually, I wrote dynamic parser with pb2 and it works. And didn't call scalac at the runtime.
The only issue is - creating new MyParser object for each message.
Ok, I'll try to minimize the effort.

Alexander Myltsev

unread,
Aug 2, 2015, 2:55:59 PM8/2/15
to parboiled2.org User List
Pavel, 

could you share a code on how did you do that? 

Mathias Doenitz

unread,
Aug 2, 2015, 2:57:06 PM8/2/15
to parboil...@googlegroups.com
Pavel,

you can of course select one of several rule alternatives at run-time, yes.
I guess this is what you are doing.

However, this means that your selection code is run *every time* when the rule is executed.
What you can’t do (and what I meant) is what’s possible with pb1: run you “rule configuration” code once and then have only the configured rule be executed at parser run-time,
> To view this discussion on the web visit https://groups.google.com/d/msgid/parboiled-user/a3a8a1bb-f2ad-4286-9a05-bdfb67023deb%40googlegroups.com.

pa...@blackopsdev.com

unread,
Aug 2, 2015, 3:06:05 PM8/2/15
to parboiled2.org User List
// The same code as was shown above with more details


class MyParser(format: Format) extends Parser with ParserState with ParserRules {

  // Rule construction
  // Generating Rule0 from predefined Rule0s. Data extraction process works as side-effect.
  // Parser puts data to the hashmap (LogEntry -> ParserString)
  def MainRule: Rule0 = {
     def concat(r1: Rule0, r2: Rule0) = rule { r1 ~ r2 }
     val mainRule = format.entries.map(entryToRuleDispatcher).reduce(concat)

     rule {
        mainRule ~ EOI
     }
  }

  def entryToRuleDispatcher(logFormatEntry: LogFormatEntry): Rule0 =  ??? //

  def parse(input: String): ParsedDataStructure = {
    this.input = input) // mutation
    mainRule.run()

    val event = assembleParsedDataStructure()

    clearTheState()

    event
  }
}

pa...@blackopsdev.com

unread,
Aug 3, 2015, 10:35:22 PM8/3/15
to parboiled2.org User List
I was worrying for nothing. I moved the state out of Parboiled parser to made it lightweight.
And I'm creating a Parser for each line. I've never thought that it can be THAT fast.
It eliminates the problem.

Thank you such great tool!
Reply all
Reply to author
Forward
0 new messages