On Jul 2, 2013, at 2:44 AM, 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());
> locate can be implemented that way without impacting jparsec codebase:
> 
> public static <T extends Node> Parser<T> locate(Parser<T> parser) {
>     return Parsers.array(INDEX, parser, INDEX).map(new Map<Object[], T> () {
>         @SuppressWarnings("unchecked")
>         @Override
>         public T map(Object[] from) {
>             T obj = (T) from[1];
>             obj.setInfo(new SourceInfo((Integer) from[0], (Integer) from[2]));
>             return obj;
>         }});
> }

You are mostly likely aware of this. But here is FYI just in case. You could use Parsers.sequence() which will avoid the need of unchecked casting:

return Parsers.sequence(INDEX, parser, INDEX).map(new Map3<Integer, T, Integer, T>() {…});

Parsers.array() is probably a misfeature that shouldn't have been added in the first place. Parsers.list() should be preferred the same way List<T> should be preferred over arrays.
> My pull request becomes then irrelevant. Just maybe add Parsers.MODULE to fit my needs.
> 
> 
There is no other way to get the current index without a core library support. Module name, original source can both be implemented as Parser<String> if we want, but they weren't added because it's possible they can be obtained directly without relying on the framework.

To add them we need use cases where getting them directly at the call site of Parser.parse() is infeasible.
> —
> Reply to this email directly or view it on GitHub.
>