Skip to content

Commit 8844a5d

Browse files
committed
update on grammar and lexer for signals and division
1 parent c698ffb commit 8844a5d

5 files changed

Lines changed: 147 additions & 62 deletions

File tree

grammar/grammar.html

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -781,38 +781,38 @@ <h1><code>paramElement</code></h1>
781781
<g>
782782
<path d="M86 61h0"></path>
783783
<path d="M138 61h0"></path>
784-
<rect x="86" y="50" width="52" height="22" rx="10" ry="10"></rect>
785-
<text x="112" y="65">note</text>
784+
<rect x="86" y="50" width="52" height="22"></rect>
785+
<text x="112" y="65">name</text>
786786
</g>
787787
<path d="M138 61h10"></path>
788788
</g>
789789
<path d="M164 61a10 10 0 0 0 10 -10v-10a10 10 0 0 1 10 -10"></path>
790790
<path d="M40 31a10 10 0 0 1 10 10v40a10 10 0 0 0 10 10"></path>
791791
<g>
792-
<path d="M60 91h16"></path>
793-
<path d="M148 91h16"></path>
794-
<path d="M76 91h10"></path>
792+
<path d="M60 91h12"></path>
793+
<path d="M152 91h12"></path>
794+
<path d="M72 91h10"></path>
795795
<g>
796-
<path d="M86 91h0"></path>
797-
<path d="M138 91h0"></path>
798-
<rect x="86" y="80" width="52" height="22"></rect>
799-
<text x="112" y="95">name</text>
796+
<path d="M82 91h0"></path>
797+
<path d="M142 91h0"></path>
798+
<rect x="82" y="80" width="60" height="22"></rect>
799+
<text x="112" y="95">array</text>
800800
</g>
801-
<path d="M138 91h10"></path>
801+
<path d="M142 91h10"></path>
802802
</g>
803803
<path d="M164 91a10 10 0 0 0 10 -10v-40a10 10 0 0 1 10 -10"></path>
804804
<path d="M40 31a10 10 0 0 1 10 10v70a10 10 0 0 0 10 10"></path>
805805
<g>
806-
<path d="M60 121h12"></path>
807-
<path d="M152 121h12"></path>
808-
<path d="M72 121h10"></path>
806+
<path d="M60 121h0"></path>
807+
<path d="M164 121h0"></path>
808+
<path d="M60 121h10"></path>
809809
<g>
810-
<path d="M82 121h0"></path>
811-
<path d="M142 121h0"></path>
812-
<rect x="82" y="110" width="60" height="22"></rect>
813-
<text x="112" y="125">array</text>
810+
<path d="M70 121h0"></path>
811+
<path d="M154 121h0"></path>
812+
<rect x="70" y="110" width="84" height="22"></rect>
813+
<text x="112" y="125">function</text>
814814
</g>
815-
<path d="M142 121h10"></path>
815+
<path d="M154 121h10"></path>
816816
</g>
817817
<path d="M164 121a10 10 0 0 0 10 -10v-70a10 10 0 0 1 10 -10"></path>
818818
<path d="M40 31a10 10 0 0 1 10 10v100a10 10 0 0 0 10 10"></path>
@@ -824,7 +824,7 @@ <h1><code>paramElement</code></h1>
824824
<path d="M70 151h0"></path>
825825
<path d="M154 151h0"></path>
826826
<rect x="70" y="140" width="84" height="22"></rect>
827-
<text x="112" y="155">function</text>
827+
<text x="112" y="155">division</text>
828828
</g>
829829
<path d="M154 151h10"></path>
830830
</g>
@@ -834,6 +834,50 @@ <h1><code>paramElement</code></h1>
834834
</g>
835835
</svg>
836836

837+
</div>
838+
<h1><code>division</code></h1>
839+
<div>
840+
<svg class="railroad-diagram" width="393" height="62" viewBox="0 0 393 62">
841+
<g transform="translate(.5 .5)">
842+
<path d="M 20 21 v 20 m 10 -20 v 20 m -10 -10 h 20.5"></path>
843+
<g>
844+
<path d="M40 31h0"></path>
845+
<path d="M352 31h0"></path>
846+
<path d="M40 31h20"></path>
847+
<g>
848+
<path d="M60 31h0"></path>
849+
<path d="M332 31h0"></path>
850+
<path d="M60 31h10"></path>
851+
<g>
852+
<path d="M70 31h0"></path>
853+
<path d="M138 31h0"></path>
854+
<rect x="70" y="20" width="68" height="22" rx="10" ry="10"></rect>
855+
<text x="104" y="35">number</text>
856+
</g>
857+
<path d="M138 31h10"></path>
858+
<path d="M148 31h10"></path>
859+
<g>
860+
<path d="M158 31h0"></path>
861+
<path d="M234 31h0"></path>
862+
<rect x="158" y="20" width="76" height="22" rx="10" ry="10"></rect>
863+
<text x="196" y="35">divider</text>
864+
</g>
865+
<path d="M234 31h10"></path>
866+
<path d="M244 31h10"></path>
867+
<g>
868+
<path d="M254 31h0"></path>
869+
<path d="M322 31h0"></path>
870+
<rect x="254" y="20" width="68" height="22" rx="10" ry="10"></rect>
871+
<text x="288" y="35">number</text>
872+
</g>
873+
<path d="M322 31h10"></path>
874+
</g>
875+
<path d="M332 31h20"></path>
876+
</g>
877+
<path d="M 352 31 h 20 m -10 -10 v 20 m 10 -20 v 20"></path>
878+
</g>
879+
</svg>
880+
837881
</div>
838882
<h1><code>name</code></h1>
839883
<div>

grammar/grammar.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,31 @@ const lexer = moo.compile({
2121

2222
//seperator: /[\,\;]/,
2323

24-
note: /[a-gA-G](?:[0-9])?(?:#+|b+|x)?/,
24+
//note: /[a-gA-G](?:[0-9])?(?:#+|b+|x)?/,
2525
number: /[+-]?(?:[0-9]|[0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/,
2626
// hex: /0x[0-9a-f]+/,
2727

28-
// operator: /[\+\-\*\/]/,
28+
divider: /[/:]/,
2929

3030
lParam: '(',
3131
rParam: ')',
3232
lArray: '[',
3333
rArray: ']',
3434
// lFunc: '{',
3535
// rFunc: '}'
36-
37-
identifier: /[a-zA-Z\_\-][a-zA-Z0-9\_\-\.]*/,
38-
//signal: /~(?:\\["\\]|[^\n"\\ \t])+/,
39-
//osc: /\/(?:\\["\\]|[^\n"\\ \t])*/,
40-
36+
4137
string: {
4238
match: /["|'|\`](?:\\["\\]|[^\n"\\])*["|'|\`]/,
4339
value: x => x.slice(1, x.length-1)
4440
},
4541

42+
// identifier: /[a-zA-Z\_\-][a-zA-Z0-9\_\-\.]*/,
43+
// identifier: /[a-zA-Z\_\-][^\s]*/,
44+
identifier: /[^0-9\s][^\s]*/,
45+
46+
// signal: /~(?:\\["\\]|[^\n"\\ \t])+/,
47+
// osc: /\/(?:\\["\\]|[^\n"\\ \t])*/,
48+
4649
ws: /[ \t]+/,
4750
});
4851
var grammar = {
@@ -102,11 +105,14 @@ var grammar = {
102105
{"name": "params", "symbols": ["paramElement"], "postprocess": (d) => d[0]},
103106
{"name": "params", "symbols": ["paramElement", "_", "params"], "postprocess": (d) => [d[0], d[2]]},
104107
{"name": "paramElement", "symbols": [(lexer.has("number") ? {type: "number"} : number)], "postprocess": (d) => { return { "@number" : d[0].value }}},
105-
{"name": "paramElement", "symbols": [(lexer.has("note") ? {type: "note"} : note)], "postprocess": (d) => { return { "@note" : d[0].value }}},
106-
{"name": "paramElement", "symbols": ["name"], "postprocess": (d) => d[0]},
108+
{"name": "paramElement", "symbols": ["name"], "postprocess": (d) => {
109+
return d[0]
110+
} },
107111
{"name": "paramElement", "symbols": ["array"], "postprocess": (d) => d[0]},
108112
{"name": "paramElement", "symbols": ["function"], "postprocess": (d) => d[0]},
109-
{"name": "name", "symbols": [(lexer.has("identifier") ? {type: "identifier"} : identifier)], "postprocess": (d) => { return { "@identifier" : d[0].value }}},
113+
{"name": "paramElement", "symbols": ["division"], "postprocess": (d) => d[0]},
114+
{"name": "division", "symbols": [(lexer.has("number") ? {type: "number"} : number), (lexer.has("divider") ? {type: "divider"} : divider), (lexer.has("number") ? {type: "number"} : number)], "postprocess": (d) => { return { "@division" : d[0]+"/"+d[2] }}},
115+
{"name": "name", "symbols": [(lexer.has("identifier") ? {type: "identifier"} : identifier)], "postprocess": (d) => { return IR.identifier(d) }},
110116
{"name": "name", "symbols": [(lexer.has("string") ? {type: "string"} : string)], "postprocess": (d) => { return { "@string" : d[0].value }}},
111117
{"name": "_$ebnf$1", "symbols": []},
112118
{"name": "_$ebnf$1", "symbols": ["_$ebnf$1", "wschar"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},

grammar/mercury.ne

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,31 @@ const lexer = moo.compile({
1818

1919
//seperator: /[\,\;]/,
2020

21-
note: /[a-gA-G](?:[0-9])?(?:#+|b+|x)?/,
21+
//note: /[a-gA-G](?:[0-9])?(?:#+|b+|x)?/,
2222
number: /[+-]?(?:[0-9]|[0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/,
2323
// hex: /0x[0-9a-f]+/,
2424

25-
// operator: /[\+\-\*\/]/,
25+
divider: /[/:]/,
2626

2727
lParam: '(',
2828
rParam: ')',
2929
lArray: '[',
3030
rArray: ']',
3131
// lFunc: '{',
3232
// rFunc: '}'
33-
34-
identifier: /[a-zA-Z\_\-][a-zA-Z0-9\_\-\.]*/,
35-
//signal: /~(?:\\["\\]|[^\n"\\ \t])+/,
36-
//osc: /\/(?:\\["\\]|[^\n"\\ \t])*/,
37-
33+
3834
string: {
3935
match: /["|'|\`](?:\\["\\]|[^\n"\\])*["|'|\`]/,
4036
value: x => x.slice(1, x.length-1)
4137
},
4238

39+
// identifier: /[a-zA-Z\_\-][a-zA-Z0-9\_\-\.]*/,
40+
// identifier: /[a-zA-Z\_\-][^\s]*/,
41+
identifier: /[^0-9\s][^\s]*/,
42+
43+
// signal: /~(?:\\["\\]|[^\n"\\ \t])+/,
44+
// osc: /\/(?:\\["\\]|[^\n"\\ \t])*/,
45+
4346
ws: /[ \t]+/,
4447
});
4548
%}
@@ -152,21 +155,30 @@ paramElement ->
152155
%number
153156
{% (d) => { return { "@number" : d[0].value }} %}
154157
|
155-
%note
156-
{% (d) => { return { "@note" : d[0].value }} %}
157-
|
158+
# %note
159+
# {% (d) => { return { "@note" : d[0].value }} %}
160+
# |
158161
name
159-
{% (d) => d[0] %}
162+
{% (d) => {
163+
return d[0]
164+
} %}
160165
|
161166
array
162167
{% (d) => d[0] %}
163168
|
164169
function
165170
{% (d) => d[0] %}
171+
|
172+
division
173+
{% (d) => d[0] %}
174+
175+
division ->
176+
%number %divider %number
177+
{% (d) => { return { "@division" : d[0]+"/"+d[2] }} %}
166178

167179
name ->
168180
%identifier
169-
{% (d) => { return { "@identifier" : d[0].value }} %}
181+
{% (d) => { return IR.identifier(d) } %}
170182
|
171183
%string
172184
{% (d) => { return { "@string" : d[0].value }} %}

grammar/mercuryIR.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
//==============================================================================
1+
//==========================================================================
22
// Mercury Intermediate Language
33
//
44
// Returns results for the parsing tree when parsing a line of code
5-
//==============================================================================
5+
//==========================================================================
66

7-
const bind = require('./bind-functions.gen.json');
7+
// const bind = require('./bind-functions.gen.json');
88

9-
function bindFunction(obj){
9+
// postprocessing for identifiers
10+
function identifier(obj){
11+
let v = obj[0].value;
12+
// is the identifier a note?
13+
if (v.match(/^[a-gA-G](?:[0-9])?(?:#+|b+|x)?$/)){
14+
return { "@note" : v }
15+
} else if (v.match(/^~[^\s]*$/)){
16+
return { "@signal" : v }
17+
}
18+
return { "@identifier" : v }
19+
}
20+
21+
/*function bindFunction(obj){
1022
var f = obj;
1123
var b = bind[f];
1224
@@ -19,6 +31,6 @@ function bindFunction(obj){
1931
2032
function num(obj) {
2133
return { "@number" : Number(obj[0].value) };
22-
}
34+
}*/
2335

24-
module.exports = {num, bindFunction};
36+
module.exports = { identifier };

grammar/parse-test.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@ function parse(s){
1818
for (var i=0; i<results; i++){
1919
console.log("Result", i+1, "of", results, "\n",
2020
util.inspect(parser.results[i], { depth: 10 }),
21+
// console.log(parser.results[i]),
2122
"\n");
2223
}
2324
} else {
2425
console.log("Parse succesful: \n", util.inspect(parser.results[0], { depth: 10}), "\n");
2526
}
2627
} catch (e) {
2728
console.log("!!! Parse failed: \n", e.message);
29+
// console.log("Interpret as comment:", { '@comment': parser.lexer.buffer });
30+
// console.log(parser.lexer);
2831
// console.log("Trying: \n", s.substring(0, parser.lexer.index-1));
2932
// parse(s.substring(0, parser.lexer.index-1));
3033
}
3134
}
3235

3336
// @global:
3437
parseNumbers();
35-
// parseComments();
36-
// parseStrings();
37-
// parseIdentifier();
38-
// parseKeywords();
38+
parseRhythm();
39+
parseComments();
40+
parseStrings();
41+
parseIdentifier();
42+
parseKeywords();
3943
// parseSettings();
40-
// parseSignal();
44+
parseSignal();
4145
// parseOSC();
4246

4347
// @ring:
@@ -60,19 +64,26 @@ function parseNumbers(){
6064
parse("12 34 -56 7.89");
6165
// parse("1 + 2 / 4");
6266
parse("[ 1 2 3.14 5 foo bar 'to_to t1T1' ]");
63-
parse("[ ]");
67+
// parse("[ ]");
6468

6569
parse("a A b B c C d D e E f F g G");
6670
parse("a# B# C# Dx Gb");
6771
parse("a## Bbb C## Dx Gb fbb");
6872
parse("a1 A0 b2b B4# c1 C2bb d4## D3 e2 E1 f4# F5 g6 G2");
6973
}
7074

75+
function parseRhythm(){
76+
parse("1/4");
77+
parse("3/16");
78+
parse("7/16 4/5 3/1");
79+
parse("7:8 3:2 1:5");
80+
}
81+
7182
// Parse comments
7283
function parseComments(){
7384
parse("// a comment here");
7485
// parse("/ also a comment?");
75-
parse("# another comment");
86+
parse("# not another comment");
7687
parse("$ also a type of comment");
7788
// parse("not a type of comment");
7889
// parse("// 1.0111213141E5 < commented");
@@ -119,14 +130,14 @@ function parseOSC(){
119130

120131
// Parse keywords
121132
function parseKeywords(){
122-
parse("ring");
123-
parse("new");
124-
parse("set");
125-
126-
parse("array");
127-
parse("data");
128-
parse("add");
129-
parse("apply");
133+
parse("ring ");
134+
parse("new ");
135+
parse("set ");
136+
137+
parse("array ");
138+
parse("data ");
139+
parse("add ");
140+
parse("apply ");
130141
}
131142

132143
function parseRing(){

0 commit comments

Comments
 (0)