Exemplo n.º 1
0
 def test__ActionUnderScoreNameArgValue__Valid(self):
     example = "my_action(2)"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), FunctionCall)
     self.assertEqual(len(ast.args), 1)
     self.assertEqual(type(ast.args[0]), Value)
     self.assertEqual(ast.args[0].value, '2')
Exemplo n.º 2
0
 def test__ActionArgAttribute__Valid(self):
     example = "Action2(k)"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), FunctionCall)
     self.assertEqual(len(ast.args), 1)
     self.assertEqual(type(ast.args[0]), Symbol)
     self.assertEqual(ast.args[0].name, 'k')
Exemplo n.º 3
0
 def test__ActionEmpty_ActionEmpty__Valid(self):
     example = "Action(), Action2()"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Sequence)
     self.assertEqual(type(ast[0]), FunctionCall)
     self.assertEqual(len(ast[0].args), 0)
     self.assertEqual(type(ast[1]), FunctionCall)
     self.assertEqual(len(ast[1].args), 0)
Exemplo n.º 4
0
 def test__ActionDefinitionArgType__Valid(self):
     example = "Action2(int k)"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), FunctionCall)
     self.assertEqual(len(ast.args), 1)
     self.assertEqual(type(ast.args[0]), Expression)
     self.assertEqual(type(ast.args[0][0]), Symbol)
     self.assertEqual(ast.args[0][0].name, 'int')
     self.assertEqual(type(ast.args[0][1]), Symbol)
     self.assertEqual(ast.args[0][1].name, 'k')
Exemplo n.º 5
0
 def test__Variable_NOT_EQUAL_TYPES_Variable__Valid(self):
     example = 'k !== l'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '!==')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'l')
Exemplo n.º 6
0
 def test__Value_AND_Variable__Valid(self):
     example = '2 && k'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Value)
     self.assertEqual(ast[0].value, '2')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '&&')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'k')
Exemplo n.º 7
0
 def test__StringType1_AND_Variable__Valid(self):
     example = '"My String" && l'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), String)
     self.assertEqual(ast[0].name, 'My String')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '&&')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'l')
Exemplo n.º 8
0
 def test__Variable_EQUAL_ActionVoid__Valid(self):
     example = 'k == isAction()'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), FunctionCall)
     self.assertEqual(ast[2].name, 'isAction')
     self.assertEqual(ast[2].args, [])
Exemplo n.º 9
0
 def test__ActionArgValue_ActionArgValue__Valid(self):
     example = "Action(2), Action2(2)"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Sequence)
     self.assertEqual(type(ast[0]), FunctionCall)
     self.assertEqual(len(ast[0].args), 1)
     self.assertEqual(type(ast[0].args[0]), Value)
     self.assertEqual(ast[0].args[0].value, '2')
     self.assertEqual(type(ast[1]), FunctionCall)
     self.assertEqual(len(ast[1].args), 1)
     self.assertEqual(type(ast[1].args[0]), Value)
     self.assertEqual(ast[1].args[0].value, '2')
Exemplo n.º 10
0
 def test__Variable_OR_VariableIndexer__Valid(self):
     example = 'k || l[1]'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '||')
     self.assertEqual(type(ast[2]), Indexer)
     self.assertEqual(type(ast[2].attribute), Symbol)
     self.assertEqual(ast[2].attribute.name, 'l')
Exemplo n.º 11
0
 def test__Group_of_OperatorVariable_AND_Variable__Valid(self):
     example = "(++k && l)"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 4)
     self.assertEqual(type(ast[0]), Operator)
     self.assertEqual(ast[0].name, '++')
     self.assertEqual(type(ast[1]), Symbol)
     self.assertEqual(ast[1].name, 'k')
     self.assertEqual(type(ast[2]), Operator)
     self.assertEqual(ast[2].name, '&&')
     self.assertEqual(type(ast[3]), Symbol)
     self.assertEqual(ast[3].name, 'l')
Exemplo n.º 12
0
 def test_Variable_AND_VariableComplex1Indexer__Valid(self):
     example = 'k && l[getIndex() { return 1; }]'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '&&')
     self.assertEqual(type(ast[2]), Indexer)
     self.assertEqual(type(ast[2].attribute), Symbol)
     self.assertEqual(ast[2].attribute.name, 'l')
     self.assertEqual(type(ast[2].expression), Function)
     self.assertEqual(ast[2].expression.name, 'getIndex')
     self.assertEqual(len(ast[2].expression.params), 0)
Exemplo n.º 13
0
 def test__OperatorVariable_AND_VariableInitializer__Valid(self):
     example = "++k && l{1}"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 4)
     self.assertEqual(type(ast[0]), Operator)
     self.assertEqual(ast[0].name, '++')
     self.assertEqual(type(ast[1]), Symbol)
     self.assertEqual(ast[1].name, 'k')
     self.assertEqual(type(ast[2]), Operator)
     self.assertEqual(ast[2].name, '&&')
     self.assertEqual(type(ast[3]), Symbol)
     self.assertEqual(ast[3].name, 'l')
     self.assertEqual(len(ast[3].args), 1)
     self.assertEqual(type(ast[3].args[0]), Value)
     self.assertEqual(ast[3].args[0].value, '1')
Exemplo n.º 14
0
 def test__NamespaceActionArg0Arg1_EQUAL_Variable__Valid(self):
     example = 'MyNamespace::isAction(arg0, arg1) == k'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), FunctionCall)
     self.assertEqual(ast[0].name, 'MyNamespace::isAction')
     self.assertEqual(len(ast[0].args), 2)
     self.assertEqual(type(ast[0].args[0]), Symbol)
     self.assertEqual(ast[0].args[0].name, 'arg0')
     self.assertEqual(type(ast[0].args[1]), Symbol)
     self.assertEqual(ast[0].args[1].name, 'arg1')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'k')
Exemplo n.º 15
0
 def test_Variable_EQUAL_AnonymousActionArg0Arg1Body__Valid(self):
     example = 'k == (arg0, arg1) { arg0 = arg1; }'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), Lambda)
     self.assertEqual(ast[2].name, None)
     self.assertEqual(len(ast[2].params), 2)
     self.assertEqual(type(ast[2].params[0]), Symbol)
     self.assertEqual(ast[2].params[0].name, 'arg0')
     self.assertEqual(type(ast[2].params[1]), Symbol)
     self.assertEqual(ast[2].params[1].name, 'arg1')
     self.assertEqual(ast[2].body, ' arg0 = arg1; ')
Exemplo n.º 16
0
 def test_Variable_EQUAL_ActionArg0Arg1Body__ReturnAuto__Valid(self):
     example = 'k == isAction(arg0, arg1) -> { arg0 = arg1; return arg0; }'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), Function)
     self.assertEqual(ast[2].name, 'isAction')
     self.assertEqual(len(ast[2].params), 2)
     self.assertEqual(type(ast[2].params[0]), Symbol)
     self.assertEqual(ast[2].params[0].name, 'arg0')
     self.assertEqual(type(ast[2].params[1]), Symbol)
     self.assertEqual(ast[2].params[1].name, 'arg1')
     self.assertEqual(ast[2].body, ' arg0 = arg1; return arg0; ')
Exemplo n.º 17
0
 def test__VariableComplexIndexer_AND_Variable__Valid(self):
     example = 'k[arr[1]] && l'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Indexer)
     self.assertEqual(type(ast[0].attribute), Symbol)
     self.assertEqual(ast[0].attribute.name, 'k')
     self.assertEqual(type(ast[0].expression), Indexer)
     self.assertEqual(type(ast[0].expression.attribute), Symbol)
     self.assertEqual(ast[0].expression.attribute.name, 'arr')
     self.assertEqual(type(ast[0].expression.expression), Value)
     self.assertEqual(ast[0].expression.expression.value, '1')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '&&')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'l')
Exemplo n.º 18
0
 def test_Variable_EQUAL_ObjectActionArg0Arg1__Valid(self):
     example = 'k == name.isAction(arg0, arg1)'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Symbol)
     self.assertEqual(ast[0].name, 'k')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), FunctionCall)
     self.assertEqual(ast[2].name, 'isAction')
     self.assertEqual(type(ast[2].object), Object)
     self.assertEqual(ast[2].object.name, 'name')
     self.assertEqual(len(ast[2].args), 2)
     self.assertEqual(type(ast[2].args[0]), Symbol)
     self.assertEqual(ast[2].args[0].name, 'arg0')
     self.assertEqual(type(ast[2].args[1]), Symbol)
     self.assertEqual(ast[2].args[1].name, 'arg1')
Exemplo n.º 19
0
 def test__AnonymousActionArg0Arg1Body__ReturnStdString__EQUAL__Variable__Valid(
         self):
     example = '(arg0, arg1) -> std::string { arg0 = arg1; } == k'
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Expression)
     self.assertEqual(len(ast), 3)
     self.assertEqual(type(ast[0]), Lambda)
     self.assertEqual(ast[0].name, None)
     self.assertEqual(len(ast[0].params), 2)
     self.assertEqual(type(ast[0].params[0]), Symbol)
     self.assertEqual(ast[0].params[0].name, 'arg0')
     self.assertEqual(type(ast[0].params[1]), Symbol)
     self.assertEqual(ast[0].params[1].name, 'arg1')
     self.assertEqual(ast[0].body, ' arg0 = arg1; ')
     self.assertEqual(ast[0].return_value, 'std::string')
     self.assertEqual(type(ast[1]), Operator)
     self.assertEqual(ast[1].name, '==')
     self.assertEqual(type(ast[2]), Symbol)
     self.assertEqual(ast[2].name, 'k')
Exemplo n.º 20
0
 def parse_internal(self, internal_actions, path):
     lang = None
     actions = []
     for action_name, action_body in internal_actions.items():
         options = []
         if type(action_body) == list:
             action_body = ''.join(action_body)
         elif re.match(ActionParser.scratch_file_pattern, action_body):
             match = re.match(ActionParser.scratch_file_pattern,
                              action_body)
             lang = match.group('lang')
             with open(os.path.join(path, action_body)) as f:
                 lines = f.readlines()
                 action_body = []
                 for line in lines:
                     match = re.match(ActionParser.import_module_pattern,
                                      line)
                     if match:
                         options.append(match.group(0).strip())
                     else:
                         action_body.append(line)
                 action_body = ''.join(action_body)
                 action_body = action_body.strip()
         expression_parser = ExpressionParser(action_body)
         ast = expression_parser.get_ast()
         function = Function(action_name,
                             body=ast.body,
                             return_value=ast.return_value)
         function.params = ast.params
         if not isinstance(function, Function):
             raise ValueError(
                 f'Type of function[{function}] should be Function !!')
         if lang:
             function.lang = lang
         if options:
             function.options = options
         actions.append(function)
     return actions
Exemplo n.º 21
0
 def _parse_events(self, events):
     parser = ExpressionParser(events)
     expression = parser.get_ast()
     return [Event(exp) for exp in expression] if isinstance(
         expression, Expression) else [Event(expression)]
Exemplo n.º 22
0
 def _parse_condition(self, condition):
     parser = ExpressionParser(condition)
     return parser.get_ast()
Exemplo n.º 23
0
 def test_Variable_EQUAL_ActionArg0Arg1Body_DoubleBraces__Valid(self):
     example = 'k == isAction(arg0, arg1) { { arg0 = arg1; } }'
     parser = ExpressionParser(example)
Exemplo n.º 24
0
 def test__OperatorVariable_AND_Variable__Invalid(self):
     example = "{++k && l}"
     with self.assertRaises(ValidationError) as context:
         parser = ExpressionParser(example)
Exemplo n.º 25
0
 def test__VariableComplexIndexer_AND_Variable__Invalid(self):
     example = 'k[{arr[1]}] && l'
     with self.assertRaises(ValidationError) as context:
         parser = ExpressionParser(example)
Exemplo n.º 26
0
 def test__VariableIndexer_EQUAL_Variable__Invalid(self):
     example = 'k[] == l'
     with self.assertRaises(ValidationError) as context:
         parser = ExpressionParser(example)
Exemplo n.º 27
0
 def test__InlineAction__Valid(self):
     example = "Action() { int l = are; }"
     parser = ExpressionParser(example)
     ast = parser.get_ast()
     self.assertEqual(type(ast), Function)
     self.assertEqual(ast.name, 'Action')
Exemplo n.º 28
0
 def test_Variable_AND_VariableIndexer__Invalid(self):
     example = 'k && l[{1}]'
     with self.assertRaises(ValidationError) as context:
         parser = ExpressionParser(example)
Exemplo n.º 29
0
 def _parse_actions(self, actions):
     parser = ExpressionParser(actions)
     expression = parser.get_ast()
     return [exp for exp in expression] if isinstance(
         expression, Expression) else [expression]