Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions trunk_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ pub enum Statement {
flags: Vec<PropertyFlag>,
},
Constant {
constants: Vec<Constant>,
},
ClassConstant {
name: Identifier,
value: Expression,
flags: Vec<ConstFlag>,
Expand Down Expand Up @@ -306,6 +309,12 @@ pub enum Statement {
Noop,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Constant {
pub name: Identifier,
pub value: Expression,
}

#[derive(Debug, Clone, PartialEq)]
pub struct DeclareItem {
pub key: Identifier,
Expand Down
63 changes: 45 additions & 18 deletions trunk_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ast::{
Arg, ArrayItem, BackedEnumType, ClassFlag, ClosureUse, DeclareItem, ElseIf, IncludeKind,
MagicConst, MethodFlag, StaticVar, Use, UseKind,
Arg, ArrayItem, BackedEnumType, ClassFlag, ClosureUse, Constant, DeclareItem, ElseIf,
IncludeKind, MagicConst, MethodFlag, StaticVar, Use, UseKind,
},
Block, Case, Catch, Expression, Identifier, MatchArm, Program, Statement, Type,
};
Expand Down Expand Up @@ -426,7 +426,7 @@ impl Parser {
let mut body = Block::new();
while self.current.kind != TokenKind::RightBrace {
match self.class_statement()? {
Statement::Constant { .. } => {
Statement::ClassConstant { .. } => {
return Err(ParseError::TraitCannotContainConstant(self.current.span))
}
s => {
Expand Down Expand Up @@ -975,20 +975,26 @@ impl Parser {
TokenKind::Const => {
self.next();

// TODO: Support defining multiple constants in one go.
let name = self.ident()?;
let mut constants = vec![];

expect!(self, TokenKind::Equals, "expected =");
while self.current.kind != TokenKind::SemiColon {
let name = self.ident()?;

let value = self.expression(0)?;
expect!(self, TokenKind::Equals, "expected =");

self.semi()?;
let value = self.expression(0)?;

Statement::Constant {
name: name.into(),
value,
flags: vec![],
constants.push(Constant {
name: name.into(),
value,
});

self.optional_comma()?;
}

self.semi()?;

Statement::Constant { constants }
}
_ => {
let expr = self.expression(0)?;
Expand Down Expand Up @@ -1112,7 +1118,7 @@ impl Parser {

self.semi()?;

Ok(Statement::Constant {
Ok(Statement::ClassConstant {
name: name.into(),
value,
flags: vec![],
Expand Down Expand Up @@ -1206,7 +1212,7 @@ impl Parser {

self.semi()?;

Ok(Statement::Constant {
Ok(Statement::ClassConstant {
name: name.into(),
value,
flags: flags.into_iter().map(|f| f.into()).collect(),
Expand Down Expand Up @@ -2187,7 +2193,8 @@ mod tests {
use super::Parser;
use crate::{
ast::{
Arg, ArrayItem, DeclareItem, ElseIf, IncludeKind, InfixOp, MethodFlag, PropertyFlag,
Arg, ArrayItem, Constant, DeclareItem, ElseIf, IncludeKind, InfixOp, MethodFlag,
PropertyFlag,
},
Catch, Expression, Identifier, Param, Statement, Type,
};
Expand Down Expand Up @@ -3351,9 +3358,29 @@ mod tests {
assert_ast(
"<?php const FOO = 1;",
&[Statement::Constant {
name: "FOO".as_bytes().into(),
value: Expression::Int { i: 1 },
flags: vec![],
constants: vec![Constant {
name: "FOO".as_bytes().into(),
value: Expression::Int { i: 1 },
}],
}],
);
}

#[test]
fn top_level_constant_multiple() {
assert_ast(
"<?php const FOO = 1, BAR = 2;",
&[Statement::Constant {
constants: vec![
Constant {
name: "FOO".as_bytes().into(),
value: Expression::Int { i: 1 },
},
Constant {
name: "BAR".as_bytes().into(),
value: Expression::Int { i: 2 },
},
],
}],
);
}
Expand Down