예제 #1
0
 def _update_counters(self, ch):
     if ch == '(':
         self._num_of_open_braces += 1
     elif ch == ')':
         self._num_of_open_braces -= 1
         if self._num_of_open_braces < 0:
             raise ValidationError()
     elif ch == '{':
         self._num_of_open_parentheses += 1
     elif ch == '}':
         self._num_of_open_parentheses -= 1
         if self._num_of_open_parentheses < 0:
             raise ValidationError()
     elif ch == '[':
         self._num_of_open_square_brackets += 1
     elif ch == ']':
         self._num_of_open_square_brackets -= 1
         if self._num_of_open_square_brackets < 0:
             raise ValidationError()
     elif ch == '\'':
         if self._num_of_open_single_quote == 0:
             self._num_of_open_single_quote += 1
         elif self._num_of_open_single_quote == 1:
             self._num_of_open_single_quote -= 1
     elif ch == '\"':
         if self._num_of_open_double_quote == 0:
             self._num_of_open_double_quote += 1
         elif self._num_of_open_double_quote == 1:
             self._num_of_open_double_quote -= 1
예제 #2
0
 def _context_analyzer(self, ch, contexts):
     if ch == ',':
         contexts.append('Comma')
         sequence = Sequence()
         sequence.append(self.get_most_inner_expression(self._expressions))
         self._expressions = sequence
     elif self.is_first_name_letter(ch):
         contexts.append('Text')
         if len(self._expressions) == 0 or \
             type(self._expressions[-1]) != Symbol or \
             self._expressions[-1].object == None or \
             self._expressions[-1].name != None:
             if len(self._expressions) > 0 and \
                type(self._expressions[-1]) == Expression and \
                type(self._expressions) != Sequence:
                 self._expressions[-1].append(Symbol(None))
             else:
                 self._expressions.append(Symbol(None))
     elif ch.isdigit():
         contexts.append('Number')
     elif self.is_operator_char(ch):
         contexts.append('Operator')
     elif len(self._expressions) > 0 and \
         type(self._expressions[-1]) == Symbol and \
         ch == '.':
         contexts.append('Text')
         old_attrib = self._expressions[-1]
         obj = Object(old_attrib.name)
         attrib = Symbol(None)
         attrib.object = obj
         self._expressions[-1] = attrib
     elif len(self._expressions) > 0 and \
         type(self._expressions[-1]) == Symbol and \
         self.is_group_operator(ch):
         contexts[-1] = 'FunctionCall'
         if len(self._expressions) > 0 and type(
                 self._expressions[-1]) == Symbol:
             attr = self._expressions[-1]
             func = FunctionCall(attr.name)
             self._expressions[-1] = func
             func.object = attr.object
     elif self.is_function_body_operator(ch):
         contexts.append('Function')
     elif self.is_group_operator(ch):
         contexts.append('Group')
     elif self.is_string_operator(ch):
         contexts.append('String')
     elif self.is_index_operator(ch):
         contexts.append('Indexer')
         if len(self._expressions) > 0 and type(
                 self._expressions[-1]) == Symbol:
             attr = self._expressions[-1]
             self._expressions[-1] = Indexer(attr)
         else:
             raise ValidationError()
     else:
         raise ValidationError()
예제 #3
0
 def parse_operator(self, ch):
     if self.is_operator_char(ch):
         self._temp.append(ch)
     elif self.is_operator(''.join(self._temp)):
         if self._temp[0] == '-' and self._temp[1] == '>':
             if len(self._expressions) > 0 and isinstance(
                     self._expressions[-1], FunctionCall):
                 if self.is_name_letter(ch) or ch == ' ':
                     if not hasattr(self, 'return_value'):
                         self.return_value = ''
                     self.return_value += ch
                 else:
                     func = self._expressions[-1]
                     func = Function(func.name, func.object, *func.args)
                     self._expressions[-1] = func
                     return_value = self.return_value.strip()
                     if ' ' in return_value:
                         raise ValidationError()
                     if len(return_value) != 0:
                         func.return_value = return_value
                     self._temp = []
                     self._parse_expression(ch)
             elif len(self._expressions) > 0 and isinstance(
                     self._expressions[-1], Expression):
                 if self.is_name_letter(ch) or ch == ' ':
                     if not hasattr(self, 'return_value'):
                         self.return_value = ''
                     self.return_value += ch
                 else:
                     group = self._expressions[-1]
                     body = ''.join(self._temp)
                     anon_func = Lambda(*group._items, body=body)
                     return_value = self.return_value.strip()
                     if ' ' in return_value:
                         raise ValidationError()
                     if len(return_value) != 0:
                         anon_func.return_value = return_value
                     self._expressions[-1] = anon_func
                     self._temp = []
                     self._parse_expression(ch)
             else:
                 raise ValidationError()
         else:
             self._expressions.append(Operator(''.join(self._temp)))
             self._temp = []
             self._parse_expression(ch)
     else:
         raise ValidationError()
예제 #4
0
    def _handle_letter(self, ch):
        current_context = self._contexts[-1]
        if ExpressionParser._context[
                current_context] == 'Number' and ch.isalpha():
            raise ValidationError()

        ExpressionParser._context[current_context](self, ch)
예제 #5
0
 def parse_function(self, ch):
     self._temp.append(ch)
     if ch == '{':
         self._num_of_open_parentheses += 1
     elif ch == '}':
         self._num_of_open_parentheses -= 1
         if self._num_of_open_parentheses == 0:
             del self._temp[0]
             del self._temp[-1]
             if len(self._expressions) > 0 and isinstance(
                     self._expressions[-1], Function):
                 func = self._expressions[-1]
                 func.body = ''.join(self._temp)
             elif len(self._expressions) > 0 and isinstance(
                     self._expressions[-1], FunctionCall):
                 func = self._expressions[-1]
                 func = Function(func.name, func.object, *func.args)
                 self._expressions[-1] = func
                 func.body = ''.join(self._temp)
             elif len(self._expressions) > 0 and isinstance(
                     self._expressions[-1], Expression):
                 group = self._expressions[-1]
                 body = ''.join(self._temp)
                 anon_func = Lambda(*group._items, body=body)
                 self._expressions[-1] = anon_func
             elif len(self._expressions) > 0 and type(
                     self._expressions[-1]) == Symbol:
                 attrib = self._expressions[-1]
                 parser = ExpressionParser(''.join(self._temp))
                 expression = parser.get_ast()
                 attrib.args = list(expression) if isinstance(
                     expression, Expression) else [expression]
             else:
                 raise ValidationError()
             self._temp = []
예제 #6
0
 def _validate_parsed_result(self):
     if self._num_of_open_braces != 0 or \
        self._num_of_open_parentheses != 0 or \
        self._num_of_open_square_brackets != 0 or \
        self._num_of_open_single_quote != 0 or \
        self._num_of_open_double_quote != 0:
         raise ValidationError()
예제 #7
0
 def parse_number(self, ch):
     if ch.isdigit():
         self._temp.append(ch)
     elif not ch.isalpha():
         self._expressions.append(Value(''.join(self._temp)))
         self._temp = []
         self._parse_expression(ch)
     else:
         raise ValidationError()
예제 #8
0
 def parse_object(self, ch):
     self._temp.append(ch)
     if ch == '}':
         del self._temp[0]
         del self._temp[-1]
         if len(self._expressions) > 0 and type(
                 self._expressions[-1]) == Function:
             func = self._expressions[-1]
             func.body = ''.join(self._temp)
         else:
             raise ValidationError()
         self._temp = []
예제 #9
0
 def parse_indexer(self, ch):
     self._temp.append(ch)
     if ch == '[':
         self._num_of_open_square_bracket += 1
     elif ch == ']':
         self._num_of_open_square_bracket -= 1
         if self._num_of_open_square_bracket == 0:
             del self._temp[0]
             del self._temp[-1]
             indexer = self._expressions[-1]
             parser = ExpressionParser(''.join(self._temp))
             indexer.expression = parser.get_ast()
             if not indexer.expression:
                 raise ValidationError()
             self._temp = []