-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.jison
117 lines (100 loc) · 2.17 KB
/
parser.jison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* description: lexical and syntactical rules for the music language. */
/* lexical grammar */
%lex
%{
if (!('instructions' in yy)) {
yy.instructions = [];
yy.chars = 0;
yy.words = 0;
yy.lines = 1;
yy.prev = 'none'
}
%}
%%
\s+ /* skip whitespace */
"play" return 'PLAY'
"bpm" return 'BPM'
"sequence" return 'SEQUENCE'
"transport" return 'TRANSPORT'
"start" return 'START'
[a-g]|[A-G] return 'NOTE'
[0-9]+ return 'NUMBER'
\"[^"]+\" yytext = yytext.slice(1,-1); return 'STRING'
. return 'ERROR'
<<EOF>> return 'EOF'
/lex
%start expressions
%% /* language grammar */
expressions
: instructions eof
{
typeof console !== 'undefined' ? console.log($1) : print($1);
return $1;
}
| error
;
eof : EOF {
const instructions = yy.instructions;
yy.instructions = new Array();
return instructions
}
;
instructions
: instruction instructions
|
;
instruction
: play
| bpm
| sequence
| transport
;
sequence
: SEQUENCE STRING NUMBER {
yy.prev = 'sequence';
yy.instructions.push({
action: $1,
time: $2,
startTime: $3,
instructions: new Array()
});
}
;
play
: PLAY NOTE NUMBER {
if ( yy.prev == 'sequence'){
var index = yy.instructions.length - 1;
yy.instructions[index].instructions.push({
action: $1,
note:$2,
number: $3
});
yy.prev = 'play';
}else{
yy.instructions.push({
action: $1,
note:$2,
number: $3
});
}
}
;
bpm
: BPM NUMBER{
yy.instructions.push({
action: $1,
number: $2
});
}
;
transport
: TRANSPORT START {
yy.instructions.push({
action: $1,
command: $2
});
}
;
error
: ERROR {return {error: "error"};}
;