Skip to content

Commit

Permalink
use xdsl BaseParser
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed Dec 7, 2024
1 parent 28bb142 commit 4476110
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 155 deletions.
38 changes: 17 additions & 21 deletions asl_xdsl/frontend/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@
import re
from typing import NamedTuple

from asl_xdsl.frontend.parser import Parser
from xdsl.parser import BaseParser

IDENTIFIER = re.compile("[A-z_][A-z_\\d]*")


def parse_optional_identifier(parser: Parser) -> str | None:
return parser.parse_optional_pattern(IDENTIFIER)


def parse_identifier(parser: Parser) -> str:
return parser.expect("identifier", parse_optional_identifier)


class Ty(NamedTuple):
ty: T_Exception

@staticmethod
def parse_ast(parser: Parser) -> Ty:
parser.parse_chars("annot (")
id = parser.expect("Ty", lambda parser: parser.peek_optional(IDENTIFIER))[0]
def parse_ast(parser: BaseParser) -> Ty:
parser.parse_characters("annot")
parser.parse_punctuation("(")
id = parser.expect(parser.parse_optional_identifier, "Ty")
if id != T_Exception.__name__:
raise NotImplementedError(f"Unimplemented type {id}")
ty = T_Exception.parse_ast(parser)
ty = T_Exception.parse_ast_tail(parser)
parser.parse_punctuation(")")
return Ty(ty)


Expand All @@ -34,20 +28,22 @@ class Field(NamedTuple):
ty: Ty

@staticmethod
def parse_ast(parser: Parser) -> Field:
def parse_ast(parser: BaseParser) -> Field:
raise NotImplementedError


class T_Exception(NamedTuple):
fields: tuple[Field, ...]

@staticmethod
def parse_ast(parser: Parser) -> T_Exception:
parser.parse_chars(T_Exception.__name__)
parser.parse_chars(" [")
fields = parser.parse_list(
Field.parse_ast,
lambda parser: parser.parse_chars(", "),
lambda parser: parser.parse_optional_chars("]"),
def parse_ast_tail(parser: BaseParser) -> T_Exception:
# parser.parse_characters(" ")
fields = parser.parse_comma_separated_list(
BaseParser.Delimiter.SQUARE, lambda: Field.parse_ast(parser)
)
return T_Exception(tuple(fields))

@staticmethod
def parse_ast(parser: BaseParser) -> T_Exception:
parser.parse_characters(T_Exception.__name__)
return T_Exception.parse_ast_tail(parser)
128 changes: 0 additions & 128 deletions asl_xdsl/frontend/parser.py

This file was deleted.

17 changes: 11 additions & 6 deletions tests/test_ast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import pytest
from xdsl.parser import BaseParser, ParserState
from xdsl.utils.lexer import Input, Lexer

from asl_xdsl.frontend.ast import T_Exception, Ty, parse_identifier
from asl_xdsl.frontend.parser import Parser
from asl_xdsl.frontend.ast import T_Exception, Ty


def p(input: str) -> BaseParser:
return BaseParser(ParserState(Lexer(Input(input, "<unknown>"))))


@pytest.mark.parametrize(
Expand All @@ -13,15 +18,15 @@
],
)
def test_parse_identifier(identifier: str):
parser = Parser(identifier)
assert parse_identifier(parser) == identifier
parser = p(identifier)
assert parser.parse_identifier() == identifier


def test_parse_exception():
parser = Parser("T_Exception []")
parser = p("T_Exception []")
assert T_Exception.parse_ast(parser) == T_Exception(())


def test_parse_type():
parser = Parser("annot (T_Exception [])")
parser = p("annot (T_Exception [])")
assert Ty.parse_ast(parser) == Ty(T_Exception(()))

0 comments on commit 4476110

Please sign in to comment.