@@ -125,40 +125,52 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
125
125
. Where ( line => ! string . IsNullOrWhiteSpace ( line ) && ! line . StartsWith ( "//" ) ) // Ignore empty lines and comments.
126
126
. Select ( ( line , index ) =>
127
127
{
128
+ Instruction instruction = null ;
129
+
128
130
// Remove comments
129
131
var lineComments = line . Split ( new [ ] { "//" } , StringSplitOptions . None ) ;
130
- var lineWithoutComments = lineComments . First ( ) ;
131
-
132
- // Take BlockName and ActionName
133
- var lineItems = lineWithoutComments . Trim ( ) . Split ( new [ ] { "->" } , StringSplitOptions . RemoveEmptyEntries ) ;
134
- string blockName = null , actionName = null ;
135
- bool isValid ;
132
+ var lineWithoutComments = lineComments . First ( ) . Trim ( ) ;
136
133
137
- if ( lineItems . Length > 0 )
134
+ // Create instruction.
135
+ if ( lineWithoutComments . Contains ( "->" ) )
138
136
{
139
- blockName = lineItems [ 0 ] . Trim ( ) ;
137
+ // Instruction of IMyTerminalBlock -> Action
138
+ instruction = CreateInstruction ( lineWithoutComments ) ;
140
139
}
141
- if ( lineItems . Length > 1 )
140
+ else
142
141
{
143
- actionName = lineItems [ 1 ] . Trim ( ) ;
144
- }
145
- isValid = lineItems . Length == 2 ;
146
-
147
- // Arguments
148
- var arguments = GetArguments ( actionName ) ;
142
+ // Script reserved instructions.
143
+ var lineItems = lineWithoutComments . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) ;
144
+ var command = lineItems . FirstOrDefault ( ) ;
149
145
146
+ if ( command == null )
147
+ {
148
+ instruction = null ;
149
+ }
150
+ else if ( command . Equals ( "DELAY" , StringComparison . OrdinalIgnoreCase ) )
151
+ {
152
+ instruction = CreateInstructionDelay ( lineItems ) ;
153
+ }
154
+ if ( instruction == null )
155
+ {
156
+ instruction = new Instruction
157
+ {
158
+ BlockName = null ,
159
+ ActionName = null ,
160
+ Arguments = null ,
161
+ IsValid = false
162
+ } ;
163
+ }
164
+ }
150
165
return new
151
166
{
152
167
Line = index + 1 ,
153
- BlockName = blockName ,
154
- ActionName = arguments [ "" ] . Trim ( ) ,
155
- Arguments = arguments . Where ( x => ! string . IsNullOrEmpty ( x . Key ) ) . ToDictionary ( x => x . Key , x => x . Value , StringComparer . OrdinalIgnoreCase ) ,
156
- IsValid = isValid
168
+ Instruction = instruction ,
157
169
} ;
158
170
} )
159
171
;
160
172
161
- var invalid = actions . Where ( x => ! x . IsValid ) ;
173
+ var invalid = actions . Where ( x => ! x . Instruction . IsValid ) ;
162
174
if ( invalid . Any ( ) )
163
175
{
164
176
throw new SyntaxException (
@@ -197,13 +209,7 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
197
209
{
198
210
Alias = alias ,
199
211
PreviousAlias = previousActionsMatch . Select ( x => x . Alias ) ,
200
- Instructions = actions . Select ( x => new Instruction
201
- {
202
- BlockName = x . BlockName ,
203
- ActionName = x . ActionName ,
204
- Arguments = x . Arguments ,
205
- IsValid = x . IsValid
206
- } )
212
+ Instructions = actions . Select ( x => x . Instruction )
207
213
} ) ;
208
214
}
209
215
}
@@ -237,6 +243,55 @@ private static InstructionCommand CreateCommand(System.Text.RegularExpressions.M
237
243
return result ;
238
244
}
239
245
246
+ private static Instruction CreateInstruction ( string lineWithoutComments )
247
+ {
248
+ // Take BlockName and ActionName
249
+ var lineItems = lineWithoutComments . Split ( new [ ] { "->" } , StringSplitOptions . RemoveEmptyEntries ) ;
250
+ string blockName = null , actionName = null ;
251
+ bool isValid ;
252
+
253
+ if ( lineItems . Length > 0 )
254
+ {
255
+ blockName = lineItems [ 0 ] . Trim ( ) ;
256
+ }
257
+ if ( lineItems . Length > 1 )
258
+ {
259
+ actionName = lineItems [ 1 ] . Trim ( ) ;
260
+ }
261
+ isValid = lineItems . Length == 2 ;
262
+
263
+ // Arguments
264
+ var arguments = GetArguments ( actionName ) ;
265
+
266
+ // Return
267
+ return new Instruction
268
+ {
269
+ BlockName = blockName ,
270
+ ActionName = arguments [ "" ] . Trim ( ) ,
271
+ Arguments = arguments . Where ( x => ! string . IsNullOrEmpty ( x . Key ) ) . ToDictionary ( x => x . Key , x => x . Value , StringComparer . OrdinalIgnoreCase ) ,
272
+ IsValid = isValid
273
+ } ;
274
+ }
275
+
276
+ private static Instruction CreateInstructionDelay ( string [ ] lineItems )
277
+ {
278
+ string timeString = null ;
279
+
280
+ if ( lineItems . Length >= 1 )
281
+ {
282
+ timeString = lineItems [ 1 ] ;
283
+ }
284
+ return new Instruction
285
+ {
286
+ BlockName = null ,
287
+ ActionName = "DELAY" ,
288
+ Arguments = new Dictionary < string , string > ( ) {
289
+ { "TIME" , timeString }
290
+ } ,
291
+ IsValid = true
292
+ } ;
293
+ }
294
+
240
295
private static ConditionCommandInstruction CreateConditionCommand ( System . Text . RegularExpressions . Match commandMatch )
241
296
{
242
297
ConditionCommandInstruction result = null ;
0 commit comments