Skip to content

Commit

Permalink
Merge pull request #6 from rctay/rc/opt-ci
Browse files Browse the repository at this point in the history
support optional, case-insensitive strings
  • Loading branch information
jcoglan committed Jun 29, 2015
2 parents 97ec0e7 + 6822ffd commit dd956dd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion source/canopy/builders/javascript.js
Expand Up @@ -342,7 +342,8 @@
},

stringMatchCI_: function(expression, string) {
return expression + '.toLowerCase() === ' + this._quote(string) + '.toLowerCase()';
return expression + ' !== null && ' +
expression + '.toLowerCase() === ' + this._quote(string) + '.toLowerCase()';
},

regexMatch_: function(regex, string) {
Expand Down
3 changes: 2 additions & 1 deletion source/canopy/builders/python.js
Expand Up @@ -315,7 +315,8 @@
},

stringMatchCI_: function(expression, string) {
return expression + '.lower() == ' + this._quote(string) + '.lower()';
return expression + ' is not None and ' +
expression + '.lower() == ' + this._quote(string) + '.lower()';
},

regexMatch_: function(regex, string) {
Expand Down
3 changes: 2 additions & 1 deletion source/canopy/builders/ruby.js
Expand Up @@ -332,7 +332,8 @@
},

stringMatchCI_: function(expression, string) {
return expression + '.downcase == ' + this._quote(string) + '.downcase';
return '!' + expression + '.nil? && ' +
expression + '.downcase == ' + this._quote(string) + '.downcase';
},

regexMatch_: function(regex, string) {
Expand Down
16 changes: 16 additions & 0 deletions spec/canopy/compiler/string_spec.js
Expand Up @@ -46,4 +46,20 @@ function() { with(this) {
assertParse( ['FOO', 0, []], CIStringTest.parse('FOO') )
}})
}})

describe('optional case-insensitive strings', function() { with(this) {
before(function() { with(this) {
Canopy.compile('grammar JS.ENV.CIStringTest \
root <- string1 string2? \
string1 <- "foo" \
string2 <- `bar`')
}})

it('parses when absent', function() { with(this) {
assertParse( ['foo', 0, []
['foo', 0, []],
['', 3, []]],
CIStringTest.parse('foo') )
}})
}})
}})

0 comments on commit dd956dd

Please sign in to comment.