jsxbin-parser 1.0.0

Binary parser for Adobe JSX .jsxbin files with comprehensive AST representation
Documentation
use serde::{Deserialize, Serialize};

/// Represents a complete JSXBIN file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsxBinFile {
    pub version: u32,
    pub target_engine: String,
    pub string_table: Vec<String>,
    pub tokens: Vec<Token>,
    pub metadata: FileMetadata,
}

/// File metadata
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FileMetadata {
    pub file_size: usize,
    pub parse_time_ms: u64,
    pub token_count: usize,
    pub string_count: usize,
}

/// Represents an opcode/token in the binary
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Token {
    /// Function declaration
    Function {
        name: String,
        params: Vec<String>,
        body: Box<Block>,
    },

    /// Variable declaration with optional initializer
    VariableDeclaration {
        name: String,
        value: Option<Box<Expression>>,
    },

    /// String literal
    StringLiteral(String),

    /// Numeric literal
    NumberLiteral(f64),

    /// Boolean literal
    BooleanLiteral(bool),

    /// Null value
    Null,

    /// Undefined value
    Undefined,

    /// Identifier reference
    Identifier(String),

    /// Binary operation (e.g., +, -, *, /)
    BinaryOperation {
        left: Box<Expression>,
        operator: String,
        right: Box<Expression>,
    },

    /// Unary operation (e.g., !, -, +)
    UnaryOperation {
        operator: String,
        operand: Box<Expression>,
    },

    /// Function call
    FunctionCall {
        callee: Box<Expression>,
        arguments: Vec<Expression>,
    },

    /// Member access (obj.prop or obj["prop"])
    MemberAccess {
        object: Box<Expression>,
        property: Box<Expression>,
        computed: bool,
    },

    /// If statement
    IfStatement {
        condition: Box<Expression>,
        consequent: Box<Block>,
        alternate: Option<Box<Block>>,
    },

    /// While loop
    WhileLoop {
        condition: Box<Expression>,
        body: Box<Block>,
    },

    /// For loop
    ForLoop {
        init: Option<Box<Token>>,
        condition: Option<Box<Expression>>,
        update: Option<Box<Expression>>,
        body: Box<Block>,
    },

    /// Return statement
    ReturnStatement(Option<Box<Expression>>),

    /// Break statement
    BreakStatement,

    /// Continue statement
    ContinueStatement,

    /// Expression statement
    ExpressionStatement(Box<Expression>),

    /// Block of statements
    Block(Block),

    /// Assignment (var = value)
    Assignment {
        target: Box<Expression>,
        value: Box<Expression>,
    },

    /// Array literal
    ArrayLiteral(Vec<Expression>),

    /// Object literal
    ObjectLiteral(Vec<(String, Expression)>),

    /// Ternary conditional (condition ? consequent : alternate)
    Conditional {
        condition: Box<Expression>,
        consequent: Box<Expression>,
        alternate: Box<Expression>,
    },

    /// Try-catch block
    TryCatch {
        body: Box<Block>,
        handler: Option<CatchClause>,
        finalizer: Option<Box<Block>>,
    },

    /// Throw statement
    ThrowStatement(Box<Expression>),

    /// Unknown/unhandled token (with raw bytes for debugging)
    Unknown(Vec<u8>),
}

/// Represents a block of statements
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Block {
    pub statements: Vec<Token>,
}

/// Catch clause for try-catch
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CatchClause {
    pub param: Option<String>,
    pub body: Block,
}

/// Expression type (wrapper for expression tokens)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Expression {
    pub token: Box<Token>,
}

impl From<Token> for Expression {
    fn from(token: Token) -> Self {
        Expression {
            token: Box::new(token),
        }
    }
}

/// Opcode definitions for JSXBIN format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Opcode {
    // Literals and values (0x00-0x0F)
    EndOfStatement = 0x00,
    Null = 0x01,
    Undefined = 0x02,
    Number = 0x03,
    String = 0x04,
    Boolean = 0x05,
    Object = 0x06,
    Array = 0x07,

    // Keywords and control flow (0x10-0x1F)
    Function = 0x10,
    Return = 0x11,
    If = 0x12,
    Else = 0x13,
    While = 0x14,
    For = 0x15,
    Break = 0x16,
    Continue = 0x17,
    Try = 0x18,
    Catch = 0x19,
    Finally = 0x1A,
    Throw = 0x1B,

    // Operators (0x20-0x3F)
    Plus = 0x20,
    Minus = 0x21,
    Multiply = 0x22,
    Divide = 0x23,
    Modulo = 0x24,
    Assign = 0x25,
    Equal = 0x26,
    NotEqual = 0x27,
    StrictEqual = 0x28,
    StrictNotEqual = 0x29,
    Less = 0x2A,
    LessEqual = 0x2B,
    Greater = 0x2C,
    GreaterEqual = 0x2D,
    LogicalAnd = 0x2E,
    LogicalOr = 0x2F,
    LogicalNot = 0x30,
    BitwiseAnd = 0x31,
    BitwiseOr = 0x32,
    BitwiseXor = 0x33,
    BitwiseNot = 0x34,
    LeftShift = 0x35,
    RightShift = 0x36,

    // Identifiers and variable declarations (0x40-0x4F)
    Identifier = 0x40,
    Var = 0x41,
    Const = 0x42,

    // Other
    Call = 0x50,
    MemberAccess = 0x51,
    ComputedMemberAccess = 0x52,
    Ternary = 0x53,
}

impl Opcode {
    pub fn from_byte(byte: u8) -> Option<Self> {
        match byte {
            0x00 => Some(Opcode::EndOfStatement),
            0x01 => Some(Opcode::Null),
            0x02 => Some(Opcode::Undefined),
            0x03 => Some(Opcode::Number),
            0x04 => Some(Opcode::String),
            0x05 => Some(Opcode::Boolean),
            0x06 => Some(Opcode::Object),
            0x07 => Some(Opcode::Array),
            0x10 => Some(Opcode::Function),
            0x11 => Some(Opcode::Return),
            0x12 => Some(Opcode::If),
            0x13 => Some(Opcode::Else),
            0x14 => Some(Opcode::While),
            0x15 => Some(Opcode::For),
            0x16 => Some(Opcode::Break),
            0x17 => Some(Opcode::Continue),
            0x18 => Some(Opcode::Try),
            0x19 => Some(Opcode::Catch),
            0x1A => Some(Opcode::Finally),
            0x1B => Some(Opcode::Throw),
            0x20 => Some(Opcode::Plus),
            0x21 => Some(Opcode::Minus),
            0x22 => Some(Opcode::Multiply),
            0x23 => Some(Opcode::Divide),
            0x24 => Some(Opcode::Modulo),
            0x25 => Some(Opcode::Assign),
            0x26 => Some(Opcode::Equal),
            0x27 => Some(Opcode::NotEqual),
            0x28 => Some(Opcode::StrictEqual),
            0x29 => Some(Opcode::StrictNotEqual),
            0x2A => Some(Opcode::Less),
            0x2B => Some(Opcode::LessEqual),
            0x2C => Some(Opcode::Greater),
            0x2D => Some(Opcode::GreaterEqual),
            0x2E => Some(Opcode::LogicalAnd),
            0x2F => Some(Opcode::LogicalOr),
            0x30 => Some(Opcode::LogicalNot),
            0x40 => Some(Opcode::Identifier),
            0x41 => Some(Opcode::Var),
            0x42 => Some(Opcode::Const),
            0x50 => Some(Opcode::Call),
            0x51 => Some(Opcode::MemberAccess),
            0x52 => Some(Opcode::ComputedMemberAccess),
            0x53 => Some(Opcode::Ternary),
            _ => None,
        }
    }

    pub fn as_string(&self) -> &'static str {
        match self {
            Opcode::EndOfStatement => "EndOfStatement",
            Opcode::Null => "Null",
            Opcode::Undefined => "Undefined",
            Opcode::Number => "Number",
            Opcode::String => "String",
            Opcode::Boolean => "Boolean",
            Opcode::Function => "Function",
            Opcode::Return => "Return",
            Opcode::If => "If",
            Opcode::Else => "Else",
            Opcode::While => "While",
            Opcode::For => "For",
            Opcode::Break => "Break",
            Opcode::Continue => "Continue",
            Opcode::Identifier => "Identifier",
            Opcode::Plus => "Plus",
            Opcode::Minus => "Minus",
            Opcode::Multiply => "Multiply",
            Opcode::Divide => "Divide",
            _ => "Unknown",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_opcode_conversion() {
        assert_eq!(Opcode::from_byte(0x00), Some(Opcode::EndOfStatement));
        assert_eq!(Opcode::from_byte(0x03), Some(Opcode::Number));
        assert_eq!(Opcode::from_byte(0xFF), None);
    }
}