File: query.rb

package info (click to toggle)
mhc 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,320 kB
  • ctags: 3,529
  • sloc: ruby: 12,404; lisp: 7,448; makefile: 70; sh: 69
file content (210 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module Mhc
  class Query
    def initialize(query_string)
      @expression = Expression.new(Context.new(query_string))
      @query_string = query_string
    end

    def to_proc
      return @expression.to_proc
    end

    def to_s
      @query_string.to_s
    end

    class ParseError < StandardError; end

    #
    # Expression :: Term ('|' Term)*
    #
    class Expression
      def initialize(context)
        @terms = [Term.new(context)]
        @terms << Term.new(context) while context.eat_if(:orop)
      end

      def to_proc
        @procs = @terms.map(&:to_proc)
        return lambda {|ev| @procs.any? {|p| p.call(ev)}}
      end
    end # class Expression

    #
    # Term :: Factor ('&' Factor)*
    #
    class Term
      def initialize(context)
        @factors = [Factor.new(context)]
        @factors << Factor.new(context) while context.eat_if(:andop)
      end

      def to_proc
        @procs = @factors.map(&:to_proc)
        return lambda {|ev| @procs.all? {|p| p.call(ev)}}
      end
    end # class Term

    #
    # Factor :: '!'* ( '(' Expression ')' || RelationalExpression )
    #
    class Factor
      def initialize(context)
        @expected_value = true
        @expected_value = !@expected_value while context.eat_if(:negop)

        if context.eat_if(:lparen)
          @value = Expression.new(context)
          context.expect(:rparen)
        else
          @value = RelationalExpression.new(context)
        end
      end

      def to_proc
        @proc = @value.to_proc
        return lambda {|ev| @proc.call(ev) == @expected_value}
      end
    end # class Factor

    #
    # RelationalExpression :: Symbol Operator (Argument || '[' Argument Argument* ']')
    #
    class RelationalExpression
      KEYWORDS = [:subject, :category, :body, :location, :recurrence_tag]

      def initialize(context)
        @name = context.expect(:symbol).value.downcase.to_sym
        raise ParseError, "unknown keyword '#{@name}'" unless KEYWORDS.member?(@name)

        context.expect(:sepop) # Currently, operator is only ":"

        @arguments = []
        if context.eat_if(:lbracket)
          loop do
            @arguments << Argument.new(context)
            break if context.eat_if(:rbracket)
          end
        else
          @arguments << Argument.new(context)
        end
      end

      def to_proc
        case @name
        when :category
          @arguments = @arguments.map{|arg| arg.value.downcase}
          return lambda {|ev| !(ev.categories.map{|c| c.to_s.downcase} & @arguments).empty?}
        when :recurrence_tag
          @arguments = @arguments.map{|arg| arg.value.downcase}
          return lambda {|ev| !!@arguments.find{|v| ev.send(@name).to_s.downcase.toutf8 == v}}
        else
          @arguments = @arguments.map{|arg| Regexp.quote(arg.value)}
          return lambda {|ev| !!@arguments.find{|v| ev.send(@name).to_s.toutf8.match(v)}}
        end
      end
    end # class RelationalExpression

    #
    # Argument :: Symbol || String
    #
    class Argument
      def initialize(context)
        token = context.expect(:symbol, :string)
        @type  = token.type
        @value = token.value
      end

      def value
        case @type
        when :string
          @value[1..-2]
        else
          @value
        end
      end
    end # class Argument

    class Context
      TOKENS = {
        symbol:   /[a-zA-Z_][a-zA-Z_\d]*/,
        string:   /"(?:[^"\\]|\\.)*"/,
        negop:    /!/,
        andop:    /&/,
        orop:     /\|/,
        sepop:    /:/,
        lparen:   /\(/,
        rparen:   /\)/,
        lbracket: /\[/,
        rbracket: /\]/
      }.map{|type,regexp| "(?<#{type}>#{regexp})"}.join("|")

      TOKEN_REGEXP = Regexp.new('^\s*(' + TOKENS + ')')

      def initialize(string)
        @tokens = tokenize(string)
      end

      def eat_if(*expected_types)
        expected_types.each do |expected_type|
          if @tokens.first and @tokens.first.type == expected_type
            return @tokens.shift
          end
        end
        return nil
      end

      def expect(*expected_types)
        token = eat_if(*expected_types) and return token
        raise ParseError, "#{expected_types.map(&:upcase).join(' or ')} expected before #{@tokens.first.value rescue 'END'}"
      end

      def debug_dump
        @tokens.map{|token| "#{token.type} => #{token.value}"}.join(", ")
      end

      private

      def tokenize(string)
        tokens = []

        loop do
          token, string = get_token(string)
          break if token.nil?
          tokens << token
        end

        raise ParseError, "can not tokenize '#{string}'" unless string.length == 0
        return tokens
      end

      def get_token(string)
        if match = TOKEN_REGEXP.match(string)
          name   = match.names.find{|name| match[name]}
          value  = match[name]
          remain = match.post_match.strip
          return [Token.new(name, value), remain]
        end
        return [nil, string]
      end
    end # class Context

    class Token
      attr_reader :type, :value

      def initialize(type, string)
        @type, @value = type.to_sym, string
      end
    end # class Token

    class Test
      attr_reader :categories, :subject

      def initialize(categories = [], subject = "", body = "")
        @categories = categories
        @subject = subject
        @body = body
      end
    end
  end
end