On Jul 1, 2013, at 3:15 PM, Xikilze <notifications@github.com> wrote:

> In src/main/java/org/codehaus/jparsec/LocatableParser2.java:
> 
> > +	  private final Parser<T> parser;
> > +	  private LocatableHandler<U> handler;
> > +
> > +	  public LocatableParser2(Parser<T> parser, LocatableHandler<U> handler) {
> > +	    this.parser = parser;
> > +	    this.handler = handler;
> > +	  }
> > +
> > +	  @SuppressWarnings("unchecked")
> > +	  @Override boolean apply(ParseContext ctxt) {
> > +	    int begin = ctxt.getIndex();
> > +	    if (!parser.apply(ctxt)) {
> > +	      return false;
> > +	    }
> > +
> > +	    handler.handle((U) ctxt.result, ctxt.source.subSequence(begin, ctxt.getIndex()).toString(), begin, ctxt.getIndex());
> Hello As I said before I know the solution is not the best one, and an interceptor that can apply to all parsers could me much better. I might try an implementation with Parser.run when I find some free time. At least I will do it next time I have to implement a new parser. I don't see how this feature could be doable without framework change. How can you get the index and the module without accessing to ParseContext ? This implies creating an

By "module", do you mean the substring that's parsed into the current result?

Yes, you can't get that without having access to the raw source string.

I just got the impression from earlier conversation that the substring isn't necessarily useful and the [begin, end) indices are good enough. Maybe I misread?

Anyway, I said it's doable only meaning getting the indices is doable. As you can see there is already Parsers.INDEX which is a Parser<Integer> that returns the current index.

So sequence(INDEX, parser, INDEX).map(…) will do almost everything locate() does except that it has no source substring. In order to get the substring, you have to have the raw source:

sequence(INDEX, parser, INDEX).map({begin, v, end ->
    return new LocationAware<V>(begin, v, end);
});


> subclass of Parser, that is not possible outside of the framework. My initial implementation of LocatableParser was outside of the core framework, but I had to change the visibility of some classes and methods. Sylvain Le 01/07/13 18:56, fluentfuture a écrit :
> …
> —
> Reply to this email directly or view it on GitHub.
>