Impl Tuple parsing

This commit is contained in:
Shunsuke Shibayama 2022-08-29 12:14:26 +09:00
parent e3135778d5
commit 2f225c4630
2 changed files with 117 additions and 30 deletions

View file

@ -130,7 +130,7 @@ impl KwArg {
pub struct Args {
pos_args: Vec<PosArg>,
kw_args: Vec<KwArg>,
paren: Option<(Token, Token)>,
pub paren: Option<(Token, Token)>,
}
impl NestedDisplay for Args {
@ -358,6 +358,10 @@ impl Accessor {
Self::Attr(Attribute::new(obj, name))
}
pub fn tuple_attr(obj: Expr, index: Literal) -> Self {
Self::TupleAttr(TupleAttribute::new(obj, index))
}
pub fn subscr(obj: Expr, index: Expr) -> Self {
Self::Subscr(Subscript::new(obj, index))
}
@ -497,8 +501,10 @@ pub struct NormalTuple {
}
impl NestedDisplay for NormalTuple {
fn fmt_nest(&self, f: &mut fmt::Formatter<'_>, _level: usize) -> fmt::Result {
write!(f, "({})", self.elems)
fn fmt_nest(&self, f: &mut fmt::Formatter<'_>, level: usize) -> fmt::Result {
writeln!(f, "(")?;
self.elems.fmt_nest(f, level + 1)?;
write!(f, "\n{})", " ".repeat(level))
}
}
@ -507,9 +513,7 @@ impl_locational!(NormalTuple, elems, elems);
impl NormalTuple {
pub fn new(elems: Args) -> Self {
Self {
elems,
}
Self { elems }
}
}
@ -765,6 +769,30 @@ impl ConstAttribute {
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ConstTupleAttribute {
tup: Box<ConstExpr>,
index: Literal,
}
impl NestedDisplay for ConstTupleAttribute {
fn fmt_nest(&self, f: &mut fmt::Formatter<'_>, _level: usize) -> fmt::Result {
write!(f, "{}.{}", self.tup, self.index)
}
}
impl_display_from_nested!(ConstTupleAttribute);
impl_locational!(ConstTupleAttribute, tup, index);
impl ConstTupleAttribute {
pub fn new(tup: ConstExpr, index: Literal) -> Self {
Self {
tup: Box::new(tup),
index,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ConstSubscript {
obj: Box<ConstExpr>,
@ -794,12 +822,13 @@ pub enum ConstAccessor {
Local(ConstLocal),
SelfDot(ConstLocal),
Attr(ConstAttribute),
TupleAttr(ConstTupleAttribute),
Subscr(ConstSubscript),
}
impl_nested_display_for_enum!(ConstAccessor; Local, SelfDot, Attr, Subscr);
impl_nested_display_for_enum!(ConstAccessor; Local, SelfDot, Attr, TupleAttr, Subscr);
impl_display_from_nested!(ConstAccessor);
impl_locational_for_enum!(ConstAccessor; Local, SelfDot, Attr, Subscr);
impl_locational_for_enum!(ConstAccessor; Local, SelfDot, Attr, TupleAttr, Subscr);
impl ConstAccessor {
pub const fn local(symbol: Token) -> Self {