Sha256: 5acad3943e8fafa133e287aa159ed263a221c350c2e1cbc5b564381450557f8a

Contents?: true

Size: 1.28 KB

Versions: 4

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.sexpr, r.sexpr]
      }
    | bool_and
  end

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

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

  rule bool_term
    bool_paren | bool_lit | var_ref
  end

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

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

  rule var_ref
    (!(keyword (spaces | !.)) [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

4 entries across 4 versions & 1 rubygems

Version Path
sexpr-0.6.0 examples/bool_expr/bool_expr.citrus
sexpr-0.5.1 examples/bool_expr/bool_expr.citrus
sexpr-0.5.0 examples/bool_expr/bool_expr.citrus
sexpr-0.4.0 examples/bool_expr/bool_expr.citrus