use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq)]
pub enum LiteralValue {
Integer(i64),
Float(f64),
Number(String),
Text(String),
Bool(bool),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Not,
NotWord,
Negate,
Plus,
BitwiseNot,
Boolean,
Address,
PreIncrement,
PreDecrement,
PostIncrement,
PostDecrement,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Subtract,
Multiply,
Divide,
IntegerDivide,
Power,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
ShiftLeft,
ShiftRight,
ShiftRightUnsigned,
Concat,
Equal,
NotEqual,
StrictEqual,
StrictNotEqual,
Less,
Greater,
LessEqual,
GreaterEqual,
RegexMatch,
And,
Or,
Is,
Coalesce,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeclareKind {
Global,
Local,
Static,
}
impl DeclareKind {
pub fn word(self) -> &'static str {
match self {
DeclareKind::Global => "global",
DeclareKind::Local => "local",
DeclareKind::Static => "static",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Declared {
pub name: String,
pub value: Option<Box<Node>>,
pub text: String,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Declarations {
pub globals: BTreeSet<String>,
pub locals: BTreeSet<String>,
pub statics: BTreeSet<String>,
pub assume_global: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MethodDef {
pub name: String,
pub parameters: Vec<Parameter>,
pub body: Vec<Node>,
pub declarations: Declarations,
pub is_static: bool,
pub line: u32,
pub body_line: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct PropertyDef {
pub name: String,
pub value: Option<Box<Node>>,
pub is_static: bool,
pub line: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AccessBody {
pub body: Vec<Node>,
pub declarations: Declarations,
pub line: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AccessDef {
pub name: String,
pub parameters: Vec<Parameter>,
pub getter: Option<AccessBody>,
pub setter: Option<AccessBody>,
pub is_static: bool,
pub line: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoopKind {
Count,
Files,
Reg,
Read,
Parse,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssignOp {
Set,
Add,
Subtract,
Multiply,
Divide,
IntegerDivide,
Concat,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
ShiftLeft,
ShiftRight,
ShiftRightUnsigned,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Branch {
pub condition: Node,
pub tail: Vec<Node>,
pub body: Vec<Node>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Case {
pub values: Vec<Node>,
pub body: Vec<Node>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Catch {
pub class: String,
pub variable: Option<String>,
pub body: Vec<Node>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DynamicPiece {
Text(String),
Value(Box<Node>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum MemberName {
Text(String),
Dynamic(Vec<DynamicPiece>),
}
impl PartialEq<str> for MemberName {
fn eq(&self, other: &str) -> bool {
match self {
MemberName::Text(text) => text == other,
MemberName::Dynamic(_) => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ObjectItem {
Property(MemberName, Node),
LegacyKey(Node, Node),
Value(Node),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
Literal { value: LiteralValue, line: u32 },
Var { name: String, line: u32 },
Unary {
op: UnaryOp,
operand: Box<Node>,
line: u32,
},
Binary {
op: BinaryOp,
left: Box<Node>,
right: Box<Node>,
line: u32,
},
Unset { line: u32 },
Ternary {
condition: Box<Node>,
then: Box<Node>,
otherwise: Box<Node>,
line: u32,
},
DynamicName {
pieces: Vec<DynamicPiece>,
line: u32,
},
AssignDynamic {
name: Vec<DynamicPiece>,
op: AssignOp,
value: Box<Node>,
line: u32,
},
Assign {
name: String,
op: AssignOp,
value: Box<Node>,
line: u32,
},
LegacyAssign {
name: String,
text: String,
line: u32,
},
LegacyComparison {
left: Box<Node>,
op: BinaryOp,
text: String,
line: u32,
},
LegacyMatch {
left: Box<Node>,
form: LegacyForm,
texts: Vec<String>,
negated: bool,
line: u32,
},
LegacyControl { condition: Box<Node>, line: u32 },
Index {
target: Box<Node>,
index: Box<Node>,
line: u32,
},
Call {
name: String,
arguments: Vec<Node>,
line: u32,
},
New {
path: Vec<String>,
arguments: Vec<Node>,
line: u32,
},
LegacyCall {
name: String,
parameters: Vec<LegacyParameter>,
line: u32,
},
LegacyIf {
name: String,
parameters: Vec<String>,
body: Vec<Node>,
otherwise: Vec<Node>,
line: u32,
},
Expression { value: Box<Node>, line: u32 },
LegacyExpression { value: Box<Node>, line: u32 },
Block { body: Vec<Node>, line: u32 },
If {
branches: Vec<Branch>,
otherwise: Vec<Node>,
line: u32,
},
Switch {
subject: Box<Node>,
cases: Vec<Case>,
line: u32,
},
While {
condition: Box<Node>,
body: Vec<Node>,
line: u32,
},
Loop {
mode: LoopKind,
arguments: Vec<Node>,
body: Vec<Node>,
until: Option<Box<Node>>,
line: u32,
},
Directive {
name: String,
argument: String,
line: u32,
},
Break { line: u32 },
Continue { line: u32 },
Return { value: Option<Box<Node>>, line: u32 },
ExitApp { code: Option<Box<Node>>, line: u32 },
FunctionDef {
name: String,
parameters: Vec<Parameter>,
body: Vec<Node>,
declarations: Declarations,
line: u32,
body_line: u32,
},
Super { line: u32 },
Declare {
scope: DeclareKind,
names: Vec<Declared>,
line: u32,
},
ClassDef {
name: String,
extends: Option<String>,
methods: Vec<MethodDef>,
properties: Vec<PropertyDef>,
accessors: Vec<AccessDef>,
nested: Vec<Node>,
line: u32,
},
MethodCall {
target: Box<Node>,
name: MemberName,
arguments: Vec<Node>,
line: u32,
},
Property {
target: Box<Node>,
name: MemberName,
line: u32,
},
AssignProperty {
target: Box<Node>,
name: MemberName,
op: AssignOp,
value: Box<Node>,
line: u32,
},
AssignIndex {
target: Box<Node>,
index: Box<Node>,
op: AssignOp,
value: Box<Node>,
line: u32,
},
ArrayLiteral { items: Vec<Option<Node>>, line: u32 },
Group { items: Vec<Node>, line: u32 },
Empty { line: u32 },
Legacy { items: Vec<Node>, line: u32 },
ObjectLiteral { items: Vec<ObjectItem>, line: u32 },
Lambda {
parameters: Vec<Parameter>,
body: Box<Node>,
line: u32,
},
Try {
body: Vec<Node>,
catches: Vec<Catch>,
otherwise: Vec<Node>,
finally: Vec<Node>,
line: u32,
},
Throw { value: Option<Box<Node>>, line: u32 },
For {
variables: Vec<String>,
source: Box<Node>,
body: Vec<Node>,
line: u32,
},
Label { name: String, line: u32 },
Goto { name: String, line: u32 },
HotkeyDefinition {
name: String,
body: Vec<Node>,
on_the_line: bool,
line: u32,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Parameter {
pub name: String,
pub default: Option<Box<Node>>,
pub variadic: bool,
pub by_ref: bool,
}
impl Node {
pub fn line(&self) -> u32 {
match self {
Node::Literal { line, .. }
| Node::Var { line, .. }
| Node::Unary { line, .. }
| Node::Binary { line, .. }
| Node::Unset { line }
| Node::Ternary { line, .. }
| Node::DynamicName { line, .. }
| Node::AssignDynamic { line, .. }
| Node::Assign { line, .. }
| Node::LegacyAssign { line, .. }
| Node::LegacyComparison { line, .. }
| Node::LegacyMatch { line, .. }
| Node::LegacyControl { line, .. }
| Node::AssignIndex { line, .. }
| Node::Index { line, .. }
| Node::Call { line, .. }
| Node::New { line, .. }
| Node::LegacyCall { line, .. }
| Node::LegacyIf { line, .. }
| Node::Expression { line, .. }
| Node::LegacyExpression { line, .. }
| Node::Block { line, .. }
| Node::If { line, .. }
| Node::Switch { line, .. }
| Node::While { line, .. }
| Node::Loop { line, .. }
| Node::Directive { line, .. }
| Node::Break { line }
| Node::Continue { line }
| Node::Return { line, .. }
| Node::ExitApp { line, .. }
| Node::FunctionDef { line, .. }
| Node::ClassDef { line, .. }
| Node::MethodCall { line, .. }
| Node::Property { line, .. }
| Node::AssignProperty { line, .. }
| Node::ArrayLiteral { line, .. }
| Node::Group { line, .. }
| Node::Empty { line }
| Node::Legacy { line, .. }
| Node::ObjectLiteral { line, .. }
| Node::Lambda { line, .. }
| Node::Try { line, .. }
| Node::Throw { line, .. }
| Node::Declare { line, .. }
| Node::Super { line, .. }
| Node::For { line, .. }
| Node::Label { line, .. }
| Node::Goto { line, .. }
| Node::HotkeyDefinition { line, .. } => *line,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LegacyForm {
Between,
In,
Contains,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LegacyParameter {
Text(String),
Expression(Node),
}