1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, rc::Rc};34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]5pub enum FieldName {6 7 Fixed(String),8 9 Dyn(LocExpr),10}1112#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]13pub enum Visibility {14 15 Normal,16 17 Hidden,18 19 Unhide,20}2122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]23pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);2425#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]26pub struct FieldMember {27 pub name: FieldName,28 pub plus: bool,29 pub params: Option<ParamsDesc>,30 pub visibility: Visibility,31 pub value: LocExpr,32}3334#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]35pub enum Member {36 Field(FieldMember),37 BindStmt(BindSpec),38 AssertStmt(AssertStmt),39}4041#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]42pub enum UnaryOpType {43 Plus,44 Minus,45 BitNot,46 Not,47}4849#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]50pub enum BinaryOpType {51 Mul,52 Div,53 54 55 Add,56 Sub,5758 Lhs,59 Rhs,6061 Lt,62 Gt,63 Lte,64 Gte,6566 In,6768 69 70 7172 BitAnd,73 BitOr,74 BitXor,7576 And,77 Or,78}798081#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]82pub struct Param(pub String, pub Option<LocExpr>);8384#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]85pub struct ParamsDesc(pub Vec<Param>);86impl ParamsDesc {87 pub fn with_defaults(&self) -> Vec<Param> {88 self.0.iter().filter(|e| e.1.is_some()).cloned().collect()89 }90}9192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]93pub struct Arg(pub Option<String>, pub LocExpr);94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]95pub struct ArgsDesc(pub Vec<Arg>);9697#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]98pub struct BindSpec {99 pub name: String,100 pub params: Option<ParamsDesc>,101 pub value: LocExpr,102}103104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]105pub struct IfSpecData(pub LocExpr);106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]107pub struct ForSpecData(pub String, pub LocExpr);108109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]110pub enum CompSpec {111 IfSpec(IfSpecData),112 ForSpec(ForSpecData),113}114115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]116pub enum ObjBody {117 MemberList(Vec<Member>),118 ObjComp {119 pre_locals: Vec<BindSpec>,120 key: LocExpr,121 value: LocExpr,122 post_locals: Vec<BindSpec>,123 first: ForSpecData,124 rest: Vec<CompSpec>,125 },126}127128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]129pub enum LiteralType {130 This,131 Super,132 Dollar,133 Null,134 True,135 False,136}137138#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]139pub struct SliceDesc {140 pub start: Option<LocExpr>,141 pub end: Option<LocExpr>,142 pub step: Option<LocExpr>,143}144145146#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]147pub enum Expr {148 Literal(LiteralType),149150 151 Str(String),152 153 Num(f64),154 155 Var(String),156157 158 Arr(Vec<LocExpr>),159 160 161 162 163 164 165 166 167 168 169 170 ArrComp(LocExpr, Vec<CompSpec>),171172 173 Obj(ObjBody),174 175 ObjExtend(LocExpr, ObjBody),176177 178 Parened(LocExpr),179180 181 182 Params(ParamsDesc),183 184 185 Args(ArgsDesc),186187 188 UnaryOp(UnaryOpType, LocExpr),189 190 BinaryOp(LocExpr, BinaryOpType, LocExpr),191 192 AssertExpr(AssertStmt, LocExpr),193 194 LocalExpr(Vec<BindSpec>, LocExpr),195196 197 Bind(BindSpec),198 199 Import(String),200 201 ImportStr(String),202 203 Error(LocExpr),204 205 Apply(LocExpr, ArgsDesc),206 207 Select(LocExpr, String),208 209 Index(LocExpr, LocExpr),210 211 Slice(LocExpr, SliceDesc),212 213 Function(ParamsDesc, LocExpr),214 215 IfElse {216 cond: IfSpecData,217 cond_then: LocExpr,218 cond_else: Option<LocExpr>,219 },220 221 IfSpec(IfSpecData),222 223 ForSpec(ForSpecData),224}225226227#[derive(Clone, PartialEq, Serialize, Deserialize)]228pub struct ExprLocation(pub String, pub usize, pub usize);229impl Debug for ExprLocation {230 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {231 write!(f, "{}:{:?}-{:?}", self.0, self.1, self.2)232 }233}234235236#[derive(Clone, PartialEq, Serialize, Deserialize)]237pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);238impl Debug for LocExpr {239 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {240 write!(f, "{:?} from {:?}", self.0, self.1)241 }242}243244245#[macro_export]246macro_rules! loc_expr {247 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {248 LocExpr(249 std::rc::Rc::new($expr),250 if $need_loc {251 Some(std::rc::Rc::new(ExprLocation(252 $name.to_owned(),253 $start,254 $end,255 )))256 } else {257 None258 },259 )260 };261}262263264#[macro_export]265macro_rules! loc_expr_todo {266 ($expr:expr) => {267 LocExpr(Rc::new($expr), None)268 };269}