SPB Git

Repository Public

ast.rs 10.1 KB 502 lines rust

1//! ORIX Sonata2//! File: crates/sonata-parser/src/ast.rs3//! Description: The Sonata abstract syntax tree. Every node carries its source span.4//!5//! Author: Simon-Pierre Boucher6//! Contact: contact@spboucher.ai78use serde::Serialize;9use sonata_ir::Span;10use sonata_lexer::{NoteLit, NumLit, PitchClassLit, UnitSuffix};1112#[derive(Clone, Debug, PartialEq, Serialize)]13pub struct Ident {14    pub name: String,15    pub span: Span,16}1718#[derive(Clone, Debug, PartialEq, Serialize)]19pub struct StrLit {20    pub value: String,21    pub span: Span,22}2324#[derive(Clone, Debug, PartialEq, Serialize)]25pub struct File {26    /// `sonata 0.1` header, when present.27    pub version: Option<Version>,28    pub items: Vec<Item>,29    pub span: Span,30}3132#[derive(Clone, Debug, PartialEq, Serialize)]33pub struct Version {34    pub major: u32,35    pub minor: u32,36    pub span: Span,37}3839#[derive(Clone, Debug, PartialEq, Serialize)]40#[serde(tag = "item", rename_all = "snake_case")]41pub enum Item {42    Import(Import),43    Let(LetDecl),44    Fn(FnDecl),45    Pattern(PatternDecl),46    Instrument(InstrumentDef),47    Composition(Composition),48}4950impl Item {51    pub fn span(&self) -> Span {52        match self {53            Item::Import(x) => x.span,54            Item::Let(x) => x.span,55            Item::Fn(x) => x.span,56            Item::Pattern(x) => x.span,57            Item::Instrument(x) => x.span,58            Item::Composition(x) => x.span,59        }60    }61}6263#[derive(Clone, Debug, PartialEq, Serialize)]64pub struct Import {65    pub path: Vec<Ident>,66    pub span: Span,67}6869impl Import {70    pub fn dotted(&self) -> String {71        self.path.iter().map(|i| i.name.as_str()).collect::<Vec<_>>().join(".")72    }73}7475#[derive(Clone, Debug, PartialEq, Serialize)]76pub struct TypeExpr {77    pub name: Ident,78    pub args: Vec<TypeExpr>,79    pub span: Span,80}8182#[derive(Clone, Debug, PartialEq, Serialize)]83pub struct LetDecl {84    pub is_const: bool,85    pub name: Ident,86    pub ty: Option<TypeExpr>,87    pub value: Expr,88    pub span: Span,89}9091#[derive(Clone, Debug, PartialEq, Serialize)]92pub struct Param {93    pub name: Ident,94    pub ty: Option<TypeExpr>,95    pub default: Option<Expr>,96    pub span: Span,97}9899#[derive(Clone, Debug, PartialEq, Serialize)]100pub struct FnDecl {101    pub name: Ident,102    pub params: Vec<Param>,103    pub ret: Option<TypeExpr>,104    pub body: Block,105    pub span: Span,106}107108#[derive(Clone, Debug, PartialEq, Serialize)]109pub struct PatternDecl {110    pub name: Ident,111    pub params: Vec<Param>,112    pub body: Block,113    pub span: Span,114}115116#[derive(Clone, Debug, PartialEq, Serialize)]117pub struct InstrumentDef {118    pub name: Ident,119    pub base: Option<Ident>,120    pub members: Vec<InstrMember>,121    pub span: Span,122}123124#[derive(Clone, Debug, PartialEq, Serialize)]125pub struct InstrMember {126    pub kind: InstrMemberKind,127    pub span: Span,128}129130#[derive(Clone, Debug, PartialEq, Serialize)]131#[serde(tag = "member", rename_all = "snake_case")]132pub enum InstrMemberKind {133    Model {134        name: Ident,135    },136    Preset {137        name: Ident,138    },139    /// `damping 0.015` or `envelope.attack 5ms`. `name` holds the dotted path.140    Param {141        name: Ident,142        value: Expr,143    },144    /// `excitation pluck(position: 0.35)`.145    Slot {146        slot: Ident,147        kind: Ident,148        args: Vec<Arg>,149    },150    Graph(GraphBlock),151}152153#[derive(Clone, Debug, PartialEq, Serialize)]154pub struct GraphBlock {155    pub nodes: Vec<GraphNode>,156    pub out: Option<Expr>,157    pub span: Span,158}159160#[derive(Clone, Debug, PartialEq, Serialize)]161pub struct GraphNode {162    pub name: Ident,163    pub value: Expr,164    pub span: Span,165}166167#[derive(Clone, Debug, PartialEq, Serialize)]168pub struct Composition {169    pub title: StrLit,170    pub body: Block,171    pub span: Span,172}173174#[derive(Clone, Debug, PartialEq, Serialize)]175pub struct Block {176    pub stmts: Vec<Stmt>,177    /// Span including the braces.178    pub span: Span,179}180181#[derive(Clone, Debug, PartialEq, Serialize)]182pub struct Stmt {183    pub kind: StmtKind,184    pub span: Span,185}186187#[derive(Clone, Debug, PartialEq, Serialize)]188#[serde(tag = "stmt", rename_all = "snake_case")]189pub enum StmtKind {190    Let(LetDecl),191    Assign {192        target: Ident,193        value: Expr,194    },195    Tempo {196        value: Expr,197        ramp: Option<Expr>,198    },199    Key {200        tonic: PitchClassLit,201        tonic_span: Span,202        mode: Ident,203    },204    Signature {205        numerator: u32,206        denominator: u32,207    },208    Seed {209        value: Expr,210    },211    Tuning {212        value: Expr,213    },214    Swing {215        ratio: Expr,216        unit: Option<Expr>,217        body: Option<Block>,218    },219    /// `instrument name: Type { … }` inside a composition.220    Track {221        name: Ident,222        ty: Ident,223        body: Block,224    },225    AudioTrack {226        name: Ident,227        body: Block,228    },229    Clip {230        asset: StrLit,231        options: Vec<(Ident, Expr)>,232    },233    Bus {234        name: Ident,235        body: Block,236    },237    Master {238        body: Block,239    },240    Marker {241        name: Expr,242        at: Option<Expr>,243    },244    Play {245        is_chord: bool,246        what: Expr,247        modifiers: Vec<Modifier>,248    },249    Rest {250        duration: Expr,251    },252    Sequence {253        body: Block,254    },255    Voice {256        body: Block,257    },258    Parallel {259        body: Block,260    },261    At {262        position: Expr,263        body: Block,264    },265    Repeat {266        count: Expr,267        body: Block,268    },269    For {270        var: Ident,271        iter: Expr,272        body: Block,273    },274    If {275        cond: Expr,276        then: Block,277        otherwise: Option<Block>,278    },279    Tuplet {280        n: Expr,281        m: Expr,282        body: Block,283    },284    Transpose {285        interval: Expr,286        body: Option<Block>,287    },288    Return {289        value: Option<Expr>,290    },291    Effects {292        items: Vec<EffectItem>,293    },294    Automate {295        target: AutoPath,296        points: Vec<AutoPoint>,297    },298    Send {299        bus: Ident,300        gain: Option<Expr>,301        pre_fader: bool,302    },303    Default {304        what: Ident,305        value: Expr,306    },307    /// `gain -8dB`, `pan 0.2`, `mute`, `articulation legato`, `sample_rate 48000Hz`, `output hall` …308    Setting {309        name: Ident,310        value: Option<Expr>,311    },312    /// A pattern call (or any expression evaluated for its effect).313    Expr {314        expr: Expr,315    },316}317318#[derive(Clone, Debug, PartialEq, Serialize)]319pub struct Modifier {320    pub kind: ModifierKind,321    pub span: Span,322}323324#[derive(Clone, Debug, PartialEq, Serialize)]325#[serde(tag = "modifier", rename_all = "snake_case")]326pub enum ModifierKind {327    Duration {328        value: Expr,329    },330    Velocity {331        value: Expr,332    },333    Articulation {334        name: Ident,335    },336    Tie,337    /// `bend { 0ms: 0st, 200ms: 2st }`.338    Bend {339        points: Vec<AutoPoint>,340    },341}342343#[derive(Clone, Debug, PartialEq, Serialize)]344pub struct EffectItem {345    pub bypass: bool,346    pub kind: Ident,347    pub preset: Option<Ident>,348    pub args: Vec<Arg>,349    pub span: Span,350}351352/// `gain`, `pan`, `instrument.brightness`, `reverb.size`, `delay[1].time`.353#[derive(Clone, Debug, PartialEq, Serialize)]354pub struct AutoPath {355    pub head: Ident,356    pub index: Option<u32>,357    pub param: Option<Ident>,358    pub span: Span,359}360361#[derive(Clone, Debug, PartialEq, Serialize)]362pub struct AutoPoint {363    pub at: Expr,364    pub value: Expr,365    pub curve: Option<Ident>,366    pub span: Span,367}368369#[derive(Clone, Debug, PartialEq, Serialize)]370pub struct Arg {371    pub name: Option<Ident>,372    pub value: Expr,373    pub span: Span,374}375376#[derive(Clone, Debug, PartialEq, Serialize)]377pub struct Expr {378    pub kind: ExprKind,379    pub span: Span,380}381382#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]383#[serde(rename_all = "snake_case")]384pub enum UnOp {385    Neg,386    Not,387}388389#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]390#[serde(rename_all = "snake_case")]391pub enum BinOp {392    Or,393    And,394    Eq,395    Ne,396    Lt,397    Le,398    Gt,399    Ge,400    Add,401    Sub,402    Mul,403    Div,404    Rem,405}406407impl BinOp {408    pub fn symbol(self) -> &'static str {409        match self {410            BinOp::Or => "or",411            BinOp::And => "and",412            BinOp::Eq => "==",413            BinOp::Ne => "!=",414            BinOp::Lt => "<",415            BinOp::Le => "<=",416            BinOp::Gt => ">",417            BinOp::Ge => ">=",418            BinOp::Add => "+",419            BinOp::Sub => "-",420            BinOp::Mul => "*",421            BinOp::Div => "/",422            BinOp::Rem => "%",423        }424    }425    /// Binding power; higher binds tighter. Ranges sit between comparisons (3) and sums (5).426    pub fn precedence(self) -> u8 {427        match self {428            BinOp::Or => 1,429            BinOp::And => 2,430            BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge => 3,431            BinOp::Add | BinOp::Sub => 5,432            BinOp::Mul | BinOp::Div | BinOp::Rem => 6,433        }434    }435}436437#[derive(Clone, Debug, PartialEq, Serialize)]438#[serde(tag = "expr", rename_all = "snake_case")]439pub enum ExprKind {440    Number {441        value: NumLit,442        unit: Option<UnitSuffix>,443    },444    Str {445        value: String,446    },447    Bool {448        value: bool,449    },450    Note {451        note: NoteLit,452    },453    PitchClass {454        pitch_class: PitchClassLit,455    },456    Ident {457        name: String,458    },459    List {460        items: Vec<Expr>,461    },462    Range {463        start: Box<Expr>,464        end: Box<Expr>,465        inclusive: bool,466    },467    Unary {468        op: UnOp,469        operand: Box<Expr>,470    },471    Binary {472        op: BinOp,473        left: Box<Expr>,474        right: Box<Expr>,475    },476    Call {477        callee: Ident,478        args: Vec<Arg>,479    },480    Index {481        base: Box<Expr>,482        index: Box<Expr>,483    },484    Member {485        base: Box<Expr>,486        name: Ident,487    },488    IfElse {489        cond: Box<Expr>,490        then: Box<Expr>,491        otherwise: Box<Expr>,492    },493    Paren {494        inner: Box<Expr>,495    },496    /// Placeholder left by error recovery; a diagnostic was already reported.497    Error,498}499500/// Names accepted as articulation modifiers after `play`, and as values of `articulation`.501pub const ARTICULATIONS: &[&str] = &["normal", "legato", "staccato", "staccatissimo", "tenuto", "marcato", "accent", "pizzicato", "tremolo"];502
Files main
Menu

Theme