forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppScope.cxx
More file actions
3469 lines (3034 loc) · 110 KB
/
ppScope.cxx
File metadata and controls
3469 lines (3034 loc) · 110 KB
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
// Filename: ppScope.cxx
// Created by: drose (25Sep00)
//
////////////////////////////////////////////////////////////////////
#include "ppremake.h"
#include "ppScope.h"
#include "ppNamedScopes.h"
#include "ppFilenamePattern.h"
#include "ppDirectory.h"
#include "ppSubroutine.h"
#include "ppCommandFile.h"
#include "ppDependableFile.h"
#include "ppMain.h"
#include "tokenize.h"
#include "filename.h"
#include "dSearchPath.h"
#include "globPattern.h"
#include "md5.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <stdlib.h>
#include <algorithm>
#include <ctype.h>
#include <sys/stat.h>
#include <stdio.h> // for perror() and sprintf().
#include <errno.h>
#include <signal.h>
#include <assert.h>
#ifdef WIN32_VC
#include <windows.h> // for GetFileAttributes()
#endif // WIN32_VC
static const string variable_patsubst(VARIABLE_PATSUBST);
PPScope::MapVariableDefinition PPScope::_null_map_def;
PPScope::ScopeStack PPScope::_scope_stack;
////////////////////////////////////////////////////////////////////
// Function: PPScope::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
PPScope::
PPScope(PPNamedScopes *named_scopes) :
_named_scopes(named_scopes)
{
_directory = (PPDirectory *)NULL;
_parent_scope = (PPScope *)NULL;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_named_scopes
// Access: Public
// Description: Returns a pointer to the PPNamedScopes collection
// associated with this scope. This pointer could be
// NULL.
////////////////////////////////////////////////////////////////////
PPNamedScopes *PPScope::
get_named_scopes() {
return _named_scopes;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::set_parent
// Access: Public
// Description: Sets a static parent scope to this scope. When a
// variable reference is undefined in this scope, it
// will search first up the static parent chain before
// it searches the dynamic scope stack.
////////////////////////////////////////////////////////////////////
void PPScope::
set_parent(PPScope *parent) {
_parent_scope = parent;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_parent
// Access: Public
// Description: Returns the static parent scope to this scope, if
// any, or NULL if the static parent has not been set.
// See set_parent().
////////////////////////////////////////////////////////////////////
PPScope *PPScope::
get_parent() {
return _parent_scope;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::define_variable
// Access: Public
// Description: Makes a new variable definition. If the variable
// does not already exist in this scope, a new variable
// is created, possibly shadowing a variable declaration
// in some parent scope.
////////////////////////////////////////////////////////////////////
void PPScope::
define_variable(const string &varname, const string &definition) {
_variables[varname] = definition;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::set_variable
// Access: Public
// Description: Changes the definition of an already-existing
// variable. The variable is changed in whichever scope
// it is defined. Returns false if the variable has not
// been defined.
////////////////////////////////////////////////////////////////////
bool PPScope::
set_variable(const string &varname, const string &definition) {
if (p_set_variable(varname, definition)) {
return true;
}
// Check the scopes on the stack for the variable definition.
ScopeStack::reverse_iterator si;
for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
if ((*si)->p_set_variable(varname, definition)) {
return true;
}
}
// If the variable isn't defined, we check the environment.
const char *env = getenv(varname.c_str());
if (env != (const char *)NULL) {
// It is defined in the environment; thus, it is implicitly
// defined here at the global scope: the bottom of the stack.
PPScope *bottom = this;
if (!_scope_stack.empty()) {
bottom = _scope_stack.front();
}
bottom->define_variable(varname, definition);
return true;
}
// The variable isn't defined anywhere. Too bad.
return false;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::define_map_variable
// Access: Public
// Description: Makes a new map variable definition. This defines a
// new variable that can be used as a function to
// retrieve variables from within a named scope, based
// on a particular key variable.
//
// In this variant of define_map_variable(), the
// definition is a string of the form
// key_varname(scope_names).
////////////////////////////////////////////////////////////////////
void PPScope::
define_map_variable(const string &varname, const string &definition) {
size_t p = definition.find(VARIABLE_OPEN_NESTED);
if (p != string::npos && definition[definition.length() - 1] == VARIABLE_CLOSE_NESTED) {
size_t q = definition.length() - 1;
string scope_names = definition.substr(p + 1, q - (p + 1));
string key_varname = definition.substr(0, p);
define_map_variable(varname, key_varname, scope_names);
} else {
// No scoping; not really a map variable.
define_map_variable(varname, definition, "");
}
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::define_map_variable
// Access: Public
// Description: Makes a new map variable definition. This defines a
// new variable that can be used as a function to
// retrieve variables from within a named scope, based
// on a particular key variable.
////////////////////////////////////////////////////////////////////
void PPScope::
define_map_variable(const string &varname, const string &key_varname,
const string &scope_names) {
MapVariableDefinition &def = _map_variables[varname];
def.clear();
define_variable(varname, "");
if (_named_scopes == (PPNamedScopes *)NULL) {
return;
}
if (key_varname.empty()) {
return;
}
vector<string> names;
tokenize_whitespace(scope_names, names);
// Get all of the named scopes.
PPNamedScopes::Scopes scopes;
vector<string>::const_iterator ni;
for (ni = names.begin(); ni != names.end(); ++ni) {
const string &name = (*ni);
_named_scopes->get_scopes(name, scopes);
}
if (scopes.empty()) {
return;
}
// Now go through the scopes and build up the results.
vector<string> results;
PPNamedScopes::Scopes::const_iterator si;
for (si = scopes.begin(); si != scopes.end(); ++si) {
PPScope *scope = (*si);
string key_string = scope->expand_variable(key_varname);
vector<string> keys;
tokenize_whitespace(key_string, keys);
if (!keys.empty()) {
vector<string>::const_iterator ki;
results.insert(results.end(), keys.begin(), keys.end());
for (ki = keys.begin(); ki != keys.end(); ++ki) {
def[*ki] = scope;
}
}
}
// Also define a traditional variable along with the map variable.
define_variable(varname, repaste(results, " "));
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::add_to_map_variable
// Access: Public
// Description: Adds a new key/scope pair to a previous map variable
// definition.
////////////////////////////////////////////////////////////////////
void PPScope::
add_to_map_variable(const string &varname, const string &key,
PPScope *scope) {
MapVariableDefinition &def = find_map_variable(varname);
if (&def == &_null_map_def) {
cerr << "Warning: undefined map variable: " << varname << "\n";
return;
}
def[key] = scope;
// We need to do all this work to define the traditional expansion.
// Maybe not a great idea.
vector<string> results;
MapVariableDefinition::const_iterator di;
for (di = def.begin(); di != def.end(); ++di) {
results.push_back((*di).first);
}
set_variable(varname, repaste(results, " "));
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::define_formals
// Access: Public
// Description: Supplies values to a slew of variables at once,
// typically to define actual values for a list of
// formal parameters to a user-defined subroutine or
// function.
//
// Formals is a vector of variable names to be defined,
// and actuals is a comma-separated list of expressions
// to be substituted in, one-per-one. The
// subroutine_name is used only for error reporting.
////////////////////////////////////////////////////////////////////
void PPScope::
define_formals(const string &subroutine_name,
const vector<string> &formals, const string &actuals) {
vector<string> actual_words;
tokenize_params(actuals, actual_words, true);
if (actual_words.size() < formals.size()) {
cerr << "Warning: not all parameters defined for " << subroutine_name
<< ": " << actuals << "\n";
} else if (actual_words.size() > formals.size()) {
cerr << "Warning: more parameters defined for " << subroutine_name
<< " than actually exist: " << actuals << "\n";
}
for (int i = 0; i < (int)formals.size(); i++) {
if (i < (int)actual_words.size()) {
define_variable(formals[i], actual_words[i]);
} else {
define_variable(formals[i], string());
}
}
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_variable
// Access: Public
// Description: Returns the variable definition associated with the
// indicated variable name.
////////////////////////////////////////////////////////////////////
string PPScope::
get_variable(const string &varname) {
// Is it a user-defined function?
const PPSubroutine *sub = PPSubroutine::get_func(varname);
if (sub != (const PPSubroutine *)NULL) {
return expand_function(varname, sub, string());
}
// cerr << "getvar arg is: '" << varname << "'" << endl;
string result;
if (p_get_variable(varname, result)) {
return result;
}
// Check the scopes on the stack for the variable definition.
ScopeStack::reverse_iterator si;
for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
if ((*si)->p_get_variable(varname, result)) {
return result;
}
}
// If the variable isn't defined, we check the environment.
const char *env = getenv(varname.c_str());
if (env != (const char *)NULL) {
return env;
}
// It's not defined anywhere, so it's implicitly empty.
return string();
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::expand_defined
// Access: Private
// Description: Expands the "defined" function variable. Code mimics get_variable()
////////////////////////////////////////////////////////////////////
string PPScope::
expand_defined(const string ¶ms) {
// Split the string up into tokens based on the commas.
vector<string> tokens;
tokenize_params(params, tokens, true);
if (tokens.size() != 1) {
cerr << "error: defined requires one parameter.\n";
errors_occurred = true;
return string();
}
string varname = tokens[0];
string falsestr;
string truestr = "1";
// Is it a user-defined function?
const PPSubroutine *sub = PPSubroutine::get_func(varname);
string nullstr;
if (sub != (const PPSubroutine *)NULL) {
if(nullstr != expand_function(varname, sub, string())) {
return truestr;
}
}
string result;
if (p_get_variable(varname, result)) {
return truestr;
}
// Check the scopes on the stack for the variable definition.
ScopeStack::reverse_iterator si;
for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
if ((*si)->p_get_variable(varname, result)) {
return truestr;
}
}
// If the variable isn't defined, we check the environment.
const char *env = getenv(varname.c_str());
if (env != (const char *)NULL) {
return truestr;
}
// It's not defined anywhere, so it's implicitly empty.
return falsestr;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::expand_variable
// Access: Public
// Description: Similar to get_variable(), except the variable
// definition is in turn expanded.
////////////////////////////////////////////////////////////////////
string PPScope::
expand_variable(const string &varname) {
return expand_string(get_variable(varname));
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::find_map_variable
// Access: Public
// Description: Looks for the map variable definition in this scope
// or some ancestor scope. Returns the map variable
// definition if it is found, or _null_map_def if it is
// not.
////////////////////////////////////////////////////////////////////
PPScope::MapVariableDefinition &PPScope::
find_map_variable(const string &varname) {
MapVariableDefinition &def = p_find_map_variable(varname);
if (&def != &_null_map_def) {
return def;
}
// No such map variable. Check the stack.
ScopeStack::reverse_iterator si;
for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
MapVariableDefinition &def = (*si)->p_find_map_variable(varname);
if (&def != &_null_map_def) {
return def;
}
}
// Nada.
return _null_map_def;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_directory
// Access: Public
// Description: Returns the directory level associated with this
// scope, if any, or with the nearest parent to this
// scope.
////////////////////////////////////////////////////////////////////
PPDirectory *PPScope::
get_directory() {
if (_directory != (PPDirectory *)NULL) {
return _directory;
}
// Check the stack.
ScopeStack::reverse_iterator si;
for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
if ((*si)->_directory != (PPDirectory *)NULL) {
return (*si)->_directory;
}
}
return (PPDirectory *)NULL;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::set_directory
// Access: Public
// Description: Associates this scope with the indicated directory
// level. Typically this is done when definition a
// scope for a particular source file which exists at a
// known directory level.
////////////////////////////////////////////////////////////////////
void PPScope::
set_directory(PPDirectory *directory) {
_directory = directory;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::expand_string
// Access: Public
// Description: Expands out all the variable references in the given
// string. Variables are expanded recursively; that is,
// if a variable expansion includes a reference to
// another variable name, the second variable name is
// expanded. However, cyclical references are not
// expanded.
////////////////////////////////////////////////////////////////////
string PPScope::
expand_string(const string &str) {
string result = r_expand_string(str, (ExpandedVariable *)NULL);
if (debug_expansions > 0 && str != result) {
// Look for the str in our table--how many times has this
// particular string been expanded?
ExpandResultCount &result_count = debug_expand[str];
// Then, how many times has it expanded to this same result?
// First, assuming this is the first time it has expanded to this
// result, try to insert the result string with an initial count
// of 1.
pair<ExpandResultCount::iterator, bool> r =
result_count.insert(ExpandResultCount::value_type(result, 1));
if (!r.second) {
// If the result string was not successfully inserted into the
// map, it was already there--so increment the count.
ExpandResultCount::iterator rci = r.first;
(*rci).second++;
}
}
return result;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::expand_self_reference
// Access: Public
// Description: Similar to expand_string(), except that only simple
// references to the named variable are expanded--other
// variable references are left unchanged. This allows
// us to define a variable in terms of its previous
// definition.
////////////////////////////////////////////////////////////////////
string PPScope::
expand_self_reference(const string &str, const string &varname) {
// Look for a simple reference to the named variable. A more
// complex reference, like a computed variable name or something
// equally loopy, won't work with this simple test. Too bad.
string reference;
reference += VARIABLE_PREFIX;
reference += VARIABLE_OPEN_BRACE;
reference += varname;
reference += VARIABLE_CLOSE_BRACE;
string result;
size_t p = 0;
size_t q = str.find(reference, p);
while (q != string::npos) {
result += str.substr(p, q - p);
p = q;
result += r_expand_variable(str, p, (ExpandedVariable *)NULL);
q = str.find(reference, p);
}
result += str.substr(p);
return result;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::push_scope
// Access: Public, Static
// Description: Pushes the indicated scope onto the top of the stack.
// When a variable reference is unresolved in the
// current scope, the scope stack is searched, in LIFO
// order.
////////////////////////////////////////////////////////////////////
void PPScope::
push_scope(PPScope *scope) {
_scope_stack.push_back(scope);
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::pop_scope
// Access: Public, Static
// Description: Pops another level off the top of the stack. See
// push_scope().
////////////////////////////////////////////////////////////////////
PPScope *PPScope::
pop_scope() {
assert(!_scope_stack.empty());
PPScope *back = _scope_stack.back();
_scope_stack.pop_back();
return back;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_bottom_scope
// Access: Public, Static
// Description: Returns the scope on the bottom of the stack. This
// was the very first scope ever pushed, e.g. the global
// scope.
////////////////////////////////////////////////////////////////////
PPScope *PPScope::
get_bottom_scope() {
assert(!_scope_stack.empty());
return _scope_stack.front();
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::get_enclosing_scope
// Access: Public, Static
// Description: Returns the scope n below the top of the stack, or
// the bottom scope if the stack has exactly n or fewer
// scopes.
//
// This will be the scope associated with the nth
// enclosing syntax in the source file.
////////////////////////////////////////////////////////////////////
PPScope *PPScope::
get_enclosing_scope(int n) {
assert(n >= 0);
if (n >= _scope_stack.size()) {
return get_bottom_scope();
}
return _scope_stack[_scope_stack.size() - 1 - n];
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::tokenize_params
// Access: Public
// Description: Separates a string into tokens based on comma
// delimiters, e.g. for parameters to a function.
// Nested variable references are skipped correctly,
// even if they include commas. Leading and trailing
// whitespace in each token is automatically stripped.
//
// If expand is true, the nested variables are
// automatically expanded as the string is tokenized;
// otherwise, they are left unexpanded.
////////////////////////////////////////////////////////////////////
void PPScope::
tokenize_params(const string &str, vector<string> &tokens,
bool expand) {
size_t p = 0;
while (p < str.length()) {
// Skip initial whitespace.
while (p < str.length() && isspace(str[p])) {
p++;
}
string token;
while (p < str.length() && str[p] != FUNCTION_PARAMETER_SEPARATOR) {
if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
str[p + 1] == VARIABLE_OPEN_BRACE) {
// Skip a nested variable reference.
if (expand) {
token += r_expand_variable(str, p, (ExpandedVariable *)NULL);
} else {
token += r_scan_variable(str, p);
}
} else {
token += str[p];
p++;
}
}
// Back up past trailing whitespace.
size_t q = token.length();
while (q > 0 && isspace(token[q - 1])) {
q--;
}
tokens.push_back(token.substr(0, q));
p++;
if (p == str.length()) {
// In this case, we have just read past a trailing comma symbol
// at the end of the string, so we have one more empty token.
tokens.push_back(string());
}
}
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::tokenize_numeric_pair
// Access: Public
// Description: This function is used by all the numeric comparision
// functions, e.g. nne, nlt, etc. It splits the string
// up into two parameters based on commas, and evaluates
// each parameter as a number, into a and b. It returns
// true if successful, or false if there was some user
// error.
////////////////////////////////////////////////////////////////////
bool PPScope::
tokenize_numeric_pair(const string &str, double &a, double &b) {
vector<string> words;
tokenize_params(str, words, true);
if (words.size() != 2) {
cerr << words.size() << " parameters supplied when two were expected:\n"
<< str << "\n";
errors_occurred = true;
return false;
}
double results[2];
for (int i = 0; i < 2; i++) {
const char *param = words[i].c_str();
char *n;
results[i] = strtod(param, &n);
if (*n != '\0') {
// strtod failed--not a numeric representation.
cerr << "Warning: " << words[i] << " is not a number.\n";
if (n == param) {
results[i] = 0.0;
}
}
}
a = results[0];
b = results[1];
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::tokenize_ints
// Access: Public
// Description: This function is used by the arithmetic functions +,
// -, etc. It separates the string into parameters
// based on the comma, interprets each parameter as an
// integer, and fills up the indicated vector.
////////////////////////////////////////////////////////////////////
bool PPScope::
tokenize_ints(const string &str, vector<int> &tokens) {
vector<string> words;
tokenize_params(str, words, true);
vector<string>::const_iterator wi;
for (wi = words.begin(); wi != words.end(); ++wi) {
const char *param = (*wi).c_str();
char *n;
int result = strtol(param, &n, 0);
if (*n != '\0') {
// strtol failed--not an integer.
cerr << "Warning: " << param << " is not an integer.\n";
if (n == param) {
result = 0;
}
}
tokens.push_back(result);
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::scan_to_whitespace
// Access: Public
// Description: Scans to the end of the first whitespace-delimited
// word in the indicated string, even if it includes a
// nested variable reference (which is itself allowed to
// contain whitespace).
//
// On input, str is a string, and start is the starting
// position within the string of the scan; it should
// point to a non-whitespace character.
//
// The return value is the position within the string of
// the first whitespace character encountered at its
// original position or later, that is not part of a
// variable reference. All variable references are left
// unexpanded.
////////////////////////////////////////////////////////////////////
size_t PPScope::
scan_to_whitespace(const string &str, size_t start) {
size_t p = start;
while (p < str.length() && !isspace(str[p])) {
string token;
if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
str[p + 1] == VARIABLE_OPEN_BRACE) {
// Skip a nested variable reference.
r_scan_variable(str, p);
} else {
p++;
}
}
return p;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::format_int
// Access: Private, Static
// Description: Formats the indicated integer as a string and returns
// the string.
////////////////////////////////////////////////////////////////////
string PPScope::
format_int(int num) {
char buffer[32];
sprintf(buffer, "%d", num);
return buffer;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::p_set_variable
// Access: Private
// Description: The private implementation of p_set_variable.
// Returns true if the variable's definition is found
// and set, false otherwise.
////////////////////////////////////////////////////////////////////
bool PPScope::
p_set_variable(const string &varname, const string &definition) {
Variables::iterator vi;
vi = _variables.find(varname);
if (vi != _variables.end()) {
(*vi).second = definition;
return true;
}
if (_parent_scope != (PPScope *)NULL) {
return _parent_scope->p_set_variable(varname, definition);
}
return false;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::p_get_variable
// Access: Private
// Description: The private implementation of get_variable(). This
// checks the local scope only; it does not check the
// stack. It returns true if the variable is defined,
// false otherwise..
////////////////////////////////////////////////////////////////////
bool PPScope::
p_get_variable(const string &varname, string &result) {
Variables::const_iterator vi;
vi = _variables.find(varname);
if (vi != _variables.end()) {
result = (*vi).second;
return true;
}
if (varname == "RELDIR" &&
_directory != (PPDirectory *)NULL &&
current_output_directory != (PPDirectory *)NULL) {
// $[RELDIR] is a special variable name that evaluates to the
// relative directory of the current scope to the current output
// directory.
result = current_output_directory->get_rel_to(_directory);
return true;
}
if (varname == "DEPENDS_INDEX" &&
_directory != (PPDirectory *)NULL) {
// $[DEPENDS_INDEX] is another special variable name that
// evaluates to the numeric sorting index assigned to this
// directory based on its dependency relationship with other
// directories. It's useful primarily for debugging.
char buffer[32];
sprintf(buffer, "%d", _directory->get_depends_index());
result = buffer;
return true;
}
if (_parent_scope != (PPScope *)NULL) {
return _parent_scope->p_get_variable(varname, result);
}
return false;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::r_expand_string
// Access: Private
// Description: The recursive implementation of expand_string().
// This function detects cycles in the variable
// expansion by storing the set of variable names that
// have thus far been expanded in the linked list.
////////////////////////////////////////////////////////////////////
string PPScope::
r_expand_string(const string &str, PPScope::ExpandedVariable *expanded) {
string result;
// Search for a variable reference.
size_t p = 0;
while (p < str.length()) {
if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
str[p + 1] == VARIABLE_OPEN_BRACE) {
// Here's a nested variable! Expand it fully.
result += r_expand_variable(str, p, expanded);
} else {
result += str[p];
p++;
}
}
return result;
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::r_scan_variable
// Access: Private
// Description: Scans past a single variable reference without
// expanding it. On input, str is a string containing a
// variable reference (among other stuff), and vp is the
// position within the string of the prefix character at
// the beginning of the variable reference.
//
// On output, vp is set to the position within the
// string of the first character after the variable
// reference's closing bracket. The variable reference
// itself is returned.
////////////////////////////////////////////////////////////////////
string PPScope::
r_scan_variable(const string &str, size_t &vp) {
// Search for the end of the variable name: an unmatched square
// bracket.
size_t start = vp;
size_t p = vp + 2;
while (p < str.length() && str[p] != VARIABLE_CLOSE_BRACE) {
if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
str[p + 1] == VARIABLE_OPEN_BRACE) {
// Here's a nested variable! Scan past it, matching braces
// properly.
r_scan_variable(str, p);
} else {
p++;
}
}
if (p < str.length()) {
assert(str[p] == VARIABLE_CLOSE_BRACE);
p++;
} else {
cerr << "Warning! Unclosed variable reference:\n"
<< str.substr(vp) << "\n";
}
vp = p;
return str.substr(start, vp - start);
}
////////////////////////////////////////////////////////////////////
// Function: PPScope::r_expand_variable
// Access: Private
// Description: Expands a single variable reference. On input, str
// is a string containing a variable reference (among
// other stuff), and vp is the position within the
// string of the prefix character at the beginning of
// the variable reference.
//
// On output, vp is set to the position within the
// string of the first character after the variable
// reference's closing bracket, and the string expansion
// of the variable reference is returned.
////////////////////////////////////////////////////////////////////
string PPScope::
r_expand_variable(const string &str, size_t &vp,
PPScope::ExpandedVariable *expanded) {
string varname;
size_t whitespace_at = 0;
size_t open_nested_at = 0;
// Search for the end of the variable name: an unmatched square
// bracket.
size_t p = vp + 2;
while (p < str.length() && str[p] != VARIABLE_CLOSE_BRACE) {
if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
str[p + 1] == VARIABLE_OPEN_BRACE) {
if (whitespace_at != 0) {
// Once we have encountered whitespace, we don't expand
// variables inline anymore. These are now function
// parameters, and might need to be expanded in some other
// scope.
varname += r_scan_variable(str, p);
} else {
varname += r_expand_variable(str, p, expanded);
}
} else {
if (open_nested_at == 0 && str[p] == VARIABLE_OPEN_NESTED) {
open_nested_at = p - (vp + 2);
}
if (open_nested_at == 0 && whitespace_at == 0 && isspace(str[p])) {
whitespace_at = p - (vp + 2);
}
varname += str[p];
p++;
}
}
if (p < str.length()) {
assert(str[p] == VARIABLE_CLOSE_BRACE);
p++;
} else {
cerr << "Warning! Unclosed variable reference:\n"
<< str.substr(vp) << "\n";
}
vp = p;
// Check for a function expansion.
if (whitespace_at != 0) {
string funcname = varname.substr(0, whitespace_at);
p = whitespace_at;
while (p < varname.length() && isspace(varname[p])) {
p++;
}
string params = varname.substr(p);
// Is it a user-defined function?
const PPSubroutine *sub = PPSubroutine::get_func(funcname);
if (sub != (const PPSubroutine *)NULL) {