Created
August 11, 2013 20:39
-
-
Save baroquebobcat/6206761 to your computer and use it in GitHub Desktop.
For when you want to get the source for an expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RawAttribute < Parslet::Atoms::Base | |
| attr_reader :slice | |
| def initialize(slice) | |
| super() | |
| @slice = slice | |
| end | |
| def apply(source, context, consume_all) | |
| succ(@slice) | |
| end | |
| def to_s_inner(prec) | |
| "RawAttribute:#{slice}" | |
| end | |
| end | |
| def collect_raw_as name, &block | |
| any.present?.capture(:start) >> | |
| block.call >> | |
| dynamic {|s,c| | |
| old_pos = s.pos; s.pos = c.captures[:start].offset | |
| RawAttribute.new(s.consume(old_pos - s.pos)) | |
| }.as(name) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment