parent
2e3550db14
commit
687eebee29
4 changed files with 237 additions and 177 deletions
@ -0,0 +1,79 @@ |
||||
# Copyright 2013-2021 The Meson development team |
||||
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
from .. import mparser |
||||
from .exceptions import InvalidCode |
||||
from .helpers import flatten |
||||
|
||||
import typing as T |
||||
|
||||
TV_fw_var = T.Union[str, int, float, bool, list, dict, 'InterpreterObject', 'ObjectHolder'] |
||||
TV_fw_args = T.List[T.Union[mparser.BaseNode, TV_fw_var]] |
||||
TV_fw_kwargs = T.Dict[str, T.Union[mparser.BaseNode, TV_fw_var]] |
||||
|
||||
TV_func = T.TypeVar('TV_func', bound=T.Callable[..., T.Any]) |
||||
|
||||
TYPE_elementary = T.Union[str, int, float, bool] |
||||
TYPE_var = T.Union[TYPE_elementary, T.List[T.Any], T.Dict[str, T.Any], 'InterpreterObject', 'ObjectHolder'] |
||||
TYPE_nvar = T.Union[TYPE_var, mparser.BaseNode] |
||||
TYPE_nkwargs = T.Dict[str, TYPE_nvar] |
||||
TYPE_key_resolver = T.Callable[[mparser.BaseNode], str] |
||||
|
||||
class InterpreterObject: |
||||
def __init__(self) -> None: |
||||
self.methods = {} # type: T.Dict[str, T.Callable[[T.List[TYPE_nvar], TYPE_nkwargs], TYPE_var]] |
||||
# Current node set during a method call. This can be used as location |
||||
# when printing a warning message during a method call. |
||||
self.current_node = None # type: mparser.BaseNode |
||||
|
||||
def method_call( |
||||
self, |
||||
method_name: str, |
||||
args: TV_fw_args, |
||||
kwargs: TV_fw_kwargs |
||||
) -> TYPE_var: |
||||
if method_name in self.methods: |
||||
method = self.methods[method_name] |
||||
if not getattr(method, 'no-args-flattening', False): |
||||
args = flatten(args) |
||||
return method(args, kwargs) |
||||
raise InvalidCode('Unknown method "%s" in object.' % method_name) |
||||
|
||||
class MutableInterpreterObject(InterpreterObject): |
||||
def __init__(self) -> None: |
||||
super().__init__() |
||||
|
||||
TV_InterpreterObject = T.TypeVar('TV_InterpreterObject') |
||||
|
||||
class ObjectHolder(T.Generic[TV_InterpreterObject]): |
||||
def __init__(self, obj: TV_InterpreterObject, subproject: str = '') -> None: |
||||
self.held_object = obj |
||||
self.subproject = subproject |
||||
|
||||
def __repr__(self) -> str: |
||||
return f'<Holder: {self.held_object!r}>' |
||||
|
||||
class RangeHolder(InterpreterObject): |
||||
def __init__(self, start: int, stop: int, step: int) -> None: |
||||
super().__init__() |
||||
self.range = range(start, stop, step) |
||||
|
||||
def __iter__(self) -> T.Iterator[int]: |
||||
return iter(self.range) |
||||
|
||||
def __getitem__(self, key: int) -> int: |
||||
return self.range[key] |
||||
|
||||
def __len__(self) -> int: |
||||
return len(self.range) |
@ -0,0 +1,107 @@ |
||||
# Copyright 2013-2021 The Meson development team |
||||
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
|
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
from .. import mparser, mlog |
||||
from .exceptions import InvalidArguments, InterpreterException |
||||
|
||||
import collections.abc |
||||
import typing as T |
||||
|
||||
if T.TYPE_CHECKING: |
||||
from .baseobjects import TYPE_nvar, TV_fw_args, TV_fw_kwargs |
||||
|
||||
def flatten(args: T.Union['TYPE_nvar', T.List['TYPE_nvar']]) -> T.List['TYPE_nvar']: |
||||
if isinstance(args, mparser.StringNode): |
||||
assert isinstance(args.value, str) |
||||
return [args.value] |
||||
if not isinstance(args, collections.abc.Sequence): |
||||
return [args] |
||||
result: T.List['TYPE_nvar'] = [] |
||||
for a in args: |
||||
if isinstance(a, list): |
||||
rest = flatten(a) |
||||
result = result + rest |
||||
elif isinstance(a, mparser.StringNode): |
||||
result.append(a.value) |
||||
else: |
||||
result.append(a) |
||||
return result |
||||
|
||||
def check_stringlist(a: T.Any, msg: str = 'Arguments must be strings.') -> None: |
||||
if not isinstance(a, list): |
||||
mlog.debug('Not a list:', str(a)) |
||||
raise InvalidArguments('Argument not a list.') |
||||
if not all(isinstance(s, str) for s in a): |
||||
mlog.debug('Element not a string:', str(a)) |
||||
raise InvalidArguments(msg) |
||||
|
||||
def default_resolve_key(key: mparser.BaseNode) -> str: |
||||
if not isinstance(key, mparser.IdNode): |
||||
raise InterpreterException('Invalid kwargs format.') |
||||
return key.value |
||||
|
||||
def get_callee_args(wrapped_args: T.Sequence[T.Any], want_subproject: bool = False) -> T.Tuple[T.Any, mparser.BaseNode, 'TV_fw_args', 'TV_fw_kwargs', T.Optional[str]]: |
||||
s = wrapped_args[0] |
||||
n = len(wrapped_args) |
||||
# Raise an error if the codepaths are not there |
||||
subproject = None # type: T.Optional[str] |
||||
if want_subproject and n == 2: |
||||
if hasattr(s, 'subproject'): |
||||
# Interpreter base types have 2 args: self, node |
||||
node = wrapped_args[1] |
||||
# args and kwargs are inside the node |
||||
args = None |
||||
kwargs = None |
||||
subproject = s.subproject |
||||
elif hasattr(wrapped_args[1], 'subproject'): |
||||
# Module objects have 2 args: self, interpreter |
||||
node = wrapped_args[1].current_node |
||||
# args and kwargs are inside the node |
||||
args = None |
||||
kwargs = None |
||||
subproject = wrapped_args[1].subproject |
||||
else: |
||||
raise AssertionError(f'Unknown args: {wrapped_args!r}') |
||||
elif n == 3: |
||||
# Methods on objects (*Holder, MesonMain, etc) have 3 args: self, args, kwargs |
||||
node = s.current_node |
||||
args = wrapped_args[1] |
||||
kwargs = wrapped_args[2] |
||||
if want_subproject: |
||||
if hasattr(s, 'subproject'): |
||||
subproject = s.subproject |
||||
elif hasattr(s, 'interpreter'): |
||||
subproject = s.interpreter.subproject |
||||
elif n == 4: |
||||
# Meson functions have 4 args: self, node, args, kwargs |
||||
# Module functions have 4 args: self, state, args, kwargs |
||||
from .interpreterbase import InterpreterBase # TODO: refactor to avoid this import |
||||
if isinstance(s, InterpreterBase): |
||||
node = wrapped_args[1] |
||||
else: |
||||
node = wrapped_args[1].current_node |
||||
args = wrapped_args[2] |
||||
kwargs = wrapped_args[3] |
||||
if want_subproject: |
||||
if isinstance(s, InterpreterBase): |
||||
subproject = s.subproject |
||||
else: |
||||
subproject = wrapped_args[1].subproject |
||||
else: |
||||
raise AssertionError(f'Unknown args: {wrapped_args!r}') |
||||
# Sometimes interpreter methods are called internally with None instead of |
||||
# empty list/dict |
||||
args = args if args is not None else [] |
||||
kwargs = kwargs if kwargs is not None else {} |
||||
return s, node, args, kwargs, subproject |
Loading…
Reference in new issue