feat: store tokens for all primitive nodes in the ast
This commit is contained in:
parent
d88d2e7f2d
commit
96d3e11951
@ -13,7 +13,7 @@ impl<'a> PHPTransformable<'a> for Expression<'_> {
|
||||
fn into_php_ast(&'a self) -> Self::Item {
|
||||
match self {
|
||||
Expression::String(value) => {
|
||||
let expr = PhpPrimaryExpression::StringLiteral(value);
|
||||
let expr = PhpPrimaryExpression::StringLiteral(&value.value);
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(expr))
|
||||
}
|
||||
Expression::Int(value) => {
|
||||
@ -21,7 +21,7 @@ impl<'a> PHPTransformable<'a> for Expression<'_> {
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(expr))
|
||||
}
|
||||
Expression::Float(value) => {
|
||||
let expr = PhpPrimaryExpression::FloatingLiteral(value);
|
||||
let expr = PhpPrimaryExpression::FloatingLiteral(&value.value);
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(expr))
|
||||
}
|
||||
_ => todo!("transformation for expression: {:?}", self),
|
||||
@ -42,8 +42,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn should_transform_string() {
|
||||
let value = String::from("Hello");
|
||||
let input = Expression::String(&value);
|
||||
let t = Token::new_string("Hello".into(), 0);
|
||||
let input = Expression::String(&t);
|
||||
let output = input.into_php_ast();
|
||||
|
||||
match output {
|
||||
@ -74,8 +74,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn should_transform_float() {
|
||||
let value = String::from("322.644");
|
||||
let input = Expression::Float(&value);
|
||||
let t = Token::new_float("322.644".into(), 0);
|
||||
let input = Expression::Float(&t);
|
||||
let output = input.into_php_ast();
|
||||
|
||||
match output {
|
||||
|
@ -37,7 +37,7 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
|
||||
match e {
|
||||
Expression::String(v) => {
|
||||
expressions.push(
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(PhpPrimaryExpression::StringLiteral(v)))
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(PhpPrimaryExpression::StringLiteral(&v.value)))
|
||||
)
|
||||
},
|
||||
_ => todo!("Non string expressions not supported")
|
||||
@ -61,14 +61,14 @@ impl<'a> PHPTransformable<'a> for ModuleAST<'_> {
|
||||
Expression::Float(value) => {
|
||||
php_statements.push(PhpStatement::PhpExpressionStatement(
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
||||
PhpPrimaryExpression::FloatingLiteral(value),
|
||||
PhpPrimaryExpression::FloatingLiteral(&value.value),
|
||||
)),
|
||||
));
|
||||
}
|
||||
Expression::String(value) => {
|
||||
php_statements.push(PhpStatement::PhpExpressionStatement(
|
||||
PhpExpression::Assignment(PhpAssignmentExpression::Primary(
|
||||
PhpPrimaryExpression::StringLiteral(value),
|
||||
PhpPrimaryExpression::StringLiteral(&value.value),
|
||||
)),
|
||||
));
|
||||
}
|
||||
|
@ -58,8 +58,9 @@ mod tests {
|
||||
value: "name".into(),
|
||||
position: 0,
|
||||
};
|
||||
let expr_value = String::from("Hello");
|
||||
let expression = Expression::String(&expr_value);
|
||||
|
||||
let t = Token::new_string("Hello".into(), 0);
|
||||
let expression = Expression::String(&t);
|
||||
let binding = Statement::Binding(VariableBinding {
|
||||
datatype: None,
|
||||
identifier: &identifier_token,
|
||||
|
@ -61,9 +61,9 @@ pub struct Parameter<'a> {
|
||||
#[derive(Debug)]
|
||||
pub enum Expression<'a> {
|
||||
Int(&'a Token),
|
||||
Float(&'a String),
|
||||
String(&'a String),
|
||||
Boolean(bool),
|
||||
Float(&'a Token),
|
||||
String(&'a Token),
|
||||
Boolean(&'a Token),
|
||||
Identifier(&'a Token),
|
||||
FunctionCall(FunctionCall<'a>),
|
||||
UnaryOperator(&'a String, Box<Expression<'a>>),
|
||||
@ -77,9 +77,9 @@ impl Expression<'_> {
|
||||
match self {
|
||||
Expression::Identifier(id) => (id.position, id.get_end_position()),
|
||||
Expression::Int(id) => (id.position, id.get_end_position()),
|
||||
Expression::Float(_) => todo!(),
|
||||
Expression::String(_) => todo!(),
|
||||
Expression::Boolean(_) => todo!(),
|
||||
Expression::Float(id) => (id.position, id.get_end_position()),
|
||||
Expression::String(id) => (id.position, id.get_end_position()),
|
||||
Expression::Boolean(id) => (id.position, id.get_end_position()),
|
||||
Expression::FunctionCall(_) => todo!(),
|
||||
Expression::UnaryOperator(_, _) => todo!(),
|
||||
Expression::BinaryOperator(_, _, _) => todo!(),
|
||||
|
@ -14,10 +14,10 @@ pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> ParsingResult<Expression> {
|
||||
match tokens.get_significant(pos) {
|
||||
Some((token, token_pos)) => match token.token_type {
|
||||
TokenType::Int => Ok((Expression::Int(&token), token_pos + 1)),
|
||||
TokenType::Float => Ok((Expression::Float(&token.value), token_pos + 1)),
|
||||
TokenType::String => Ok((Expression::String(&token.value), token_pos + 1)),
|
||||
TokenType::Float => Ok((Expression::Float(&token), token_pos + 1)),
|
||||
TokenType::String => Ok((Expression::String(&token), token_pos + 1)),
|
||||
TokenType::Identifier if token.value == "true" || token.value == "false" => {
|
||||
Ok((Expression::Boolean(token.value == "true"), token_pos + 1))
|
||||
Ok((Expression::Boolean(&token), token_pos + 1))
|
||||
}
|
||||
TokenType::Identifier => Ok((Expression::Identifier(&token), token_pos + 1)),
|
||||
TokenType::LeftParen => parse_parenthesized_expression(tokens, token_pos),
|
||||
@ -66,7 +66,7 @@ mod tests {
|
||||
|
||||
match expression {
|
||||
Ok((Expression::String(value), _)) => {
|
||||
assert_eq!("Hello", format!("{}", value))
|
||||
assert_eq!("Hello", format!("{}", value.value))
|
||||
}
|
||||
_ => panic!(),
|
||||
}
|
||||
@ -78,7 +78,7 @@ mod tests {
|
||||
let expression = try_parse(&tokens, 0);
|
||||
|
||||
match expression {
|
||||
Ok((Expression::Boolean(value), _)) => assert!(value),
|
||||
Ok((Expression::Boolean(value), _)) => assert_eq!(value.value, "true"),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user