Sha256: 2d6c360ac6050204b682f1e7b282ed47f13b5e53c5cf6e37b0cd3cb70601ed39

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

#
#       ActiveFacts CQL Parser.
#       Parse rules relating to Expressions
#
# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
#
module ActiveFacts
  module CQL
    grammar Expressions
      rule comparison
        e1:expression s
	comparator s e2:expression
        {
	  def ast
	    Compiler::Comparison.new comparator.text_value, e1.ast, e2.ast
	  end
        }
      end

      rule comparator
        '<=' / '<' / '=' / '>=' / '>'
        # REVISIT: These words occur in readings, find alternates:
        # / matches / includes
      end

      rule expression
        sum
      end

      rule sum
        t0:product s tail:( op:add_op s t1:product s )*
        {
	  def ast
	    Compiler::Sum.new(t0.ast, *tail.elements.map{|e| e.op.text_value == '-' ? Compiler::Negate.new(e.t1.ast) : e.t1.ast})
	  end
        }
      end

      rule add_op
        '+' / '-'
      end

      rule product
        f0:factor s tail:( op:mul_op s f1:factor s )*
        {
	  def ast
	    Compiler::Product.new(f0.ast, *tail.elements.map{|e| e.op.text_value != '*' ? Compiler::DivideBy.new(e.op.text_value, e.f1.ast) : e.t1.ast})
	  end
        }
      end

      rule factor
        literal u:unit? s
            {
	      def ast
		Compiler::Literal.new(literal.value, u.empty? ? nil : u.text_value)
	      end
            }
        / derived_variable
        / !context_note '(' s sum s ')' s                   { def ast; sum.ast; end }
      end

      rule derived_variable
        variable:term s p:function_call*
        {
	  def ast
	    Compiler::FunctionCallChain.new(term.ast, *p.elements.each{|f| f.ast})
	  end
        }
      end

      rule function_call
        '.' s func:id s param_list:( '(' s params:( p0:expression s tail:( ',' s p1:expression s )* )? ')' s )?
        {
	  def ast
	    Compiler::FunctionCall.new(func.value, *param_list.elements.map{|param| param.ast })
	  end
        }
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activefacts-0.8.9 lib/activefacts/cql/Expressions.treetop
activefacts-0.8.8 lib/activefacts/cql/Expressions.treetop