Retrieve not parsed input after run

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Retrieve not parsed input after run

lucas
Hi All,

First of all, I'd like to say that I really enjoy using parboiled for
writing my parsers.

I am facing the following problem : I have an input that I'd like to
parse, but actually I don't want to parse the entire input first. I'd
like to get the first parsed result along with the rest of the input
that was not parsed.
Concretely, I'd like to be able to write something like this (in scala):

  sealed trait Node
  case object A extends Node
  case object B extends Node
  case object C extends Node

  object AbcParser extends Parser {
    def abc: Rule1[Node] = rule {
      "a" ~ push(A) | "b" ~ push(B) | "c" ~ push(C)
    }
  }

  val input = "bac"

  val runner = ReportingParseRunner(AbcParser.abc)
  def parse(toParse: String) = {
    val result = runner.run(toParse)
    result.result match {
      case Some(r) =>
        println("parsed: " + r)
        // retrieve the rest of the input buffer that was not parsed and
return it
        ...
      case None =>
        throw new Exception(ErrorUtils.printParseErrors(result))
    }
  }

  // then the call
  parse(parse(parse(input)))
  // would yield
  // parsed: B
  // parsed: A
  // parsed: C

I didn't find a simple way to do this. What would be the best solution
in this case? Push the rest of the input onto the value stack? How can I
extract it? Is at possible at all?

Thanks in advance.

Lucas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieve not parsed input after run

mathias
Administrator
Lucas,

yes, there is a way to do what you are looking for.
Check out the `pushFromContext` action.

The parsing context gives you access the the current index position in the inputbuffer.
Using this you can extract the remaining characters.

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 26.09.2012, at 17:37, lucas [via parboiled users] wrote:

> Hi All,
>
> First of all, I'd like to say that I really enjoy using parboiled for
> writing my parsers.
>
> I am facing the current problem : I have an input that I'd like to
> parse, but actually I don't want to parse the entire input first. I'd
> like to get the first parsed result along with the rest of the input
> that was not parsed.
> Concretely, I'd like to be able to write something like this (in scala):
>
>   sealed trait Node
>   case object A extends Node
>   case object B extends Node
>   case object C extends Node
>
>   object AbcParser extends Parser {
>     def abc: Rule1[Node] = rule {
>       "a" ~ push(A) | "b" ~ push(B) | "c" ~ push(C)
>     }
>   }
>
>   val input = "bac"
>
>   val runner = ReportingParseRunner(AbcParser.abc)
>   def parse(toParse: String) = {
>     val result = runner.run(toParse)
>     result.result match {
>       case Some(r) =>
>         println("parsed: " + r)
>         // retrieve the rest of the input buffer that was not parsed and
> return it
>         ...
>       case None =>
>         throw new Exception(ErrorUtils.printParseErrors(result))
>     }
>   }
>
>   // then the call
>   parse(parse(parse(input)))
>   // would yield
>   // parsed: B
>   // parsed: A
>   // parsed: C
>
> I didn't find a simple way to do this. What would be the best solution
> in this case? Push the rest of the input onto the value stack? How can I
> extract it? Is at possible at all?
>
> Thanks in advance.
>
> Lucas
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Retrieve-not-parsed-input-after-run-tp4024072.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieve not parsed input after run

lucas
Thanks, I'll take a look at it!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieve not parsed input after run

lucas
In reply to this post by mathias
Thank you Mathias, it works really well!

If it is of any interest for anybody, I use parboiled to implement a complete TeX parser (which is available here : https://github.com/gnieh/toolxit-tex/blob/master/src/main/scala/toolxit/astex/parser/) and the parser must perform macro expansion (and also some primitive control sequence expansions) that may modify the way the rest of the input is parsed. That is why I need to parse each token individually.

Lucas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Retrieve not parsed input after run

mathias
Administrator
Cool!!
Interesting parsing application, thanks for sharing!

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

On 26.09.2012, at 22:39, lucas [via parboiled users] wrote:

> Thank you Mathias, it works really well!
>
> If it is of any interest for anybody, I use parboiled to implement a complete TeX parser (which is available here : https://github.com/gnieh/toolxit-tex/blob/master/src/main/scala/toolxit/astex/parser/) and the parser must perform macro expansion (and also some primitive control sequence expansions) that may modify the way the rest of the input is parsed. That is why I need to parse each token individually.
>
> Lucas
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/Retrieve-not-parsed-input-after-run-tp4024072p4024075.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Loading...