Skip to content

Commit

Permalink
parser: yapf formatting run
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Jan 13, 2023
1 parent 81f4e0d commit 726dba6
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions xdsl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def print_with_context(self, msg: str | None = None) -> str:
offset = self.start - offset_of_first_line
remaining_len = max(self.len, 1)
capture = StringIO()
print("{}:{}:{}".format(self.input.name, line_no, offset), file=capture)
print("{}:{}:{}".format(self.input.name, line_no, offset),
file=capture)
for line in lines:
print(line, file=capture)
if remaining_len < 0:
Expand Down Expand Up @@ -714,8 +715,7 @@ def must_parse_block_arg_list(self) -> list[tuple[Span, Attribute]]:
args = self.must_parse_list_of(self.try_parse_value_id_and_type,
"Expected value-id and type here!")

self.must_parse_characters(')',
'Expected closing of block arguments!')
self.must_parse_characters(')', 'Expected closing of block arguments!')

return args

Expand Down Expand Up @@ -765,7 +765,7 @@ def must_parse_list_of(self,
items.append(first_item)

while (match := self.tokenizer.next_token_of_pattern(separator_pattern)
) is not None:
) is not None:
next_item = try_parse()
if next_item is None:
# if the separator is emtpy, we are good here
Expand Down Expand Up @@ -988,7 +988,7 @@ def must_parse_vector_attrs(self) -> AnyVectorType:

def must_parse_tensor_or_memref_dims(self) -> list[int] | None:
with self.tokenizer.configured(break_on=self.tokenizer.break_on +
('x',)):
('x', )):
# check for unranked-ness
if self.tokenizer.next_token_of_pattern('*') is not None:
# consume `x`
Expand Down Expand Up @@ -1046,7 +1046,8 @@ def must_parse_type_params(self) -> list[Attribute]:
params = self.must_parse_list_of(self.try_parse_type,
'Expected a type here!')

self.must_parse_characters('>', 'Expected end of type parameterization here!')
self.must_parse_characters(
'>', 'Expected end of type parameterization here!')

return params

Expand Down Expand Up @@ -1090,8 +1091,7 @@ def must_parse_operation(self) -> Operation:
if len(result_list) > 0:
self.must_parse_characters(
'=',
'Operation definitions expect an `=` after op-result-list!'
)
'Operation definitions expect an `=` after op-result-list!')

# check for custom op format
op_name = self.try_parse_bare_id()
Expand Down Expand Up @@ -1120,8 +1120,7 @@ def must_parse_operation(self) -> Operation:
result_types=ret_types,
attributes=attrs,
successors=[
self.blocks[block_name.text]
for block_name in successors
self.blocks[block_name.text] for block_name in successors
],
regions=regions)

Expand Down Expand Up @@ -1168,7 +1167,7 @@ def must_parse_region(self) -> Region:
'The following block references are dangling:',
[(span, "Reference to block \"{}\" without implementation!"
.format(span.text)) for span in itertools.chain(
*self.forward_block_references.values())],
*self.forward_block_references.values())],
self.tokenizer.history)

return region
Expand Down Expand Up @@ -1249,13 +1248,15 @@ def try_parse_builtin_attr(self) -> Attribute | None:

def try_parse_builtin_dense_attr(self) -> Attribute | None:
with self.tokenizer.backtracking("dense attribute"):
self.must_parse_characters("dense", "builtin dense attribute must start with `dense`")
self.must_parse_characters(
"dense", "builtin dense attribute must start with `dense`")
err_msg = "Malformed dense attribute, format must be (`dense<` array-attr `>:` type)"
self.must_parse_characters("<", err_msg)
info = list(self.must_parse_builtin_dense_attr_args())
self.must_parse_characters(">", err_msg)
self.must_parse_characters(":", err_msg)
type = self.expect(self.try_parse_type, "Dense attribute must be typed!")
type = self.expect(self.try_parse_type,
"Dense attribute must be typed!")
return DenseIntOrFPElementsAttr.from_list(type, info)

def must_parse_builtin_dense_attr_args(self) -> Iterable[int | float]:
Expand All @@ -1265,12 +1266,14 @@ def must_parse_builtin_dense_attr_args(self) -> Iterable[int | float]:
dense-attr-params := float-literal | int-literal | list-of-dense-attrs-params
list-of-dense-attrs-params := `[` dense-attr-params (`,` dense-attr-params)* `]`
"""

def try_parse_int_or_float():
if (literal := self.try_parse_float_literal()) is not None:
return float(literal.text)
if (literal := self.try_parse_integer_literal()) is not None:
return int(literal.text)
self.raise_error('Expected int or float literal here!')

if not self.tokenizer.starts_with('['):
yield try_parse_int_or_float()
return
Expand All @@ -1282,7 +1285,6 @@ def try_parse_int_or_float():
break
self.must_parse_characters(']', '')


def try_parse_ref_attr(self) -> FlatSymbolRefAttr | None:
if not self.tokenizer.starts_with("@"):
return None
Expand Down Expand Up @@ -1568,7 +1570,9 @@ def parse_op(self) -> Operation:
return self.must_parse_operation()

def parse_int_literal(self) -> int:
return int(self.expect(self.try_parse_integer_literal, 'Expected integer literal here').text)
return int(
self.expect(self.try_parse_integer_literal,
'Expected integer literal here').text)


class MLIRParser(BaseParser):
Expand Down Expand Up @@ -1804,9 +1808,12 @@ def must_parse_generic_attribute_args(self, name: StringLiteral):
self.raise_error("Unknown attribute name!", name)
if not issubclass(attr, ParametrizedAttribute):
self.raise_error("Expected ParametrizedAttribute name here!", name)
self.must_parse_characters('<', 'Expected generic attribute arguments here!')
args = self.must_parse_list_of(self.try_parse_attribute, 'Unexpected end of attribute list!')
self.must_parse_characters('>', 'Malformed attribute arguments, reached end of args list!')
self.must_parse_characters(
'<', 'Expected generic attribute arguments here!')
args = self.must_parse_list_of(self.try_parse_attribute,
'Unexpected end of attribute list!')
self.must_parse_characters(
'>', 'Malformed attribute arguments, reached end of args list!')
return attr(args)


Expand All @@ -1818,8 +1825,14 @@ class Source(Enum):
MLIR = 2


def Parser(ctx: MLContext, prog: str, source: Source = Source.XDSL, filename: str = '<unknown>') -> BaseParser:
selected_parser = {Source.XDSL: XDSLParser, Source.MLIR: MLIRParser}[source]
def Parser(ctx: MLContext,
prog: str,
source: Source = Source.XDSL,
filename: str = '<unknown>') -> BaseParser:
selected_parser = {
Source.XDSL: XDSLParser,
Source.MLIR: MLIRParser
}[source]
return selected_parser(ctx, prog, filename)


Expand Down

0 comments on commit 726dba6

Please sign in to comment.