mjackson / citrus

Parsing Expressions for Ruby
http://mjackson.github.io/citrus
405 stars 28 forks source link

Traceability of matches towards parsed source #40

Closed blambeau closed 12 years ago

blambeau commented 12 years ago

Hi!

Here is a bunch of commits that provide support for keeping traceability of the matches in the input source (text). This is typically useful for compiling chains where semantic passes need to provide feedback in terms of locations in the source text.

Roughly, with this pull request, Citrus supports a scenario like the one below + more. Commits are well commented to ease your review.

rule additive
  (factor operator:('+' | '-') space* term){
    location = [source, offset..(offset+length)]
    semantic_value = ...
    semantic_value.location = location
  }
end

# ...

path = Pathname.new("a/path/to/a/file")
sem_value = grammar.parse_file(path).value
sem_value.location
# [ path, 0..5 ]