Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit 8078663

Browse files
authored
Remove Range type parameter from AST nodes (#15)
1 parent f60e204 commit 8078663

File tree

13 files changed

+974
-1284
lines changed

13 files changed

+974
-1284
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: run tests with num-bigint
4141
run: cargo test --all --no-default-features --features num-bigint
4242
- name: run tests with malachite-bigint and all features
43-
run: cargo test --all --features malachite-bigint,all-nodes-with-ranges,full-lexer,serde
43+
run: cargo test --all --features malachite-bigint,full-lexer,serde
4444

4545
lint:
4646
name: Check Rust code with rustfmt and clippy
@@ -55,7 +55,7 @@ jobs:
5555
- name: run clippy
5656
run: cargo clippy --all --no-default-features --features num-bigint
5757
- name: run clippy
58-
run: cargo clippy --all --features malachite-bigint,all-nodes-with-ranges,full-lexer,serde -- -Dwarnings
58+
run: cargo clippy --all --features malachite-bigint,full-lexer,serde -- -Dwarnings
5959

6060
- uses: actions/setup-python@v4
6161
with:

ast/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ license = "MIT"
99

1010
[features]
1111
default = ["malachite-bigint"]
12-
all-nodes-with-ranges = []
1312

1413
[dependencies]
1514
rustpython-parser-core = { workspace = true }

ast/asdl_rs.py

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,6 @@ def customized_type_info(self, type_name):
286286
def has_user_data(self, typ):
287287
return self.type_info[typ].has_user_data
288288

289-
def apply_generics(self, typ, *generics):
290-
needs_generics = not self.type_info[typ].is_simple
291-
if needs_generics:
292-
return [f"<{g}>" for g in generics]
293-
else:
294-
return ["" for g in generics]
295-
296289

297290
class EmitVisitor(asdl.VisitorBase, TypeInfoMixin):
298291
"""Visit that emits lines"""
@@ -393,37 +386,33 @@ def emit_attrs(self, depth):
393386
self.emit("#[derive(Clone, Debug, PartialEq)]", depth)
394387

395388
def emit_range(self, has_attributes, depth):
396-
if has_attributes:
397-
self.emit("pub range: R,", depth + 1)
398-
else:
399-
self.emit("pub range: OptionalRange<R>,", depth + 1)
389+
self.emit("pub range: TextRange,", depth + 1)
400390

401391
def visitModule(self, mod):
402392
self.emit_attrs(0)
403393
self.emit(
404394
"""
405395
#[derive(is_macro::Is)]
406-
pub enum Ast<R=TextRange> {
396+
pub enum Ast {
407397
""",
408398
0,
409399
)
410400
for dfn in mod.dfns:
411401
info = self.customized_type_info(dfn.name)
412402
dfn = info.custom
413403
rust_name = info.full_type_name
414-
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
415404
if dfn.name == "mod":
416405
# This is exceptional rule to other enums.
417406
# Unlike other enums, this is justified because `Mod` is only used as
418407
# the top node of parsing result and never a child node of other nodes.
419408
# Because it will be very rarely used in very particular applications,
420409
# "ast_" prefix to everywhere seems less useful.
421410
self.emit('#[is(name = "module")]', 1)
422-
self.emit(f"{rust_name}({rust_name}{generics}),", 1)
411+
self.emit(f"{rust_name}({rust_name}),", 1)
423412
self.emit(
424413
"""
425414
}
426-
impl<R> Node for Ast<R> {
415+
impl Node for Ast {
427416
const NAME: &'static str = "AST";
428417
const FIELD_NAMES: &'static [&'static str] = &[];
429418
}
@@ -433,11 +422,10 @@ def visitModule(self, mod):
433422
for dfn in mod.dfns:
434423
info = self.customized_type_info(dfn.name)
435424
rust_name = info.full_type_name
436-
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
437425
self.emit(
438426
f"""
439-
impl<R> From<{rust_name}{generics}> for Ast<R> {{
440-
fn from(node: {rust_name}{generics}) -> Self {{
427+
impl From<{rust_name}> for Ast {{
428+
fn from(node: {rust_name}) -> Self {{
441429
Ast::{rust_name}(node)
442430
}}
443431
}}
@@ -462,10 +450,9 @@ def visitSum(self, sum, type, depth):
462450
else:
463451
self.sum_with_constructors(sum, type, depth)
464452

465-
(generics_applied,) = self.apply_generics(type.name, "R")
466453
self.emit(
467454
f"""
468-
impl{generics_applied} Node for {rust_type_name(type.name)}{generics_applied} {{
455+
impl Node for {rust_type_name(type.name)} {{
469456
const NAME: &'static str = "{type.name}";
470457
const FIELD_NAMES: &'static [&'static str] = &[];
471458
}}
@@ -512,7 +499,7 @@ def simple_sum(self, sum, type, depth):
512499
{rust_name}::{cons.name}
513500
}}
514501
}}
515-
impl<R> From<{rust_name}{cons.name}> for Ast<R> {{
502+
impl From<{rust_name}{cons.name}> for Ast {{
516503
fn from(_: {rust_name}{cons.name}) -> Self {{
517504
{rust_name}::{cons.name}.into()
518505
}}
@@ -537,15 +524,15 @@ def sum_with_constructors(self, sum, type, depth):
537524

538525
self.emit_attrs(depth)
539526
self.emit("#[derive(is_macro::Is)]", depth)
540-
self.emit(f"pub enum {rust_name}<R = TextRange> {{", depth)
527+
self.emit(f"pub enum {rust_name} {{", depth)
541528
needs_escape = any(rust_field_name(t.name) in RUST_KEYWORDS for t in sum.types)
542529
for t in sum.types:
543530
if needs_escape:
544531
self.emit(
545532
f'#[is(name = "{rust_field_name(t.name)}_{rust_name.lower()}")]',
546533
depth + 1,
547534
)
548-
self.emit(f"{t.name}({rust_name}{t.name}<R>),", depth + 1)
535+
self.emit(f"{t.name}({rust_name}{t.name}),", depth + 1)
549536
self.emit("}", depth)
550537
self.emit("", depth)
551538

@@ -559,7 +546,7 @@ def sum_subtype_struct(self, sum_type_info, t, rust_name, depth):
559546
)
560547
self.emit_attrs(depth)
561548
payload_name = f"{rust_name}{t.name}"
562-
self.emit(f"pub struct {payload_name}<R = TextRange> {{", depth)
549+
self.emit(f"pub struct {payload_name} {{", depth)
563550
self.emit_range(sum_type_info.has_attributes, depth)
564551
for f in t.fields:
565552
self.visit(f, sum_type_info, "pub ", depth + 1, t.name)
@@ -572,17 +559,17 @@ def sum_subtype_struct(self, sum_type_info, t, rust_name, depth):
572559
field_names = [f'"{f.name}"' for f in t.fields]
573560
self.emit(
574561
f"""
575-
impl<R> Node for {payload_name}<R> {{
562+
impl Node for {payload_name} {{
576563
const NAME: &'static str = "{t.name}";
577564
const FIELD_NAMES: &'static [&'static str] = &[{', '.join(field_names)}];
578565
}}
579-
impl<R> From<{payload_name}<R>> for {rust_name}<R> {{
580-
fn from(payload: {payload_name}<R>) -> Self {{
566+
impl From<{payload_name}> for {rust_name} {{
567+
fn from(payload: {payload_name}) -> Self {{
581568
{rust_name}::{t.name}(payload)
582569
}}
583570
}}
584-
impl<R> From<{payload_name}<R>> for Ast<R> {{
585-
fn from(payload: {payload_name}<R>) -> Self {{
571+
impl From<{payload_name}> for Ast {{
572+
fn from(payload: {payload_name}) -> Self {{
586573
{rust_name}::from(payload).into()
587574
}}
588575
}}
@@ -609,7 +596,7 @@ def visitField(self, field, parent, vis, depth, constructor=None):
609596
field_type = None
610597
typ = rust_type_name(field.type)
611598
if field_type and not field_type.is_simple:
612-
typ = f"{typ}<R>"
599+
typ = f"{typ}"
613600
# don't box if we're doing Vec<T>, but do box if we're doing Vec<Option<Box<T>>>
614601
if (
615602
field_type
@@ -642,7 +629,7 @@ def visitProduct(self, product, type, depth):
642629
type_info = self.type_info[type.name]
643630
product_name = type_info.full_type_name
644631
self.emit_attrs(depth)
645-
self.emit(f"pub struct {product_name}<R = TextRange> {{", depth)
632+
self.emit(f"pub struct {product_name} {{", depth)
646633
self.emit_range(product.attributes, depth + 1)
647634
for f in product.fields:
648635
self.visit(f, type_info, "pub ", depth + 1)
@@ -652,7 +639,7 @@ def visitProduct(self, product, type, depth):
652639
field_names = [f'"{f.name}"' for f in product.fields]
653640
self.emit(
654641
f"""
655-
impl<R> Node for {product_name}<R> {{
642+
impl Node for {product_name} {{
656643
const NAME: &'static str = "{type.name}";
657644
const FIELD_NAMES: &'static [&'static str] = &[
658645
{', '.join(field_names)}
@@ -692,8 +679,6 @@ def visitSum(self, sum, name, depth):
692679
self.emit_type_alias(variant_info)
693680
self.emit_ranged_impl(variant_info)
694681

695-
if not info.no_cfg(self.type_info):
696-
self.emit('#[cfg(feature = "all-nodes-with-ranges")]', 0)
697682

698683
self.emit(
699684
f"""
@@ -716,21 +701,16 @@ def visitProduct(self, product, name, depth):
716701

717702
def emit_type_alias(self, info):
718703
return # disable
719-
generics = "" if info.is_simple else "::<TextRange>"
720-
721704
self.emit(
722-
f"pub type {info.full_type_name} = crate::generic::{info.full_type_name}{generics};",
705+
f"pub type {info.full_type_name} = crate::generic::{info.full_type_name};",
723706
0,
724707
)
725708
self.emit("", 0)
726709

727710
def emit_ranged_impl(self, info):
728-
if not info.no_cfg(self.type_info):
729-
self.emit('#[cfg(feature = "all-nodes-with-ranges")]', 0)
730-
731711
self.file.write(
732712
f"""
733-
impl Ranged for crate::generic::{info.full_type_name}::<TextRange> {{
713+
impl Ranged for crate::generic::{info.full_type_name} {{
734714
fn range(&self) -> TextRange {{
735715
self.range
736716
}}

0 commit comments

Comments
 (0)