bd180a96a2586c96aae3a53bbc027fb8473b82a7
[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* extensionExpr* overBassNoteExpr*
21     ;
22
23 rootNote
24     : NOTE
25     ;
26
27 overBassNoteExpr
28     : SLASH NOTE
29     ;
30
31 majMinSusPowerExpr
32     : majExpr
33     | minExpr
34     | susExpr
35     | diminishedExpr
36     | augmentedExpr
37     | powerChordExpr
38     ;
39
40 majExpr: MAJOR;
41
42 minExpr: MINOR;
43
44 susExpr: SUS ('2'|'4');
45
46 diminishedExpr: DIMINISHED;
47
48 augmentedExpr: AUGMENTED;
49
50 powerChordExpr: '5';
51
52 addNotesExpr
53     : SIX
54     | SEVEN
55     | MAJ_SEVEN
56     | ADD_NINE
57     ;
58
59 extensionExpr
60     : INTERVAL
61     ;
62
63 SPACE: [ \t\r\n] -> skip;
64
65 NOTE: (AS|BS|CS|DS|ES|FS|GS) ;
66
67 AS
68     : ('A'|'a')
69     | ('Ab'|'ab')
70     | ('A#'|'a#')
71     ;
72
73 BS
74     : ('B'|'b')
75     | ('Bb'|'bb')
76     ;
77
78 CS
79     : ('C'|'c')
80     | ('C#'|'c#')
81     ;
82
83 DS
84     : ('D'|'d')
85     | ('Db'|'db')
86     | ('D#'|'d#')
87     ;
88
89 ES
90     : ('E'|'e')
91     | ('Eb'|'eb')
92     ;
93
94 FS
95     : ('F'|'f')
96     | ('F#'|'f#')
97     ;
98
99 GS
100     : ('G'|'g')
101     | ('Gb'|'gb')
102     | ('G#'|'g#')
103     ;
104
105 MAJOR: ('M'|'Maj'|'maj'|'Major'|'major');
106
107 MINOR: ('m'|'min'|'minor');
108
109 SUS: ('sus'|'suspended');
110
111 DIMINISHED: ('dim'|'diminished');
112
113 AUGMENTED: ('aug'|'augmented');
114
115 SLASH: ('/'|'\\');
116
117 SIX: '6';
118
119 SEVEN: '7';
120
121 MAJ_SEVEN: MAJOR '7';
122
123 ADD_NINE: ('add'|'Add')* '9';
124
125 INTERVAL: (MAJOR|MINOR)* ('b'|'#')* DIGITS ;
126
127 DIGITS: [1-9]+ ;