                                 stack.push element
                                 element =
                                     type: null
-                                    start: i+result_regex[0].length
                                     context: context
                                     complete: false
+
+                                start = i+result_regex[0].length
                                 continue
 
                             result_regex = @regex.object.exec query.slice i # Check for object
                             if result_regex isnt null
                                 element.type = 'object'
-                                element.data = {}
                                 element.next_key = null
-                                element.data = {}
+                                element.body = [] # We need to keep tracker of the order of pairs
                                 element.current_key_start = i+result_regex[0].length
                                 to_skip = result_regex[0].length-1
                                 stack_stop_char = ['{']
                                 continue
 
                             if char is '.'
-                                start = i+1
+                                new_start = i+1
                             else
-                                start = i
-                            result_regex = @regex.method.exec query.slice start # Check for a standard method
-                            if result_regex isnt null and stack[0]?.type is 'function' # We want the query to start with r. or arg.
-                                #TODO Check if we are dealing with row(
-                                element.type = 'function'
-                                element.name = result_regex[0]
-                                element.start += start-i
-                                to_skip = result_regex[0].length-1+start-i
-                                stack_stop_char = ['(']
-                                continue
-
-                            result_regex = @regex.method_var.exec query.slice start # Check for method without parenthesis r., r.row., doc.merge(...
+                                new_start = i
+                            #new_start = i
+
+                            result_regex = @regex.method.exec query.slice new_start # Check for a standard method
                             if result_regex isnt null
-                                element.type = 'function'
-                                element.name = result_regex[0].slice 0, result_regex[0].length-1
-                                element.start += start-i
+                                if stack[stack.length-1]?.type is 'function' # We want the query to start with r. or arg.
+                                    element.type = 'function'
+                                    element.name = result_regex[0]
+                                    start += new_start-i
+                                    to_skip = result_regex[0].length-1+new_start-i
+                                    stack_stop_char = ['(']
+                                    continue
+                                else
+                                    position_opening_parenthesis = result_regex[0].indexOf('(')
+                                    if position_opening_parenthesis isnt -1 and result_regex[0].slice(0, position_opening_parenthesis) of context
+                                        # Save the var
+                                        # TODO Look for do() to get real_type
+                                        element.type = 'var'
+                                        element.name = result_regex[0].slice(0, position_opening_parenthesis)
+                                        stack.push element
+                                        element =
+                                            type: 'function'
+                                            name: '('
+                                            context: context
+                                            complete: 'false'
+                                        stack_stop_char = ['(']
+                                        start = position_opening_parenthesis
+                                        to_skip = result_regex[0].length-1
+
+
+                            result_regex = @regex.method_var.exec query.slice new_start # Check for method without parenthesis r., r.row., doc.
+                            if result_regex isnt null
+                                if result_regex[0].slice(0, result_regex[0].length-1) of context
+                                    element.type = 'var'
+                                else
+                                    element.type = 'function'
+                                element.name = result_regex[0].slice(0, result_regex[0].length-1).replace(/\s/, '')
+                                start += new_start-i
                                 element.complete = true
-                                to_skip = element.name.length-1+start-i
+                                to_skip = element.name.length-1+new_start-i
                                 stack.push element
                                 element =
                                     type: null
-                                    start: start+to_skip+1
                                     context: context
                                     complete: false
+                                start = new_start+to_skip+1
                                 continue
-                        else # if element.start isnt i
-                            # Skip white spaces
-                            result_regex = @regex.white_and_comma.exec query.slice i
+
+                            # Look for a comma
+                            result_regex = @regex.comma.exec query.slice i
                             if result_regex isnt null
                                 # element should have been pushed in stack. If not, the query is malformed
                                 element.complete = true
                                 stack.push
                                     type: 'separator'
+                                    complete: true
                                     name: query.slice i, result_regex[0].length
                                 element =
                                     type: null
-                                    start: i+result_regex[0].length-1+1
                                     context: context
                                     complete: false
+                                start = i+result_regex[0].length-1+1
                                 to_skip = result_regex[0].length-1
-                                #element.start += to_skip
                                 continue
+
+                        #else # if element.start isnt i
+                            # Skip white spaces
+                            # TODO
+
+
                     
                     else # element.type isnt null
                         # White spaces can means a new start: r.table(...).eqJoin('id', r.table(...), 'other_id')
                         # Is that even possible?
-                        result_regex = @regex.white_and_comma.exec(query.slice(i))
+                        result_regex = @regex.comma.exec(query.slice(i))
                         if result_regex isnt null and stack_stop_char.length < 1
                             # element should have been pushed in stack. If not, the query is malformed
                             stack.push
                                 type: 'separator'
+                                complete: true
                                 name: query.slice i, result_regex[0].length
                             element =
                                 type: null
-                                start: i+result_regex[0].length-1
                                 context: context
                                 complete: false
+                            start = i+result_regex[0].length-1
                             to_skip = result_regex[0].length-1
-                            #element.start += to_skip
                             continue
 
 

                                    new_context[arg] = true
                                element.context = new_context
                                to_skip = result_regex[0].length
                                element.body_start = i+result_regex[0].length
                                stack_stop_char = ['{']
                                continue


                                    stack_stop_char.pop()
                                    if stack_stop_char.length is 0
                                        element.body = @extract_data_from_query
                                            query: query.slice start+element.name.length, i
                                            context: element.context
                                        element.complete = true
                                        stack.push element
                                        element =
                                            type: null
                                            context: context
                                        start = i+1
                        else if element.type is 'object'
                            # Since we are sure that we are not in a string, we can just look for colon and comma
                            # Still, we need to check the stack_stop_char since we can have { key: { inner: 'test, 'other_inner'}, other_key: 'other_value'}
