1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, path::PathBuf, 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,5354 Add,55 Sub,5657 Lhs,58 Rhs,5960 Lt,61 Gt,62 Lte,63 Gte,6465 In,6667 BitAnd,68 BitOr,69 BitXor,7071 And,72 Or,73}747576#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]77pub struct Param(pub String, pub Option<LocExpr>);7879#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]80pub struct ParamsDesc(pub Vec<Param>);81impl ParamsDesc {82 pub fn with_defaults(&self) -> Vec<Param> {83 self.0.iter().filter(|e| e.1.is_some()).cloned().collect()84 }85}8687#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]88pub struct Arg(pub Option<String>, pub LocExpr);89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]90pub struct ArgsDesc(pub Vec<Arg>);9192#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]93pub struct BindSpec {94 pub name: String,95 pub params: Option<ParamsDesc>,96 pub value: LocExpr,97}9899#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]100pub struct IfSpecData(pub LocExpr);101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]102pub struct ForSpecData(pub String, pub LocExpr);103104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]105pub enum CompSpec {106 IfSpec(IfSpecData),107 ForSpec(ForSpecData),108}109110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]111pub enum ObjBody {112 MemberList(Vec<Member>),113 ObjComp {114 pre_locals: Vec<BindSpec>,115 key: LocExpr,116 value: LocExpr,117 post_locals: Vec<BindSpec>,118 rest: Vec<CompSpec>,119 },120}121122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]123pub enum LiteralType {124 This,125 Super,126 Dollar,127 Null,128 True,129 False,130}131132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]133pub struct SliceDesc {134 pub start: Option<LocExpr>,135 pub end: Option<LocExpr>,136 pub step: Option<LocExpr>,137}138139140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]141pub enum Expr {142 Literal(LiteralType),143144 145 Str(String),146 147 Num(f64),148 149 Var(String),150151 152 Arr(Vec<LocExpr>),153 154 155 156 157 158 159 160 161 162 163 164 ArrComp(LocExpr, Vec<CompSpec>),165166 167 Obj(ObjBody),168 169 ObjExtend(LocExpr, ObjBody),170171 172 Parened(LocExpr),173174 175 176 Params(ParamsDesc),177 178 179 Args(ArgsDesc),180181 182 UnaryOp(UnaryOpType, LocExpr),183 184 BinaryOp(LocExpr, BinaryOpType, LocExpr),185 186 AssertExpr(AssertStmt, LocExpr),187 188 LocalExpr(Vec<BindSpec>, LocExpr),189190 191 Bind(BindSpec),192 193 Import(String),194 195 ImportStr(String),196 197 Error(LocExpr),198 199 Apply(LocExpr, ArgsDesc, bool),200 201 Select(LocExpr, String),202 203 Index(LocExpr, LocExpr),204 205 Slice(LocExpr, SliceDesc),206 207 Function(ParamsDesc, LocExpr),208 209 IfElse {210 cond: IfSpecData,211 cond_then: LocExpr,212 cond_else: Option<LocExpr>,213 },214 215 IfSpec(IfSpecData),216 217 ForSpec(ForSpecData),218}219220221#[derive(Clone, PartialEq, Serialize, Deserialize)]222pub struct ExprLocation(pub PathBuf, pub usize, pub usize);223impl Debug for ExprLocation {224 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {225 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)226 }227}228229230#[derive(Clone, PartialEq, Serialize, Deserialize)]231pub struct LocExpr(pub Rc<Expr>, pub Option<Rc<ExprLocation>>);232impl Debug for LocExpr {233 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {234 write!(f, "{:?} from {:?}", self.0, self.1)235 }236}237238239#[macro_export]240macro_rules! loc_expr {241 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {242 LocExpr(243 std::rc::Rc::new($expr),244 if $need_loc {245 Some(std::rc::Rc::new(ExprLocation(246 $name.to_owned(),247 $start,248 $end,249 )))250 } else {251 None252 },253 )254 };255}256257258#[macro_export]259macro_rules! loc_expr_todo {260 ($expr:expr) => {261 LocExpr(Rc::new($expr), None)262 };263}