Question

I'm creating a DSL using JParsec3. The parsing/linking is done in two phases:

  1. Parse the language into intermediate objects
  2. Link and translate the objects into executable objects

The objects created on stage 1 can refer to other objects that were not parsed yet, hence the 2 passes. Now, for linkage error messages, I need to send the user to the location in the file where they refer to a nonexistent item. For that, I need to attach location to the intermediate objects.

How do I do that?

Thanks!

Était-ce utile?

La solution

This appears to be a very popular topic as there has been a couple of PRs and issues related to this. Here is the latest https://github.com/abailly/jparsec/pull/16 that made it to release 2.1 (available in maven central). There was a lengthy discussion (https://github.com/abailly/jparsec/issues/5) with various solutions envisioned but we decided not to include the Locatable feature (yet?).

So basically, starting from 2.1 you can use withSource() combinator or the "old-fashioned" INDEX parsers that return parsed position in the stream. Here is a proposal from @fluentfuture:

Mapper.curry(LocationAnnotated.class).sequence(Parsers.INDEX, parser, Parsers.INDEX);

public class LocationAnnotated<T> {
   public LocationAnnotated(int begin, T value, int end) {...}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top