1use serde::{Deserialize, Serialize};2use std::{fmt::Debug, ops::Deref, path::PathBuf, rc::Rc};34#[derive(Debug, PartialEq, Serialize, Deserialize)]5pub enum FieldName {6 7 Fixed(Rc<str>),8 9 Dyn(LocExpr),10}1112#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]13pub enum Visibility {14 15 Normal,16 17 Hidden,18 19 Unhide,20}2122#[derive(Debug, PartialEq, Serialize, Deserialize)]23pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);2425#[derive(Debug, 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, 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 BitAnd,66 BitOr,67 BitXor,6869 And,70 Or,71}727374#[derive(Debug, PartialEq, Serialize, Deserialize)]75pub struct Param(pub Rc<str>, pub Option<LocExpr>);7677#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]78pub struct ParamsDesc(pub Rc<Vec<Param>>);79impl Deref for ParamsDesc {80 type Target = Vec<Param>;81 fn deref(&self) -> &Self::Target {82 &self.083 }84}8586#[derive(Debug, PartialEq, Serialize, Deserialize)]87pub struct Arg(pub Option<String>, pub LocExpr);88#[derive(Debug, PartialEq, Serialize, Deserialize)]89pub struct ArgsDesc(pub Vec<Arg>);90impl Deref for ArgsDesc {91 type Target = Vec<Arg>;92 fn deref(&self) -> &Self::Target {93 &self.094 }95}9697#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]98pub struct BindSpec {99 pub name: Rc<str>,100 pub params: Option<ParamsDesc>,101 pub value: LocExpr,102}103104#[derive(Debug, PartialEq, Serialize, Deserialize)]105pub struct IfSpecData(pub LocExpr);106#[derive(Debug, PartialEq, Serialize, Deserialize)]107pub struct ForSpecData(pub Rc<str>, pub LocExpr);108109#[derive(Debug, PartialEq, Serialize, Deserialize)]110pub enum CompSpec {111 IfSpec(IfSpecData),112 ForSpec(ForSpecData),113}114115#[derive(Debug, PartialEq, Serialize, Deserialize)]116pub struct ObjComp {117 pub pre_locals: Vec<BindSpec>,118 pub key: LocExpr,119 pub value: LocExpr,120 pub post_locals: Vec<BindSpec>,121 pub compspecs: Vec<CompSpec>,122}123124#[derive(Debug, PartialEq, Serialize, Deserialize)]125pub enum ObjBody {126 MemberList(Vec<Member>),127 ObjComp(ObjComp),128}129130#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)]131pub enum LiteralType {132 This,133 Super,134 Dollar,135 Null,136 True,137 False,138}139140#[derive(Debug, PartialEq, Serialize, Deserialize)]141pub struct SliceDesc {142 pub start: Option<LocExpr>,143 pub end: Option<LocExpr>,144 pub step: Option<LocExpr>,145}146147148#[derive(Debug, PartialEq, Serialize, Deserialize)]149pub enum Expr {150 Literal(LiteralType),151152 153 Str(Rc<str>),154 155 Num(f64),156 157 Var(Rc<str>),158159 160 Arr(Vec<LocExpr>),161 162 163 164 165 166 167 168 169 170 171 172 ArrComp(LocExpr, Vec<CompSpec>),173174 175 Obj(ObjBody),176 177 ObjExtend(LocExpr, ObjBody),178179 180 Parened(LocExpr),181182 183 UnaryOp(UnaryOpType, LocExpr),184 185 BinaryOp(LocExpr, BinaryOpType, LocExpr),186 187 AssertExpr(AssertStmt, LocExpr),188 189 LocalExpr(Vec<BindSpec>, LocExpr),190191 192 Import(PathBuf),193 194 ImportStr(PathBuf),195 196 Error(LocExpr),197 198 Apply(LocExpr, ArgsDesc, bool),199 200 Index(LocExpr, LocExpr),201 202 Function(ParamsDesc, LocExpr),203 204 IfElse {205 cond: IfSpecData,206 cond_then: LocExpr,207 cond_else: Option<LocExpr>,208 },209}210211212#[derive(Clone, PartialEq, Serialize, Deserialize)]213pub struct ExprLocation(pub Rc<PathBuf>, pub usize, pub usize);214impl Debug for ExprLocation {215 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {216 write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)217 }218}219220221#[derive(Clone, PartialEq, Serialize, Deserialize)]222pub struct LocExpr(pub Rc<Expr>, pub Option<ExprLocation>);223impl Debug for LocExpr {224 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {225 write!(f, "{:?} from {:?}", self.0, self.1)226 }227}228229230#[macro_export]231macro_rules! loc_expr {232 ($expr:expr, $need_loc:expr,($name:expr, $start:expr, $end:expr)) => {233 LocExpr(234 std::rc::Rc::new($expr),235 if $need_loc {236 Some(ExprLocation($name, $start, $end))237 } else {238 None239 },240 )241 };242}243244245#[macro_export]246macro_rules! loc_expr_todo {247 ($expr:expr) => {248 LocExpr(Rc::new($expr), None)249 };250}