Sha256: 10f8c1c37e6ea946547fac7f5d18da899d06b314aac49c833238bca013070519

Contents?: true

Size: 1.39 KB

Versions: 15

Compression:

Stored size: 1.39 KB

Contents

module Unitwise
  module Expression
    class Parser < Parslet::Parser
      attr_reader :key
      def initialize(key=:primary_code)
        @key = key
      end

      root :expression

      rule (:atom) { Matcher.atom(key).as(:atom_code) }
      rule (:metric_atom) { Matcher.metric_atom(key).as(:atom_code) }
      rule (:prefix) { Matcher.prefix(key).as(:prefix_code) }

      rule (:simpleton) do
        (prefix.as(:prefix) >> metric_atom.as(:atom) | atom.as(:atom))
      end

      rule (:annotation) do
        str('{') >> match['^}'].repeat.as(:annotation) >> str('}')
      end

      rule (:digits) { match['0-9'].repeat(1) }

      rule (:integer) { (str('-').maybe >> digits).as(:integer) }

      rule (:fixnum) do
        (str('-').maybe >> digits >> str('.') >> digits).as(:fixnum)
      end

      rule (:number) { fixnum | integer }

      rule (:exponent) { integer.as(:exponent) }

      rule (:factor) { number.as(:factor) }

      rule (:operator) { (str('.') | str('/')).as(:operator) }

      rule (:term) do
        ((factor >> simpleton | simpleton | factor) >> exponent.maybe >> annotation.maybe).as(:term)
      end

      rule (:group) do
        (factor.maybe >> str('(') >> expression.as(:nested) >> str(')') >> exponent.maybe).as(:group)
      end

      rule (:expression) do
        ((group | term).as(:left)).maybe >> (operator >> expression.as(:right)).maybe
      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
unitwise-0.10.0 lib/unitwise/expression/parser.rb
unitwise-0.9.1 lib/unitwise/expression/parser.rb
unitwise-0.9.0 lib/unitwise/expression/parser.rb
unitwise-0.8.1 lib/unitwise/expression/parser.rb
unitwise-0.8.0 lib/unitwise/expression/parser.rb
unitwise-0.7.1 lib/unitwise/expression/parser.rb
unitwise-0.7.0 lib/unitwise/expression/parser.rb
unitwise-0.6.2 lib/unitwise/expression/parser.rb
unitwise-0.6.1 lib/unitwise/expression/parser.rb
unitwise-0.6.0 lib/unitwise/expression/parser.rb
unitwise-0.5.1 lib/unitwise/expression/parser.rb
unitwise-0.5.0 lib/unitwise/expression/parser.rb
unitwise-0.4.0 lib/unitwise/expression/parser.rb
unitwise-0.3.2 lib/unitwise/expression/parser.rb
unitwise-0.3.1 lib/unitwise/expression/parser.rb