summaryrefslogtreecommitdiffstats
path: root/src/Parse.vala
blob: 79b4cea741f4952380807a0263cd29c786465f3e (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
namespace Eva {
  internal static Term? parse(string str, ...) {
    va_list va = va_list();
    string s = str.chomp();
    
    if(s.length == 0)
      return null;
    
    Term? ret = parse_term(ref s, ref va);
    
    if(s.length != 0)
      return null;
    else
      return ret;
  }
  
  private static Term? parse_term(ref string str, ref va_list va) {
    str = str.chug();
    unichar c = str.get_char();
    
    if(c.isdigit() || c == '-') {
      return parse_number(ref str);
    }
    else if(c.islower() ||  c == '\'') {
      return parse_atom(ref str);
    }
    else if(c == '"') {
      return parse_string(ref str);
    }
    else if(c.isupper() ||  c == '_') {
      return parse_var(ref str);
    }
    else if(c == '<') {
      return parse_binary(ref str);
    }
    else if(c == '{') {
      return parse_tuple(ref str, ref va);
    }
    else if(c == '[') {
      return parse_list(ref str, ref va);
    }
    else if(c == '~') {
      return get_va(ref str, ref va);
    }
    else {
      return null;
    }
  }
  
  private static Term? parse_number(ref string str) {
    bool positive = true;
    bool fractional = false;
    
    unowned string s = str;
    
    if(s.get_char() == '-') {
      positive = false;
      s = s.next_char();
      
      if(s.length == 0 || !s.get_char().isdigit())
        return null;
    }
    
    if(s.get_char() == '.')
      return null;
    
    while(s.length > 0) {
      unichar c = s.get_char();
      
      if(c == '.') {
        if(fractional)
          return null;
        
        fractional = true;
        
        s = s.next_char();
        if(s.length == 0 || !s.get_char().isdigit())
          return null;
          
        continue;
      }
      
      if(!c.isdigit())
        break;
      
      s = s.next_char();
    }
    
    string number = str.substring(0, str.pointer_to_offset(s));
    str = s;
    
    if(fractional) {
      return new Double(number.to_double());
    }
    else if(positive) {
      return new UInt(number.to_ulong());
    }
    else {
      return new Int(number.to_long());
    }
  }
  
  private static Term? parse_atom(ref string str) {
    if(str.get_char() == '\'') {
      string atom = "";
      unowned string s = str.next_char();
      
      while(s.length > 0) {
        unichar c = s.get_char();
        
        if(c == '\'')
          break;
        
        if(c == '\\') {
          s = s.next_char();
          if(s.length == 0)
            return null;
          c = s.get_char();
        }
        
        if(c > 255) {
          return null;
        }
        
        string buf = string.nfill(6, 0);
        c.to_utf8(buf);
        atom += buf;
        
        s = s.next_char();
      }
      
      if(s.length == 0)
        return null;
      
      Term ret = new Atom(atom);
      str = s.next_char();
      
      return ret;
    }
    else {
      unowned string s = str;
      
      while(s.length > 0) {
        unichar c = s.get_char();
        
        if(c != '_' && !c.isalnum()) {
          break;
        }
        
        s = s.next_char();
      }
      
      Term ret = new Atom(str.substring(0, str.pointer_to_offset(s)));
      str = s;
      
      return ret;
    }
  }
  
  private static Term? parse_string(ref string str) {
    string ret = do_parse_string(ref str);
    if(ret == null)
      return null;
    
    return new String(ret);
  }
  
  private static string? do_parse_string(ref string str) {
    if(str.get_char() != '"') {
      return null;
    }
    
    string retstr = "";
    unowned string s = str.next_char();
      
    while(s.length > 0) {
      unichar c = s.get_char();
        
      if(c == '\"')
        break;
        
      if(c == '\\') {
        s = s.next_char();
        if(s.length == 0)
          return null;
        c = s.get_char();
      }
        
      string buf = string.nfill(6, 0);
      c.to_utf8(buf);
      retstr += buf;
        
      s = s.next_char();
    }
      
    if(s.length == 0)
      return null;
      
    str = s.next_char();
      
    return retstr;
  }
  
  private static Term? parse_var(ref string str) {
    unowned string s = str;
      
    while(s.length > 0) {
      unichar c = s.get_char();
      
      if(c != '_' && !c.isalnum()) {
        break;
      }
        
      s = s.next_char();
    }
      
    Term ret = new Var(str.substring(0, str.pointer_to_offset(s)));
    str = s;
      
    return ret;
  }
  
  private static Term? parse_tuple(ref string str, ref va_list va) {
    Gee.ArrayList<Term> list = new Gee.ArrayList<Term>();
    
    if(str.get_char() != '{')
      return null;
    
    unowned string s = str.next_char();
    
    while(s.length > 0 && s.get_char().isspace()) {
      s = s.next_char();
    }
    
    if(s.length == 0)
      return null;
    
    if(s.get_char() == '}') {
      str = s.next_char();
      return new Tuple({});
    }
    
    while(s.length > 0) {
      if(s.get_char().isspace()) {
        s = s.next_char();
        continue;
      }
      
      str = s;
      Term t = parse_term(ref str, ref va);
      if(t == null)
        return null;
      list.add(t);
      
      s = str;
      
      while(s.length > 0 && s.get_char().isspace()) {
        s = s.next_char();
      }
      
      unichar c = s.get_char();
      if(c == '}') {
        str = s.next_char();
        Term[] terms = list.to_array();
        return new Tuple(terms);
      }
      else if(c != ',') {
        return null;
      }
      else {
        s = s.next_char();
      }
    }
    
    return null;
  }
  
  private static Term? parse_list(ref string str, ref va_list va) {
    if(str.get_char() != '[')
      return null;
    
    str = str.next_char();
    return parse_list_tail(ref str, ref va);
  }
  
  private static Term? parse_list_tail(ref string str, ref va_list va) {
    str = str.chug();
    
    if(str.length == 0)
      return null;
    
    if(str.get_char() == ']') {
      str = str.next_char();
      return List.empty;
    }
    
    Term head = parse_term(ref str, ref va);
    if(head == null)
      return null;
    
    str = str.chug();
    if(str.length == 0)
      return null;
    unichar c = str.get_char();
    str = str.next_char();
    
    switch(c) {
    case ']':
      return new Cons(head);
    case ',':
      return new Cons(head, parse_list_tail(ref str, ref va));
    case '|':
      Term ret = new Cons(head, parse_term(ref str, ref va));
      
      str = str.chug();
      if(str.length == 0 || str.get_char() != ']')
        return null;
      str = str.next_char();
      
      return ret;
    default:
      return null;
    }
  }
  
  private static Term? parse_binary(ref string str) {
    if(str.length < 4)
      return null;
    
    if(str[0:2] != "<<")
      return null;
    
    str = str.offset(2).chug();
    
    if(str[0:2] == ">>") {
      str = str.offset(2);
      return new Binary({});
    }
    
    if(str.get_char() == '"') {
      string ret = do_parse_string(ref str);
      if(ret == null)
        return null;
      
      str = str.chug();
      
      if(str.length < 2 || str[0:2] != ">>")
        return null;
      
      str = str.offset(2);
      char[] binary = string_to_binary(ret);
      return new Binary(binary);
    }
    else {
      Gee.ArrayList<char> data = new Gee.ArrayList<char>();
      
      while(str.length > 0) {
        unichar c = str.get_char();
        
        if(c != '-' && !c.isdigit())
          return null;
        
        unowned string beg = str;
        unowned string end = str.next_char();
        
        while(end.length > 0 && end.get_char().isdigit())
          end = end.next_char();
        
        data.add((char)beg.substring(0, beg.pointer_to_offset(end)).to_int());
        
        str = end.chug();
        
        if(str.length >= 2 && str[0:2] == ">>") {
          str = str.offset(2);
          
          // Workaround for libgee to_array bug 597737
          char[] array = new char[data.size];
          int index = 0;
          foreach(char i in data) {
            array[index++] = i;
          }
          
          return new Binary(array);
        }
        else if(str.length != 0 && str.get_char() == ',') {
          str = str.next_char().chug();
        }
        else {
          return null;
        }
      }
      
      return null;
    }
  }
  
  private static Term? get_va(ref string str, ref va_list va) {
    if(str.get_char() != '~')
      return null;
    
    if(str.length < 2)
      return null;
    
    unichar c = str[1];
    str = str.offset(2);
    
    switch(c) {
    case 'a':
      return new Atom(va.arg<string>());
    case 's':
      return new String(va.arg<string>());
    case 'i':
      return new Int(va.arg<int>());
    case 'l':
      return new Int(va.arg<long>());
    case 'u':
      return new UInt(va.arg<ulong>());
    case 'f':
    case 'd':
      return new Double(va.arg<double>());
    case 'w':
      return va.arg<Term>();
    default:
      return null;
    }
  }
}