Skip to content

Commit

Permalink
Use \G to force Regexps to match only at the first position
Browse files Browse the repository at this point in the history
  • Loading branch information
cjheath committed Jul 17, 2009
1 parent ca6e58c commit 1cee044
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/treetop/compiler/node_classes/character_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CharacterClass < AtomicExpression
def compile(address, builder, parent_expression = nil)
super

builder.if__ "input.index(Regexp.new(#{single_quote(text_value)}), index) == index" do
builder.if__ "input.index(Regexp.new(#{grounded_regexp(text_value)}), index) == index" do
assign_result "instantiate_node(#{node_class_name},input, index...(index + 1))"
extend_result_with_inline_module
builder << "@index += 1"
Expand All @@ -14,6 +14,11 @@ def compile(address, builder, parent_expression = nil)
assign_result 'nil'
end
end

def grounded_regexp(string)
# Double any backslashes, then backslash any single-quotes:
"'\\G#{string.gsub(/\\/) { '\\\\' }.gsub(/'/) { "\\'"}}'"
end
end
end
end

0 comments on commit 1cee044

Please sign in to comment.