Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions gtwrap/interface_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from .template import *
from .tokens import *
from .type import *
from .variable import *
from .diagnostics import InterfaceParseError, track_rule as _track_rule

# Fix deepcopy issue with pyparsing
# Can remove once https://github.com/pyparsing/pyparsing/issues/208 is resolved.
Expand All @@ -42,4 +44,30 @@ def fixed_get_attr(self, item):
# apply the monkey-patch
pyparsing.ParseResults.__getattr__ = fixed_get_attr

_DIAGNOSTIC_RULES = (
(Type.rule, "type", 10),
(TemplatedType.rule, "templated type", 20),
(Argument.rule, "argument", 100),
(ArgumentList.rule, "argument list", 90),
(ReturnType.rule, "return type", 30),
(Method.rule, "method declaration", 80),
(StaticMethod.rule, "static method declaration", 80),
(Constructor.rule, "constructor declaration", 80),
(Operator.rule, "operator declaration", 80),
(DunderMethod.rule, "dunder method declaration", 80),
(Variable.rule, "variable declaration", 70),
(Enumerator.rule, "enumerator", 100),
(Enum.rule, "enum declaration", 70),
(Template.rule, "template declaration", 60),
(TypedefTemplateInstantiation.rule, "typedef declaration", 60),
(ForwardDeclaration.rule, "forward declaration", 60),
(Include.rule, "include declaration", 60),
(Class.rule, "class declaration", 50),
(Namespace.rule, "namespace declaration", 40),
(GlobalFunction.rule, "global function declaration", 50),
)

for _rule, _context, _priority in _DIAGNOSTIC_RULES:
_track_rule(_rule, _context, _priority)

pyparsing.ParserElement.enablePackrat()
68 changes: 54 additions & 14 deletions gtwrap/interface_parser/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .type import TemplatedType, Typename
from .utils import collect_namespaces
from .variable import Variable
from .diagnostics import semantic_error


class Method:
Expand Down Expand Up @@ -133,18 +134,23 @@ class Constructor:
+ ArgumentList.rule("args_list") #
+ RPAREN #
+ SEMI_COLON # BR
).setParseAction(lambda t: Constructor(t.name, t.args_list, t.template))
).setParseAction(lambda s, loc, t: Constructor(
t.name, t.args_list, t.template, source=s, location=loc))

def __init__(self,
name: str,
args: ArgumentList,
template: Union[Template, Any],
parent: Union["Class", Any] = ''):
parent: Union["Class", Any] = '',
source: str = '',
location: int = 0):
self.name = name
self.args = args
self.template = template

self.parent = parent
self.source = source
self.location = location

def __repr__(self) -> str:
return "Constructor: {}{}".format(self.name, self.args)
Expand All @@ -169,16 +175,25 @@ class Overload {
+ RPAREN #
+ CONST("is_const") #
+ SEMI_COLON # BR
).setParseAction(lambda t: Operator(t.name, t.operator, t.return_type, t.
args_list, t.is_const))
).setParseAction(lambda s, loc, t: Operator(
t.name,
t.operator,
t.return_type,
t.args_list,
t.is_const,
source=s,
location=loc,
))

def __init__(self,
name: str,
operator: str,
return_type: ReturnType,
args: ArgumentList,
is_const: str,
parent: Union["Class", Any] = ''):
parent: Union["Class", Any] = '',
source: str = '',
location: int = 0):
self.name = name
self.operator = operator
self.return_type = return_type
Expand All @@ -187,21 +202,41 @@ def __init__(self,
self.is_unary = len(args) == 0

self.parent = parent
self.source = source
self.location = location

# Check for valid unary operators
if self.is_unary and self.operator not in ('+', '-'):
raise ValueError("Invalid unary operator {} used for {}".format(
self.operator, self))
raise semantic_error(
source,
location,
"operator declaration",
f"unsupported unary operator '{self.operator}'",
)

# Check that number of arguments are either 0 or 1
assert 0 <= len(args) < 2, \
"Operator overload should be at most 1 argument, " \
"{} arguments provided".format(len(args))
if not 0 <= len(args) < 2:
raise semantic_error(
source,
location,
"operator declaration",
"operator overload must have at most one argument; "
f"found {len(args)}",
)

# Check to ensure arg and return type are the same.
if len(args) == 1 and self.operator not in ("()", "[]"):
assert args.list()[0].ctype.typename.name == return_type.type1.typename.name, \
"Mixed type overloading not supported. Both arg and return type must be the same."
argument_type = args.list()[0].ctype.typename.name
return_type_name = return_type.type1.typename.name
if argument_type != return_type_name:
raise semantic_error(
source,
location,
"operator declaration",
"mixed-type operator overloads are unsupported; "
f"argument type '{argument_type}' does not match return "
f"type '{return_type_name}'",
)

def __repr__(self) -> str:
return "Operator: {}{}{}({}) {}".format(
Expand Down Expand Up @@ -345,8 +380,13 @@ def __init__(
# Make sure ctors' names and class name are the same.
for ctor in self.ctors:
if ctor.name != self.name:
raise ValueError("Error in constructor name! {} != {}".format(
ctor.name, self.name))
raise semantic_error(
ctor.source,
ctor.location,
"constructor declaration",
f"constructor name '{ctor.name}' must match class "
f"name '{self.name}'",
)

for ctor in self.ctors:
ctor.parent = self
Expand Down
Loading
Loading