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