Skip to content
Browse files

Adding binding for named atoms

  • Loading branch information...
1 parent 9befb96 commit 0fdd6a10f03b4f27b9dfb0598fe9aafc242b5031 @kschiess committed Aug 21, 2013
Showing with 20 additions and 1 deletion.
  1. +5 −0 lib/parslet/accelerator.rb
  2. +3 −1 lib/parslet/accelerator/engine.rb
  3. +12 −0 qed/accelerators.md
View
5 lib/parslet/accelerator.rb
@@ -67,6 +67,11 @@ def repeat min=0, max=nil
Expression.new(:rep, min, max, self)
end
+ # @return [Expression]
+ def as name
+ Expression.new(:as, name)
+ end
+
# @api private
# @return [Expression]
def join_or_new tag, other_expr
View
4 lib/parslet/accelerator/engine.rb
@@ -16,7 +16,9 @@ def visit_entity(name, block)
false
end
def visit_named(name, atom)
- false
+ match(:as) do |key|
+ @engine.try_bind(key, name)
+ end
end
def visit_repetition(tag, min, max, atom)
match(:rep) do |e_min, e_max, expr|
View
12 qed/accelerators.md
@@ -155,6 +155,18 @@ And then there is positive and negative lookahead.
Accelerator.match(
str('a').absent?,
A.str('a').absent?).assert != nil
+
+And named values.
+
+ Accelerator.match(
+ str('a').as(:a),
+ A.str('a').as(:a)).assert != nil
+
+Which we also want to be able to bind.
+
+ Accelerator.match(
+ str('a').as(:a),
+ A.str('a').as(:b)).assert == {:b => :a}
## Binding to Values

0 comments on commit 0fdd6a1

Please sign in to comment.
Something went wrong with that request. Please try again.