use crate::span::Span;
#[derive(Clone, Debug, PartialEq)]
pub struct Template {
pub span: Span,
pub nodes: Vec<Node>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Node {
pub span: Span,
pub kind: NodeKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Expr {
pub span: Span,
pub kind: ExprKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Pattern {
pub span: Span,
pub kind: PatternKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Stmt {
pub span: Span,
pub kind: StmtKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
pub statements: Vec<Stmt>,
pub tail: Option<Box<Expr>>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct MatchArm {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub value: Expr,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FieldDecl {
pub name: String,
pub ty: Option<String>,
pub default: Option<Expr>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Branch {
pub test: Option<Condition>,
pub body: Vec<Node>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Condition {
pub kind: ConditionKind,
pub args: Vec<Expr>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SwitchArm {
pub test: Option<Expr>,
pub body: Vec<Node>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PushBranch {
pub condition: Option<Expr>,
pub stack: Expr,
pub body: Vec<Node>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Component {
pub name: String,
pub attributes: Vec<ComponentAttr>,
pub body: Vec<Node>,
pub self_closing: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Slot {
pub name: Option<String>,
pub attributes: Vec<ComponentAttr>,
pub body: Vec<Node>,
pub self_closing: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ComponentAttr {
pub span: Span,
pub kind: ComponentAttrKind,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CallArgs {
pub types: Vec<String>,
pub values: Vec<Expr>,
}
#[derive(Clone, Debug, PartialEq, Copy, Eq)]
#[non_exhaustive]
pub enum EchoKind {
Escaped,
Raw,
Legacy,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Literal {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum UnaryOp {
Not,
Negate,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum BinaryOp {
Coalesce,
Or,
And,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
Xor,
Add,
Sub,
Mul,
Div,
Rem,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum AssignOp {
Set,
Add,
Sub,
Mul,
Div,
Rem,
Xor,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum MapKey {
Static(String),
Computed(Expr),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum PatternKind {
Binding(String),
Wildcard,
Literal(Literal),
Path(Vec<String>),
Tuple(Vec<Pattern>),
List(Vec<Pattern>),
Or(Vec<Pattern>),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ExprKind {
Literal(Literal),
Path(Vec<String>),
List(Vec<Expr>),
Tuple(Vec<Expr>),
Map(Vec<(MapKey, Expr)>),
ConditionalEntry {
value: Box<Expr>,
condition: Box<Expr>,
},
NamedArgument {
name: String,
value: Box<Expr>,
},
Field {
receiver: Box<Expr>,
name: String,
optional: bool,
},
Index {
receiver: Box<Expr>,
index: Box<Expr>,
optional: bool,
},
Call {
callee: Box<Expr>,
args: CallArgs,
},
Method {
receiver: Box<Expr>,
name: String,
optional: bool,
args: CallArgs,
},
Try(Box<Expr>),
Cast {
value: Box<Expr>,
ty: String,
},
Unary {
op: UnaryOp,
value: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Range {
start: Option<Box<Expr>>,
end: Option<Box<Expr>>,
inclusive: bool,
},
If {
test: Box<Expr>,
then: Block,
otherwise: Option<Box<Expr>>,
},
Block(Block),
Match {
value: Box<Expr>,
arms: Vec<MatchArm>,
},
Closure {
params: Vec<Pattern>,
body: Box<Expr>,
},
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum StmtKind {
Let {
pattern: Pattern,
mutable: bool,
ty: Option<String>,
value: Expr,
},
Assign {
target: Expr,
op: AssignOp,
value: Expr,
},
Expr(Expr),
For {
pattern: Pattern,
iterable: Expr,
body: Block,
},
While {
test: Expr,
body: Block,
},
Break,
Continue,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ComponentAttrKind {
Literal {
name: String,
parts: Vec<AttributePart>,
},
Bound {
name: String,
value: Expr,
},
Shorthand(String),
Escaped {
name: String,
parts: Option<Vec<AttributePart>>,
},
Bare(String),
Spread(Expr),
Class(Expr),
Style(Expr),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum AttributePart {
Text(String),
Echo(EchoKind, Expr),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum LoopKind {
For,
Foreach,
Forelse,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SectionEnd {
Stop,
Show,
Append,
Overwrite,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ConditionKind {
If,
Unless,
Isset,
Empty,
Auth,
Guest,
Can,
Cannot,
CanAny,
Env,
Production,
Session,
Context,
HasSection,
SectionMissing,
HasStack,
Error,
Custom {
name: String,
negated: bool,
},
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum BlockKind {
Once,
Push,
PushOnce,
Prepend,
PrependOnce,
Fragment,
Component,
ComponentFirst,
Slot,
Lang,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum InlineKind {
Extends,
ExtendsFirst,
Yield,
Parent,
Include,
IncludeIf,
IncludeWhen,
IncludeUnless,
IncludeFirst,
IncludeIsolated,
Each,
Stack,
Class,
Style,
Checked,
Selected,
Disabled,
Readonly,
Required,
Bool,
Csrf,
Method,
Json,
Js,
Lang,
Choice,
Vite,
ViteReactRefresh,
Fonts,
Dump,
Dd,
Unset,
Break,
Continue,
Slot,
Custom(String),
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum NodeKind {
Text(String),
Echo(EchoKind, Expr),
Comment,
Inline {
kind: InlineKind,
args: Vec<Expr>,
raw_args: Option<Span>,
},
Conditional(Vec<Branch>),
Loop {
kind: LoopKind,
pattern: Pattern,
iterable: Expr,
body: Vec<Node>,
empty: Option<Vec<Node>>,
},
While {
test: Expr,
body: Vec<Node>,
},
Switch {
value: Expr,
arms: Vec<SwitchArm>,
},
Code(Vec<Stmt>),
Block {
kind: BlockKind,
args: Vec<Expr>,
body: Vec<Node>,
},
Section {
name: Expr,
content: Option<Expr>,
body: Vec<Node>,
end: SectionEnd,
},
PushIf(Vec<PushBranch>),
Props(Vec<FieldDecl>),
Aware(Vec<FieldDecl>),
Inject(FieldDecl),
Use(String),
Component(Component),
Slot(Slot),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DirectiveName {
If,
Elseif,
Else,
Endif,
Unless,
Endunless,
Isset,
Endisset,
Empty,
Endempty,
Switch,
Case,
Default,
Endswitch,
Auth,
Elseauth,
Endauth,
Guest,
Elseguest,
Endguest,
Can,
Elsecan,
Endcan,
Cannot,
Elsecannot,
Endcannot,
CanAny,
Elsecanany,
Endcanany,
Env,
Endenv,
Production,
Endproduction,
Session,
Endsession,
Context,
Endcontext,
HasSection,
SectionMissing,
HasStack,
Foreach,
Endforeach,
Forelse,
Endforelse,
For,
Endfor,
While,
Endwhile,
Break,
Continue,
Let,
Code,
Endcode,
Php,
Endphp,
Verbatim,
Endverbatim,
Unset,
Use,
Inject,
Class,
Style,
Checked,
Selected,
Disabled,
Readonly,
Required,
Bool,
Csrf,
Method,
Error,
Enderror,
Json,
Js,
Include,
IncludeIf,
IncludeWhen,
IncludeUnless,
IncludeFirst,
IncludeIsolated,
Each,
Extends,
ExtendsFirst,
Section,
Endsection,
Stop,
Show,
Append,
Overwrite,
Yield,
Parent,
Push,
Endpush,
Prepend,
Endprepend,
PushOnce,
Endpushonce,
PrependOnce,
Endprependonce,
PushIf,
ElsePushIf,
ElsePush,
Endpushif,
Stack,
Once,
Endonce,
Fragment,
Endfragment,
Props,
Aware,
Component,
Endcomponent,
ComponentFirst,
Endcomponentfirst,
Slot,
Endslot,
Lang,
Endlang,
Choice,
Vite,
ViteReactRefresh,
Fonts,
Dump,
Dd,
Custom(String),
Condition(String),
UnlessCondition(String),
ElseCondition(String),
EndCondition(String),
}
impl DirectiveName {
pub(crate) fn builtin(name: &str) -> Option<Self> {
Some(match name {
"if" => Self::If,
"elseif" => Self::Elseif,
"else" => Self::Else,
"endif" => Self::Endif,
"unless" => Self::Unless,
"endunless" => Self::Endunless,
"isset" => Self::Isset,
"endisset" => Self::Endisset,
"empty" => Self::Empty,
"endempty" => Self::Endempty,
"switch" => Self::Switch,
"case" => Self::Case,
"default" => Self::Default,
"endswitch" => Self::Endswitch,
"auth" => Self::Auth,
"elseauth" => Self::Elseauth,
"endauth" => Self::Endauth,
"guest" => Self::Guest,
"elseguest" => Self::Elseguest,
"endguest" => Self::Endguest,
"can" => Self::Can,
"elsecan" => Self::Elsecan,
"endcan" => Self::Endcan,
"cannot" => Self::Cannot,
"elsecannot" => Self::Elsecannot,
"endcannot" => Self::Endcannot,
"canany" => Self::CanAny,
"elsecanany" => Self::Elsecanany,
"endcanany" => Self::Endcanany,
"env" => Self::Env,
"endenv" => Self::Endenv,
"production" => Self::Production,
"endproduction" => Self::Endproduction,
"session" => Self::Session,
"endsession" => Self::Endsession,
"context" => Self::Context,
"endcontext" => Self::Endcontext,
"hassection" => Self::HasSection,
"sectionmissing" => Self::SectionMissing,
"hasstack" => Self::HasStack,
"foreach" => Self::Foreach,
"endforeach" => Self::Endforeach,
"forelse" => Self::Forelse,
"endforelse" => Self::Endforelse,
"for" => Self::For,
"endfor" => Self::Endfor,
"while" => Self::While,
"endwhile" => Self::Endwhile,
"break" => Self::Break,
"continue" => Self::Continue,
"let" => Self::Let,
"code" => Self::Code,
"endcode" => Self::Endcode,
"php" => Self::Php,
"endphp" => Self::Endphp,
"verbatim" => Self::Verbatim,
"endverbatim" => Self::Endverbatim,
"unset" => Self::Unset,
"use" => Self::Use,
"inject" => Self::Inject,
"class" => Self::Class,
"style" => Self::Style,
"checked" => Self::Checked,
"selected" => Self::Selected,
"disabled" => Self::Disabled,
"readonly" => Self::Readonly,
"required" => Self::Required,
"bool" => Self::Bool,
"csrf" => Self::Csrf,
"method" => Self::Method,
"error" => Self::Error,
"enderror" => Self::Enderror,
"json" => Self::Json,
"js" => Self::Js,
"include" => Self::Include,
"includeif" => Self::IncludeIf,
"includewhen" => Self::IncludeWhen,
"includeunless" => Self::IncludeUnless,
"includefirst" => Self::IncludeFirst,
"includeisolated" => Self::IncludeIsolated,
"each" => Self::Each,
"extends" => Self::Extends,
"extendsfirst" => Self::ExtendsFirst,
"section" => Self::Section,
"endsection" => Self::Endsection,
"stop" => Self::Stop,
"show" => Self::Show,
"append" => Self::Append,
"overwrite" => Self::Overwrite,
"yield" => Self::Yield,
"parent" => Self::Parent,
"push" => Self::Push,
"endpush" => Self::Endpush,
"prepend" => Self::Prepend,
"endprepend" => Self::Endprepend,
"pushonce" => Self::PushOnce,
"endpushonce" => Self::Endpushonce,
"prependonce" => Self::PrependOnce,
"endprependonce" => Self::Endprependonce,
"pushif" => Self::PushIf,
"elsepushif" => Self::ElsePushIf,
"elsepush" => Self::ElsePush,
"endpushif" => Self::Endpushif,
"stack" => Self::Stack,
"once" => Self::Once,
"endonce" => Self::Endonce,
"fragment" => Self::Fragment,
"endfragment" => Self::Endfragment,
"props" => Self::Props,
"aware" => Self::Aware,
"component" => Self::Component,
"endcomponent" => Self::Endcomponent,
"componentfirst" => Self::ComponentFirst,
"endcomponentfirst" => Self::Endcomponentfirst,
"slot" => Self::Slot,
"endslot" => Self::Endslot,
"lang" => Self::Lang,
"endlang" => Self::Endlang,
"choice" => Self::Choice,
"vite" => Self::Vite,
"vitereactrefresh" => Self::ViteReactRefresh,
"fonts" => Self::Fonts,
"dump" => Self::Dump,
"dd" => Self::Dd,
_ => return None,
})
}
pub fn spelling(&self) -> String {
match self {
Self::If => "if".into(),
Self::Elseif => "elseif".into(),
Self::Else => "else".into(),
Self::Endif => "endif".into(),
Self::Unless => "unless".into(),
Self::Endunless => "endunless".into(),
Self::Isset => "isset".into(),
Self::Endisset => "endisset".into(),
Self::Empty => "empty".into(),
Self::Endempty => "endempty".into(),
Self::Switch => "switch".into(),
Self::Case => "case".into(),
Self::Default => "default".into(),
Self::Endswitch => "endswitch".into(),
Self::Auth => "auth".into(),
Self::Elseauth => "elseauth".into(),
Self::Endauth => "endauth".into(),
Self::Guest => "guest".into(),
Self::Elseguest => "elseguest".into(),
Self::Endguest => "endguest".into(),
Self::Can => "can".into(),
Self::Elsecan => "elsecan".into(),
Self::Endcan => "endcan".into(),
Self::Cannot => "cannot".into(),
Self::Elsecannot => "elsecannot".into(),
Self::Endcannot => "endcannot".into(),
Self::CanAny => "canany".into(),
Self::Elsecanany => "elsecanany".into(),
Self::Endcanany => "endcanany".into(),
Self::Env => "env".into(),
Self::Endenv => "endenv".into(),
Self::Production => "production".into(),
Self::Endproduction => "endproduction".into(),
Self::Session => "session".into(),
Self::Endsession => "endsession".into(),
Self::Context => "context".into(),
Self::Endcontext => "endcontext".into(),
Self::HasSection => "hassection".into(),
Self::SectionMissing => "sectionmissing".into(),
Self::HasStack => "hasstack".into(),
Self::Foreach => "foreach".into(),
Self::Endforeach => "endforeach".into(),
Self::Forelse => "forelse".into(),
Self::Endforelse => "endforelse".into(),
Self::For => "for".into(),
Self::Endfor => "endfor".into(),
Self::While => "while".into(),
Self::Endwhile => "endwhile".into(),
Self::Break => "break".into(),
Self::Continue => "continue".into(),
Self::Let => "let".into(),
Self::Code => "code".into(),
Self::Endcode => "endcode".into(),
Self::Php => "php".into(),
Self::Endphp => "endphp".into(),
Self::Verbatim => "verbatim".into(),
Self::Endverbatim => "endverbatim".into(),
Self::Unset => "unset".into(),
Self::Use => "use".into(),
Self::Inject => "inject".into(),
Self::Class => "class".into(),
Self::Style => "style".into(),
Self::Checked => "checked".into(),
Self::Selected => "selected".into(),
Self::Disabled => "disabled".into(),
Self::Readonly => "readonly".into(),
Self::Required => "required".into(),
Self::Bool => "bool".into(),
Self::Csrf => "csrf".into(),
Self::Method => "method".into(),
Self::Error => "error".into(),
Self::Enderror => "enderror".into(),
Self::Json => "json".into(),
Self::Js => "js".into(),
Self::Include => "include".into(),
Self::IncludeIf => "includeif".into(),
Self::IncludeWhen => "includewhen".into(),
Self::IncludeUnless => "includeunless".into(),
Self::IncludeFirst => "includefirst".into(),
Self::IncludeIsolated => "includeisolated".into(),
Self::Each => "each".into(),
Self::Extends => "extends".into(),
Self::ExtendsFirst => "extendsfirst".into(),
Self::Section => "section".into(),
Self::Endsection => "endsection".into(),
Self::Stop => "stop".into(),
Self::Show => "show".into(),
Self::Append => "append".into(),
Self::Overwrite => "overwrite".into(),
Self::Yield => "yield".into(),
Self::Parent => "parent".into(),
Self::Push => "push".into(),
Self::Endpush => "endpush".into(),
Self::Prepend => "prepend".into(),
Self::Endprepend => "endprepend".into(),
Self::PushOnce => "pushonce".into(),
Self::Endpushonce => "endpushonce".into(),
Self::PrependOnce => "prependonce".into(),
Self::Endprependonce => "endprependonce".into(),
Self::PushIf => "pushif".into(),
Self::ElsePushIf => "elsepushif".into(),
Self::ElsePush => "elsepush".into(),
Self::Endpushif => "endpushif".into(),
Self::Stack => "stack".into(),
Self::Once => "once".into(),
Self::Endonce => "endonce".into(),
Self::Fragment => "fragment".into(),
Self::Endfragment => "endfragment".into(),
Self::Props => "props".into(),
Self::Aware => "aware".into(),
Self::Component => "component".into(),
Self::Endcomponent => "endcomponent".into(),
Self::ComponentFirst => "componentfirst".into(),
Self::Endcomponentfirst => "endcomponentfirst".into(),
Self::Slot => "slot".into(),
Self::Endslot => "endslot".into(),
Self::Lang => "lang".into(),
Self::Endlang => "endlang".into(),
Self::Choice => "choice".into(),
Self::Vite => "vite".into(),
Self::ViteReactRefresh => "vitereactrefresh".into(),
Self::Fonts => "fonts".into(),
Self::Dump => "dump".into(),
Self::Dd => "dd".into(),
Self::Custom(name) | Self::Condition(name) => name.clone(),
Self::UnlessCondition(name) => format!("unless{name}"),
Self::ElseCondition(name) => format!("else{name}"),
Self::EndCondition(name) => format!("end{name}"),
}
}
}