Field accessor and utilities (#20)

* Apply is-macro to Constant and ast nodes
This commit is contained in:
Jeong, YunWon 2023-05-11 19:44:20 +09:00 committed by GitHub
parent 2baad9ead6
commit 947fb53d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 18 deletions

View file

@ -114,7 +114,7 @@ impl std::cmp::PartialEq<usize> for Int {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Constant {
None,
Bool(bool),
@ -127,6 +127,21 @@ pub enum Constant {
Ellipsis,
}
impl Constant {
pub fn is_true(self) -> bool {
self.bool().map_or(false, |b| b)
}
pub fn is_false(self) -> bool {
self.bool().map_or(false, |b| !b)
}
pub fn complex(self) -> Option<(f64, f64)> {
match self {
Constant::Complex { real, imag } => Some((real, imag)),
_ => None,
}
}
}
impl From<String> for Constant {
fn from(s: String) -> Constant {
Self::Str(s)
@ -247,3 +262,14 @@ impl<T, U> std::ops::Deref for Attributed<T, U> {
&self.node
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_macro() {
let none = Constant::None;
assert!(none.is_none());
assert!(!none.is_bool());
}
}

View file

@ -46,7 +46,7 @@ impl<U> From<ModFunctionType<U>> for Mod<U> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Mod<U = ()> {
Module(ModModule<U>),
Interactive(ModInteractive<U>),
@ -366,34 +366,61 @@ impl<U> From<StmtExpr<U>> for StmtKind<U> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum StmtKind<U = ()> {
#[is(name = "function_def_stmt")]
FunctionDef(StmtFunctionDef<U>),
#[is(name = "async_function_def_stmt")]
AsyncFunctionDef(StmtAsyncFunctionDef<U>),
#[is(name = "class_def_stmt")]
ClassDef(StmtClassDef<U>),
#[is(name = "return_stmt")]
Return(StmtReturn<U>),
#[is(name = "delete_stmt")]
Delete(StmtDelete<U>),
#[is(name = "assign_stmt")]
Assign(StmtAssign<U>),
#[is(name = "aug_assign_stmt")]
AugAssign(StmtAugAssign<U>),
#[is(name = "ann_assign_stmt")]
AnnAssign(StmtAnnAssign<U>),
#[is(name = "for_stmt")]
For(StmtFor<U>),
#[is(name = "async_for_stmt")]
AsyncFor(StmtAsyncFor<U>),
#[is(name = "while_stmt")]
While(StmtWhile<U>),
#[is(name = "if_stmt")]
If(StmtIf<U>),
#[is(name = "with_stmt")]
With(StmtWith<U>),
#[is(name = "async_with_stmt")]
AsyncWith(StmtAsyncWith<U>),
#[is(name = "match_stmt")]
Match(StmtMatch<U>),
#[is(name = "raise_stmt")]
Raise(StmtRaise<U>),
#[is(name = "try_stmt")]
Try(StmtTry<U>),
#[is(name = "try_star_stmt")]
TryStar(StmtTryStar<U>),
#[is(name = "assert_stmt")]
Assert(StmtAssert<U>),
#[is(name = "import_stmt")]
Import(StmtImport<U>),
#[is(name = "import_from_stmt")]
ImportFrom(StmtImportFrom<U>),
#[is(name = "global_stmt")]
Global(StmtGlobal),
#[is(name = "nonlocal_stmt")]
Nonlocal(StmtNonlocal),
#[is(name = "expr_stmt")]
Expr(StmtExpr<U>),
#[is(name = "pass_stmt")]
Pass,
#[is(name = "break_stmt")]
Break,
#[is(name = "continue_stmt")]
Continue,
}
pub type Stmt<U = ()> = Attributed<StmtKind<U>, U>;
@ -726,52 +753,79 @@ impl<U> From<ExprSlice<U>> for ExprKind<U> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum ExprKind<U = ()> {
#[is(name = "bool_op_expr")]
BoolOp(ExprBoolOp<U>),
#[is(name = "named_expr_expr")]
NamedExpr(ExprNamedExpr<U>),
#[is(name = "bin_op_expr")]
BinOp(ExprBinOp<U>),
#[is(name = "unary_op_expr")]
UnaryOp(ExprUnaryOp<U>),
#[is(name = "lambda_expr")]
Lambda(ExprLambda<U>),
#[is(name = "if_exp_expr")]
IfExp(ExprIfExp<U>),
#[is(name = "dict_expr")]
Dict(ExprDict<U>),
#[is(name = "set_expr")]
Set(ExprSet<U>),
#[is(name = "list_comp_expr")]
ListComp(ExprListComp<U>),
#[is(name = "set_comp_expr")]
SetComp(ExprSetComp<U>),
#[is(name = "dict_comp_expr")]
DictComp(ExprDictComp<U>),
#[is(name = "generator_exp_expr")]
GeneratorExp(ExprGeneratorExp<U>),
#[is(name = "await_expr")]
Await(ExprAwait<U>),
#[is(name = "yield_expr")]
Yield(ExprYield<U>),
#[is(name = "yield_from_expr")]
YieldFrom(ExprYieldFrom<U>),
#[is(name = "compare_expr")]
Compare(ExprCompare<U>),
#[is(name = "call_expr")]
Call(ExprCall<U>),
#[is(name = "formatted_value_expr")]
FormattedValue(ExprFormattedValue<U>),
#[is(name = "joined_str_expr")]
JoinedStr(ExprJoinedStr<U>),
#[is(name = "constant_expr")]
Constant(ExprConstant),
#[is(name = "attribute_expr")]
Attribute(ExprAttribute<U>),
#[is(name = "subscript_expr")]
Subscript(ExprSubscript<U>),
#[is(name = "starred_expr")]
Starred(ExprStarred<U>),
#[is(name = "name_expr")]
Name(ExprName),
#[is(name = "list_expr")]
List(ExprList<U>),
#[is(name = "tuple_expr")]
Tuple(ExprTuple<U>),
#[is(name = "slice_expr")]
Slice(ExprSlice<U>),
}
pub type Expr<U = ()> = Attributed<ExprKind<U>, U>;
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum ExprContext {
Load,
Store,
Del,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Boolop {
And,
Or,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Operator {
Add,
Sub,
@ -788,7 +842,7 @@ pub enum Operator {
FloorDiv,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Unaryop {
Invert,
Not,
@ -796,7 +850,7 @@ pub enum Unaryop {
USub,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Cmpop {
Eq,
NotEq,
@ -831,7 +885,7 @@ impl<U> From<ExcepthandlerExceptHandler<U>> for ExcepthandlerKind<U> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum ExcepthandlerKind<U = ()> {
ExceptHandler(ExcepthandlerExceptHandler<U>),
}
@ -977,7 +1031,7 @@ impl<U> From<PatternMatchOr<U>> for PatternKind<U> {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum PatternKind<U = ()> {
MatchValue(PatternMatchValue<U>),
MatchSingleton(PatternMatchSingleton),
@ -1002,7 +1056,7 @@ impl From<TypeIgnoreTypeIgnore> for TypeIgnore {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum TypeIgnore {
TypeIgnore(TypeIgnoreTypeIgnore),
}

View file

@ -2,7 +2,7 @@ use crate::{Constant, ExprKind};
impl<U> ExprKind<U> {
/// Returns a short name for the node suitable for use in error messages.
pub fn name(&self) -> &'static str {
pub fn python_name(&self) -> &'static str {
match self {
ExprKind::BoolOp { .. } | ExprKind::BinOp { .. } | ExprKind::UnaryOp { .. } => {
"operator"