Skip to content

Commit 71cb432

Browse files
committed
chore: Move bi_tuple macro to a new module
1 parent 2934cb8 commit 71cb432

3 files changed

Lines changed: 44 additions & 41 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
macro_rules! bi_tuple {
2+
($(#[$($attrss:tt)*])* $name: ident($left: ty, $right: ty)) => {
3+
$(#[$($attrss)*])*
4+
#[derive(Clone, Debug, PartialEq)]
5+
pub struct $name($left, $right);
6+
7+
impl $name {
8+
pub fn new(left: $left, right: $right) -> Self {
9+
Self(left, right)
10+
}
11+
12+
pub fn try_map_right<F, E>(self, f: F) -> Result<Self, E>
13+
where F : FnOnce($right) -> Result<$right, E>
14+
{
15+
let (left, right) = self.into();
16+
f(right).map(|new_right| Self::new(left, new_right))
17+
}
18+
19+
pub fn left(&self) -> &$left {
20+
&self.0
21+
}
22+
23+
pub fn right(&self) -> &$right {
24+
&self.1
25+
}
26+
}
27+
28+
impl From<$name> for ($left, $right) {
29+
fn from(value: $name) -> Self {
30+
(value.0, value.1)
31+
}
32+
}
33+
34+
impl<'a> From<&'a $name> for (&'a $left, &'a $right) {
35+
fn from(value: &'a $name) -> Self {
36+
(&value.0, &value.1)
37+
}
38+
}
39+
};
40+
}
41+
42+
pub(crate) use bi_tuple;

rusty_parser/src/specific/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod if_block;
2121
mod implementation;
2222
mod keyword;
2323
mod letter_range;
24+
mod macros;
2425
mod name;
2526
mod on_error;
2627
mod operator;

rusty_parser/src/specific/core/statement.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::specific::core::exit::statement_exit_p;
1010
use crate::specific::core::for_loop::for_loop_p;
1111
use crate::specific::core::go_sub::{statement_go_sub_p, statement_return_p};
1212
use crate::specific::core::if_block::if_block_p;
13+
use crate::specific::core::macros::bi_tuple;
1314
use crate::specific::core::name::{
1415
bare_name_with_dots, identifier_with_dots, token_list_to_bare_name,
1516
};
@@ -138,47 +139,6 @@ pub enum Statement {
138139
Print(Print),
139140
}
140141

141-
macro_rules! bi_tuple {
142-
($(#[$($attrss:tt)*])* $name: ident($left: ty, $right: ty)) => {
143-
$(#[$($attrss)*])*
144-
#[derive(Clone, Debug, PartialEq)]
145-
pub struct $name($left, $right);
146-
147-
impl $name {
148-
pub fn new(left: $left, right: $right) -> Self {
149-
Self(left, right)
150-
}
151-
152-
pub fn try_map_right<F, E>(self, f: F) -> Result<Self, E>
153-
where F : FnOnce($right) -> Result<$right, E>
154-
{
155-
let (left, right) = self.into();
156-
f(right).map(|new_right| Self::new(left, new_right))
157-
}
158-
159-
pub fn left(&self) -> &$left {
160-
&self.0
161-
}
162-
163-
pub fn right(&self) -> &$right {
164-
&self.1
165-
}
166-
}
167-
168-
impl From<$name> for ($left, $right) {
169-
fn from(value: $name) -> Self {
170-
(value.0, value.1)
171-
}
172-
}
173-
174-
impl<'a> From<&'a $name> for (&'a $left, &'a $right) {
175-
fn from(value: &'a $name) -> Self {
176-
(&value.0, &value.1)
177-
}
178-
}
179-
};
180-
}
181-
182142
bi_tuple!(
183143
/// A constant declaration.
184144
Constant(NamePos, ExpressionPos)

0 commit comments

Comments
 (0)