-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrammar.ne
76 lines (63 loc) · 2.66 KB
/
grammar.ne
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
@{%
const empty = d => null;
const nth = i => (d => d[i]);
const notnull = d => d != null;
const v = i => (d => i)
%}
# Main Structure
program ->
line (nl line {% nth(1) %}):* {% d => [d[0]].concat(d[1]).filter(notnull) %}
line ->
_ command eol {% nth(1) %}
| _ section eol {% d => ({node: 'section', name: d[1]}) %}
| _ label eol {% d => ({node: 'label', name: d[1]}) %}
| eol {% empty %}
command ->
"ramp" __ float sep integer {% ([,,time,,amount]) => ({node: 'ramp', time: time, steps: amount}) %}
| "wait" __ float {% ([,,time]) => ({node: 'ramp', time: time, steps: 0}) %}
| "branch" __ integer sep labelname {% d => ({node: 'branch', count: d[2], target: d[4]}) %}
| "set_pwm" __ integer {% ([,,value]) => ({node: 'pwm', value: value}) %}
| "start" {% d => ({node: 'start'}) %}
| "trigger" __ waittarget {% d=> ({node: 'trigger', send: 0, wait: d[2]}) %}
| "trigger" __ sendtarget
(sep waittarget {% nth(1) %}
):? {% d => ({node: 'trigger', send: d[2] ? d[2] : 0, wait: d[3] ? d[3] : 0})%}
| "end" (sep interruptsignal
(sep interruptsignal {% nth(1) %}
):? {% d => {const s = [d[1], d[2]]; return {reset: s.includes('r'), int: s.includes('i')}}%}
):? {% d => (Object.assign({node: 'end', reset: false, int: false}, d[1])) %}
# Line Fragments
sep -> _ "," _ {% empty %}
eol -> _ comment:? {% empty %}
comment -> "#" [^\r\n]:* {% empty %}
label -> labelname ":" {% id %}
labelname -> [._a-zA-Z0-9]:+ {% d => d[0].join('') %}
# Literals
interruptsignal ->
[rR] {% v("r") %}
| [iI] {% v("i") %}
sendtarget ->
[sS] target {% nth(1) %}
waittarget ->
[wW] target {% nth(1) %}
target ->
"1" {% v(0b001) %}
| "2" {% v(0b010) %}
| "3" {% v(0b100) %}
| "12" {% v(0b011) %}
| "13" {% v(0b101) %}
| "23" {% v(0b110) %}
| "123" {% v(0b111) %}
section ->
(".ENGINE1" | ".engine1") {% v("engine1") %}
| (".ENGINE2" | ".engine2") {% v("engine2") %}
| (".ENGINE3" | ".engine3") {% v("engine3") %}
# Numbers
integer -> "-":? [0-9]:+ {% d => parseInt(d[1].join("") * (d[0] ? -1 : 1)) %}
float -> integer mantissa:? {% d => Math.sign(d[0]) * (Math.abs(d[0]) + d[1]) %}
mantissa -> "." [0-9]:* {% d => parseFloat("0"+d.join('')) %}
# Whitespace
__ -> ws:+ {% empty %}
_ -> ws:* {% empty %}
ws -> [ \t] {% empty %}
nl -> "\r":? "\n" {% empty %}