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
2 changes: 2 additions & 0 deletions trunk_lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ fn identifier_to_keyword(ident: &str) -> Option<TokenKind> {
"foreach" => TokenKind::Foreach,
"function" => TokenKind::Function,
"if" => TokenKind::If,
"include" => TokenKind::Include,
"include_once" => TokenKind::IncludeOnce,
"implements" => TokenKind::Implements,
"interface" => TokenKind::Interface,
"instanceof" => TokenKind::Instanceof,
Expand Down
2 changes: 2 additions & 0 deletions trunk_lexer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub enum TokenKind {
Identifier(String),
If,
Implements,
Include,
IncludeOnce,
Increment,
InlineHtml(String),
Instanceof,
Expand Down
26 changes: 22 additions & 4 deletions trunk_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ pub struct StaticVar {
pub default: Option<Expression>,
}

#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum IncludeKind {
Include,
IncludeOnce,
Require,
RequireOnce,
}

impl From<&TokenKind> for IncludeKind {
fn from(k: &TokenKind) -> Self {
match k {
TokenKind::Include => IncludeKind::Include,
TokenKind::IncludeOnce => IncludeKind::IncludeOnce,
TokenKind::Require => IncludeKind::Require,
TokenKind::RequireOnce => IncludeKind::RequireOnce,
_ => unreachable!(),
}
}
}

#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum Statement {
InlineHtml(String),
Expand All @@ -166,10 +186,8 @@ pub enum Statement {
value_var: Expression,
body: Block,
},
Require {
path: Expression,
},
RequireOnce {
Include {
kind: IncludeKind,
path: Expression,
},
Var {
Expand Down
94 changes: 71 additions & 23 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, ElseIf, MagicConst, MethodFlag,
StaticVar, Use, UseKind,
Arg, ArrayItem, BackedEnumType, ClassFlag, ClosureUse, ElseIf, IncludeKind, MagicConst,
MethodFlag, StaticVar, Use, UseKind,
},
Block, Case, Catch, Expression, Identifier, MatchArm, Program, Statement, Type,
};
Expand Down Expand Up @@ -208,23 +208,18 @@ impl Parser {

Statement::While { condition, body }
}
TokenKind::Require => {
TokenKind::Include
| TokenKind::IncludeOnce
| TokenKind::Require
| TokenKind::RequireOnce => {
let kind: IncludeKind = (&self.current.kind).into();
self.next();

let path = self.expression(0)?;

self.semi()?;

Statement::Require { path }
}
TokenKind::RequireOnce => {
self.next();

let path = self.expression(0)?;

self.semi()?;

Statement::RequireOnce { path }
Statement::Include { kind, path }
}
TokenKind::For => {
self.next();
Expand Down Expand Up @@ -2089,7 +2084,7 @@ impl Display for ParseError {
mod tests {
use super::Parser;
use crate::{
ast::{Arg, ArrayItem, ElseIf, InfixOp, MethodFlag, PropertyFlag},
ast::{Arg, ArrayItem, ElseIf, IncludeKind, InfixOp, MethodFlag, PropertyFlag},
Expression, Identifier, Param, Statement, Type,
};
use trunk_lexer::Lexer;
Expand Down Expand Up @@ -2161,6 +2156,49 @@ mod tests {
};
}

#[test]
fn include() {
assert_ast(
"<?php include 'foo.php';",
&[Statement::Include {
path: Expression::ConstantString {
value: "foo.php".into(),
},
kind: IncludeKind::Include,
}],
);

assert_ast(
"<?php include_once 'foo.php';",
&[Statement::Include {
path: Expression::ConstantString {
value: "foo.php".into(),
},
kind: IncludeKind::IncludeOnce,
}],
);

assert_ast(
"<?php require 'foo.php';",
&[Statement::Include {
path: Expression::ConstantString {
value: "foo.php".into(),
},
kind: IncludeKind::Require,
}],
);

assert_ast(
"<?php require_once 'foo.php';",
&[Statement::Include {
path: Expression::ConstantString {
value: "foo.php".into(),
},
kind: IncludeKind::RequireOnce,
}],
);
}

#[test]
fn instanceof() {
assert_ast(
Expand Down Expand Up @@ -2631,7 +2669,7 @@ mod tests {
assert_ast(
"\
<?php

class Foo {
function bar() {
echo 1;
Expand All @@ -2657,7 +2695,7 @@ mod tests {
assert_ast(
"\
<?php

class Foo extends Bar {}
",
&[class!("Foo", Some("Bar".to_string().into()), &[], &[])],
Expand All @@ -2669,7 +2707,7 @@ mod tests {
assert_ast(
"\
<?php

class Foo implements Bar, Baz {}
",
&[class!(
Expand Down Expand Up @@ -2986,15 +3024,25 @@ mod tests {

#[test]
fn comment_at_end_of_class() {
assert_ast("<?php
assert_ast(
"<?php
class MyClass {
protected $a;
// my comment
}", &[
Statement::Class { name: "MyClass".into(), extends: None, implements: vec![], body: vec![
Statement::Property { var: "a".into(), value: None, r#type: None, flags: vec![PropertyFlag::Protected] }
], flag: None }
]);
}",
&[Statement::Class {
name: "MyClass".into(),
extends: None,
implements: vec![],
body: vec![Statement::Property {
var: "a".into(),
value: None,
r#type: None,
flags: vec![PropertyFlag::Protected],
}],
flag: None,
}],
);
}

fn assert_ast(source: &str, expected: &[Statement]) {
Expand Down