feat(parser): add subscript expressions
This commit is contained in:
@@ -143,4 +143,9 @@ class ListExpr:
|
||||
items: list[Expr]
|
||||
|
||||
|
||||
class SubscriptExpr:
|
||||
object: Expr
|
||||
index: Expr
|
||||
|
||||
|
||||
###<
|
||||
|
||||
@@ -664,7 +664,7 @@ class PythonAstPrinter(
|
||||
def visit_literal_expr(self, expr: p.LiteralExpr) -> None:
|
||||
self._write_line("LiteralExpr")
|
||||
with self._child_level(single=True):
|
||||
self._write_line(f"value: {expr.value}")
|
||||
self._write_line(f"value: {expr.value!r}")
|
||||
|
||||
def visit_variable_expr(self, expr: p.VariableExpr) -> None:
|
||||
self._write_line("VariableExpr")
|
||||
@@ -719,3 +719,13 @@ class PythonAstPrinter(
|
||||
if i == len(expr.items) - 1:
|
||||
self._mark_last()
|
||||
item.accept(self)
|
||||
|
||||
def visit_subscript_expr(self, expr: p.SubscriptExpr) -> None:
|
||||
self._write_line("SubscriptExpr")
|
||||
with self._child_level():
|
||||
self._write_line("object")
|
||||
with self._child_level(single=True):
|
||||
expr.object.accept(self)
|
||||
self._write_line("index", last=True)
|
||||
with self._child_level(single=True):
|
||||
expr.index.accept(self)
|
||||
|
||||
@@ -224,6 +224,9 @@ class Expr(ABC):
|
||||
@abstractmethod
|
||||
def visit_list_expr(self, expr: ListExpr) -> T: ...
|
||||
|
||||
@abstractmethod
|
||||
def visit_subscript_expr(self, expr: SubscriptExpr) -> T: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BinaryExpr(Expr):
|
||||
@@ -324,3 +327,12 @@ class ListExpr(Expr):
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_list_expr(self)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SubscriptExpr(Expr):
|
||||
object: Expr
|
||||
index: Expr
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_subscript_expr(self)
|
||||
|
||||
@@ -218,6 +218,10 @@ class PythonHighlighter(
|
||||
for item in expr.items:
|
||||
item.accept(self)
|
||||
|
||||
def visit_subscript_expr(self, expr: p.SubscriptExpr) -> None:
|
||||
expr.object.accept(self)
|
||||
expr.index.accept(self)
|
||||
|
||||
|
||||
class MidasHighlighter(
|
||||
Highlighter, m.Stmt.Visitor[None], m.Expr.Visitor[None], m.Type.Visitor[None]
|
||||
|
||||
@@ -23,6 +23,7 @@ from midas.ast.python import (
|
||||
MidasType,
|
||||
ReturnStmt,
|
||||
Stmt,
|
||||
SubscriptExpr,
|
||||
TernaryExpr,
|
||||
TypeAssign,
|
||||
UnaryExpr,
|
||||
@@ -423,6 +424,13 @@ class PythonParser:
|
||||
items=[self.parse_expr(item) for item in items],
|
||||
)
|
||||
|
||||
case ast.Subscript(value=value, slice=index):
|
||||
return SubscriptExpr(
|
||||
location=location,
|
||||
object=self.parse_expr(value),
|
||||
index=self.parse_expr(index),
|
||||
)
|
||||
|
||||
case _:
|
||||
raise UnsupportedSyntaxError(node)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ from midas.ast.python import (
|
||||
MidasType,
|
||||
ReturnStmt,
|
||||
Stmt,
|
||||
SubscriptExpr,
|
||||
TernaryExpr,
|
||||
TypeAssign,
|
||||
UnaryExpr,
|
||||
@@ -252,3 +253,10 @@ class PythonAstJsonSerializer(
|
||||
"_type": "ListExpr",
|
||||
"items": [item.accept(self) for item in expr.items],
|
||||
}
|
||||
|
||||
def visit_subscript_expr(self, expr: SubscriptExpr) -> dict:
|
||||
return {
|
||||
"_type": "SubscriptExpr",
|
||||
"object": expr.object.accept(self),
|
||||
"index": expr.index.accept(self),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user