Skip to content

Instantly share code, notes, and snippets.

@Jared314
Created December 7, 2011 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jared314/1443201 to your computer and use it in GitHub Desktop.
Save Jared314/1443201 to your computer and use it in GitHub Desktop.
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