Skip to main content

jsxbin_parser/
types.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents a complete JSXBIN file
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct JsxBinFile {
6    pub version: u32,
7    pub target_engine: String,
8    pub string_table: Vec<String>,
9    pub tokens: Vec<Token>,
10    pub metadata: FileMetadata,
11}
12
13/// File metadata
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
15pub struct FileMetadata {
16    pub file_size: usize,
17    pub parse_time_ms: u64,
18    pub token_count: usize,
19    pub string_count: usize,
20}
21
22/// Represents an opcode/token in the binary
23#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
24pub enum Token {
25    /// Function declaration
26    Function {
27        name: String,
28        params: Vec<String>,
29        body: Box<Block>,
30    },
31
32    /// Variable declaration with optional initializer
33    VariableDeclaration {
34        name: String,
35        value: Option<Box<Expression>>,
36    },
37
38    /// String literal
39    StringLiteral(String),
40
41    /// Numeric literal
42    NumberLiteral(f64),
43
44    /// Boolean literal
45    BooleanLiteral(bool),
46
47    /// Null value
48    Null,
49
50    /// Undefined value
51    Undefined,
52
53    /// Identifier reference
54    Identifier(String),
55
56    /// Binary operation (e.g., +, -, *, /)
57    BinaryOperation {
58        left: Box<Expression>,
59        operator: String,
60        right: Box<Expression>,
61    },
62
63    /// Unary operation (e.g., !, -, +)
64    UnaryOperation {
65        operator: String,
66        operand: Box<Expression>,
67    },
68
69    /// Function call
70    FunctionCall {
71        callee: Box<Expression>,
72        arguments: Vec<Expression>,
73    },
74
75    /// Member access (obj.prop or obj["prop"])
76    MemberAccess {
77        object: Box<Expression>,
78        property: Box<Expression>,
79        computed: bool,
80    },
81
82    /// If statement
83    IfStatement {
84        condition: Box<Expression>,
85        consequent: Box<Block>,
86        alternate: Option<Box<Block>>,
87    },
88
89    /// While loop
90    WhileLoop {
91        condition: Box<Expression>,
92        body: Box<Block>,
93    },
94
95    /// For loop
96    ForLoop {
97        init: Option<Box<Token>>,
98        condition: Option<Box<Expression>>,
99        update: Option<Box<Expression>>,
100        body: Box<Block>,
101    },
102
103    /// Return statement
104    ReturnStatement(Option<Box<Expression>>),
105
106    /// Break statement
107    BreakStatement,
108
109    /// Continue statement
110    ContinueStatement,
111
112    /// Expression statement
113    ExpressionStatement(Box<Expression>),
114
115    /// Block of statements
116    Block(Block),
117
118    /// Assignment (var = value)
119    Assignment {
120        target: Box<Expression>,
121        value: Box<Expression>,
122    },
123
124    /// Array literal
125    ArrayLiteral(Vec<Expression>),
126
127    /// Object literal
128    ObjectLiteral(Vec<(String, Expression)>),
129
130    /// Ternary conditional (condition ? consequent : alternate)
131    Conditional {
132        condition: Box<Expression>,
133        consequent: Box<Expression>,
134        alternate: Box<Expression>,
135    },
136
137    /// Try-catch block
138    TryCatch {
139        body: Box<Block>,
140        handler: Option<CatchClause>,
141        finalizer: Option<Box<Block>>,
142    },
143
144    /// Throw statement
145    ThrowStatement(Box<Expression>),
146
147    /// Unknown/unhandled token (with raw bytes for debugging)
148    Unknown(Vec<u8>),
149}
150
151/// Represents a block of statements
152#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
153pub struct Block {
154    pub statements: Vec<Token>,
155}
156
157/// Catch clause for try-catch
158#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
159pub struct CatchClause {
160    pub param: Option<String>,
161    pub body: Block,
162}
163
164/// Expression type (wrapper for expression tokens)
165#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
166pub struct Expression {
167    pub token: Box<Token>,
168}
169
170impl From<Token> for Expression {
171    fn from(token: Token) -> Self {
172        Expression {
173            token: Box::new(token),
174        }
175    }
176}
177
178/// Opcode definitions for JSXBIN format
179#[derive(Debug, Clone, Copy, PartialEq, Eq)]
180pub enum Opcode {
181    // Literals and values (0x00-0x0F)
182    EndOfStatement = 0x00,
183    Null = 0x01,
184    Undefined = 0x02,
185    Number = 0x03,
186    String = 0x04,
187    Boolean = 0x05,
188    Object = 0x06,
189    Array = 0x07,
190
191    // Keywords and control flow (0x10-0x1F)
192    Function = 0x10,
193    Return = 0x11,
194    If = 0x12,
195    Else = 0x13,
196    While = 0x14,
197    For = 0x15,
198    Break = 0x16,
199    Continue = 0x17,
200    Try = 0x18,
201    Catch = 0x19,
202    Finally = 0x1A,
203    Throw = 0x1B,
204
205    // Operators (0x20-0x3F)
206    Plus = 0x20,
207    Minus = 0x21,
208    Multiply = 0x22,
209    Divide = 0x23,
210    Modulo = 0x24,
211    Assign = 0x25,
212    Equal = 0x26,
213    NotEqual = 0x27,
214    StrictEqual = 0x28,
215    StrictNotEqual = 0x29,
216    Less = 0x2A,
217    LessEqual = 0x2B,
218    Greater = 0x2C,
219    GreaterEqual = 0x2D,
220    LogicalAnd = 0x2E,
221    LogicalOr = 0x2F,
222    LogicalNot = 0x30,
223    BitwiseAnd = 0x31,
224    BitwiseOr = 0x32,
225    BitwiseXor = 0x33,
226    BitwiseNot = 0x34,
227    LeftShift = 0x35,
228    RightShift = 0x36,
229
230    // Identifiers and variable declarations (0x40-0x4F)
231    Identifier = 0x40,
232    Var = 0x41,
233    Const = 0x42,
234
235    // Other
236    Call = 0x50,
237    MemberAccess = 0x51,
238    ComputedMemberAccess = 0x52,
239    Ternary = 0x53,
240}
241
242impl Opcode {
243    pub fn from_byte(byte: u8) -> Option<Self> {
244        match byte {
245            0x00 => Some(Opcode::EndOfStatement),
246            0x01 => Some(Opcode::Null),
247            0x02 => Some(Opcode::Undefined),
248            0x03 => Some(Opcode::Number),
249            0x04 => Some(Opcode::String),
250            0x05 => Some(Opcode::Boolean),
251            0x06 => Some(Opcode::Object),
252            0x07 => Some(Opcode::Array),
253            0x10 => Some(Opcode::Function),
254            0x11 => Some(Opcode::Return),
255            0x12 => Some(Opcode::If),
256            0x13 => Some(Opcode::Else),
257            0x14 => Some(Opcode::While),
258            0x15 => Some(Opcode::For),
259            0x16 => Some(Opcode::Break),
260            0x17 => Some(Opcode::Continue),
261            0x18 => Some(Opcode::Try),
262            0x19 => Some(Opcode::Catch),
263            0x1A => Some(Opcode::Finally),
264            0x1B => Some(Opcode::Throw),
265            0x20 => Some(Opcode::Plus),
266            0x21 => Some(Opcode::Minus),
267            0x22 => Some(Opcode::Multiply),
268            0x23 => Some(Opcode::Divide),
269            0x24 => Some(Opcode::Modulo),
270            0x25 => Some(Opcode::Assign),
271            0x26 => Some(Opcode::Equal),
272            0x27 => Some(Opcode::NotEqual),
273            0x28 => Some(Opcode::StrictEqual),
274            0x29 => Some(Opcode::StrictNotEqual),
275            0x2A => Some(Opcode::Less),
276            0x2B => Some(Opcode::LessEqual),
277            0x2C => Some(Opcode::Greater),
278            0x2D => Some(Opcode::GreaterEqual),
279            0x2E => Some(Opcode::LogicalAnd),
280            0x2F => Some(Opcode::LogicalOr),
281            0x30 => Some(Opcode::LogicalNot),
282            0x40 => Some(Opcode::Identifier),
283            0x41 => Some(Opcode::Var),
284            0x42 => Some(Opcode::Const),
285            0x50 => Some(Opcode::Call),
286            0x51 => Some(Opcode::MemberAccess),
287            0x52 => Some(Opcode::ComputedMemberAccess),
288            0x53 => Some(Opcode::Ternary),
289            _ => None,
290        }
291    }
292
293    pub fn as_string(&self) -> &'static str {
294        match self {
295            Opcode::EndOfStatement => "EndOfStatement",
296            Opcode::Null => "Null",
297            Opcode::Undefined => "Undefined",
298            Opcode::Number => "Number",
299            Opcode::String => "String",
300            Opcode::Boolean => "Boolean",
301            Opcode::Function => "Function",
302            Opcode::Return => "Return",
303            Opcode::If => "If",
304            Opcode::Else => "Else",
305            Opcode::While => "While",
306            Opcode::For => "For",
307            Opcode::Break => "Break",
308            Opcode::Continue => "Continue",
309            Opcode::Identifier => "Identifier",
310            Opcode::Plus => "Plus",
311            Opcode::Minus => "Minus",
312            Opcode::Multiply => "Multiply",
313            Opcode::Divide => "Divide",
314            _ => "Unknown",
315        }
316    }
317}
318
319#[cfg(test)]
320mod tests {
321    use super::*;
322
323    #[test]
324    fn test_opcode_conversion() {
325        assert_eq!(Opcode::from_byte(0x00), Some(Opcode::EndOfStatement));
326        assert_eq!(Opcode::from_byte(0x03), Some(Opcode::Number));
327        assert_eq!(Opcode::from_byte(0xFF), None);
328    }
329}