Skip to content

Commit

Permalink
core: Create ParserState (NFC) (xdslproject#1114)
Browse files Browse the repository at this point in the history
`ParserState` is used to encapsulate the state of a given parser.
This will allow us to share the state between parsers, so we can better
separate the parser code.

For instance, when encountering an affine expression, an `AffineParser`
will be created with that state, and will take care of parsing the
expression.
  • Loading branch information
math-fehr authored and s1837624 committed Jun 17, 2023
1 parent 1961ca1 commit 7624c7d
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions xdsl/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ def operand_name(self) -> str:
return self.span.text[1:]


@dataclass(init=False)
class ParserState:
"""
The parser state. It contains the lexer, and the next token to parse.
The parser state should be shared between all parsers, so parsers can
share the same position.
"""

lexer: Lexer
current_token: Token

def __init__(self, lexer: Lexer):
self.lexer = lexer
self.current_token = lexer.lex()


class Parser(ABC):
"""
Basic recursive descent parser.
Expand Down Expand Up @@ -212,10 +228,7 @@ class Parser(ABC):
This field map a name and a tuple index to the forward declared SSA value.
"""

lexer: Lexer

_current_token: Token
"""Token at the current location"""
parser_state: ParserState

T_ = TypeVar("T_")
"""
Expand All @@ -231,9 +244,8 @@ def __init__(
input: str,
name: str = "<unknown>",
allow_unregistered_dialect: bool = False,
):
self.lexer = Lexer(Input(input, name))
self._current_token = self.lexer.lex()
) -> None:
self.parser_state = ParserState(Lexer(Input(input, name)))
self.ctx = ctx
self.ssa_values = dict()
self.blocks = dict()
Expand All @@ -248,6 +260,18 @@ def resume_from(self, pos: Position):
self.lexer.pos = pos
self._current_token = self.lexer.lex()

@property
def _current_token(self) -> Token:
return self.parser_state.current_token

@_current_token.setter
def _current_token(self, token: Token):
self.parser_state.current_token = token

@property
def lexer(self) -> Lexer:
return self.parser_state.lexer

@property
def pos(self) -> Position:
"""Get the position of the next token."""
Expand Down

0 comments on commit 7624c7d

Please sign in to comment.