diff --git a/src/ast.rs b/src/ast.rs index 30091ef..4dbdc26 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -2,6 +2,7 @@ mod basic; mod call; mod expr; mod field; +mod func_def; mod lit; mod table_constr; mod table_destr; @@ -13,6 +14,7 @@ pub use self::basic::*; pub use self::call::*; pub use self::expr::*; pub use self::field::*; +pub use self::func_def::*; pub use self::lit::*; pub use self::table_constr::*; pub use self::table_destr::*; diff --git a/src/ast/expr.rs b/src/ast/expr.rs index 8a0cf04..d156271 100644 --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -3,6 +3,7 @@ use crate::span::{HasSpan, Span}; use super::basic::Space; use super::call::Call; use super::field::Field; +use super::func_def::FuncDef; use super::lit::Lit; use super::table_constr::TableConstr; use super::table_destr::TableDestr; @@ -38,6 +39,7 @@ pub enum Expr { Var(Var), TableConstr(TableConstr), TableDestr(TableDestr), + FuncDef(FuncDef), /// `(a)` /// @@ -93,6 +95,7 @@ impl HasSpan for Expr { Expr::Var(var) => var.span(), Expr::TableConstr(constr) => constr.span(), Expr::TableDestr(destr) => destr.span(), + Expr::FuncDef(def) => def.span(), Expr::Paren { span, .. } => *span, Expr::Neg { span, .. } => *span, Expr::Not { span, .. } => *span, diff --git a/src/ast/func_def.rs b/src/ast/func_def.rs new file mode 100644 index 0000000..b7eaded --- /dev/null +++ b/src/ast/func_def.rs @@ -0,0 +1,95 @@ +use crate::span::{HasSpan, Span}; + +use super::{Expr, Ident, Space, TablePattern}; + +#[derive(Debug, Clone)] +pub enum FuncDef { + /// `function() a + /// + /// Structure: `function s0 ( s1 ) s2 body` + AnonNoArg { + s0: Space, + s1: Space, + s2: Space, + body: Box, + span: Span, + }, + + /// `function(foo) a` + /// + /// Structure: `function s0 ( s1 arg s2 ) s3 body` + AnonArg { + s0: Space, + s1: Space, + arg: Ident, + s2: Space, + s3: Space, + body: Box, + span: Span, + }, + + /// `function{..} a` + /// + /// Structure: `function s0 pattern s1 body` + AnonDestr { + s0: Space, + pattern: TablePattern, + s1: Space, + body: Box, + span: Span, + }, + + /// `function foo() a` + /// + /// Structure: `function s0 name s1 ( s2 ) s3 body` + NamedNoArg { + s0: Space, + name: Ident, + s1: Space, + s2: Space, + s3: Space, + body: Box, + span: Span, + }, + + /// `function foo(bar) a` + /// + /// Structure: `function s0 name s1 ( s2 arg s3 ) s4 body` + NamedArg { + s0: Space, + name: Ident, + s1: Space, + s2: Space, + arg: Ident, + s3: Space, + s4: Space, + body: Box, + span: Span, + }, + + /// `function foo{..} a` + /// + /// Structure: `function s0 name s1 pattern s2 body` + NamedDestr { + s0: Space, + name: Ident, + s1: Space, + pattern: TablePattern, + s2: Space, + body: Box, + span: Span, + }, +} + +impl HasSpan for FuncDef { + fn span(&self) -> Span { + match self { + FuncDef::AnonNoArg { span, .. } => *span, + FuncDef::AnonArg { span, .. } => *span, + FuncDef::AnonDestr { span, .. } => *span, + FuncDef::NamedNoArg { span, .. } => *span, + FuncDef::NamedArg { span, .. } => *span, + FuncDef::NamedDestr { span, .. } => *span, + } + } +}