Last active
February 28, 2016 16:03
-
-
Save CalebFenton/ea4ad22c9661bc7aacf4 to your computer and use it in GitHub Desktop.
Boolean expression evaluation grammar for Citrus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'citrus' | |
Citrus.load 'boolean' | |
m = Boolean.parse 'false or true' | |
puts m.value # > true | |
m = Boolean.parse 'true and (!true && True) || FALSE' | |
puts m.value # > false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A grammar for boolean expressions. This grammar should provide a similiar interpretation | |
# of boolean expressions as Ruby. | |
grammar Boolean | |
## Hierarchical syntax | |
rule term | |
oritive | factor | |
end | |
rule oritive | |
(factor operator:(/or/i | '||') space* term) { | |
capture(:factor).value || capture(:term).value | |
} | |
end | |
rule factor | |
anditive | prefix | |
end | |
rule anditive | |
(prefix operator:(/and/i | '&&') space* factor) { | |
capture(:prefix).value && capture(:factor).value | |
} | |
end | |
rule prefix | |
prefixed | primary | |
end | |
rule prefixed | |
(operator:(/not/i | '!') space* prefix) { | |
!capture(:prefix).value | |
} | |
end | |
rule primary | |
group | boolean | |
end | |
rule group | |
(lparen term rparen) { | |
capture(:term).value | |
} | |
end | |
## Lexical syntax | |
rule boolean | |
(bool_value space*) { | |
to_str.rstrip.downcase.eql?('true') ? true : false | |
} | |
end | |
rule bool_value | |
/true/i | /false/i | |
end | |
rule lparen '(' space* end | |
rule rparen ')' space* end | |
rule space [ \t\n\r] end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment