Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parslet Transform Mod
require 'parslet'
class Parslet::Transform
def transform_elt(elt, context) # :nodoc:
rules.each do |pattern, block|
if bindings=pattern.match(elt, context)
# Produces transformed value
return call_on_match({:node => elt}.merge(bindings), block)
end
end
# No rule matched - element is not transformed
return elt
end
end
class Parslet::Pattern
def element_match_hash(tree, exp, bindings)
# We iterate over expected pattern, since we demand that the keys that
# are there should be in tree as well.
exp.each do |expected_key, expected_value|
return false unless tree.has_key? expected_key
# Recurse into the value and stop early on failure
value = tree[expected_key]
return false unless element_match(value, expected_value, bindings)
end
return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment