Sha256: 40f731e59078b757a566bf8206103719862a32fdba5a57533c94db2804bcf633

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

#
# This is a Citrus grammar for boolean expressions.
#
# The parser is automatically loaded under the BoolExpr::Parser constant by Citrus
# itself. It is also automatically registered under BoolExpr::Grammar.parser by
# `Sexpr.load` when invoked on the bool_expr.sexp.yml file.
#
# The coupon codes returns s-expressions that correctly refer to the abstract grammar
# definition in that file (AST).
#
grammar BoolExpr::Parser

  rule bool_expr
    bool_or
  end

  rule bool_or
      (l:bool_and spaces 'or' spaces r:bool_or){
        [:bool_or, l.value, r.value]
      }
    | bool_and
  end

  rule bool_and
      (l:bool_not spaces 'and' spaces r:bool_and){
        [:bool_and, l.value, r.value]
      }
    | bool_not
  end

  rule bool_not
      ('not' spacing e:bool_not){
        [:bool_not, e.value]
      }
    | bool_term
  end

  rule bool_term
    bool_paren | bool_lit | var_ref
  end

  rule bool_paren
    ('(' spacing e:bool_or spacing ')'){
      e.value
    }
  end

  rule bool_lit
    ("true" | "false"){ 
      [:bool_lit, ::Kernel.eval(strip) ]
    }
  end

  rule var_ref
    (!(keyword spacing) [a-z]+){
      [:var_ref, strip]
    }
  end

  rule spacing
    [ \t]+ | &'(' | !.
  end

  rule spaces
    [ \t]+
  end
  
  rule keyword
    "true" | "false" | "or" | "and" | "not"
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sexpr-0.3.0 examples/bool_expr/bool_expr.citrus