feat(parser): add slice expression
This commit is contained in:
@@ -148,4 +148,10 @@ class SubscriptExpr:
|
||||
index: Expr
|
||||
|
||||
|
||||
class SliceExpr:
|
||||
lower: Optional[Expr]
|
||||
upper: Optional[Expr]
|
||||
step: Optional[Expr]
|
||||
|
||||
|
||||
###<
|
||||
|
||||
@@ -729,3 +729,10 @@ class PythonAstPrinter(
|
||||
self._write_line("index", last=True)
|
||||
with self._child_level(single=True):
|
||||
expr.index.accept(self)
|
||||
|
||||
def visit_slice_expr(self, expr: p.SliceExpr) -> None:
|
||||
self._write_line("SliceExpr")
|
||||
with self._child_level():
|
||||
self._write_optional_child("lower", expr.lower)
|
||||
self._write_optional_child("upper", expr.upper)
|
||||
self._write_optional_child("step", expr.step, last=True)
|
||||
|
||||
@@ -227,6 +227,9 @@ class Expr(ABC):
|
||||
@abstractmethod
|
||||
def visit_subscript_expr(self, expr: SubscriptExpr) -> T: ...
|
||||
|
||||
@abstractmethod
|
||||
def visit_slice_expr(self, expr: SliceExpr) -> T: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BinaryExpr(Expr):
|
||||
@@ -336,3 +339,13 @@ class SubscriptExpr(Expr):
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_subscript_expr(self)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SliceExpr(Expr):
|
||||
lower: Optional[Expr]
|
||||
upper: Optional[Expr]
|
||||
step: Optional[Expr]
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_slice_expr(self)
|
||||
|
||||
@@ -222,6 +222,14 @@ class PythonHighlighter(
|
||||
expr.object.accept(self)
|
||||
expr.index.accept(self)
|
||||
|
||||
def visit_slice_expr(self, expr: p.SliceExpr) -> None:
|
||||
if expr.lower is not None:
|
||||
expr.lower.accept(self)
|
||||
if expr.upper is not None:
|
||||
expr.upper.accept(self)
|
||||
if expr.step is not None:
|
||||
expr.step.accept(self)
|
||||
|
||||
|
||||
class MidasHighlighter(
|
||||
Highlighter, m.Stmt.Visitor[None], m.Expr.Visitor[None], m.Type.Visitor[None]
|
||||
|
||||
@@ -22,6 +22,7 @@ from midas.ast.python import (
|
||||
LogicalExpr,
|
||||
MidasType,
|
||||
ReturnStmt,
|
||||
SliceExpr,
|
||||
Stmt,
|
||||
SubscriptExpr,
|
||||
TernaryExpr,
|
||||
@@ -431,6 +432,14 @@ class PythonParser:
|
||||
index=self.parse_expr(index),
|
||||
)
|
||||
|
||||
case ast.Slice(lower=lower, upper=upper, step=step):
|
||||
return SliceExpr(
|
||||
location=location,
|
||||
lower=self.parse_expr(lower) if lower is not None else None,
|
||||
upper=self.parse_expr(upper) if upper is not None else None,
|
||||
step=self.parse_expr(step) if step is not None else None,
|
||||
)
|
||||
|
||||
case _:
|
||||
raise UnsupportedSyntaxError(node)
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ from midas.ast.python import (
|
||||
LogicalExpr,
|
||||
MidasType,
|
||||
ReturnStmt,
|
||||
SliceExpr,
|
||||
Stmt,
|
||||
SubscriptExpr,
|
||||
TernaryExpr,
|
||||
@@ -260,3 +261,11 @@ class PythonAstJsonSerializer(
|
||||
"object": expr.object.accept(self),
|
||||
"index": expr.index.accept(self),
|
||||
}
|
||||
|
||||
def visit_slice_expr(self, expr: SliceExpr) -> dict:
|
||||
return {
|
||||
"_type": "SliceExpr",
|
||||
"lower": self._serialize_optional(expr.lower),
|
||||
"upper": self._serialize_optional(expr.upper),
|
||||
"step": self._serialize_optional(expr.step),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user