Sha256: 25cce76e5a8566a47dc45084abe3234c27ce9427f200173a59885328359afffa

Contents?: true

Size: 1.96 KB

Versions: 9

Compression:

Stored size: 1.96 KB

Contents

module CEML
grammar Lexer

    rule text
      (!eol .)+
    end

    rule nl
      (ws? comment? eol)+
    end

    rule ws
      [ \t\f]+
    end

    rule comment
      '//' text
    end

    rule eol
      ("\r" "\n"?) / "\n"
    end

    rule number
      ([1-9]+ [0-9]* / '0') {
          def value
            text_value.to_i
          end
      }
    end

    rule and
        (ws? ',')? ws 'and' ws / ws? ',' ws?
    end

    rule reserved_word
        'and' / 'within' / 'over'
    end

    rule id
      !(reserved_word ws) [a-zA-Z_]+
    end

    rule duration
      number ws? time_unit:('seconds' / 'second' / 's' / 'minutes' / 'minute' / 'min' / 'hours' / 'hour' / 'hr' / 'h')
      {
          def seconds
              number.text_value.to_f * case time_unit.text_value
              when /^h/; 60*60; when /^mi/; 60; else 1; end
          end
      }
    end

    rule distance
      number ws? distance_unit:('miles' / 'mile' / 'mi' / 'km' / 'kilometers' / 'k' / 'meters' / 'm' / 'ft' / 'feet' / 'f' / 'blocks' / 'block')
      {
        def meters
          number.text_value.to_f * case distance_unit.text_value
          when /^mi/; 1600; when /^k/;  1000; when /^m/;  1;
          when /^f/;  0.35; when /^b/;  200; else 1; end
        end
      }
    end

    rule range
      (
          min:number '-' max:number / floor:number '+'? /
          'an' / 'a' / 'one' / 'the' / 'some' / 'several' / 'many'
      ) {
          def value
            case text_value
            when 'an', 'a', 'one', 'the' then (1..1)
            when 'some'                  then (1..10000)
            when 'several'               then (3..10000)
            when 'many'                  then (5..10000)
            when /^\d+\+$/               then (floor.value..10000)
            when /^\d+\-\d+$/            then (min.value..max.value)
            when /^\d+$/                 then (floor.value..floor.value)
            else raise 'hell'
            end
          end
      }
    end

end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ceml-0.7.3 lib/ceml/tt/lexer.treetop
ceml-0.7.2 lib/ceml/tt/lexer.treetop
ceml-0.7.1 lib/ceml/tt/lexer.treetop
ceml-0.7.0 lib/ceml/tt/lexer.treetop
ceml-0.6.4 lib/ceml/tt/lexer.treetop
ceml-0.6.3 lib/ceml/tt/lexer.treetop
ceml-0.6.2 lib/ceml/tt/lexer.treetop
ceml-0.6.1 lib/ceml/tt/lexer.treetop
ceml-0.6.0 lib/ceml/tt/lexer.treetop