Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ You can use pure ruby regular expression with `*=` operator. You need to close r
Machete.matches?('"abcd"'.to_ast, 'StringLiteral<string *= /^[0-9]{2}aab/>') # => false
Machete.matches?('"19aabcd"'.to_ast, 'StringLiteral<string *= /^[0-9]{2}aab/>') # => true

Machete support also regexp with additional options (for example case sensitive).

Machete.matches?('"AB"'.to_ast, 'StringLiteral<string *= /ab/>') # => false
Machete.matches?('"AB"'.to_ast, 'StringLiteral<string *= /ab/i>') # => true

#### Array Attributes

When matching array attribute values, the simplest way is to specify the array elements exactly. They will be matched one-by-one.
Expand Down
21 changes: 19 additions & 2 deletions lib/machete/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ quantifier : "*" { result = [0, nil, 1] }
literal : SYMBOL { result = LiteralMatcher.new(symbol_value(val[0]).to_sym) }
| INTEGER { result = LiteralMatcher.new(integer_value(val[0])) }
| STRING { result = LiteralMatcher.new(string_value(val[0])) }
| REGEXP { result = LiteralMatcher.new(Regexp.new(regexp_value(val[0]))) }
| REGEXP { result = LiteralMatcher.new(regexp_value(val[0])) }
| TRUE { result = LiteralMatcher.new(true) }
| FALSE { result = LiteralMatcher.new(false) }
| NIL { result = LiteralMatcher.new(nil) }
Expand All @@ -152,6 +152,12 @@ any : ANY { result = AnyMatcher.new }

include Matchers

REGEXP_OPTIONS = {
'i' => ::Regexp::IGNORECASE,
'm' => ::Regexp::MULTILINE,
'x' => ::Regexp::EXTENDED
}

class SyntaxError < StandardError; end

def parse(input)
Expand Down Expand Up @@ -208,7 +214,17 @@ def symbol_value(value)
end

def regexp_value(value)
value.to_s[1...-1]
/\A\/(.*)\/([^\/]*)\z/u =~ value
content = $1
inline_options = $2
options = nil

if inline_options
options = inline_options.split(//).map { |opt| REGEXP_OPTIONS[opt] }
options = options.inject(&:+)
end

Regexp.new(content, options)
end

# "^" needs to be here because if it were among operators recognized by
Expand Down Expand Up @@ -303,6 +319,7 @@ COMPLEX_TOKENS = [
[^\/] # regular character
)*
\/
[imx]*
/x
],
# ANY, EVEN and ODD need to be before METHOD_NAME, otherwise they would be
Expand Down
20 changes: 20 additions & 0 deletions spec/machete/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ def array_matcher_with_quantifier(min, max, step)
/abcd/
))
)
'Foo<a *= /abcd/i>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(
/abcd/i
)))
'Foo<a *= /abcd/m>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(
/abcd/m
)))
'Foo<a *= /abcd/x>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(
/abcd/x
)))
'Foo<a *= /abcd/ix>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(
/abcd/ix
)))
'Foo<a *= /abcd/imx>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(
/abcd/imx
)))
'Foo<a *= /a/, b *= /b/>'.should be_parsed_as(
NodeMatcher.new(:Foo, :a => RegexpMatcher.new(/a/),
:b => RegexpMatcher.new(/b/)
Expand Down