Skip to content

Instantly share code, notes, and snippets.

@CalebFenton
Last active February 28, 2016 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CalebFenton/ea4ad22c9661bc7aacf4 to your computer and use it in GitHub Desktop.
Save CalebFenton/ea4ad22c9661bc7aacf4 to your computer and use it in GitHub Desktop.
Boolean expression evaluation grammar for Citrus
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
# 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