mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 13:54:55 +00:00
Field accessor and utilities (#20)
* Apply is-macro to Constant and ast nodes
This commit is contained in:
parent
2baad9ead6
commit
947fb53d0b
6 changed files with 117 additions and 18 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("_"))
|
||||
|
@ -271,6 +280,7 @@ class StructVisitor(EmitVisitor):
|
|||
def simple_sum(self, sum, name, depth):
|
||||
rust_name = rust_type_name(name)
|
||||
self.emit_attrs(depth)
|
||||
self.emit("#[derive(is_macro::Is)]", depth)
|
||||
self.emit(f"pub enum {rust_name} {{", depth)
|
||||
for variant in sum.types:
|
||||
self.emit(f"{variant.name},", depth + 1)
|
||||
|
@ -291,8 +301,15 @@ class StructVisitor(EmitVisitor):
|
|||
|
||||
generics, generics_applied = self.apply_generics(name, "U = ()", "U")
|
||||
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(
|
||||
|
@ -361,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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue