From af1261355ab12eb1aee40deaabafc78e77c1588b Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Mon, 12 Sep 2022 22:23:34 +1200 Subject: [PATCH] Parse include and include_once --- trunk_lexer/src/lexer.rs | 2 + trunk_lexer/src/token.rs | 2 + trunk_parser/src/ast.rs | 26 ++++++++-- trunk_parser/src/parser/mod.rs | 94 +++++++++++++++++++++++++--------- 4 files changed, 97 insertions(+), 27 deletions(-) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 7851a9f..f969e5e 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -846,6 +846,8 @@ fn identifier_to_keyword(ident: &str) -> Option { "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, diff --git a/trunk_lexer/src/token.rs b/trunk_lexer/src/token.rs index 8421b9a..f18d89e 100644 --- a/trunk_lexer/src/token.rs +++ b/trunk_lexer/src/token.rs @@ -90,6 +90,8 @@ pub enum TokenKind { Identifier(String), If, Implements, + Include, + IncludeOnce, Increment, InlineHtml(String), Instanceof, diff --git a/trunk_parser/src/ast.rs b/trunk_parser/src/ast.rs index 82ce978..5d75052 100644 --- a/trunk_parser/src/ast.rs +++ b/trunk_parser/src/ast.rs @@ -143,6 +143,26 @@ pub struct StaticVar { pub default: Option, } +#[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), @@ -166,10 +186,8 @@ pub enum Statement { value_var: Expression, body: Block, }, - Require { - path: Expression, - }, - RequireOnce { + Include { + kind: IncludeKind, path: Expression, }, Var { diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index c278de3..8375aff 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -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, }; @@ -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(); @@ -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; @@ -2161,6 +2156,49 @@ mod tests { }; } + #[test] + fn include() { + assert_ast( + "