-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.m
286 lines (236 loc) · 10.8 KB
/
Parser.m
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
classdef Parser
%PARSER Parse .qa files containing LOCI code.
% This class reads the specified LOCI code and parses it. Parsing
% includes scanning and syntax checking.
% The parsed code only contains necessary information and lines.
% Symbols such as comments are removed.
properties(Access = public)
% File to parse
filename;
% Parsed code
gCode;
end
properties(Access = private)
% Available and allowed (logical) parts.
gParts = {'directional_coupler', 'waveguide_crossing', 'phase'};
% gParts = {'directional_standard', 'directional_heated', ...
% 'hresonator', 'hdirectional', 'waveguide_sspd', 'ring', ...
% 'waveguide_crossing', 'grating_coupler'};
end
methods(Access = public)
function [Parse, gCode, gDescription] = Parser(filename)
%PARSER Parse LOCI code in filename.
Parse.filename = filename;
% Parse file
[Parse, gCode, gDescription] = Parse.parse();
Parse.gCode = gCode;
end
end
methods(Access = private)
function [Parse, gCode, gDescription] = parse(Parse)
%PARSE Parse LOCI code in specified file.
% Parse the specified file. Operations include extraction of
% (graphical) circuit representation, as well as the parts
% and their properties.
% Perform scanning
% Get the code
[Parse, gCode] = Parse.get_code();
% Aliases
[Parse, gCode] = Parse.get_alias(gCode);
% Description
[Parse, gDescription] = Parse.get_description(gCode);
% Remove comments
[Parse, gCode] = Parse.remove_comments(gCode);
% Translate everything to tokens
[Parse, gCode] = Parse.get_tokens(gCode);
% Check if code is well defined
% Check if all label are unique
Parse = Parse.check_labels(gCode);
% Check if identifier are well defined
Parse = Parse.check_identifiers(gCode);
% Check if properties are well defined
Parse = Parse.check_constants(gCode);
% Check if properties are well defined
Parse = Parse.check_properties(gCode);
end
function [Parse, gContent] = get_code(Parse)
%GET_CODE Read the code from the specified file
% Read everything from the file specified in filename. The
% file contains the programmcode of the circuit to build.
% More specifically, all used parts and their relationship
% are present.
gContent = {};
fid = fopen(Parse.filename);
line = fgetl(fid);
gContent{end+1} = line;
while ischar(line)
line = fgetl(fid);
gContent{end+1} = line;
end
gContent{end} = '';
fclose(fid);
% Remove leading empty spaces
for j = 1:size(gContent, 2)
gContent{j} = strtrim(gContent{j});
end
% Remove empty cells
gContent = gContent(~cellfun('isempty', gContent));
end
function [Parse, gContent] = get_alias(Parse, gContent)
%GET_ALIAS Evaluate aliases.
% Replace all aliases with their corresponding code.
warning('Parser: Aliases not supported!');
end
function [Parse, gDescription] = get_description(Parse, gContent)
%GET_DESCRIPTION Get the circuit description.
% Returns the circuit model later used as description (on
% chip) by the builder.
% The description is enclosed by:
% - DESCRIPTION_BEGIN: Beginning of the description
% - DESCRIPTION_END: End of the description
idx_start = 0;
idx_end = 0;
for j = 1:size(gContent, 2)
if startsWith(strtrim(gContent{j}), '# DESCRIPTION_BEGIN')
idx_start = j;
elseif startsWith(strtrim(gContent{j}), '# DESCRIPTION_END')
idx_end = j;
end
end
if idx_start ~= idx_end
gDescription = gContent(idx_start+1:idx_end-1);
else
gDescription = {};
end
end
function [Parse, gContent] = remove_comments(Parse, gContent)
%REMOVE_COMMENTS Remove all comments.
% All lines containing a comments are removed.
% Comment syntax: # <Comment>
for j = 1:size(gContent, 2)
if startsWith(strtrim(gContent{j}), '#')
gContent{j} = '';
end
end
% Remove empty cells
gContent = gContent(~cellfun('isempty', gContent));
end
function [Parse, gContent] = get_tokens(Parse, gContent)
%GET_TOKENS Generate tokens based on code.
% Each line of code is translated into tokens consisting of
% e.g names, properties for parts.
for j = 1:size(gContent, 2)
obj = split(gContent{j});
% Tokenize all parts
if contains(obj{1}, Parse.gParts)
newPart = containers.Map({'part', 'input', 'output', 'properties'}, ...
{obj{1}, obj{2}, obj{3}, obj(4:end)});
gContent{j} = newPart;
end
end
end
function Parse = check_properties(Parse, gContent)
%CHECK_PROPERTIES Check if properties make sense.
% Check for each part if the present properties do make sense
% and it is possible to place and generate the part this way.
% Check if input and output are set
for j = 1:size(gContent, 2)
if isa(gContent{j}, 'containers.Map')
in = gContent{j}('input');
out = gContent{j}('output');
if isempty(regexp(in, regexptranslate('wildcard', '(*)'))) || ...
isempty(regexp(out, regexptranslate('wildcard', '(*)')))
error('Parser: Ports are not set!');
end
end
end
end
function Parse = check_constants(Parse, gContent)
%CHECK_CONSTANTS Check if constants are well defined.
% Check for each identifier if present constants, in this
% case also strings, are well defined and conform to proper
% convention.
% Convention: default, property_name:<Value>, <Value>
for j = 1:size(gContent, 2)
if Parse.isa_part(gContent{j})
props = gContent{j}('properties');
for p = 1:size(props, 1)
% Check if property conforms to convent
if ~isempty(regexp(props{p}, regexptranslate('wildcard', '*:*'))) || ...
~isnan(str2double(props{p})) || ...
strcmp(props{p}, 'default') ~= 0 || ...
~isempty(str2num(props{p}))
break;
else
error(strcat('Parser: Property ', props{p}, ' does not conform to the convention!'));
end
end
end
end
end
function Parse = check_identifiers(Parse, gContent)
%CHECK_IDENTIFIERS Check if all identifiers exist.
% Check if all tokens are well defined and can be used.
% Check if all calls to labels do exist.
warning('Parser: Identifiers and tokens are not checked!');
end
function Parse = check_labels(Parse, gContent)
%CHECK_LABELS Check labels.
% Check if all labels present in the code are unique. This is
% necessary due to the possibility of referencing to labels
% instead of repeating code.
for j = 1:size(gContent, 2)
% Format of labels: _<Name>:
if Parse.isa_sym_label(gContent{j})
for k = j+1:size(gContent, 2)
% Check if label is unique
if strcmp(gContent{j}, gContent{k})
error(strcat('Parser: Label ', gContent{j}, ' is not unique!'));
break
end
end
end
end
end
function ret = isa_return(Parse, line)
%ISA_RETURN Check if line represents return statement.
% Check if the current instruction is the return statement.
% The return statement declares the end of the program.
ret = strcmp(line, 'ret');
end
function ret = isa_part(Parse, line)
%ISA_PART Check if line defines a part.
% All parts are represented by a map structure, and can thus
% be identified by it.
if isa(line, 'containers.Map')
ret = true;
else
ret = false;
end
end
function ret = isa_label(Parse, line)
%ISA_NUM_LABEL Check if line jumps to a label.
% Check if the passed line defines a call to a certain label.
% Such a call consists of the name of the label to jump to:
% _<Name>
if ~isa(line, 'containers.Map') && ...
strcmp(line(1), '_') && ~strcmp(line(end), ':')
ret = true;
else
ret = false;
end
end
function ret = isa_sym_label(Parse, line)
%ISA_SYM_LABEL Check if line is symbolic label.
% Check if the passed line defines a symbolic label. A
% symbolic label consists of a identifier followed by a
% colon: _<Name>:
if ~isa(line, 'containers.Map') && ...
strcmp(line(1), '_') && strcmp(line(end), ':')
ret = true;
else
ret = false;
end
end
end
end