| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use libcst_derive::{cst_node, Codegen}; |
| 7 |
|
| 8 |
pub enum Error {} |
| 9 |
|
| 10 |
type TokenRef<'r, 'a> = &'r &'a str; |
| 11 |
pub type Result<T> = std::result::Result<T, Error>; |
| 12 |
|
| 13 |
pub struct Config<'a> { |
| 14 |
#[allow(dead_code)] |
| 15 |
foo: &'a str, |
| 16 |
} |
| 17 |
pub trait Inflate<'a> |
| 18 |
where |
| 19 |
Self: Sized, |
| 20 |
{ |
| 21 |
type Inflated; |
| 22 |
fn inflate(self, config: &Config<'a>) -> Result<Self::Inflated>; |
| 23 |
} |
| 24 |
|
| 25 |
impl<'a, T: Inflate<'a> + ?Sized> Inflate<'a> for Box<T> { |
| 26 |
type Inflated = Box<T::Inflated>; |
| 27 |
fn inflate(self, config: &Config<'a>) -> Result<Self::Inflated> { |
| 28 |
match (*self).inflate(config) { |
| 29 |
Ok(a) => Ok(Box::new(a)), |
| 30 |
Err(e) => Err(e), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
pub struct CodegenState<'a> { |
| 36 |
#[allow(dead_code)] |
| 37 |
foo: &'a str, |
| 38 |
} |
| 39 |
pub trait Codegen<'a> { |
| 40 |
fn codegen(&self, state: &mut CodegenState<'a>); |
| 41 |
} |
| 42 |
|
| 43 |
#[derive(Debug, PartialEq, Eq, Clone)] |
| 44 |
pub struct WS<'a> { |
| 45 |
pub last_line: &'a str, |
| 46 |
} |
| 47 |
|
| 48 |
#[cst_node] |
| 49 |
pub struct Parameters<'a> { |
| 50 |
pub params: Vec<Param<'a>>, |
| 51 |
pub foo: Param<'a>, |
| 52 |
} |
| 53 |
|
| 54 |
impl<'r, 'a> Inflate<'a> for DeflatedParameters<'r, 'a> { |
| 55 |
type Inflated = Parameters<'a>; |
| 56 |
fn inflate(self, config: &Config<'a>) -> Result<Self::Inflated> { |
| 57 |
let params = vec![]; |
| 58 |
#[allow(clippy::blacklisted_name)] |
| 59 |
let foo = self.foo.inflate(config)?; |
| 60 |
Ok(Self::Inflated { params, foo }) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
#[cst_node] |
| 65 |
pub struct Param<'a> { |
| 66 |
pub star: Option<&'a str>, |
| 67 |
pub(crate) star_tok: Option<TokenRef<'a>>, |
| 68 |
} |
| 69 |
|
| 70 |
impl<'r, 'a> Inflate<'a> for DeflatedParam<'r, 'a> { |
| 71 |
type Inflated = Param<'a>; |
| 72 |
fn inflate(self, _config: &Config<'a>) -> Result<Self::Inflated> { |
| 73 |
Ok(Self::Inflated { star: self.star }) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl<'a> Codegen<'a> for Param<'a> { |
| 78 |
fn codegen(&self, _state: &mut CodegenState<'a>) {} |
| 79 |
} |
| 80 |
|
| 81 |
#[cst_node] |
| 82 |
pub struct BitOr<'a> { |
| 83 |
pub whitespace_before: WS<'a>, |
| 84 |
pub whitespace_after: WS<'a>, |
| 85 |
|
| 86 |
pub(crate) tok: TokenRef<'a>, |
| 87 |
} |
| 88 |
|
| 89 |
#[cst_node] |
| 90 |
pub enum CompOp<'a> { |
| 91 |
LessThan { |
| 92 |
whitespace_before: WS<'a>, |
| 93 |
tok: TokenRef<'a>, |
| 94 |
}, |
| 95 |
GreaterThan { |
| 96 |
whitespace_after: WS<'a>, |
| 97 |
tok: TokenRef<'a>, |
| 98 |
}, |
| 99 |
} |
| 100 |
|
| 101 |
impl<'r, 'a> Inflate<'a> for DeflatedCompOp<'r, 'a> { |
| 102 |
type Inflated = CompOp<'a>; |
| 103 |
fn inflate(self, _config: &Config<'a>) -> Result<Self::Inflated> { |
| 104 |
Ok(match self { |
| 105 |
Self::LessThan { tok: _, .. } => Self::Inflated::LessThan { |
| 106 |
whitespace_before: WS { last_line: "yo" }, |
| 107 |
}, |
| 108 |
Self::GreaterThan { tok: _, .. } => Self::Inflated::GreaterThan { |
| 109 |
whitespace_after: WS { last_line: "" }, |
| 110 |
}, |
| 111 |
}) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<'a> Codegen<'a> for CompOp<'a> { |
| 116 |
fn codegen(&self, _state: &mut CodegenState<'a>) {} |
| 117 |
} |
| 118 |
|
| 119 |
#[cst_node(Codegen)] |
| 120 |
enum Expr<'a> { |
| 121 |
#[allow(dead_code)] |
| 122 |
One(Box<Param<'a>>), |
| 123 |
#[allow(dead_code)] |
| 124 |
Two(CompOp<'a>), |
| 125 |
} |
| 126 |
|
| 127 |
fn main() {} |