SPB Git

Repository Public

grammar.ebnf 8.7 KB 161 lines

1(* ORIX Sonata2   File: Documentation/spec/grammar.ebnf3   Description: Grammar of the Sonata language, version 0.1 (ISO 14977 EBNF).4                The parser in crates/sonata-parser is the reference; this file must agree with it.56   Author: Simon-Pierre Boucher7   Contact: contact@spboucher.ai *)89(* ---------- Lexical grammar ---------- *)1011source          = { whitespace | comment | token } ;12whitespace      = " " | "\t" | "\r" | newline ;13newline         = "\n" ;14comment         = "//" , { ? any character except newline ? }15                | "/*" , { comment | ? any character ? } , "*/" ;      (* block comments nest *)1617token           = number | string | note | pitch class | word | punctuation ;1819digit           = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;20digits          = digit , { digit | "_" } ;21number          = digits , [ "." , digits ] , [ unit ] ;                (* no space before the unit *)22unit            = "bpm" | "Hz" | "kHz" | "dB" | "ms" | "s" | "st" | "cents" | "%"23                | "bars" | "bar" | "beats" | "beat" | "deg" ;2425string          = '"' , { string char | escape } , '"' ;                (* on one line *)26string char     = ? any character except '"', "\" and newline ? ;27escape          = "\n" | "\t" | '\"' | "\\" | "\u{" , hex , { hex } , "}" ;   (* 1 to 6 hex digits *)28hex             = digit | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" ;2930letter name     = "A" | "B" | "C" | "D" | "E" | "F" | "G" ;31note            = letter name , [ "##" | "#" | "bb" | "b" ] , ( "-1" | digit ) ;32pitch class     = letter name , [ "#" | "b" ] ;33(* A note or pitch class is only recognised when the next character cannot continue a word.34   Tokens of these shapes are never identifiers: C4, Bb, A are literals; a4, Amp, Cmaj are words. *)3536word            = ( ? ASCII letter ? | "_" ) , { ? ASCII letter ? | digit | "_" } ;37identifier      = ? a word that is not a keyword, a note or a pitch class ? ;38label           = word ;                                                 (* keywords allowed *)3940keyword         = "import" | "const" | "let" | "fn" | "pattern" | "instrument" | "composition"41                | "if" | "then" | "else" | "for" | "in" | "repeat" | "return" | "true" | "false"42                | "and" | "or" | "not" | "play" | "chord" | "rest" | "sequence" | "voice"43                | "parallel" | "at" | "tuplet" | "transpose" | "swing" | "tempo" | "signature"44                | "key" | "seed" | "tuning" | "master" | "bus" | "audio" | "clip" | "marker"45                | "effects" | "automate" | "graph" | "node" | "out" | "model" | "preset"46                | "send" | "default" | "sonata" ;4748punctuation     = "(" | ")" | "{" | "}" | "[" | "]" | "," | ":" | ";" | "." | ".." | "..="49                | "->" | "=" | "==" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" | "%" ;5051(* ---------- Line structure ----------52   "sep" below is a line break or ";". Statements end at a sep or at the closing brace.53   An infix or postfix operator must be on the same line as the end of its left operand:54   a line may END with an operator, a comma or an open bracket to continue, but a line that55   STARTS with "-", "(", "[" or any operator begins a new statement.56   Modifiers of play, options of clip, "ramp", "of", "pre", presets and argument lists of57   effects, and the value of a setting must start on the line of the statement they belong to. *)5859sep             = newline | ";" ;6061(* ---------- Files ---------- *)6263file            = [ version ] , { item } ;64version         = "sonata" , digits , [ "." , digits ] ;65item            = import | variable | function | pattern | instrument def | composition ;6667import          = "import" , label , { "." , label } ;68variable        = ( "const" | "let" ) , identifier , [ ":" , type ] , "=" , expression ;69function        = "fn" , identifier , parameters , [ "->" , type ] , block ;70pattern         = "pattern" , identifier , [ parameters ] , block ;71parameters      = "(" , [ parameter , { "," , parameter } , [ "," ] ] , ")" ;72parameter       = identifier , [ ":" , type ] , [ "=" , expression ] ;73type            = identifier , [ "<" , type , { "," , type } , ">" ] ;7475(* ---------- Instrument definitions ---------- *)7677instrument def  = "instrument" , identifier , [ ":" , identifier ] , "{" , { member , [ sep ] } , "}" ;78member          = "model" , label79                | "preset" , label80                | "graph" , "{" , { graph line , [ sep ] } , "}"81                | path , label , arguments                              (* slot:  excitation pluck(position: 0.35) *)82                | path , expression ;                                   (* parameter:  damping 0.015 *)83path            = label , { "." , label } ;84graph line      = "node" , identifier , "=" , expression85                | "out" , expression ;                                  (* exactly one per graph *)8687(* ---------- Compositions and blocks ---------- *)8889composition     = "composition" , string , block ;90block           = "{" , { statement , [ sep ] } , "}" ;9192statement       = variable93                | identifier , "=" , expression                          (* assignment *)94                | "tempo" , expression , [ "ramp" , expression ]95                | "key" , pitch class , [ label ]96                | "signature" , digits , "/" , digits97                | "seed" , expression98                | "tuning" , expression99                | "swing" , expression , [ "of" , expression ] , [ block ]100                | "instrument" , identifier , ":" , identifier , block  (* a track *)101                | "audio" , identifier , block102                | "clip" , string , { label , expression }103                | "bus" , identifier , block104                | "master" , block105                | "marker" , expression , [ "at" , expression ]106                | ( "play" | "chord" ) , expression , { modifier }107                | "rest" , expression108                | ( "sequence" | "voice" | "parallel" ) , block109                | "at" , expression , block110                | "repeat" , expression , block111                | "for" , identifier , "in" , expression , block112                | "if" , expression , block , [ "else" , ( if statement | block ) ]113                | "tuplet" , expression , "in" , expression , block114                | "transpose" , expression , [ block ]115                | "return" , [ expression ]116                | "effects" , "{" , { effect , [ sep | "," ] } , "}"117                | "automate" , target , points118                | "send" , identifier , [ expression ] , [ "pre" ]119                | "default" , label , expression120                | identifier , [ expression ]                            (* setting:  gain -8dB,  mute *)121                | call ;                                                 (* pattern call *)122if statement    = "if" , expression , block , [ "else" , ( if statement | block ) ] ;123124modifier        = "duration" , expression125                | "velocity" , expression126                | "tie"127                | "bend" , points128                | articulation ;129articulation    = "normal" | "legato" | "staccato" | "staccatissimo" | "tenuto" | "marcato"130                | "accent" | "pizzicato" | "tremolo" ;131132effect          = [ "bypass" ] , label , [ label ] , [ arguments ] ;     (* kind, preset, arguments *)133target          = label , [ "[" , digits , "]" ] , { "." , label } ;134points          = "{" , { point , [ sep | "," ] } , "}" ;135point           = expression , ":" , expression , [ label ] ;           (* position: value curve *)136137(* ---------- Expressions, lowest precedence first ---------- *)138139expression      = or ;140or              = and , { "or" , and } ;141and             = not , { "and" , not } ;142not             = [ "not" ] , comparison ;143comparison      = range , { ( "==" | "!=" | "<" | "<=" | ">" | ">=" ) , range } ;144range           = sum , [ ( ".." | "..=" ) , sum ] ;145sum             = product , { ( "+" | "-" ) , product } ;146product         = unary , { ( "*" | "/" | "%" ) , unary } ;147unary           = { "-" | "+" | "not" } , postfix ;   (* a leading "+" has no effect: gain +4dB *)148postfix         = primary , { "[" , expression , "]" | "." , label } ;149primary         = number | string | note | pitch class | "true" | "false"150                | identifier151                | call152                | "(" , expression , ")"153                | "[" , [ expression , { "," , expression } , [ "," ] ] , "]"154                | "if" , expression , "then" , expression , "else" , expression ;155call            = ( identifier | "chord" | "clip" ) , arguments ;        (* "(" on the same line *)156arguments       = "(" , [ argument , { "," , argument } , [ "," ] ] , ")" ;157argument        = [ label , ":" ] , expression ;158159(* All binary operators are left-associative; the range operator does not chain.160   Integer division is exact: 1/4 is the rational one quarter. *)161
Files main
Menu

Theme