|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""The core parser""" |
| 18 | +import ast |
| 19 | +from typing import Any, Dict, List, Optional, Union |
| 20 | + |
| 21 | +from ..builder import def_ |
| 22 | +from . import dispatch |
| 23 | +from .evaluator import eval_assign, eval_expr |
| 24 | +from .utils import deferred |
| 25 | +from .var_table import VarTable |
| 26 | + |
| 27 | + |
| 28 | +def _dispatch(self: "Parser", type_name: str) -> dispatch.ParseMethod: |
| 29 | + for token in [self.dispatch_tokens[-1], "default"]: |
| 30 | + func = dispatch.get(token=token, type_name=type_name, default=None) |
| 31 | + if func is not None: |
| 32 | + return func |
| 33 | + return lambda self, node: self.generic_visit(node) |
| 34 | + |
| 35 | + |
| 36 | +def _handle_function(self: "Parser", node: ast.FunctionDef) -> None: |
| 37 | + if not node.decorator_list: |
| 38 | + self.report_error(node, "Function must be decorated") |
| 39 | + # TODO: only the last decorator is parsed |
| 40 | + decorator = self.eval_expr(node.decorator_list[-1]) |
| 41 | + if hasattr(decorator, "dispatch_token"): |
| 42 | + token = decorator.dispatch_token |
| 43 | + func = dispatch.get(token=token, type_name="FunctionDef", default=None) |
| 44 | + if func is not None: |
| 45 | + func(self, node) |
| 46 | + return |
| 47 | + self.report_error(node, "The parser does not understand the decorator") |
| 48 | + |
| 49 | + |
| 50 | +class Parser(ast.NodeVisitor): |
| 51 | + """The TVMScript parser""" |
| 52 | + |
| 53 | + dispatch_tokens: List[str] |
| 54 | + var_table: VarTable |
| 55 | + |
| 56 | + def __init__(self) -> None: |
| 57 | + self.dispatch_tokens = ["default"] |
| 58 | + self.var_table = VarTable() |
| 59 | + |
| 60 | + def with_dispatch_token(self, token: str): |
| 61 | + def pop_token(): |
| 62 | + self.dispatch_tokens.pop() |
| 63 | + |
| 64 | + self.dispatch_tokens.append(token) |
| 65 | + return deferred(pop_token) |
| 66 | + |
| 67 | + def eval_expr( |
| 68 | + self, |
| 69 | + node: Union[ast.Expression, ast.expr], |
| 70 | + extra_vars: Optional[Dict[str, Any]] = None, |
| 71 | + ) -> Any: |
| 72 | + var_values = self.var_table.get() |
| 73 | + if extra_vars is not None: |
| 74 | + for k, v in extra_vars.items(): |
| 75 | + var_values[k] = v |
| 76 | + return eval_expr(node, var_values) |
| 77 | + |
| 78 | + def eval_assign( |
| 79 | + self, |
| 80 | + target: ast.expr, |
| 81 | + source: Any, |
| 82 | + ) -> Dict[str, Any]: |
| 83 | + var_values = eval_assign(target, source) |
| 84 | + for k, v in var_values.items(): |
| 85 | + def_(k, v) |
| 86 | + self.var_table.add(k, v) |
| 87 | + return var_values |
| 88 | + |
| 89 | + def report_error(self, node: ast.AST, msg: str) -> None: # pylint: disable=no-self-use |
| 90 | + raise SyntaxError(f"At {node.lineno}:{node.col_offset}: {msg}") |
| 91 | + |
| 92 | + def visit_FunctionDef(self, node: ast.FunctionDef) -> Any: # pylint: disable=invalid-name |
| 93 | + _handle_function(self, node) |
| 94 | + |
| 95 | + def visit_body(self, node: List[ast.stmt]) -> Any: |
| 96 | + for stmt in node: |
| 97 | + self.visit(stmt) |
| 98 | + |
| 99 | + def visit_arguments(self, node: ast.arguments) -> Any: |
| 100 | + _dispatch(self, "arguments")(self, node) |
| 101 | + |
| 102 | + def visit_For(self, node: ast.For) -> Any: # pylint: disable=invalid-name |
| 103 | + _dispatch(self, "For")(self, node) |
| 104 | + |
| 105 | + def visit_With(self, node: ast.With) -> Any: # pylint: disable=invalid-name |
| 106 | + _dispatch(self, "With")(self, node) |
| 107 | + |
| 108 | + def visit_Assign(self, node: ast.Assign) -> Any: # pylint: disable=invalid-name |
| 109 | + _dispatch(self, "Assign")(self, node) |
0 commit comments