Sha256: 0326099ca679a0fdc2677d1ea39d2ecf4567cbe801751d3e7e26257cebd82e2a

Contents?: true

Size: 1.78 KB

Versions: 13

Compression:

Stored size: 1.78 KB

Contents

grammar Calc
  # If "additive" were not already the first rule declared in this grammar, we
  # could use the following line to make it the root rule.
  #root additive

  rule additive
    (multitive_additive | multitive) {
      def value
        first.value
      end
    }
  end

  rule multitive_additive
    (multitive additive_op additive) {
      def value
        if additive_op == '+'
          multitive.value + additive.value
        else
          multitive.value - additive.value
        end
      end
    }
  end

  rule multitive
    (primary_multitive | primary) {
      def value
        first.value
      end
    }
  end

  rule primary_multitive
    (primary multitive_op multitive) {
      def value
        if multitive_op == '*'
          primary.value * multitive.value
        else
          primary.value / multitive.value
        end
      end
    }
  end

  rule primary
    (additive_paren | number) {
      def value
        first.value
      end
    }
  end

  rule additive_paren
    ('(' additive ')') {
      def value
        additive.value
      end
    }
  end

  rule additive_op
    (plus | minus) {
      def ==(other)
        text.strip == other
      end
    }
  end

  rule multitive_op
    (star | slash) {
      def ==(other)
        text.strip == other
      end
    }
  end

  rule number
    (float | integer) {
      def value
        first.value
      end
    }
  end

  rule float
    (integer '.' integer) {
      def value
        text.to_f
      end
    }
  end

  rule integer
    [0-9]+ {
      def value
        text.to_i
      end
    }
  end

  rule lparen  '(' space  end
  rule rparen  ')' space  end
  rule plus    '+' space  end
  rule minus   '-' space  end
  rule star    '*' space  end
  rule slash   '/' space  end

  rule space
    /[ \t\n\r]*/
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
citrus-2.2.2 test/_files/grammar3.citrus
citrus-2.2.1 test/_files/grammar3.citrus
citrus-2.2.0 test/_files/grammar3.citrus
citrus-2.1.2 test/_files/grammar3.citrus
citrus-2.1.1 test/_files/grammar3.citrus
citrus-2.0.1 test/_files/grammar3.citrus
citrus-2.0.0 test/_files/grammar3.citrus
citrus-1.8.0 test/_files/grammar3.citrus
citrus-1.7.0 test/_files/grammar3.citrus
citrus-1.6.0 test/_files/grammar3.citrus
citrus-1.5.2 test/_files/grammar3.citrus
citrus-1.5.1 test/_files/grammar3.citrus
citrus-1.5.0 test/_files/grammar3.citrus