Class: QueryBuilder::Parser
- Inherits:
-
Object
- Object
- QueryBuilder::Parser
- Defined in:
- lib/querybuilder_rb.rb,
lib/query_builder/parser.rb
Constant Summary collapse
- OP_PRECEDENCE =
{ :function => 50, :select_one => 50, :interval => 40, :binary => 39, :collate => 39, :"!" => 38, :"@-" => 37, :"@~" => 37, :"^" => 36, :"*" => 35, :"/" => 35, :div => 35, :"%" => 35, :mod => 35, :"-" => 34, :"+" => 34, :"<<" => 33, :">>" => 33, :"&" => 32, :"|" => 31, :"=" => 30, :"<=>" => 30, :">=" => 30, :">" => 30, :"<=" => 30, :"<" => 30, :"<>" => 30, :"!=" => 30, :is => 30, :like => 30, :regexp => 30, :in => 30, :lt => 30, :le => 30, :eq => 30, :ne => 30, :ge => 30, :gt => 30, :match => 30, :between => 29, :case => 29, :when => 29, :then => 29, :else => 29, :not => 28, :"&&" => 27, :and => 27, :xor => 26, :"||" => 25, :or => 25, :":=" => 24, :relation => 13, :filter => 13, :select => 13, :scope => 12, :from => 11, # this is not the same as SQL 'FROM', it's "icons from friends" :asc => 10, :desc => 10, :clause => 5, :clause_and => 4, :clause_or => 3, :offset => 2, :paginate => 2, :limit => 2, :order => 2, :group => 2, :having => 2, :query => 1, :par_close => 0, :clause_par_close => 0, :par => -1, :clause_par => -1 }
Class Attribute Summary collapse
-
._querybuilder_actions ⇒ Object
Returns the value of attribute _querybuilder_actions.
-
._querybuilder_eof_actions ⇒ Object
Returns the value of attribute _querybuilder_eof_actions.
-
._querybuilder_index_offsets ⇒ Object
Returns the value of attribute _querybuilder_index_offsets.
-
._querybuilder_indicies ⇒ Object
Returns the value of attribute _querybuilder_indicies.
-
._querybuilder_key_offsets ⇒ Object
Returns the value of attribute _querybuilder_key_offsets.
-
._querybuilder_range_lengths ⇒ Object
Returns the value of attribute _querybuilder_range_lengths.
-
._querybuilder_single_lengths ⇒ Object
Returns the value of attribute _querybuilder_single_lengths.
-
._querybuilder_trans_actions ⇒ Object
Returns the value of attribute _querybuilder_trans_actions.
-
._querybuilder_trans_keys ⇒ Object
Returns the value of attribute _querybuilder_trans_keys.
-
._querybuilder_trans_targs ⇒ Object
Returns the value of attribute _querybuilder_trans_targs.
-
.querybuilder_en_clause_p ⇒ Object
Returns the value of attribute querybuilder_en_clause_p.
-
.querybuilder_en_expr_p ⇒ Object
Returns the value of attribute querybuilder_en_expr_p.
-
.querybuilder_en_main ⇒ Object
Returns the value of attribute querybuilder_en_main.
-
.querybuilder_error ⇒ Object
Returns the value of attribute querybuilder_error.
-
.querybuilder_first_final ⇒ Object
Returns the value of attribute querybuilder_first_final.
-
.querybuilder_start ⇒ Object
Returns the value of attribute querybuilder_start.
Class Method Summary collapse
-
.apply_op(stack, op, change_last = true) ⇒ Object
Transform the stack to wrap the last element with an operator: [a, b, c] ==> [a, b, [op, c, d]].
- .debug_stack(stack, msg = '') ⇒ Object
- .insert(stack, arg) ⇒ Object
-
.parse(arg) ⇒ Object
line 216 “querybuilder_rb.rl”.
- .pop_stack(stack, op) ⇒ Object
Class Attribute Details
._querybuilder_actions ⇒ Object
Returns the value of attribute _querybuilder_actions.
12 13 14 |
# File 'lib/querybuilder_rb.rb', line 12 def _querybuilder_actions @_querybuilder_actions end |
._querybuilder_eof_actions ⇒ Object
Returns the value of attribute _querybuilder_eof_actions.
2552 2553 2554 |
# File 'lib/querybuilder_rb.rb', line 2552 def _querybuilder_eof_actions @_querybuilder_eof_actions end |
._querybuilder_index_offsets ⇒ Object
Returns the value of attribute _querybuilder_index_offsets.
1195 1196 1197 |
# File 'lib/querybuilder_rb.rb', line 1195 def _querybuilder_index_offsets @_querybuilder_index_offsets end |
._querybuilder_indicies ⇒ Object
Returns the value of attribute _querybuilder_indicies.
1356 1357 1358 |
# File 'lib/querybuilder_rb.rb', line 1356 def _querybuilder_indicies @_querybuilder_indicies end |
._querybuilder_key_offsets ⇒ Object
Returns the value of attribute _querybuilder_key_offsets.
43 44 45 |
# File 'lib/querybuilder_rb.rb', line 43 def _querybuilder_key_offsets @_querybuilder_key_offsets end |
._querybuilder_range_lengths ⇒ Object
Returns the value of attribute _querybuilder_range_lengths.
1034 1035 1036 |
# File 'lib/querybuilder_rb.rb', line 1034 def _querybuilder_range_lengths @_querybuilder_range_lengths end |
._querybuilder_single_lengths ⇒ Object
Returns the value of attribute _querybuilder_single_lengths.
873 874 875 |
# File 'lib/querybuilder_rb.rb', line 873 def _querybuilder_single_lengths @_querybuilder_single_lengths end |
._querybuilder_trans_actions ⇒ Object
Returns the value of attribute _querybuilder_trans_actions.
2294 2295 2296 |
# File 'lib/querybuilder_rb.rb', line 2294 def _querybuilder_trans_actions @_querybuilder_trans_actions end |
._querybuilder_trans_keys ⇒ Object
Returns the value of attribute _querybuilder_trans_keys.
204 205 206 |
# File 'lib/querybuilder_rb.rb', line 204 def _querybuilder_trans_keys @_querybuilder_trans_keys end |
._querybuilder_trans_targs ⇒ Object
Returns the value of attribute _querybuilder_trans_targs.
2036 2037 2038 |
# File 'lib/querybuilder_rb.rb', line 2036 def _querybuilder_trans_targs @_querybuilder_trans_targs end |
.querybuilder_en_clause_p ⇒ Object
Returns the value of attribute querybuilder_en_clause_p.
2730 2731 2732 |
# File 'lib/querybuilder_rb.rb', line 2730 def querybuilder_en_clause_p @querybuilder_en_clause_p end |
.querybuilder_en_expr_p ⇒ Object
Returns the value of attribute querybuilder_en_expr_p.
2726 2727 2728 |
# File 'lib/querybuilder_rb.rb', line 2726 def querybuilder_en_expr_p @querybuilder_en_expr_p end |
.querybuilder_en_main ⇒ Object
Returns the value of attribute querybuilder_en_main.
2734 2735 2736 |
# File 'lib/querybuilder_rb.rb', line 2734 def querybuilder_en_main @querybuilder_en_main end |
.querybuilder_error ⇒ Object
Returns the value of attribute querybuilder_error.
2721 2722 2723 |
# File 'lib/querybuilder_rb.rb', line 2721 def querybuilder_error @querybuilder_error end |
.querybuilder_first_final ⇒ Object
Returns the value of attribute querybuilder_first_final.
2717 2718 2719 |
# File 'lib/querybuilder_rb.rb', line 2717 def querybuilder_first_final @querybuilder_first_final end |
.querybuilder_start ⇒ Object
Returns the value of attribute querybuilder_start.
2713 2714 2715 |
# File 'lib/querybuilder_rb.rb', line 2713 def querybuilder_start @querybuilder_start end |
Class Method Details
.apply_op(stack, op, change_last = true) ⇒ Object
Transform the stack to wrap the last element with an operator:
- a, b, c
-
> [a, b, [op, c, d]]
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/query_builder/parser.rb', line 41 def apply_op(stack, op, change_last = true) pop_stack(stack, op) last = stack.last change_elem = last.last last[-1] = [op.to_sym, change_elem] if change_last stack.push last[-1] end stack.last end |
.debug_stack(stack, msg = '') ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/query_builder/parser.rb', line 71 def debug_stack(stack, msg = '') puts "======= #{msg} =======" stack.reverse_each do |s| puts s.inspect end puts "======================" end |
.insert(stack, arg) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/query_builder/parser.rb', line 52 def insert(stack, arg) # insert [:relation, "..."] # stack: [[:query]] --> [[:query, [:relation, "..."]], [:relation, "..."]] pop_stack(stack, arg.first) last = stack.last last << arg stack.push last.last stack.last end |
.parse(arg) ⇒ Object
line 216 “querybuilder_rb.rl”
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 |
# File 'lib/querybuilder_rb.rb', line 2741 def self.parse(arg) if arg.kind_of?(Array) data = "(#{arg.join(') or (')})\n" else data = "#{arg}\n" end par_count = 0 stack = [[:query]] last = stack.last str_buf = "" clause_state = :relation eof = 0; # line 2755 "querybuilder_rb.rb" begin p ||= 0 pe ||= data.length cs = querybuilder_start end # line 230 "querybuilder_rb.rl" # line 2764 "querybuilder_rb.rb" begin _klen, _trans, _keys, _acts, _nacts = nil _goto_level = 0 _resume = 10 _eof_trans = 15 _again = 20 _test_eof = 30 _out = 40 while true _trigger_goto = false if _goto_level <= 0 if p == pe _goto_level = _test_eof next end if cs == 0 _goto_level = _out next end end if _goto_level <= _resume _keys = _querybuilder_key_offsets[cs] _trans = _querybuilder_index_offsets[cs] _klen = _querybuilder_single_lengths[cs] _break_match = false begin if _klen > 0 _lower = _keys _upper = _keys + _klen - 1 loop do break if _upper < _lower _mid = _lower + ( (_upper - _lower) >> 1 ) if data[p].ord < _querybuilder_trans_keys[_mid] _upper = _mid - 1 elsif data[p].ord > _querybuilder_trans_keys[_mid] _lower = _mid + 1 else _trans += (_mid - _keys) _break_match = true break end end # loop break if _break_match _keys += _klen _trans += _klen end _klen = _querybuilder_range_lengths[cs] if _klen > 0 _lower = _keys _upper = _keys + (_klen << 1) - 2 loop do break if _upper < _lower _mid = _lower + (((_upper-_lower) >> 1) & ~1) if data[p].ord < _querybuilder_trans_keys[_mid] _upper = _mid - 2 elsif data[p].ord > _querybuilder_trans_keys[_mid+1] _lower = _mid + 2 else _trans += ((_mid - _keys) >> 1) _break_match = true break end end # loop break if _break_match _trans += _klen end end while false _trans = _querybuilder_indicies[_trans] cs = _querybuilder_trans_targs[_trans] if _querybuilder_trans_actions[_trans] != 0 _acts = _querybuilder_trans_actions[_trans] _nacts = _querybuilder_actions[_acts] _acts += 1 while _nacts > 0 _nacts -= 1 _acts += 1 case _querybuilder_actions[_acts - 1] when 0 then # line 6 "querybuilder_rb.rl" begin str_buf += data[p].ord.chr end when 1 then # line 10 "querybuilder_rb.rl" begin last << [:string, str_buf] str_buf = "" end when 2 then # line 15 "querybuilder_rb.rl" begin last << [:dstring, str_buf] str_buf = "" end when 3 then # line 20 "querybuilder_rb.rl" begin last << [:rubyless, str_buf] str_buf = "" end when 4 then # line 25 "querybuilder_rb.rl" begin last << [:integer, str_buf] str_buf = "" end when 5 then # line 30 "querybuilder_rb.rl" begin last << [:real, str_buf] str_buf = "" end when 6 then # line 35 "querybuilder_rb.rl" begin last << [:field, str_buf] str_buf = "" end when 7 then # line 40 "querybuilder_rb.rl" begin last << [:method, str_buf] str_buf = "" end when 8 then # line 45 "querybuilder_rb.rl" begin last << [:raw, str_buf] str_buf = "" end when 9 then # line 50 "querybuilder_rb.rl" begin last = apply_op(stack, :select_one) last << str_buf str_buf = "" # last should be [:select, ...], not the [:select_one] just added. stack.pop last = stack.last end when 10 then # line 59 "querybuilder_rb.rl" begin last = apply_op(stack, :function) str_buf = "" end when 11 then # line 64 "querybuilder_rb.rl" begin last = apply_op(stack, str_buf.downcase.to_sym, false) str_buf = "" end when 12 then # line 69 "querybuilder_rb.rl" begin if clause_state == :relation || clause_state == :parenthesis last = insert(stack, [:relation, str_buf]) str_buf = "" end end when 13 then # line 76 "querybuilder_rb.rl" begin last = apply_op(stack, str_buf.downcase.to_sym) str_buf = "" end when 14 then # line 81 "querybuilder_rb.rl" begin last = apply_op(stack, :in) end when 15 then # line 85 "querybuilder_rb.rl" begin # We need the 'is' operator to avoid confusion with 'in site'. last = apply_op(stack, :is) end when 16 then # line 90 "querybuilder_rb.rl" begin last = apply_op(stack, :interval) last << str_buf str_buf = "" end when 17 then # line 96 "querybuilder_rb.rl" begin last = apply_op(stack, :filter) clause_state = :filter end when 18 then # line 101 "querybuilder_rb.rl" begin last = apply_op(stack, :having) clause_state = :having end when 19 then # line 106 "querybuilder_rb.rl" begin last = apply_op(stack, :select) clause_state = :select end when 20 then # line 111 "querybuilder_rb.rl" begin # remember current machine state 'cs' par_count += 1 last << [:par, cs] stack.push last.last last = last.last begin cs = 878 _trigger_goto = true _goto_level = _again break end end when 21 then # line 120 "querybuilder_rb.rl" begin pop_stack(stack, :par_close) # reset machine state 'cs' par_count -= 1 cs = stack.last.delete_at(1) # one more time to remove [:par...] line stack.pop last = stack.last # closing ')' must be parsed twice p = p - 1; end when 22 then # line 132 "querybuilder_rb.rl" begin # remember current machine state 'cs' clause_state = :parenthesis last << [:clause_par, cs] stack.push last.last last = last.last begin cs = 1010 _trigger_goto = true _goto_level = _again break end end when 23 then # line 141 "querybuilder_rb.rl" begin pop_stack(stack, :clause_par_close) clause_state = :relation # reset machine state 'cs' cs = stack.last.delete_at(1) # one more time to remove [:clause_par...] line stack.pop last = stack.last # closing ')' must be parsed twice p = p - 1; end when 24 then # line 153 "querybuilder_rb.rl" begin last = apply_op(stack, :scope) last << str_buf str_buf = "" end when 25 then # line 159 "querybuilder_rb.rl" begin last = apply_op(stack, :offset) end when 26 then # line 163 "querybuilder_rb.rl" begin last << [:param, str_buf] str_buf = "" end when 27 then # line 168 "querybuilder_rb.rl" begin last = apply_op(stack, :paginate) end when 28 then # line 172 "querybuilder_rb.rl" begin last = apply_op(stack, :limit) str_buf = "" end when 29 then # line 177 "querybuilder_rb.rl" begin last = apply_op(stack, :order) str_buf = "" end when 30 then # line 182 "querybuilder_rb.rl" begin last = apply_op(stack, :group) end when 31 then # line 186 "querybuilder_rb.rl" begin last = apply_op(stack, :from) clause_state = :relation end when 32 then # line 191 "querybuilder_rb.rl" begin if clause_state == :relation last = apply_op(stack, "clause_#{str_buf}".to_sym) str_buf = "" end end when 33 then # line 202 "querybuilder_rb.rl" begin p = p - 3 p = 0 if p < 0 raise QueryBuilder::SyntaxError.new("Syntax error near #{data[p..-1].chomp.inspect}.") end # line 3123 "querybuilder_rb.rb" end # action switch end end if _trigger_goto next end end if _goto_level <= _again if cs == 0 _goto_level = _out next end p += 1 if p != pe _goto_level = _resume next end end if _goto_level <= _test_eof if p == eof __acts = _querybuilder_eof_actions[cs] __nacts = _querybuilder_actions[__acts] __acts += 1 while __nacts > 0 __nacts -= 1 __acts += 1 case _querybuilder_actions[__acts - 1] when 33 then # line 202 "querybuilder_rb.rl" begin p = p - 3 p = 0 if p < 0 raise QueryBuilder::SyntaxError.new("Syntax error near #{data[p..-1].chomp.inspect}.") end # line 3159 "querybuilder_rb.rb" end # eof action switch end if _trigger_goto next end end end if _goto_level <= _out break end end end # line 231 "querybuilder_rb.rl" if p < pe p = p - 3 p = 0 if p < 0 raise QueryBuilder::SyntaxError.new("Syntax error near #{data[p..-2].inspect}.") end if par_count > 0 raise QueryBuilder::SyntaxError.new("Missing closing parenthesis in #{data[0..-2].inspect}.") end stack.first end |
.pop_stack(stack, op) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/query_builder/parser.rb', line 62 def pop_stack(stack, op) #debug_stack(stack, op) stack_op = stack.last.first while OP_PRECEDENCE[op] <= OP_PRECEDENCE[stack_op] stack.pop stack_op = stack.last.first end end |