review: Initial fixup

pull/6316/head
Daniel Mensinger 5 years ago
parent ad5df1b9c3
commit ab988198c7
No known key found for this signature in database
GPG Key ID: 54DD94C131E277D4
  1. 10
      mesonbuild/ast/interpreter.py
  2. 2
      mesonbuild/cmake/interpreter.py
  3. 10
      mesonbuild/interpreter.py
  4. 11
      mesonbuild/interpreterbase.py
  5. 59
      mesonbuild/mparser.py
  6. 4
      mesonbuild/rewriter.py

@ -19,7 +19,7 @@ from .visitor import AstVisitor
from .. import interpreterbase, mparser, mesonlib
from .. import environment
from ..interpreterbase import InvalidArguments, BreakRequest, ContinueRequest, TYPE_nvar
from ..interpreterbase import InvalidArguments, BreakRequest, ContinueRequest, TYPE_nvar, TYPE_nkwargs
from ..mparser import (
AndNode,
ArgumentNode,
@ -144,7 +144,7 @@ class AstInterpreter(interpreterbase.InterpreterBase):
sys.stderr.write('Unable to evaluate subdir({}) in AstInterpreter --> Skipping\n'.format(args))
return
prev_subdir = self.subdir # type: str
prev_subdir = self.subdir
subdir = os.path.join(prev_subdir, args[0])
absdir = os.path.join(self.source_root, subdir)
buildfilename = os.path.join(subdir, environment.build_filename)
@ -194,7 +194,7 @@ class AstInterpreter(interpreterbase.InterpreterBase):
def evaluate_plusassign(self, node: PlusAssignmentNode) -> None:
assert(isinstance(node, PlusAssignmentNode))
# Cheat by doing a reassignment
self.assignments[node.var_name] = node.value # Save a reference to the value node
self.assignments[node.var_name] = node.value # Save a reference to the value node
if node.value.ast_id:
self.reverse_assignment[node.value.ast_id] = node
self.assign_vals[node.var_name] = self.evaluate_statement(node.value)
@ -205,9 +205,9 @@ class AstInterpreter(interpreterbase.InterpreterBase):
def unknown_function_called(self, func_name: str) -> None:
pass
def reduce_arguments(self, args: ArgumentNode, resolve_key_nodes: bool = True) -> T.Tuple[list, dict]:
def reduce_arguments(self, args: ArgumentNode, resolve_key_nodes: bool = True) -> T.Tuple[T.List[TYPE_nvar], TYPE_nkwargs]:
if isinstance(args, ArgumentNode):
kwargs = {} # type: T.Dict[T.Union[str, BaseNode], BaseNode]
kwargs = {} # type: T.Dict[T.Union[str, BaseNode], TYPE_nvar]
for key, val in args.kwargs.items():
if isinstance(key, (StringNode, IdNode)):
assert isinstance(key.value, str)

@ -978,7 +978,7 @@ class CMakeInterpreter:
if isinstance(value, str):
return string(value)
elif isinstance(value, bool):
return BooleanNode(token(), value)
return BooleanNode(token(val=value))
elif isinstance(value, int):
return number(value)
elif isinstance(value, list):

@ -4425,15 +4425,15 @@ Try setting b_lundef to false instead.'''.format(self.coredata.base_options['b_s
ef = extract_as_list(kwargs, 'extra_files')
kwargs['extra_files'] = self.source_strings_to_files(ef)
self.check_sources_exist(os.path.join(self.source_root, self.subdir), sources)
if targetholder is ExecutableHolder:
if targetholder == ExecutableHolder:
targetclass = build.Executable
elif targetholder is SharedLibraryHolder:
elif targetholder == SharedLibraryHolder:
targetclass = build.SharedLibrary
elif targetholder is SharedModuleHolder:
elif targetholder == SharedModuleHolder:
targetclass = build.SharedModule
elif targetholder is StaticLibraryHolder:
elif targetholder == StaticLibraryHolder:
targetclass = build.StaticLibrary
elif targetholder is JarHolder:
elif targetholder == JarHolder:
targetclass = build.Jar
else:
mlog.debug('Unknown target type:', str(targetholder))

@ -19,8 +19,9 @@ from . import mparser, mesonlib, mlog
from . import environment, dependencies
import os, copy, re
import collections.abc
from functools import wraps
from typing import Any, Callable, Dict, List, Set, Sequence, Tuple, Optional, Union
from typing import Any, Callable, ClassVar, Dict, Generic, List, Set, Sequence, Tuple, TypeVar, Optional, Union
class InterpreterObject:
def __init__(self):
@ -37,7 +38,9 @@ class InterpreterObject:
return method(args, kwargs)
raise InvalidCode('Unknown method "%s" in object.' % method_name)
class ObjectHolder:
TV_InterpreterObject = TypeVar('TV_InterpreterObject')
class ObjectHolder(Generic[TV_InterpreterObject]):
def __init__(self, obj: InterpreterObject, subproject: Optional[str] = None):
self.held_object = obj # type: InterpreterObject
self.subproject = subproject # type: str
@ -125,7 +128,7 @@ def flatten(args: Union[TYPE_nvar, List[TYPE_nvar]]) -> List[TYPE_nvar]:
if isinstance(args, mparser.StringNode):
assert isinstance(args.value, str)
return [args.value]
if isinstance(args, (int, float, bool, str, ObjectHolder, mparser.BaseNode, mesonlib.File, InterpreterObject)):
if not isinstance(args, collections.abc.Sequence):
return [args]
result = [] # type: List[TYPE_nvar]
for a in args:
@ -203,7 +206,7 @@ class FeatureCheckBase:
# Class variable, shared across all instances
#
# Format: {subproject: {feature_version: set(feature_names)}}
feature_registry = {} # type: Dict[str, Dict[str, Set[str]]]
feature_registry = {} # type: ClassVar[Dict[str, Dict[str, Set[str]]]]
def __init__(self, feature_name: str, version: str) -> None:
self.feature_name = feature_name # type: str

@ -17,7 +17,7 @@ import codecs
import textwrap
import types
import typing as T
from typing import Dict, List, Tuple, Optional, Union, TYPE_CHECKING
from typing import Dict, Generic, Generator, List, Tuple, TypeVar, Optional, Union, TYPE_CHECKING
from .mesonlib import MesonException
from . import mlog
@ -77,15 +77,17 @@ class BlockParseException(MesonException):
self.lineno = lineno
self.colno = colno
class Token:
def __init__(self, tid: str, filename: str, line_start: int, lineno: int, colno: int, bytespan: Tuple[int, int], value: Union[str, bool, int]) -> None:
TV_TokenTypes = TypeVar('TV_TokenTypes', int, str, bool)
class Token(Generic[TV_TokenTypes]):
def __init__(self, tid: str, filename: str, line_start: int, lineno: int, colno: int, bytespan: Tuple[int, int], value: TV_TokenTypes) -> None:
self.tid = tid # type: str
self.filename = filename # type: str
self.line_start = line_start # type: int
self.lineno = lineno # type: int
self.colno = colno # type: int
self.bytespan = bytespan # type: Tuple[int, int]
self.value = value # type: Union[str, bool, int]
self.value = value # type: TV_TokenTypes
def __eq__(self, other) -> bool:
if isinstance(other, str):
@ -244,35 +246,34 @@ class BaseNode:
if callable(func):
func(self)
class ElementaryNode(BaseNode):
def __init__(self, token: Token) -> None:
class ElementaryNode(Generic[TV_TokenTypes], BaseNode):
def __init__(self, token: Token[TV_TokenTypes]) -> None:
super().__init__(token.lineno, token.colno, token.filename)
self.value = token.value
self.bytespan = token.bytespan
self.value = token.value # type: TV_TokenTypes
self.bytespan = token.bytespan # type: Tuple[int, int]
class BooleanNode(ElementaryNode):
def __init__(self, token: Token, value: bool) -> None:
class BooleanNode(ElementaryNode[bool]):
def __init__(self, token: Token[bool]) -> None:
super().__init__(token)
self.value = value
assert(isinstance(self.value, bool))
assert isinstance(self.value, bool)
class IdNode(ElementaryNode):
def __init__(self, token: Token) -> None:
class IdNode(ElementaryNode[str]):
def __init__(self, token: Token[str]) -> None:
super().__init__(token)
assert(isinstance(self.value, str))
assert isinstance(self.value, str)
def __str__(self):
return "Id node: '%s' (%d, %d)." % (self.value, self.lineno, self.colno)
class NumberNode(ElementaryNode):
def __init__(self, token: Token) -> None:
class NumberNode(ElementaryNode[int]):
def __init__(self, token: Token[int]) -> None:
super().__init__(token)
assert(isinstance(self.value, int))
assert isinstance(self.value, int)
class StringNode(ElementaryNode):
def __init__(self, token: Token) -> None:
class StringNode(ElementaryNode[str]):
def __init__(self, token: Token[str]) -> None:
super().__init__(token)
assert(isinstance(self.value, str))
assert isinstance(self.value, str)
def __str__(self):
return "String node: '%s' (%d, %d)." % (self.value, self.lineno, self.colno)
@ -284,10 +285,10 @@ class BreakNode(ElementaryNode):
pass
class ArgumentNode(BaseNode):
def __init__(self, token: Token) -> None:
def __init__(self, token: Token[TV_TokenTypes]) -> None:
super().__init__(token.lineno, token.colno, token.filename)
self.arguments = [] # type: List[BaseNode]
self.commas = [] # type: List[Token]
self.commas = [] # type: List[Token[TV_TokenTypes]]
self.kwargs = {} # type: Dict[BaseNode, BaseNode]
self.order_error = False
@ -366,12 +367,12 @@ class ArithmeticNode(BaseNode):
self.operation = operation # type: str
class NotNode(BaseNode):
def __init__(self, token: Token, value: BaseNode) -> None:
def __init__(self, token: Token[TV_TokenTypes], value: BaseNode) -> None:
super().__init__(token.lineno, token.colno, token.filename)
self.value = value # type: BaseNode
class CodeBlockNode(BaseNode):
def __init__(self, token: Token) -> None:
def __init__(self, token: Token[TV_TokenTypes]) -> None:
super().__init__(token.lineno, token.colno, token.filename)
self.lines = [] # type: List[BaseNode]
@ -470,7 +471,7 @@ class Parser:
def __init__(self, code: str, filename: str) -> None:
self.lexer = Lexer(code)
self.stream = self.lexer.lex(filename)
self.current = Token('eof', '', 0, 0, 0, (0, 0), None)
self.current = Token('eof', '', 0, 0, 0, (0, 0), None) # type: Token
self.getsym()
self.in_ternary = False
@ -643,9 +644,11 @@ class Parser:
def e9(self) -> BaseNode:
t = self.current
if self.accept('true'):
return BooleanNode(t, True)
t.value = True
return BooleanNode(t)
if self.accept('false'):
return BooleanNode(t, False)
t.value = False
return BooleanNode(t)
if self.accept('id'):
return IdNode(t)
if self.accept('number'):

@ -159,7 +159,7 @@ class MTypeBool(MTypeBase):
super().__init__(node)
def _new_node(self):
return StringNode(Token('', '', 0, 0, 0, None, False))
return BooleanNode(Token('', '', 0, 0, 0, None, False))
def supported_nodes(self):
return [BooleanNode]
@ -172,7 +172,7 @@ class MTypeID(MTypeBase):
super().__init__(node)
def _new_node(self):
return StringNode(Token('', '', 0, 0, 0, None, ''))
return IdNode(Token('', '', 0, 0, 0, None, ''))
def supported_nodes(self):
return [IdNode]

Loading…
Cancel
Save