mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
Apply is-macro to ast nodes
This commit is contained in:
parent
706ac3364c
commit
b2673a4019
4 changed files with 87 additions and 29 deletions
|
@ -6,6 +6,7 @@
|
|||
import sys
|
||||
import json
|
||||
import textwrap
|
||||
import re
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from pathlib import Path
|
||||
|
@ -16,26 +17,34 @@ import asdl
|
|||
TABSIZE = 4
|
||||
AUTO_GEN_MESSAGE = "// File automatically generated by {}.\n\n"
|
||||
|
||||
builtin_type_mapping = {
|
||||
BUILTIN_TYPE_NAMES = {
|
||||
"identifier": "Identifier",
|
||||
"string": "String",
|
||||
"int": "Int",
|
||||
"constant": "Constant",
|
||||
}
|
||||
assert builtin_type_mapping.keys() == asdl.builtin_types
|
||||
assert BUILTIN_TYPE_NAMES.keys() == asdl.builtin_types
|
||||
|
||||
builtin_int_mapping = {
|
||||
BUILTIN_INT_NAMES = {
|
||||
"simple": "bool",
|
||||
"is_async": "bool",
|
||||
}
|
||||
|
||||
RUST_KEYWORDS = {"if", "while", "for", "return", "match", "try", "await", "yield"}
|
||||
|
||||
|
||||
def rust_field_name(name):
|
||||
name = rust_type_name(name)
|
||||
return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()
|
||||
|
||||
|
||||
def rust_type_name(name):
|
||||
"""Return a string for the C name of the type.
|
||||
|
||||
This function special cases the default types provided by asdl.
|
||||
"""
|
||||
if name in asdl.builtin_types:
|
||||
builtin = builtin_type_mapping[name]
|
||||
builtin = BUILTIN_TYPE_NAMES[name]
|
||||
return builtin
|
||||
elif name.islower():
|
||||
return "".join(part.capitalize() for part in name.split("_"))
|
||||
|
@ -294,7 +303,13 @@ class StructVisitor(EmitVisitor):
|
|||
self.emit_attrs(depth)
|
||||
self.emit("#[derive(is_macro::Is)]", depth)
|
||||
self.emit(f"pub enum {rust_name}{suffix}{generics} {{", depth)
|
||||
needs_escape = any(rust_field_name(t.name) in RUST_KEYWORDS for t in sum.types)
|
||||
for t in sum.types:
|
||||
if needs_escape:
|
||||
self.emit(
|
||||
f'#[is(name = "{rust_field_name(t.name)}_{rust_name.lower()}")]',
|
||||
depth + 1,
|
||||
)
|
||||
if t.fields:
|
||||
(t_generics_applied,) = self.apply_generics(t.name, "U")
|
||||
self.emit(
|
||||
|
@ -363,7 +378,7 @@ class StructVisitor(EmitVisitor):
|
|||
if field.seq:
|
||||
typ = f"Vec<{typ}>"
|
||||
if typ == "Int":
|
||||
typ = builtin_int_mapping.get(field.name, typ)
|
||||
typ = BUILTIN_INT_NAMES.get(field.name, typ)
|
||||
name = rust_field(field.name)
|
||||
self.emit(f"{vis}{name}: {typ},", depth)
|
||||
|
||||
|
|
|
@ -272,4 +272,4 @@ mod tests {
|
|||
assert!(none.is_none());
|
||||
assert!(!none.is_bool());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,8 +46,7 @@ impl<U> From<ModFunctionType<U>> for Mod<U> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Mod<U = ()> {
|
||||
Module(ModModule<U>),
|
||||
Interactive(ModInteractive<U>),
|
||||
|
@ -367,35 +366,61 @@ impl<U> From<StmtExpr<U>> for StmtKind<U> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[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>;
|
||||
|
@ -728,56 +753,79 @@ impl<U> From<ExprSlice<U>> for ExprKind<U> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[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)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum ExprContext {
|
||||
Load,
|
||||
Store,
|
||||
Del,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Boolop {
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Operator {
|
||||
Add,
|
||||
Sub,
|
||||
|
@ -794,8 +842,7 @@ pub enum Operator {
|
|||
FloorDiv,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Unaryop {
|
||||
Invert,
|
||||
Not,
|
||||
|
@ -803,8 +850,7 @@ pub enum Unaryop {
|
|||
USub,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum Cmpop {
|
||||
Eq,
|
||||
NotEq,
|
||||
|
@ -839,8 +885,7 @@ impl<U> From<ExcepthandlerExceptHandler<U>> for ExcepthandlerKind<U> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum ExcepthandlerKind<U = ()> {
|
||||
ExceptHandler(ExcepthandlerExceptHandler<U>),
|
||||
}
|
||||
|
@ -986,8 +1031,7 @@ impl<U> From<PatternMatchOr<U>> for PatternKind<U> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum PatternKind<U = ()> {
|
||||
MatchValue(PatternMatchValue<U>),
|
||||
MatchSingleton(PatternMatchSingleton),
|
||||
|
@ -1012,8 +1056,7 @@ impl From<TypeIgnoreTypeIgnore> for TypeIgnore {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum TypeIgnore {
|
||||
TypeIgnore(TypeIgnoreTypeIgnore),
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue