forked from jamiepg1/valencia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
program.vala
1642 lines (1330 loc) · 52.7 KB
/
program.vala
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2009-2015 Yorba Foundation
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
using Gee;
namespace Valencia {
public class SymbolSet : Object {
// Since the set stores Symbols, but we actually want to hash their (name) strings, we must
// provide custom hash and equality functions
HashSet<Symbol> symbols =
new HashSet<Symbol>(Symbol.hash, Symbol.equal);
string name;
bool exact;
bool type;
bool constructor;
bool local_symbols;
public SymbolSet(string name, bool type, bool exact, bool constructor, bool local_symbols) {
this.name = exact ? name : name.down(); // case-insensitive matching
this.type = type;
this.exact = exact;
this.constructor = constructor;
this.local_symbols = local_symbols;
}
public SymbolSet.empty() {
name = "";
type = false;
exact = false;
constructor = false;
local_symbols = false;
}
void add_constructor(Symbol sym) {
Class c = sym as Class;
if (c != null) {
if (exact) {
Symbol? s = c.lookup_constructor();
if (s != null)
symbols.add(s);
} else {
// Recursively add subclass constructors to the set
foreach (Node n in c.members) {
Class subclass = n as Class;
if (subclass != null)
add_constructor(subclass);
else if (n is Constructor)
symbols.add((Symbol) n);
}
}
// Recursively add subclass constructors to the set
} else if (sym is Constructor) {
symbols.add(sym);
}
}
public bool add(Symbol sym) {
if (sym.name == null)
return false;
// Case-insensitive matching for inexact matching
if (exact) {
if (sym.name != name)
return false;
} else if (!sym.name.down().has_prefix(name)) {
return false;
}
if (type && sym as TypeSymbol == null)
return false;
if (constructor) {
add_constructor(sym);
// Don't add constructors to non-constructor sets
} else if (!(sym is Constructor))
symbols.add(sym);
return exact;
}
// Convenience function for getting the first element without having to use iterators.
// This is mostly for users expecting exact matches.
public Symbol? first() {
foreach (Symbol s in symbols)
return s;
return null;
}
public unowned HashSet<Symbol>? get_symbols() {
// It doesn't make sense to display the exact match of a partial search if there is only
// one symbol found that matches perfectly (only for autocomplete!)
if (symbols.size == 0 || (symbols.size == 1 && !exact && !local_symbols &&
first().name == name))
return null;
return symbols;
}
public string get_name() {
return name;
}
public Symbol? get_symbol(string name) {
foreach (Symbol symbol in symbols) {
if (symbol.name == name)
return symbol;
}
return null;
}
public bool local_symbols_only() {
return local_symbols;
}
}
public abstract class Node : Object {
public int start;
public int end;
Node(int start, int end) {
this.start = start;
this.end = end;
}
// Return all children which may possibly contain a scope.
public virtual ArrayList<Node>? children() { return null; }
protected static ArrayList<Node>? single_node(Node? n) {
if (n == null)
return null;
ArrayList<Node> a = new ArrayList<Node>();
a.add(n);
return a;
}
public Chain? find(Chain? parent, int pos) {
Chain c = parent;
Scope s = this as Scope;
if (s != null)
c = new Chain(s, parent); // link this scope in
ArrayList<Node> nodes = children();
if (nodes != null)
foreach (Node n in nodes)
if (n.start <= pos && pos <= n.end)
return n.find(c, pos);
return c;
}
public static bool lookup_in_array(ArrayList<Node> a, SymbolSet symbols) {
foreach (Node n in a) {
Symbol s = n as Symbol;
if (s != null && symbols.add(s))
return true;
}
return false;
}
public abstract void print(int level);
protected void do_print(int level, string s) {
stdout.printf("%s%s\n", string.nfill(level * 2, ' '), s);
}
}
public abstract class Symbol : Node {
public SourceFile source;
public string name; // symbol name, or null for a constructor
public Symbol(string? name, SourceFile source, int start, int end) {
base(start, end);
this.source = source;
this.name = name;
}
protected void print_name(int level, string s) {
do_print(level, s + " " + name);
}
public int name_length() {
int length = 0;
// Since unnamed constructors' names are null, just use the parent class' name
if (name == null) {
if (this is Constructor) {
Constructor c = (Constructor) this;
length = (int) c.parent.name.length;
}
} else {
length = (int) name.length;
}
return length;
}
public static uint hash(Symbol symbol) {
// Unnamed constructors always have null names, so hash their parent class' name
if (symbol.name == null) {
Constructor c = symbol as Constructor;
assert(c != null);
return c.parent.name.hash();
} else return symbol.name.hash();
}
public static bool equal(Symbol a_symbol, Symbol b_symbol) {
return a_symbol.name == b_symbol.name;
}
}
public interface Scope : Object {
// Adds all members not past the position specified by 'pos' inside this scope to 'symbols'
// (members meaning fields, methods, classes, enums, etc...)
public abstract bool lookup(SymbolSet symbols, int pos);
}
public abstract class TypeSymbol : Symbol {
public TypeSymbol(string? name, SourceFile source, int start, int end) {
base(name, source, start, end);
}
}
public abstract class Statement : Node {
public Statement(int start, int end) { base(start, end); }
public virtual bool defines_symbol(SymbolSet symbols) { return false; }
}
public abstract class Variable : Symbol {
public Expression type;
public Variable(Expression type, string name, SourceFile source, int start, int end) {
base(name, source, start, end);
this.type = type;
}
protected abstract string kind();
public override void print(int level) {
print_name(level, kind() + " " + type.to_string());
}
}
public class LocalVariable : Variable {
public LocalVariable(Expression type, string name, SourceFile source, int start, int end) {
base(type, name, source, start, end);
}
protected override string kind() { return "local"; }
}
public class DeclarationStatement : Statement {
public ArrayList<LocalVariable> variables;
public DeclarationStatement(ArrayList<LocalVariable> variables, int start, int end) {
base(start, end);
this.variables = variables;
}
public override bool defines_symbol(SymbolSet symbols) {
foreach (LocalVariable variable in variables)
if (symbols.add(variable))
return true;
return false;
}
public override void print(int level) {
foreach (LocalVariable variable in variables)
variable.print(level);
}
}
// The For class will handle both for and foreach statements
class For : Statement, Scope {
public DeclarationStatement declaration;
public Statement statement;
public For(DeclarationStatement declaration, Statement? statement, int start, int end) {
base(start, end);
this.declaration = declaration;
this.statement = statement;
}
public override ArrayList<Node>? children() { return single_node(statement); }
bool lookup(SymbolSet symbols, int pos) {
return declaration.defines_symbol(symbols);
}
protected override void print(int level) {
do_print(level, "foreach");
foreach (LocalVariable variable in declaration.variables) {
variable.print(level + 1);
if (statement != null)
statement.print(level + 1);
}
}
}
public class Chain : Object {
Scope scope;
Chain parent;
public Chain(Scope scope, Chain? parent) {
this.scope = scope;
this.parent = parent;
}
public void lookup(SymbolSet symbols, int pos) {
if (scope.lookup(symbols, pos))
return;
if (parent != null)
parent.lookup(symbols, pos);
}
// Returns the symbol of the first parent class it finds
public Symbol? lookup_this() {
if (parent == null)
return null;
if (parent.scope is Class) {
return (Symbol) parent.scope;
}
return parent.lookup_this();
}
// Returns the symbol of the base class of the parent class
public Symbol? lookup_base(SourceFile sf) {
Class? parent_class = (Class) lookup_this();
if (parent_class == null)
return null;
foreach (Expression base_name in parent_class.super) {
Symbol base_class = sf.resolve_type(base_name, parent_class.start - 1);
if (base_class != null && !(base_class is Interface)) {
return base_class;
}
}
return null;
}
}
public class Block : Statement, Scope {
public ArrayList<Statement> statements = new ArrayList<Statement>();
public override ArrayList<Node>? children() { return statements; }
public Block() {
base(0, 0); // caller will fill in start and end later
}
bool lookup(SymbolSet symbols, int pos) {
foreach (Statement s in statements) {
if (s.start > pos)
return false;
if (s.defines_symbol(symbols))
return true;
}
return false;
}
protected override void print(int level) {
do_print(level, "block");
foreach (Statement s in statements)
s.print(level + 1);
}
}
public class Parameter : Variable {
public Parameter(Expression type, string name, SourceFile source, int start, int end) {
base(type, name, source, start, end);
}
protected override string kind() { return "parameter"; }
}
// a construct block
public class Construct : Node {
public Block body;
public Construct(Block body, int start, int end) {
base(start, end);
this.body = body;
}
public override ArrayList<Node>? children() {
return single_node(body);
}
public override void print(int level) {
do_print(level, "construct");
if (body != null)
body.print(level + 1);
}
}
public class Method : Symbol, Scope {
public ArrayList<Parameter> parameters = new ArrayList<Parameter>();
public Expression return_type;
public Block body;
string prototype = "";
public Method(string? name, Expression? return_type, SourceFile source) {
base(name, source, 0, 0);
this.return_type = return_type;
}
public override ArrayList<Node>? children() { return single_node(body); }
bool lookup(SymbolSet symbols, int pos) {
return Node.lookup_in_array(parameters, symbols);
}
protected virtual void print_type(int level) {
print_name(level, "method");
}
public override void print(int level) {
print_type(level);
foreach (Parameter p in parameters)
p.print(level + 1);
if (body != null)
body.print(level + 1);
}
public void update_prototype(string proto) {
prototype = proto;
prototype.chomp();
// Clean up newlines and remove extra spaces
if (prototype.contains("\n")) {
string[] split_lines = prototype.split("\n");
prototype = "";
for (int i = 0; split_lines[i] != null; ++i) {
weak string str = split_lines[i];
str.strip();
prototype += str;
if (split_lines[i + 1] != null)
prototype += " ";
}
}
}
public string to_string() {
return prototype;
}
}
// We use the name "VSignal" to avoid a name conflict with GLib.Signal.
public class VSignal : Method {
public VSignal(string? name, Expression return_type, SourceFile source) {
base(name, return_type, source);
}
}
public class Delegate : Method {
public Delegate(string? name, Expression return_type, SourceFile source) {
base(name, return_type, source);
}
}
public class Constructor : Method {
public weak Class parent;
public Constructor(string? unqualified_name, Class parent, SourceFile source) {
base(unqualified_name, null, source);
this.parent = parent;
}
public override void print_type(int level) {
do_print(level, "constructor");
}
}
public class Field : Variable {
public Field(Expression type, string name, SourceFile source, int start, int end) {
base(type, name, source, start, end);
}
protected override string kind() { return "field"; }
}
public class Property : Variable {
// A Block containing property getters and/or setters.
public Block body;
public Property(Expression type, string name, SourceFile source, int start, int end) {
base(type, name, source, start, end);
}
public override ArrayList<Node>? children() {
return single_node(body);
}
protected override string kind() { return "property"; }
public override void print(int level) {
base.print(level);
body.print(level + 1);
}
}
// a class, struct, interface or enum
public class Class : TypeSymbol, Scope {
public ArrayList<Expression> super = new ArrayList<Expression>();
public ArrayList<Node> members = new ArrayList<Node>();
weak Class enclosing_class;
public Class(string name, SourceFile source, Class? enclosing_class) {
base(name, source, 0, 0);
this.enclosing_class = enclosing_class;
}
public override ArrayList<Node>? children() { return members; }
public Symbol? lookup_constructor() {
foreach (Node n in members) {
Constructor c = n as Constructor;
// Don't accept named constructors
if (c != null && c.name == null) {
return (Symbol) c;
}
}
return null;
}
bool lookup1(SymbolSet symbols, HashSet<Class> seen) {
if (Node.lookup_in_array(members, symbols))
return true;
// Make sure we don't run into an infinite loop if a user makes this mistake:
// class Foo : Foo { ...
seen.add(this);
// look in superclasses
foreach (Expression s in super) {
// We look up the parent class in the scope at (start - 1); that excludes
// this class itself (but will include the containing sourcefile,
// even if start == 0.)
Class c = source.resolve_type(s, start - 1) as Class;
if (c != null && !seen.contains(c)) {
if (c.lookup1(symbols, seen))
return true;
}
}
return false;
}
bool lookup(SymbolSet symbols, int pos) {
return lookup1(symbols, new HashSet<Class>());
}
public override void print(int level) {
StringBuilder sb = new StringBuilder();
sb.append("class " + name);
for (int i = 0 ; i < super.size ; ++i) {
sb.append(i == 0 ? " : " : ", ");
sb.append(super.get(i).to_string());
}
do_print(level, sb.str);
foreach (Node n in members)
n.print(level + 1);
}
public string to_string() {
return (enclosing_class != null) ? enclosing_class.to_string() + "." + name : name;
}
}
public class Interface : Class {
public Interface(string name, SourceFile source, Class? enclosing_class) {
base(name, source, enclosing_class);
}
}
// A Namespace is a TypeSymbol since namespaces can be used in type names.
public class Namespace : TypeSymbol, Scope {
public string full_name;
public Namespace(string? name, string? full_name, SourceFile source) {
base(name, source, 0, 0);
this.full_name = full_name;
}
public ArrayList<Symbol> symbols = new ArrayList<Symbol>();
public override ArrayList<Node>? children() { return symbols; }
public bool lookup(SymbolSet symbols, int pos) {
return source.program.lookup_in_namespace(full_name, symbols);
}
public bool lookup1(SymbolSet symbols) {
return Node.lookup_in_array(this.symbols, symbols);
}
public void lookup_all_toplevel_symbols(SymbolSet symbols) {
foreach (Symbol s in this.symbols) {
if (s is Namespace) {
Namespace n = (Namespace) s;
n.lookup_all_toplevel_symbols(symbols);
} else {
symbols.add(s);
}
}
}
public override void print(int level) {
print_name(level, "namespace");
foreach (Symbol s in symbols)
s.print(level + 1);
}
}
public class SourceFile : Node, Scope {
public weak Program program;
public string filename;
ArrayList<string> using_namespaces = new ArrayList<string>();
public ArrayList<Namespace> namespaces = new ArrayList<Namespace>();
public Namespace top;
public SourceFile(Program? program, string filename) {
base(0, 0);
this.program = program;
this.filename = filename;
alloc_top();
}
void alloc_top() {
top = new Namespace(null, null, this);
namespaces.add(top);
using_namespaces.add("GLib");
}
public void clear() {
using_namespaces.clear();
namespaces.clear();
alloc_top();
}
public override ArrayList<Node>? children() { return single_node(top); }
public void add_using_namespace(string name) {
// Make sure there isn't a duplicate, since GLib is always added
if (name == "GLib")
return;
using_namespaces.add(name);
}
bool lookup(SymbolSet symbols, int pos) {
foreach (string ns in using_namespaces) {
if (program.lookup_in_namespace(ns, symbols))
return true;
}
return false;
}
public bool lookup_in_namespace(string? namespace_name, SymbolSet symbols) {
foreach (Namespace n in namespaces)
if (n.full_name == namespace_name) {
if (symbols.local_symbols_only())
n.lookup_all_toplevel_symbols(symbols);
else if (n.lookup1(symbols))
return true;
}
return false;
}
public SymbolSet resolve_non_compound(Expression name, Chain chain, int pos, bool find_type,
bool exact, bool constructor, bool local_symbols) {
Symbol s;
SymbolSet symbols;
if (name is This) {
s = chain.lookup_this();
} else if (name is Base) {
s = chain.lookup_base(this);
} else if (name is MethodCall) {
// First find the method symbol... (doesn't support constructors yet)
MethodCall method_call = (MethodCall) name;
symbols = resolve1(method_call.method, chain, pos, false, exact, false, local_symbols);
s = symbols.first();
Constructor c = s as Constructor;
if (c != null)
s = c.parent;
else {
Method m = s as Method;
if (m != null)
// find the return type symbol of the method
return resolve1(m.return_type, find(null, m.start), m.start, true, exact, false, local_symbols);
return new SymbolSet.empty();
}
} else if (name is Id) {
Id id = (Id) name;
symbols = new SymbolSet(id.name, find_type, exact, constructor, local_symbols);
chain.lookup(symbols, pos);
return symbols;
} else { // name is New
New n = (New) name;
return resolve1(n.class_name, chain, pos, find_type, exact, true, local_symbols);
}
if (s != null) {
symbols = new SymbolSet(s.name, find_type, true, constructor, local_symbols);
symbols.add(s);
} else symbols = new SymbolSet.empty(); // return an "empty" set
return symbols;
}
public SymbolSet resolve1(Expression name, Chain chain, int pos, bool find_type, bool exact,
bool constructor, bool local_symbols) {
if (!(name is CompoundExpression)) {
return resolve_non_compound(name, chain, pos, find_type, exact, constructor, local_symbols);
}
// The basename of a qualified name is always going to be an exact match, and never a
// constructor
CompoundExpression compound = (CompoundExpression) name;
SymbolSet left_set = resolve1(compound.left, chain, pos, find_type, true, false, local_symbols);
Symbol left = left_set.first();
if (!find_type) {
Variable v = left as Variable;
if (v != null) {
left = v.source.resolve_type(v.type, v.start);
}
}
Scope scope = left as Scope;
// It doesn't make sense to be looking up members of a method as a qualified name
if (scope is Method)
return new SymbolSet.empty();
SymbolSet symbols = new SymbolSet(compound.right, find_type, exact, constructor, local_symbols);
if (scope != null)
scope.lookup(symbols, 0);
return symbols;
}
public Symbol? resolve(Expression name, int pos, bool constructor) {
SymbolSet symbols = resolve1(name, find(null, pos), pos, false, true, constructor, false);
return symbols.first();
}
public Symbol? resolve_type(Expression type, int pos) {
SymbolSet symbols = resolve1(type, find(null, pos), 0, true, true, false, false);
return symbols.first();
}
public SymbolSet resolve_prefix(Expression prefix, int pos, bool constructor) {
return resolve1(prefix, find(null, pos), pos, false, false, constructor, false);
}
public SymbolSet resolve_all_locals(Expression prefix, int pos) {
return resolve1(prefix, find(null, pos), pos, false, false, false, true);
}
public Symbol? resolve_local(Expression name, int pos) {
SymbolSet symbols = resolve1(name, find(null, pos), pos, false, true, false, true);
return symbols.first();
}
public override void print(int level) {
top.print(level);
}
}
public class ErrorInfo : Object {
public string filename;
public string start_line;
public string start_char;
public string end_line;
public string end_char;
}
public class ErrorPair : Object {
public Gtk.TextMark document_pane_error;
public Gtk.TextMark build_pane_error;
public ErrorInfo error_info;
public ErrorPair(Gtk.TextMark document_err, Gtk.TextMark build_err, ErrorInfo err_info) {
document_pane_error = document_err;
build_pane_error = build_err;
error_info = err_info;
}
}
public class ErrorList : Object {
public Gee.ArrayList<ErrorPair> errors;
public int error_index;
public ErrorList() {
errors = new Gee.ArrayList<ErrorPair>();
error_index = -1;
}
}
public class Makefile : Object {
public string path;
public string relative_binary_run_path;
bool regex_parse(GLib.DataInputStream datastream) {
Regex program_regex, rule_regex, root_regex;
try {
root_regex = new Regex("""^\s*BUILD_ROOT\s*=\s*1\s*$""");
program_regex = new Regex("""^\s*PROGRAM\s*=\s*(\S+)\s*$""");
rule_regex = new Regex("""^ *([^: ]+) *:""");
} catch (RegexError e) {
GLib.warning("A RegexError occured when creating a new regular expression.\n");
return false; // TODO: report error
}
bool rule_matched = false;
bool program_matched = false;
bool root_matched = false;
MatchInfo info;
// this line is necessary because of a vala compiler bug that thinks info is uninitialized
// within the block: if (!program_matched && program_regex.match(line, 0, out info)) {
program_regex.match(" ", 0, out info);
while (true) {
size_t length;
string line;
try {
line = datastream.read_line(out length, null);
} catch (GLib.Error err) {
GLib.warning("An unexpected error occurred while parsing the Makefile.\n");
return false;
}
// The end of the document was reached, ending...
if (line == null)
break;
if (!program_matched && program_regex.match(line, 0, out info)) {
// The 'PROGRAM = xyz' regex can be matched anywhere in the makefile, where the rule
// regex can only be matched the first time.
relative_binary_run_path = info.fetch(1);
program_matched = true;
} else if (!rule_matched && !program_matched && rule_regex.match(line, 0, out info)) {
rule_matched = true;
relative_binary_run_path = info.fetch(1);
} else if (!root_matched && root_regex.match(line, 0, out info)) {
root_matched = true;
}
if (program_matched && root_matched)
break;
}
return root_matched;
}
// Return: true if current directory will be root, false if not
public bool parse(GLib.File makefile) {
GLib.FileInputStream stream;
try {
stream = makefile.read(null);
} catch (GLib.Error err) {
GLib.warning("Unable to open %s for parsing.\n", path);
return false;
}
GLib.DataInputStream datastream = new GLib.DataInputStream(stream);
return regex_parse(datastream);
}
public void reparse() {
if (path == null)
return;
GLib.File makefile = GLib.File.new_for_path(path);
parse(makefile);
}
public void reset_paths() {
path = null;
relative_binary_run_path = null;
}
}
public class ConfigurationFile : Object {
weak Program parent_program;
const string version_keyword = "version";
const string version = "1";
const string build_command_keyword = "build_command";
const string clean_command_keyword = "clean_command";
const string default_build_command = "make";
const string default_clean_command = "make clean";
const string pkg_blacklist_keyword = "pkg_blacklist";
const string default_pkg_blacklist = "";
string build_command;
string clean_command;
string pkg_blacklist;
string[]? blacklisted_vapis = null;
enum MatchValue {
MATCHED,
UNMATCHED,
ERROR
}
public ConfigurationFile(Program parent_program) {
this.parent_program = parent_program;
build_command = null;
clean_command = null;
}
string get_file_path() {
return Path.build_filename(parent_program.get_top_directory(), ".valencia");
}
void load() {
string file_path = get_file_path();
if (!FileUtils.test(file_path, FileTest.EXISTS))
return;
string contents;
try {
FileUtils.get_contents(file_path, out contents);
} catch (FileError e) {
GLib.warning("Problem while trying to read %s\n", file_path);
return;
}
Regex config_regex;
try {
// Match something like: "word_group = value"
config_regex = new Regex("""^\s*([^\s]+)\s*=\s*(.+)\s*$""");
} catch (RegexError e) {
GLib.warning("Problem creating a regex to parse the config file\n");
return;
}
string[] lines = contents.split("\n");
bool matched_version = false;
bool matched_build = false;
bool matched_clean = false;
foreach (string line in lines) {
// Ignore lines with whitespace
line.chomp();
if (line == "")
continue;
MatchInfo match_info;
if (!config_regex.match(line, 0, out match_info)) {
warning("Incorrect file format, ignoring...\n");
return;
}
string match1 = match_info.fetch(1);
string match2 = match_info.fetch(2);
// Only match the version on the first line with text, any other line is a parse error
if (!matched_build && !matched_clean && match1 == version_keyword && match2 == version) {
matched_version = true;
continue;
} else if (!matched_version) {
warning("Mismatched config file version, ignoring...\n");
return;
}
if (match1 == build_command_keyword && match2 != null && build_command == null)
build_command = match2;
else if (match1 == clean_command_keyword && match2 != null && clean_command == null)
clean_command = match2;
else if (match1 == pkg_blacklist_keyword && match2 != null && pkg_blacklist == null)
pkg_blacklist = match2;
else {
warning("Incorrect file format, ignoring...\n");
return;
}
}
}
public string? get_build_command() {
if (build_command == null)
load();