ezno_parser/
generator_helpers.rs

1use crate::{ASTNode, Expression, PropertyReference, Statement, VariableIdentifier};
2
3/// A trait which means that self can be pushed to a [`TokenSender`]
4pub trait IntoAST<T> {
5	fn into_ast(self) -> T;
6}
7
8impl<T: ASTNode> IntoAST<T> for T {
9	fn into_ast(self) -> T {
10		self
11	}
12}
13
14pub struct Ident<'a>(&'a str);
15
16impl<'a> From<&'a str> for Ident<'a> {
17	fn from(name: &'a str) -> Self {
18		Self(name)
19	}
20}
21
22impl<'a> IntoAST<Expression> for Ident<'a> {
23	fn into_ast(self) -> Expression {
24		Expression::VariableReference(self.0.to_owned(), source_map::Nullable::NULL)
25	}
26}
27
28impl<'a> IntoAST<Expression> for &'a str {
29	fn into_ast(self) -> Expression {
30		Expression::StringLiteral(
31			self.to_owned(),
32			crate::Quoted::Double,
33			source_map::Nullable::NULL,
34		)
35	}
36}
37
38impl<'a> IntoAST<PropertyReference> for &'a str {
39	fn into_ast(self) -> PropertyReference {
40		PropertyReference::Standard { property: self.to_owned(), is_private: false }
41	}
42}
43
44impl<'a> IntoAST<VariableIdentifier> for &'a str {
45	fn into_ast(self) -> VariableIdentifier {
46		VariableIdentifier::Standard(self.to_owned(), source_map::Nullable::NULL)
47	}
48}
49
50#[allow(clippy::cast_precision_loss)]
51impl IntoAST<Expression> for usize {
52	fn into_ast(self) -> Expression {
53		Expression::NumberLiteral(
54			crate::number::NumberRepresentation::from(self as f64),
55			source_map::Nullable::NULL,
56		)
57	}
58}
59
60impl IntoAST<Expression> for f64 {
61	fn into_ast(self) -> Expression {
62		Expression::NumberLiteral(
63			crate::number::NumberRepresentation::from(self),
64			source_map::Nullable::NULL,
65		)
66	}
67}
68
69impl IntoAST<Statement> for Expression {
70	fn into_ast(self) -> Statement {
71		Statement::Expression(self.into())
72	}
73}