def test_bool_literal(self):
     x = parser.parse_literal('true')
     self.assertIsInstance(x, ast.BoolLiteral)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 5))
     self.assertEqual(x.value, True)
     x = parser.parse_literal('false')
     self.assertIsInstance(x, ast.BoolLiteral)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 6))
     self.assertEqual(x.value, False)
Exemple #2
0
 def get_range(self, ctx: antlr4.ParserRuleContext) -> ast.Range:
     start_tok = ctx.start  # type: antlr4.Token
     end_tok = self._token_stream.get(ctx.stop.tokenIndex +
                                      1)  # type: antlr4.Token
     file_name = start_tok.getInputStream().name
     return ast.Range(file_name, start_tok.line, start_tok.column + 1,
                      end_tok.line, end_tok.column + 1)
 def test_list_literal(self):
     x = parser.parse_literal('[1, (true, \"abc\"), ()]')
     self.assertIsInstance(x, ast.ListLiteral)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 23))
     self.assertEqual(len(x.expressions), 3)
     self.assertIsInstance(x.expressions[0], ast.IntLiteral)
     self.assertEqual(x.expressions[0].value, 1)
     self.assertIsInstance(x.expressions[1], ast.TupleLiteral)
     self.assertEqual(x.expressions[1].range,
                      ast.Range('<empty>', 1, 5, 1, 18))
     self.assertEqual(len(x.expressions[1].expressions), 2)
     self.assertIsInstance(x.expressions[1].expressions[0], ast.BoolLiteral)
     self.assertEqual(x.expressions[1].expressions[0].value, True)
     self.assertIsInstance(x.expressions[1].expressions[1],
                           ast.StringLiteral)
     self.assertEqual(x.expressions[1].expressions[1].value, 'abc')
     self.assertIsInstance(x.expressions[2], ast.TupleLiteral)
     self.assertEqual(x.expressions[2].range,
                      ast.Range('<empty>', 1, 20, 1, 22))
     self.assertEqual(len(x.expressions[2].expressions), 0)
 def test_expr_priority(self):
     x = parser.parse_expression('-1 + 2 * + 3')
     self.assertIsInstance(x, ast.BinaryOperatorExpression)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 13))
     x_lhs = x.lhs
     self.assertIsInstance(x_lhs, ast.PrefixOperatorExpression)
     self.assertEqual(x_lhs.range, ast.Range('<empty>', 1, 1, 1, 3))
     self.assertEqual(x_lhs.op, '-')
     self.assertIsInstance(x_lhs.expr, ast.IntLiteral)
     self.assertEqual(x_lhs.expr.value, 1)
     x_rhs = x.rhs
     self.assertIsInstance(x_rhs, ast.BinaryOperatorExpression)
     self.assertEqual(x_rhs.range, ast.Range('<empty>', 1, 6, 1, 13))
     x_rhs_lhs = x_rhs.lhs
     self.assertIsInstance(x_rhs_lhs, ast.IntLiteral)
     self.assertEqual(x_rhs_lhs.range, ast.Range('<empty>', 1, 6, 1, 7))
     self.assertEqual(x_rhs_lhs.value, 2)
     x_rhs_rhs = x_rhs.rhs
     self.assertIsInstance(x_rhs_rhs, ast.PrefixOperatorExpression)
     self.assertEqual(x_rhs_rhs.range, ast.Range('<empty>', 1, 10, 1, 13))
     self.assertEqual(x_rhs_rhs.op, '+')
     x_rhs_rhs_expr = x_rhs_rhs.expr
     self.assertIsInstance(x_rhs_rhs_expr, ast.IntLiteral)
     self.assertEqual(x_rhs_rhs_expr.range,
                      ast.Range('<empty>', 1, 12, 1, 13))
     self.assertEqual(x_rhs_rhs_expr.value, 3)
 def test_literal_parsing(self):
     x = parser.parse_literal('123')
     self.assertIsInstance(x, ast.IntLiteral)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 4))
     self.assertEqual(x.value, 123)
 def test_string_literal(self):
     x = parser.parse_literal('\"some\\ntext\"')
     self.assertIsInstance(x, ast.StringLiteral)
     self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 13))
     self.assertEqual(x.value, 'some\ntext')
Exemple #7
0
 def p_range_step(self, p):
     """
      range : expression COLON expression COLON expression
     """
     p[0] = ast.Range(p[1], p[3], p.lexer.lexer.lineno, p[5])