Add function definitions to ast
This commit is contained in:
parent
5466f7afbf
commit
36e40d254d
3 changed files with 100 additions and 0 deletions
|
|
@ -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::*;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
95
src/ast/func_def.rs
Normal file
95
src/ast/func_def.rs
Normal file
|
|
@ -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<Expr>,
|
||||
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<Expr>,
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// `function{..} a`
|
||||
///
|
||||
/// Structure: `function s0 pattern s1 body`
|
||||
AnonDestr {
|
||||
s0: Space,
|
||||
pattern: TablePattern,
|
||||
s1: Space,
|
||||
body: Box<Expr>,
|
||||
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<Expr>,
|
||||
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<Expr>,
|
||||
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<Expr>,
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue