Easier and more self documenting patterns for loading/saving Persistent
[python_utils.git] / music / chords.g4
1 // © Copyright 2022, Scott Gasch
2 //
3 // antlr4 -Dlanguage=Python3 ./chords.g4
4 //
5 // Hi, self.  In ANTLR grammars, there are two separate types of symbols: those
6 // for the lexer and those for the parser.  The former begin with a CAPITAL
7 // whereas the latter begin with lowercase.  The order of the lexer symbols
8 // is the order that the lexer will recognize them in.  There's a good tutorial
9 // on this shit at:
10 //
11 //    https://tomassetti.me/antlr-mega-tutorial/
12 //
13 // There are also a zillion premade grammars at:
14 //
15 //    https://github.com/antlr/grammars-v4
16
17 grammar chords;
18
19 parse
20     : rootNote majMinSusPowerExpr* addNotesExpr* overBassNoteExpr*
21     | rootNote majMinSusPowerExpr* addNotesExpr extensionExpr* overBassNoteExpr
22     ;
23
24 rootNote
25     : NOTE
26     ;
27
28 overBassNoteExpr
29     : SLASH NOTE
30     ;
31
32 majMinSusPowerExpr
33     : majExpr
34     | minExpr
35     | susExpr
36     | diminishedExpr
37     | augmentedExpr
38     | powerChordExpr
39     ;
40
41 majExpr: MAJOR;
42
43 minExpr: MINOR;
44
45 susExpr: SUS ('2'|'4');
46
47 diminishedExpr: DIMINISHED;
48
49 augmentedExpr: AUGMENTED;
50
51 powerChordExpr: '5';
52
53 addNotesExpr
54     : ADD* SIX
55     | ADD* SEVEN
56     | MAJ_SEVEN
57     | MIN_SEVEN
58     | ADD* NINE
59     | ADD* ELEVEN
60     ;
61
62 extensionExpr
63     : INTERVAL
64     ;
65
66 SPACE: [ \t\r\n] -> skip;
67
68 NOTE: (AS|BS|CS|DS|ES|FS|GS) ;
69
70 AS
71     : ('A'|'a')
72     | ('Ab'|'ab')
73     | ('A#'|'a#')
74     ;
75
76 BS
77     : ('B'|'b')
78     | ('Bb'|'bb')
79     ;
80
81 CS
82     : ('C'|'c')
83     | ('C#'|'c#')
84     ;
85
86 DS
87     : ('D'|'d')
88     | ('Db'|'db')
89     | ('D#'|'d#')
90     ;
91
92 ES
93     : ('E'|'e')
94     | ('Eb'|'eb')
95     ;
96
97 FS
98     : ('F'|'f')
99     | ('F#'|'f#')
100     ;
101
102 GS
103     : ('G'|'g')
104     | ('Gb'|'gb')
105     | ('G#'|'g#')
106     ;
107
108 MAJOR: ('M'|'Maj'|'maj'|'Major'|'major');
109
110 MINOR: ('m'|'min'|'minor');
111
112 SUS: ('sus'|'suspended');
113
114 DIMINISHED: ('dim'|'diminished'|'-');
115
116 AUGMENTED: ('aug'|'augmented'|'+');
117
118 SLASH: ('/'|'\\');
119
120 ADD: ('add'|'Add'|'dom');
121
122 SIX: '6';
123
124 SEVEN: '7';
125
126 NINE: '9';
127
128 ELEVEN: '11';
129
130 MAJ_SEVEN: MAJOR '7';
131
132 MIN_SEVEN: MINOR '7';
133
134 INTERVAL: (MAJOR|MINOR)* ('b'|'#')* DIGITS ;
135
136 DIGITS: [1-9]+ ;