SPB Git

Repository Public

Parser.swift 22.2 KB 473 lines swift

1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXScript/Parser.swift3// Description: ORIX Script recursive-descent parser with error recovery: tokens to syntax tree plus diagnostics.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89public struct Parser {10    private var tokens: [Token]11    private var pos = 012    public private(set) var diagnostics: [Diagnostic] = []13    private var depth = 014    private static let maxNesting = 2015    private var exprDepth = 01617    public init(tokens: [Token]) { self.tokens = tokens }1819    /// Parses a whole script file. With `console` set, statements may appear outside an automation.20    public static func parse(_ source: String, console: Bool = false) -> (file: ScriptFile, diagnostics: [Diagnostic]) {21        var lexer = Lexer(source)22        let tokens = lexer.tokenize()23        var parser = Parser(tokens: tokens)24        let file = parser.parseFile(console: console)25        return (file, lexer.diagnostics + parser.diagnostics)26    }2728    // MARK: Token helpers2930    private var current: Token { tokens[min(pos, tokens.count - 1)] }31    private func peekToken(_ k: Int = 1) -> Token { tokens[min(pos + k, tokens.count - 1)] }32    private mutating func next() -> Token { let t = current; if pos < tokens.count - 1 { pos += 1 }; return t }33    private var atEnd: Bool { current.kind == .eof }3435    private func isSymbol(_ s: String) -> Bool { current.kind == .symbol(s) }36    private func isWord(_ w: String) -> Bool { current.kind == .identifier(w) }3738    private mutating func accept(symbol s: String) -> Bool { if isSymbol(s) { _ = next(); return true }; return false }39    private mutating func accept(word w: String) -> Bool { if isWord(w) { _ = next(); return true }; return false }4041    private mutating func skipNewlines() { while current.kind == .newline { _ = next() } }4243    private mutating func error(_ message: String, code: String = "E-SYNTAX", at token: Token? = nil) {44        let t = token ?? current45        // One diagnostic per position keeps cascades short.46        if diagnostics.last?.location != t.location { diagnostics.append(Diagnostic(code: code, message, at: t.location, length: max(1, t.length))) }47    }4849    private func describe(_ t: Token) -> String {50        switch t.kind {51        case let .identifier(n): "'\(n)'"52        case let .number(x): "'\(formatNumber(x))'"53        case let .literal(v): "'\(v)'"54        case .string: "a string"55        case let .symbol(s): "'\(s)'"56        case .newline: "end of line"57        case .eof: "end of script"58        }59    }6061    @discardableResult private mutating func expect(symbol s: String, _ context: String) -> Bool {62        if accept(symbol: s) { return true }63        error("Expected '\(s)' \(context), found \(describe(current)).")64        return false65    }6667    /// Skips to the end of the current statement after an error.68    private mutating func synchronize() {69        var braces = 070        while !atEnd {71            if current.kind == .newline, braces == 0 { return }72            if isSymbol("{") { braces += 1 }73            if isSymbol("}") { if braces == 0 { return }; braces -= 1 }74            _ = next()75        }76    }7778    private mutating func endStatement() {79        if current.kind == .newline || atEnd || isSymbol("}") { return }80        error("Expected end of line, found \(describe(current)).")81        synchronize()82    }8384    // MARK: File8586    public mutating func parseFile(console: Bool) -> ScriptFile {87        var file = ScriptFile()88        skipNewlines()89        while !atEnd {90            let start = pos91            if isWord("automation") { if let a = parseAutomation() { file.automations.append(a) } }92            else if isWord("fn") { if let f = parseFn() { file.functions.append(f) } }93            else if isWord("let"), !console { if let s = parseStatement() { file.constants.append(s) } }94            else if console { if let s = parseStatement(console: true) { file.loose.append(s) } }95            else {96                error("Expected 'automation', 'fn' or 'let' at the top level, found \(describe(current)).")97                synchronize()98            }99            if isSymbol("}") { error("Unexpected '}'."); _ = next() }100            if pos == start { _ = next() }101            skipNewlines()102        }103        return file104    }105106    private mutating func parseAutomation() -> AutomationDecl? {107        let start = next()108        guard case let .string(name) = current.kind else { error("Expected the automation name in quotes, e.g. automation \"Fade\" { … }."); synchronize(); return nil }109        _ = next()110        var quantize = StartQuantize.none111        if accept(word: "quantize") {112            if case let .identifier(q) = current.kind, let v = StartQuantize(rawValue: q) { quantize = v; _ = next() }113            else { error("Expected 'bar', 'beat' or 'none' after 'quantize'.") }114        }115        guard let body = parseBlock("after the automation name") else { return nil }116        return AutomationDecl(name: name, quantize: quantize, body: body, location: start.location)117    }118119    private mutating func parseFn() -> FnDecl? {120        let start = next()121        guard case let .identifier(name) = current.kind else { error("Expected a function name after 'fn'."); synchronize(); return nil }122        _ = next()123        guard expect(symbol: "(", "after the function name") else { synchronize(); return nil }124        var params: [FnParam] = []125        skipNewlines()126        while !isSymbol(")"), !atEnd {127            guard case let .identifier(pname) = current.kind else { error("Expected a parameter name."); break }128            if ScriptSchema.keywords.contains(pname) { error("'\(pname)' is a reserved word and cannot name a parameter.") }129            let ploc = next().location130            guard expect(symbol: ":", "after the parameter name (every parameter needs a type, e.g. d: deck)") else { break }131            guard let type = parseType() else { break }132            params.append(FnParam(name: pname, type: type, location: ploc))133            skipNewlines()134            if !accept(symbol: ",") { break }135            skipNewlines()136        }137        guard expect(symbol: ")", "to close the parameter list") else { synchronize(); return nil }138        var ret: ScriptType?139        if accept(symbol: "->") { ret = parseType() }140        guard let body = parseBlock("to start the function body") else { return nil }141        return FnDecl(name: name, params: params, returnType: ret, body: body, location: start.location)142    }143144    private mutating func parseType() -> ScriptType? {145        if accept(symbol: "[") {146            guard let inner = parseType() else { return nil }147            expect(symbol: "]", "to close the array type")148            return .array(inner)149        }150        if case let .identifier(n) = current.kind, let t = ScriptType.named(n) { _ = next(); return t }151        error("Expected a type: number, duration, gain, frequency, bool, string or deck.")152        return nil153    }154155    private mutating func parseBlock(_ context: String) -> [Stmt]? {156        skipNewlinesBeforeBrace()157        guard expect(symbol: "{", context) else { synchronize(); return nil }158        depth += 1159        defer { depth -= 1 }160        if depth > Self.maxNesting { error("Blocks are nested more than \(Self.maxNesting) levels deep.", code: "E-LIMIT"); synchronize(); return nil }161        var body: [Stmt] = []162        skipNewlines()163        while !isSymbol("}"), !atEnd {164            let start = pos165            if let s = parseStatement() { body.append(s) }166            if pos == start { _ = next() }167            skipNewlines()168        }169        expect(symbol: "}", "to close the block")170        return body171    }172173    /// Allows `{` on the line after the header.174    private mutating func skipNewlinesBeforeBrace() {175        var k = 0176        while peekToken(k).kind == .newline { k += 1 }177        if k > 0, peekToken(k).kind == .symbol("{") { pos += k }178    }179180    // MARK: Statements181182    private mutating func parseStatement(console: Bool = false) -> Stmt? {183        let t = current184        let loc = t.location185        guard case let .identifier(word) = t.kind else {186            if console, let e = parseExpression() { endStatement(); return Stmt(.call(PathExpr(segments: ["print"], location: loc, length: 1), [e]), loc) }187            error("Expected a statement, found \(describe(t))."); synchronize(); return nil188        }189        switch word {190        case "let", "var":191            _ = next()192            guard case let .identifier(name) = current.kind else { error("Expected a name after '\(word)'."); synchronize(); return nil }193            let nameToken = next()194            if ScriptSchema.keywords.contains(name) { error("'\(name)' is a reserved word and cannot name a variable.", at: nameToken) }195            var type: ScriptType?196            if accept(symbol: ":") { type = parseType() }197            guard expect(symbol: "=", "after the name (a declaration needs an initial value)") else { synchronize(); return nil }198            guard let value = parseExpression() else { synchronize(); return nil }199            endStatement()200            return Stmt(.declare(name: name, type: type, value: value, mutable: word == "var"), loc)201        case "wait":202            _ = next()203            if accept(word: "nextBeat") { endStatement(); return Stmt(.wait(.nextBeat), loc) }204            if accept(word: "nextBar") { endStatement(); return Stmt(.wait(.nextBar), loc) }205            if accept(word: "until") {206                guard let a = parseAnchor() else { synchronize(); return nil }207                endStatement(); return Stmt(.wait(.until(a)), loc)208            }209            guard let e = parseExpression() else { synchronize(); return nil }210            endStatement()211            return Stmt(.wait(.duration(e)), loc)212        case "at":213            _ = next()214            guard let a = parseAnchor() else { synchronize(); return nil }215            guard let body = parseBlock("after the 'at' position") else { return nil }216            return Stmt(.at(a, body), loc)217        case "over":218            _ = next()219            guard let d = parseExpression() else { synchronize(); return nil }220            var curve: String?221            if accept(word: "curve") {222                if case let .identifier(c) = current.kind { curve = c; _ = next() } else { error("Expected a curve name: \(ScriptSchema.rampCurves.joined(separator: ", ")).") }223            }224            guard let body = parseBlock("after the 'over' duration") else { return nil }225            return Stmt(.over(d, curve: curve, body), loc)226        case "every":227            _ = next()228            guard let period = parseExpression() else { synchronize(); return nil }229            guard accept(word: "for") else { error("Expected 'for' and a total length, e.g. every 1beat for 8beats { … }."); synchronize(); return nil }230            guard let total = parseExpression() else { synchronize(); return nil }231            guard let body = parseBlock("after the 'every' header") else { return nil }232            return Stmt(.every(period: period, total: total, body), loc)233        case "repeat":234            _ = next()235            guard let n = parseExpression() else { synchronize(); return nil }236            guard let body = parseBlock("after the repeat count") else { return nil }237            return Stmt(.repeatLoop(n, body), loc)238        case "for":239            _ = next()240            guard case let .identifier(name) = current.kind else { error("Expected a loop variable name after 'for'."); synchronize(); return nil }241            if ScriptSchema.keywords.contains(name) { error("'\(name)' is a reserved word and cannot name a variable.") }242            _ = next()243            guard accept(word: "in") else { error("Expected 'in' after the loop variable."); synchronize(); return nil }244            guard let first = parseExpression() else { synchronize(); return nil }245            if isSymbol("...") || isSymbol("..<") {246                let inclusive = next().kind == .symbol("...")247                guard let last = parseExpression() else { synchronize(); return nil }248                guard let body = parseBlock("after the range") else { return nil }249                return Stmt(.forRange(name: name, from: first, to: last, inclusive: inclusive, body), loc)250            }251            guard let body = parseBlock("after the array") else { return nil }252            return Stmt(.forIn(name: name, first, body), loc)253        case "while":254            _ = next()255            guard let cond = parseExpression() else { synchronize(); return nil }256            guard accept(word: "limit") else {257                error("A 'while' loop needs an iteration bound: while condition limit 64 { … }.", code: "E-UNBOUNDED"); synchronize(); return nil258            }259            guard let limit = parseExpression() else { synchronize(); return nil }260            guard let body = parseBlock("after the loop limit") else { return nil }261            return Stmt(.whileLoop(cond, limit: limit, body), loc)262        case "if":263            return parseIf()264        case "on":265            _ = next()266            guard let path = parsePath() else { synchronize(); return nil }267            var arg: Expr?268            if accept(symbol: "(") { arg = parseExpression(); expect(symbol: ")", "after the event argument") }269            let once = accept(word: "once")270            guard let body = parseBlock("after the event") else { return nil }271            return Stmt(.on(EventSpec(path: path, argument: arg), once: once, body), loc)272        case "return":273            _ = next()274            if current.kind == .newline || isSymbol("}") || atEnd { return Stmt(.returnValue(nil), loc) }275            guard let e = parseExpression() else { synchronize(); return nil }276            endStatement()277            return Stmt(.returnValue(e), loc)278        case "stop": _ = next(); endStatement(); return Stmt(.stop, loc)279        case "break": _ = next(); endStatement(); return Stmt(.breakLoop, loc)280        case "continue": _ = next(); endStatement(); return Stmt(.continueLoop, loc)281        case "automation", "fn":282            error("'\(word)' can only appear at the top level of a script."); synchronize(); return nil283        case "else":284            error("'else' without a matching 'if'."); synchronize(); return nil285        default:286            break287        }288        // Console: a bare expression prints its value.289        if console {290            let save = pos; let saveDiagnostics = diagnostics291            if let path = parsePath(), isSymbol("=") || isSymbol("->") || (isSymbol("(") && (path.segments.count > 1 || path.segments[0] == "print")) { pos = save; diagnostics = saveDiagnostics }292            else {293                pos = save; diagnostics = saveDiagnostics294                guard let e = parseExpression() else { synchronize(); return nil }295                endStatement()296                return Stmt(.call(PathExpr(segments: ["print"], location: loc, length: 1), [e]), loc)297            }298        }299        guard let path = parsePath() else { synchronize(); return nil }300        if accept(symbol: "=") {301            guard let e = parseExpression() else { synchronize(); return nil }302            endStatement()303            return Stmt(.assign(path, e), loc)304        }305        if accept(symbol: "->") {306            guard let e = parseExpression() else { synchronize(); return nil }307            endStatement()308            return Stmt(.ramp(path, e), loc)309        }310        if isSymbol("(") {311            guard let args = parseArguments() else { synchronize(); return nil }312            if console, !(current.kind == .newline || atEnd) {313                // `f(x) + 1` typed in the console: treat the whole line as an expression.314                pos = tokens.firstIndex { $0.location == loc } ?? pos315                guard let e = parseExpression() else { synchronize(); return nil }316                endStatement()317                return Stmt(.call(PathExpr(segments: ["print"], location: loc, length: 1), [e]), loc)318            }319            endStatement()320            return Stmt(.call(path, args), loc)321        }322        error("Expected '=', '->' or '(' after '\(path.text)', found \(describe(current)).")323        synchronize()324        return nil325    }326327    private mutating func parseIf() -> Stmt? {328        let loc = next().location329        guard let cond = parseExpression() else { synchronize(); return nil }330        guard let thenBody = parseBlock("after the condition") else { return nil }331        var k = 0332        while peekToken(k).kind == .newline { k += 1 }333        if peekToken(k).kind == .identifier("else") {334            pos += k; _ = next()335            if isWord("if") {336                guard let nested = parseIf() else { return nil }337                return Stmt(.ifElse(cond, thenBody, [nested]), loc)338            }339            guard let elseBody = parseBlock("after 'else'") else { return nil }340            return Stmt(.ifElse(cond, thenBody, elseBody), loc)341        }342        return Stmt(.ifElse(cond, thenBody, nil), loc)343    }344345    private mutating func parseAnchor() -> Anchor? {346        if accept(word: "nextBeat") { return .nextBeat }347        if accept(word: "nextBar") { return .nextBar }348        if isWord("bar") { _ = next(); return parseExpression().map(Anchor.bar) }349        if isWord("beat") { _ = next(); return parseExpression().map(Anchor.beat) }350        return parseExpression().map(Anchor.offset)351    }352353    private mutating func parsePath() -> PathExpr? {354        guard case let .identifier(first) = current.kind else { error("Expected a name, found \(describe(current))."); return nil }355        let start = next()356        var segments = [first]357        var end = start.location.column + start.length358        while isSymbol("."), case let .identifier(seg) = peekToken().kind {359            _ = next(); let t = next()360            segments.append(seg); end = t.location.column + t.length361        }362        if isSymbol("."), case .number = peekToken().kind { error("A path segment cannot start with a digit.", at: peekToken()) }363        return PathExpr(segments: segments, location: start.location, length: max(1, end - start.location.column))364    }365366    private mutating func parseArguments() -> [Expr]? {367        guard expect(symbol: "(", "to start the argument list") else { return nil }368        var args: [Expr] = []369        skipNewlines()370        while !isSymbol(")"), !atEnd {371            guard let e = parseExpression() else { return nil }372            args.append(e)373            skipNewlines()374            if !accept(symbol: ",") { break }375            skipNewlines()376        }377        guard expect(symbol: ")", "to close the argument list") else { return nil }378        return args379    }380381    // MARK: Expressions382383    private mutating func parseExpression() -> Expr? {384        exprDepth += 1385        defer { exprDepth -= 1 }386        if exprDepth > Self.maxNesting { error("Expression nested more than \(Self.maxNesting) levels deep.", code: "E-LIMIT"); return nil }387        return parseBinary(0)388    }389390    private static let precedence: [[String]] = [["||", "or"], ["&&", "and"], ["==", "!="], ["<", "<=", ">", ">="], ["+", "-"], ["*", "/", "%"]]391392    private func binaryOperator(level: Int) -> String? {393        switch current.kind {394        case let .symbol(s) where Self.precedence[level].contains(s): return s395        case let .identifier(w) where Self.precedence[level].contains(w): return w == "or" ? "||" : "&&"396        default: return nil397        }398    }399400    private mutating func parseBinary(_ level: Int) -> Expr? {401        if level >= Self.precedence.count { return parseUnary() }402        guard var lhs = parseBinary(level + 1) else { return nil }403        while let op = binaryOperator(level: level) {404            let loc = next().location405            guard let rhs = parseBinary(level + 1) else { return nil }406            lhs = .binary(op, lhs, rhs, loc)407        }408        return lhs409    }410411    private mutating func parseUnary() -> Expr? {412        if isSymbol("-") || isSymbol("!") || isWord("not") || isSymbol("+") {413            let t = next()414            exprDepth += 1415            defer { exprDepth -= 1 }416            if exprDepth > Self.maxNesting { error("Expression nested more than \(Self.maxNesting) levels deep.", code: "E-LIMIT"); return nil }417            guard let operand = parseUnary() else { return nil }418            let op: String = switch t.kind { case .symbol("-"): "-"; case .symbol("+"): "+"; default: "!" }419            return .unary(op, operand, t.location)420        }421        return parsePostfix()422    }423424    private mutating func parsePostfix() -> Expr? {425        guard var e = parsePrimary() else { return nil }426        while isSymbol("[") {427            let loc = next().location428            guard let idx = parseExpression() else { return nil }429            guard expect(symbol: "]", "to close the index") else { return nil }430            e = .index(e, idx, loc)431        }432        return e433    }434435    private mutating func parsePrimary() -> Expr? {436        let t = current437        switch t.kind {438        case let .number(x): _ = next(); return .literal(.number(x), t.location)439        case let .literal(v): _ = next(); return .literal(v, t.location)440        case let .string(s): _ = next(); return .literal(.string(s), t.location)441        case .identifier("true"): _ = next(); return .literal(.bool(true), t.location)442        case .identifier("false"): _ = next(); return .literal(.bool(false), t.location)443        case .identifier:444            guard let path = parsePath() else { return nil }445            if isSymbol("(") {446                guard let args = parseArguments() else { return nil }447                return .call(path, args)448            }449            return .path(path)450        case .symbol("("):451            _ = next(); skipNewlines()452            guard let e = parseExpression() else { return nil }453            skipNewlines()454            guard expect(symbol: ")", "to close the parenthesis") else { return nil }455            return e456        case .symbol("["):457            _ = next(); skipNewlines()458            var items: [Expr] = []459            while !isSymbol("]"), !atEnd {460                guard let e = parseExpression() else { return nil }461                items.append(e); skipNewlines()462                if !accept(symbol: ",") { break }463                skipNewlines()464            }465            guard expect(symbol: "]", "to close the array") else { return nil }466            return .array(items, t.location)467        default:468            error("Expected a value, found \(describe(t)).")469            return nil470        }471    }472}473
Files main
Menu

Theme