Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created August 11, 2013 20:39
Show Gist options
  • Select an option

  • Save baroquebobcat/6206761 to your computer and use it in GitHub Desktop.

Select an option

Save baroquebobcat/6206761 to your computer and use it in GitHub Desktop.
For when you want to get the source for an expression
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