-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlanguage.pegjs
224 lines (184 loc) · 5.31 KB
/
language.pegjs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
//
// This grammar is used to create an abstract syntax tree (AST). PEGjs is a great
// tool for creating these domain specific languages.
//
// Examples:
// read values from IP 128.54.39.72 and PORT 11112
// print data
// for each subject with more than 1 study
// for each subject with more than 1 study and less than 4 studies
//
// for each subject with more than 1 study select first series that matches "3D-T1" in its SeriesDescription
// for each subject with more than 1 study select all series
}
start
= command
command
= readdata
/ printdata
/ printselected
/ move
/ foreach
/ help
/ quit
/ nothing
nothing
= ws "\n" { return [{ 'action': 'nothing' }]; }
move
= ws move_word ws "to" ws dest:word ws "\n" { return [{ 'action': 'move', 'destinationAETitle': dest.join("") }]; }
foreach
= ws for_word ws each_word ws tar:target ws "with" ws wi:with_statements ws se:selects "\n" {
return [{ 'action': 'foreach', 'target': tar, 'with': wi, 'select': se }];
}
help
= ws "help" ws what:word ws "\n" {
return [{ 'action': 'help', 'what': what }];
}
/ ws "help" ws "\n" {
return [{ 'action': 'help', 'what': 'all' }];
}
quit
= ws "quit" ws "\n" {
return [{ 'action': 'quit' }];
}
target
= "subject" { return 'subject'; }
/ "subjects" { return 'subject'; }
/ "patient" { return 'subject'; }
/ "patients" { return 'subject'; }
/ "series" { return 'series'; }
/ "study" { return 'study'; }
/ "studies" { return 'study'; }
selects
= "select" ws se:select_statements { return se; }
/ ws { return []; }
select_statements
= left:select_statement rest:(ws ("and" / "or") ws select_statement)* {
if (rest.length == 0)
return { 'chain': [left] };
else {
var l = [left];
for(var i = 0; i < rest.length; i++) {
rest[i][3].action = rest[i][1];
l.push(rest[i][3]);
}
return { 'chain': l };
}
}
select_statement
= qal:qualifiers ws tar:target ws th:that_statements { return { 'qualifier': qal, 'target': tar, 'limitTo': th }; }
that_statements
= "that" ws left:that_statement rest:(ws ("and" / "or") ws that_statement)* {
if (rest.length == 0)
return { 'chain': [left] };
else {
var l = [left];
for(var i = 0; i < rest.length; i++) {
rest[i][3].action = rest[i][1];
l.push(rest[i][3]);
}
return { 'chain': l };
}
}
/ ws { return []; }
that_statement
= "matches" ws val:string ws in_word ws tag:DicomTag {
return { 'qualifier': { 'operator': 'equals', 'value': val, 'tag': tag}, 'target': 'series' };
}
DicomTag
= chars:[a-zA-Z0-9\.]+ { return chars.join(""); }
string
= '"' chars:[a-zA-Z0-9_\-]+ '"' { return chars.join(""); }
/ '\'' chars:[a-zA-Z0-9_\-]+ '\'' { return chars.join(""); }
with_statements
= left:with_statement rest:(ws ("and" / "or") ws with_statement)* {
if (rest.length == 0)
return { 'chain': [left] };
else {
var l = [left];
for(var i = 0; i < rest.length; i++) {
rest[i][3].action = rest[i][1];
l.push(rest[i][3]);
}
return { 'chain': l };
}
}
with_statement
= qual:qualifiers ws tar:target { return { 'action': 'and', 'qualifier': qual, 'target': tar }; }
/ "not" ws qual:qualifiers ws tar:target { return { 'action': 'and', 'qualifier': qual, 'target': tar, 'logical': 'negate' }; }
qualifiers
= "more" ws "than" ws num:number { return { 'operator': 'morethan', 'value': num }; }
/ "less" ws "than" ws num:number { return { 'operator': 'lessthan', 'value': num }; }
/ first_word { return { 'operator': 'first' }; }
/ last_word { return { 'operator': 'last' }; }
/ "one" { return { 'operator': 'first' }; }
/ "all" { return { 'operator': 'all' }; }
printdata
= ws "print" ws data_word ind:index "\n" {
if (ind.value == 'all')
return [{ 'action' : 'print', 'what': 'all' }];
else
return [{ 'action' : 'print', 'what': parseInt(ind.value) }];
}
printselected
= ws "print" ws "selected" ind:index "\n" {
if (ind.value == 'all')
return [{ 'action' : 'printselected', 'what': 'all' }];
else
return [{ 'action' : 'printselected', 'what': parseInt(ind.value) }];
}
index
= '[' num:number ']' { return { 'value': num}; }
/ ws { return { 'value': 'all'}; }
readdata
= ws "read" ws data_word ws from_word ws IP_word ws ipl:ip ws and_word ws port_word ws portl:port ws "\n" {
return [{ 'action': 'dataFromDicomNode', 'ip': ipl, 'port': portl }];
}
move_word
= "move" ws "selected"
/ "move"
in_word
= "in" ws "its"
/ "in"
first_word
= "the" ws "first"
/ "first"
last_word
= "the" ws "last"
/ "last"
for_word
= "for"
each_word
= "each"
IP_word
= "IP"
/ "ip"
ws
port_word
= "PORT"
/ "port"
/ ws
and_word
= "and"
/ ws
data_word
= "data"
/ "values"
/ "value"
/ ws
from_word
= "from"
/ ws
ip "ip"
= ipnumber:[0-9\.]+ { return ipnumber.join(""); }
port "port"
= portnumber:[0-9]+ { return portnumber.join(""); }
ws "ws"
= [ \t]*
dot "dot"
= "."
number "number"
= num:[\-\+0-9]+ { return parseInt(num.join("")); }
word
= [a-zA-Z0-9_\.]+