Skip to content

Commit

Permalink
core: split out non-MLIR-specific parser functionality to GenericParser
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed Dec 8, 2024
1 parent f18858c commit 189b337
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 309 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ max-line-length = 300
"xdsl.parser.attribute_parser".msg = "Use xdsl.parser instead."
"xdsl.parser.base_parser".msg = "Use xdsl.parser instead."
"xdsl.parser.core".msg = "Use xdsl.parser instead."
"xdsl.parser.generic_parser".msg = "Use xdsl.parser instead."


[tool.ruff.lint.per-file-ignores]
Expand Down
2 changes: 1 addition & 1 deletion tests/filecheck/parser-printer/aliases.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#attr = 0

"test.op"() {"attr" = #attr : i32} : () -> ()
// CHECK: Expected '}'
// CHECK: '}' expected

// -----

Expand Down
11 changes: 7 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from io import StringIO
from typing import cast

Expand Down Expand Up @@ -569,18 +570,20 @@ def test_parse_comma_separated_list_error_delimiters(
):
input = open_bracket + "2, 4 5"
parser = Parser(MLContext(), input)
with pytest.raises(ParseError) as e:
with pytest.raises(
ParseError, match=re.escape(f"'{close_bracket}' expected in test")
) as e:
parser.parse_comma_separated_list(delimiter, parser.parse_integer, " in test")
assert e.value.span.text == "5"
assert e.value.msg == "Expected '" + close_bracket + "' in test"

parser = Parser(MLContext(), input)
with pytest.raises(ParseError) as e:
with pytest.raises(
ParseError, match=re.escape(f"'{close_bracket}' expected in test")
) as e:
parser.parse_optional_comma_separated_list(
delimiter, parser.parse_integer, " in test"
)
assert e.value.span.text == "5"
assert e.value.msg == "Expected '" + close_bracket + "' in test"


@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions xdsl/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
# We need to skip it here to allow importing from here instead.
from .affine_parser import * # noqa: TID251
from .attribute_parser import * # noqa: TID251
from .base_parser import * # noqa: TID251
from .core import * # noqa: TID251
from .generic_parser import * # noqa: TID251
5 changes: 3 additions & 2 deletions xdsl/parser/affine_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from xdsl.utils.exceptions import ParseError
from xdsl.utils.mlir_lexer import MLIRToken, MLIRTokenKind

from .base_parser import BaseParser, ParserState # noqa: TID251
from .base_parser import BaseParser # noqa: TID251
from .generic_parser import ParserState # noqa: TID251


class AffineParser(BaseParser):
Expand All @@ -23,7 +24,7 @@ class AffineParser(BaseParser):
"mod": 20,
}

def __init__(self, state: ParserState) -> None:
def __init__(self, state: ParserState[MLIRTokenKind]) -> None:
self._resume_from(state)

def _parse_primary(self, dims: list[str], syms: list[str]) -> AffineExpr:
Expand Down
Loading

0 comments on commit 189b337

Please sign in to comment.