Repository navigation
Expand file tree
/
Copy pathparser.cpp
More file actions
981 lines (928 loc) · 36.3 KB
/
Copy pathparser.cpp
File metadata and controls
981 lines (928 loc) · 36.3 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
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <script/script.hpp>
#include <script/parser.hpp>
#include <script/to_value.hpp>
#include <util/error.hpp>
#include <util/io/package_manager.hpp> // for "include file" semi hack
#include <stack>
using std::max;
using std::min;
using std::stack;
using std::swap;
#ifdef __WXMSW__
#define TokenType TokenType_ // some stupid windows header uses our name
#endif
String read_utf8_line(wxInputStream& input, bool eat_bom = true, bool until_eof = false);
extern ScriptValueP script_warning;
extern ScriptValueP script_warning_if_neq;
// ----------------------------------------------------------------------------- : Tokenizing : class
enum TokenType
{ TOK_NAME // abc
, TOK_INT // 123
, TOK_DOUBLE // 123.0
, TOK_STRING // "asdf"
, TOK_OPER // + - * / . ;
, TOK_LPAREN // ( { [
, TOK_RPAREN // ) } ]
, TOK_DUMMY // placeholder for putBack
, TOK_EOF // end of input
};
/// Tokens produced by the TokenIterator
struct Token {
TokenType type;
String value;
bool newline; ///< Is there a newline between this token and the previous one?
size_t pos; ///< Start position of the token
inline bool operator == (TokenType t) const { return type == t; }
inline bool operator != (TokenType t) const { return type != t; }
inline bool operator == (const String& s) const { return type != TOK_STRING && value == s; }
inline bool operator != (const String& s) const { return type == TOK_STRING || value != s; }
inline bool operator == (const Char* s) const { return type != TOK_STRING && value == s; }
inline bool operator != (const Char* s) const { return type == TOK_STRING || value != s; }
};
enum OpenBrace
{ BRACE_STRING // "
, BRACE_STRING_MODE // fake brace for string mode
, BRACE_PAREN // (, [, {
};
/// Iterator over a string, one token at a time.
/** Also stores errors found when tokenizing or parsing */
class TokenIterator {
public:
TokenIterator(const String& str, Packaged* package, bool string_mode, vector<ScriptParseError>& errors);
/// Peek at the next token, doesn't move to the one after that
/** Can peek further forward by using higher values of offset.
* offset=0 returns the last token that was read, or newline if putBack() was used.
*/
const Token& peek(size_t offset = 1);
/// Retrieve the next token
const Token& read();
/// Put back a token
/** Only one token can be correctly put back, the put back token will read as a newline.
*/
void putBack();
/// Get a section of source code
/** Known problems: does not work correctly when crossing an include-file border */
String getSourceCode(size_t start, size_t end);
/// Get the current line number
int getLineNumber();
private:
String input;
size_t pos;
String filename; ///< Filename of include files, "" for the main input
Packaged* package; ///< Package the input is from
vector<Token> buffer; ///< buffer of unread tokens, front() = current
stack<OpenBrace> open_braces; ///< braces/quotes we entered from script mode
bool newline; ///< Did we just pass a newline?
// more input?
struct MoreInput {
String input;
size_t pos;
String filename;
Packaged* package;
};
stack<MoreInput> more; ///< Read tokens from here when we are done with the current input
/// Add a token to the buffer, with the current newline value, resets newline
void addToken(TokenType type, const String& value, size_t start);
/// Read the next token, and add it to the buffer
void readToken();
/// Read the next token which is a string (after the opening ")
void readStringToken();
public:
/// All errors found
vector<ScriptParseError>& errors;
/// Add an error message
void add_error(const String& message);
/// Expected some token instead of what was found, possibly a matching opening bracket is known
void expected(const String& exp, const Token* opening = nullptr);
};
// ----------------------------------------------------------------------------- : Characters
bool isAlpha_(Char c) { return isAlpha(c) || c==_('_'); }
bool isAlnum_(Char c) { return isAlnum(c) || c==_('_'); }
bool isOper (Char c) { return wxStrchr(_("+-*/!.@%^&:=<>;,"),c) != nullptr; }
bool isLparen(Char c) { return c==_('(') || c==_('[') || c==_('{'); }
bool isRparen(Char c) { return c==_(')') || c==_(']') || c==_('}'); }
bool isDigitOrDot(Char c) { return isDigit(c) || c==_('.'); }
bool isLongOper(const String& s) { return s==_(":=") || s==_("==") || s==_("!=") || s==_("<=") || s==_(">="); }
// ----------------------------------------------------------------------------- : Tokenizing
TokenIterator::TokenIterator(const String& str, Packaged* package, bool string_mode, vector<ScriptParseError>& errors)
: input(str)
, pos(0)
, package(package)
, newline(false)
, errors(errors)
{
if (string_mode) {
open_braces.push(BRACE_STRING_MODE);
putBack();//dummy
readStringToken();
}
}
const Token& TokenIterator::peek(size_t offset) {
// read the next token until we have enough
while (buffer.size() <= offset) {
readToken();
}
return buffer[offset];
}
const Token& TokenIterator::read() {
if (!buffer.empty()) buffer.erase(buffer.begin());
return peek(0);
}
void TokenIterator::putBack() {
// Don't use addToken, because it canges newline
// Also, we want to push_front
Token t = {TOK_DUMMY, _(""), false};
buffer.insert(buffer.begin(), t);
}
void TokenIterator::addToken(TokenType type, const String& value, size_t start) {
Token t = {type, value, newline, start};
buffer.push_back(t);
newline = false;
}
void TokenIterator::readToken() {
if (pos >= input.size()) {
// done with input, is there more?
if (!more.empty()) {
input = more.top().input;
pos = more.top().pos;
filename = more.top().filename;
package = more.top().package;
more.pop();
} else {
// EOF
addToken(TOK_EOF, _("end of input"), input.size());
}
return;
}
// read a character from the input
Char c = input.GetChar(pos++);
if (c == _('\n')) {
newline = true;
} else if (isSpace(c)) {
// ignore
} else if (is_substr(input, pos-1, _("include file:"))) {
// include a file
// HACK: This is not really the right place for it, but it's the best I've got
// filename
pos += 12; // "nclude file:"
size_t eol = input.find_first_of(_("\r\n"), pos);
if (eol == String::npos) eol = input.size();
String include_file = trim(input.substr(pos, eol - pos));
// store the current input for later retrieval
MoreInput m = {input, eol, filename, package};
more.push(m);
// read the entire file, and start at the beginning of it
pos = 0;
filename = include_file;
InputStreamP is = package_manager.openFileFromPackage(package, include_file);
input = read_utf8_line(*is, true, true);
} else if (isAlpha(c) || c == _('_') || (isDigit(c) && !buffer.empty() && buffer.back() == _("."))) {
// name, or a number after a . token, as in array.0
size_t start = pos - 1;
while (pos < input.size() && isAlnum_(input.GetChar(pos))) ++pos;
addToken(TOK_NAME, canonical_name_form(input.substr(start, pos-start)), start); // convert name to cannocial form
} else if (isDigit(c)) {
// number
size_t start = pos - 1;
while (pos < input.size() && isDigitOrDot(input.GetChar(pos))) ++pos;
String num = input.substr(start, pos-start);
addToken(
num.find_first_of('.') == String::npos ? TOK_INT : TOK_DOUBLE,
num, start
);
} else if (isOper(c)) {
// operator
if (pos < input.size() && isLongOper(input.substr(pos - 1, 2))) {
// long operator
addToken(TOK_OPER, input.substr(pos - 1, 2), pos-1);
pos += 1;
} else {
addToken(TOK_OPER, input.substr(pos - 1, 1), pos-1);
}
} else if (c==_('"')) {
// string
open_braces.push(BRACE_STRING);
readStringToken();
} else if (c == _('}') && !open_braces.empty() && open_braces.top() != BRACE_PAREN) {
// closing smart string, resume to string parsing
// "a{e}b" --> "a" "{ e }" "b"
addToken(TOK_RPAREN, _("}\""), pos-1);
readStringToken();
} else if (isLparen(c)) {
// paranthesis/brace
open_braces.push(BRACE_PAREN);
addToken(TOK_LPAREN, String(1,c), pos-1);
} else if (isRparen(c)) {
// paranthesis/brace
if (!open_braces.empty()) open_braces.pop();
addToken(TOK_RPAREN, String(1,c), pos-1);
} else if(c==_('#')) {
// comment untill end of line
while (pos < input.size() && input[pos] != _('\n')) ++pos;
} else {
add_error(_("Unknown character in script: '") + String(1,c) + _("'"));
// just skip the character
}
}
void TokenIterator::readStringToken() {
size_t start = max((size_t)1, pos) - 1;
String str;
while (true) {
if (pos >= input.size()) {
if (!open_braces.empty() && open_braces.top() == BRACE_STRING_MODE) {
// in string mode: end of input = end of string
addToken(TOK_STRING, str, start);
return;
} else {
add_error(_("Unexpected end of input in string constant"));
// fix up
addToken(TOK_STRING, str, start);
return;
}
}
Char c = input.GetChar(pos++);
// parse the string constant
if (c == _('"') && !open_braces.empty() && open_braces.top() == BRACE_STRING) {
// end of string
addToken(TOK_STRING, str, start);
open_braces.pop();
return;
} else if (c == _('\\')) {
// escape
if (pos >= input.size()) {
add_error(_("Unexpected end of input in string constant"));
// fix up
addToken(TOK_STRING, str, start);
return;
}
c = input.GetChar(pos++);
if (c == _('n')) str += _('\n');
else if (c == _('r')) str += _('\r');
else if (c == _('t')) str += _('\t');
else if (c == _('&')); // escape for nothing
else if (c == _('<')) str += _('\1'); // escape for <
else if (c == _('\\') || c == _('"') || c == _('\'') || c == _('{') || c == _('}')) {
str += c; // \ or { or " or ', don't warn about these, since they look escape-worthy
} else if (c >= _('0') && c <= _('9')) {
// numeric escape
int i = 0;
while (c >= _('0') && c <= _('9')) {
i = i * 10 + static_cast<int>(c - _('0'));
c = input.GetChar(pos++);
}
pos--;
str += static_cast<Char>(i); // ignore
} else {
add_error(String::Format(_("Invalid string escape sequence: \"\\%c\""),c));
str += _('\\') + c; // ignore
}
} else if (c == _('{')) {
// smart string
// "a{e}b" --> "a" "{ e }" "b"
addToken(TOK_STRING, str, start);
addToken(TOK_LPAREN, _("\"{"), pos-1);
return;
} else {
str += c;
}
}
}
int line_number(size_t pos, const String& input) {
int line = 1;
for (size_t i = 0 ; i < input.size() && i < pos ; ++i) {
if (input.GetChar(i) == _('\n')) line++;
}
return line;
}
void TokenIterator::add_error(const String& message) {
if (!errors.empty() && errors.back().start == pos) return; // already an error here
// add error message
errors.push_back(ScriptParseError(pos, line_number(pos,input), filename, message));
}
void TokenIterator::expected(const String& expected, const Token* opening) {
size_t error_pos = peek(0).pos;
if (!errors.empty() && errors.back().start == pos) return; // already an error here
// add error message
if (opening) {
errors.push_back(ScriptParseError(opening->pos, error_pos, line_number(opening->pos,input), filename, opening->value, expected, peek(0).value));
} else {
errors.push_back(ScriptParseError(error_pos, line_number(error_pos,input), filename, expected, peek(0).value));
}
}
String TokenIterator::getSourceCode(size_t start, size_t end) {
start = min(start, input.size());
end = min(end, input.size());
return input.substr(start, end-start);
}
int TokenIterator::getLineNumber() {
return line_number(peek(0).pos, input);
}
// ----------------------------------------------------------------------------- : Parsing
/// Precedence levels for parsing, higher = tighter
enum Precedence
{ PREC_ALL
, PREC_NEWLINE // newline ;
, PREC_SEQ // ;
, PREC_SET // :=
, PREC_AND // and or
, PREC_CMP // == != < > <= >=
, PREC_ADD // + -
, PREC_MUL // * / mod
, PREC_POW // ^ (right associative)
, PREC_UNARY // - not (unary operators)
, PREC_FUN // [] () . (function call, member)
, PREC_STRING // +{ }+ (smart string operators)
, PREC_NONE
};
enum ExprType
{ EXPR_VAR // A single variable, which could be converted to the left hand side of an assignment
, EXPR_STATEMENT // A 'statement', i.e. an expression that shouldn't be the last one
, EXPR_OTHER
, EXPR_FAILED
};
/// Parse an expression
/** @param input Read tokens from the input
* @param scrip Add resulting instructions to the script
* @param min_prec Minimum precedence level for operators
*
* @returns the type of expression
*
* NOTE: The net stack effect of an expression should be +1
*/
ExprType parseExpr(TokenIterator& input, Script& script, Precedence min_prec);
/// Parse an expression, possibly with operators applied. Optionally adds an instruction at the end.
/** @param input Read tokens from the input
* @param script Add resulting instructions to the script
* @param min_prec Minimum precedence level for operators
* @param close_with Add this instruction at the end, or I_NOP for no instruction
* @param close_with_data Data for the instruction at the end
* NOTE: The net stack effect of an expression should be +1
*/
ExprType parseOper(TokenIterator& input, Script& script, Precedence min_prec, InstructionType close_with = I_NOP, int close_with_data = 0);
/// Parse call arguments, "(...)"
void parseCallArguments(TokenIterator& input, Script& script, vector<Variable>& arguments);
ScriptP parse(const String& s, Packaged* package, bool string_mode, vector<ScriptParseError>& errors_out) {
errors_out.clear();
// parse
TokenIterator input(s, package, string_mode, errors_out);
ScriptP script(new Script);
ExprType type = parseOper(input, *script, PREC_ALL);
Token eof = input.read();
if (eof != TOK_EOF) {
input.expected(_("end of input"));
}
// were there fatal errors?
if (type == EXPR_FAILED) {
return ScriptP();
} else {
return script;
}
}
ScriptP parse(const String& s, Packaged* package, bool string_mode) {
vector<ScriptParseError> errors;
ScriptP script = parse(s, package, string_mode, errors);
if (!errors.empty()) {
throw ScriptParseErrors(errors);
}
return script;
}
// Expect a token, adds an error if it is not found
bool expectToken(TokenIterator& input, const Char* expect, const Token* opening = nullptr, const Char* name_in_error = nullptr) {
Token token = input.read();
if (token == expect) {
return true;
} else {
input.expected(name_in_error ? name_in_error : expect, opening);
return false;
}
}
ExprType parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
Token token = input.read();
if (token == _("(")) {
// Parentheses = grouping for precedence of expressions
ExprType type = parseOper(input, script, PREC_ALL);
expectToken(input, _(")"), &token);
return type;
} else if (token == _("{")) {
// {} = function block. Parse a new Script
intrusive_ptr<Script> subScript(new Script);
ExprType t = parseOper(input, *subScript, PREC_ALL);
if (t == EXPR_STATEMENT) {
input.add_error(_("Warning: last statement of a function should be an expression, i.e. it should return a result in all cases."));
}
expectToken(input, _("}"), &token);
script.addInstruction(I_PUSH_CONST, subScript);
} else if (token == _("[")) {
// [] = list or map literal
unsigned int count = 0;
Token t = input.peek();
while (t != _("]") && t != TOK_EOF) {
if (input.peek(2) == _(":") && (t.type == TOK_NAME || t.type == TOK_INT || t.type == TOK_STRING)) {
// name: ...
script.addInstruction(I_PUSH_CONST, to_script(t.value));
input.read(); // skip the name
input.read(); // and the :
} else {
// implicit numbered element
script.addInstruction(I_PUSH_CONST, script_nil);
}
parseOper(input, script, PREC_AND);
++count;
t = input.peek();
if (t == _(",")) {
// Comma separating the elements
input.read();
t = input.peek();
}
}
expectToken(input, _("]"), &token);
script.addInstruction(I_MAKE_OBJECT, count);
} else if (minPrec <= PREC_UNARY && token == _("-")) {
parseOper(input, script, PREC_UNARY, I_UNARY, I_NEGATE); // unary negation
} else if (token == TOK_NAME) {
if (minPrec <= PREC_UNARY && token == _("not")) {
parseOper(input, script, PREC_UNARY, I_UNARY, I_NOT); // unary not
} else if (token == _("true")) {
script.addInstruction(I_PUSH_CONST, script_true); // boolean constant : true
} else if (token == _("false")) {
script.addInstruction(I_PUSH_CONST, script_false); // boolean constant : false
} else if (token == _("nil")) {
script.addInstruction(I_PUSH_CONST, script_nil); // universal constant : nil
} else if (token == _("if")) {
// if AAA then BBB else CCC
parseOper(input, script, PREC_AND); // AAA
unsigned jmpElse = script.addInstruction(I_JUMP_IF_NOT); // jnz lbl_else
expectToken(input, _("then")); // then
ExprType type1 = parseOper(input, script, PREC_SET); // BBB
unsigned jmpEnd = script.addInstruction(I_JUMP); // jump lbl_end
script.comeFrom(jmpElse); // lbl_else:
bool has_else = input.peek() == _("else"); //else
ExprType type2 = EXPR_STATEMENT;
if (has_else) {
input.read();
type2 = parseOper(input, script, PREC_SET); // CCC
} else {
script.addInstruction(I_PUSH_CONST, script_nil);
}
script.comeFrom(jmpEnd); // lbl_end:
return type1 == EXPR_STATEMENT || type2 == EXPR_STATEMENT ? EXPR_STATEMENT : EXPR_OTHER;
} else if (token == _("for")) {
// the loop body should have a net stack effect of 0, but the entire expression of +1
// solution: add all results from the body, start with nil
bool is_each = input.peek() == _("each");
if (is_each) {
// for each AAA(:BBB) in CCC do EEE
input.read(); // each?
} else {
// for AAA(:BBB) from CCC to DDD do EEE
}
// name
Token name = input.read(); // AAA
if (name != TOK_NAME) {
input.expected(_("name"));
return EXPR_FAILED;
}
Variable var = string_to_variable(name.value);
// key:value?
bool with_key = input.peek() == _(":");
Variable key = (Variable)-1;
if (with_key) {
input.read(); // :
name = input.read(); // BBB
if (name != TOK_NAME) {
input.expected(_("name"));
return EXPR_FAILED;
}
key = string_to_variable(name.value);
swap(var,key);
}
// iterator
if (is_each) {
expectToken(input, _("in")); // in
parseOper(input, script, PREC_AND); // CCC
script.addInstruction(I_UNARY, I_ITERATOR_C); // iterator_collection
} else {
expectToken(input, _("from")); // from
parseOper(input, script, PREC_AND); // CCC
expectToken(input, _("to")); // to
parseOper(input, script, PREC_AND); // DDD
script.addInstruction(I_BINARY, I_ITERATOR_R); // iterator_range
}
script.addInstruction(I_PUSH_CONST, script_nil); // push nil
unsigned lblStart = script.addInstruction(with_key
? I_LOOP_WITH_KEY // lbl_start: loop_with_key lbl_end
: I_LOOP); // lbl_start: loop lbl_end
expectToken(input, _("do")); // do
if (with_key) {
script.addInstruction(I_SET_VAR, key); // set key_name
script.addInstruction(I_POP); // pop
}
script.addInstruction(I_SET_VAR, var); // set name
script.addInstruction(I_POP); // pop
parseOper(input, script, PREC_SET, I_BINARY, I_ADD); // EEE; add
script.addInstruction(I_JUMP, lblStart); // jump lbl_start
script.comeFrom(lblStart); // lbl_end:
} else if (token == _("rgb")) {
// rgb(r, g, b)
expectToken(input, _("("));
parseOper(input, script, PREC_ALL); // r
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // g
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // b
expectToken(input, _(")"));
script.addInstruction(I_TERNARY, I_RGB);
} else if (token == _("rgba")) {
// rgba(r, g, b, a)
expectToken(input, _("("));
parseOper(input, script, PREC_ALL); // r
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // g
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // b
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // a
expectToken(input, _(")"));
script.addInstruction(I_QUATERNARY, I_RGBA);
} else if (token == _("min") || token == _("max")) {
// min(x,y,z,...)
unsigned int op = token == _("min") ? I_MIN : I_MAX;
expectToken(input, _("("));
parseOper(input, script, PREC_ALL); // first
while(input.peek() == _(",")) {
expectToken(input, _(","));
parseOper(input, script, PREC_ALL); // second, third, etc.
script.addInstruction(I_BINARY, op);
}
expectToken(input, _(")"), &token);
} else if (token == _("assert")) {
// assert(condition)
expectToken(input, _("("));
size_t start = input.peek().pos;
int line = input.getLineNumber();
size_t function_pos = script.getConstants().size();
script.addInstruction(I_PUSH_CONST, script_warning);
parseOper(input, script, PREC_ALL); // condition
size_t end = input.peek().pos;
String message = String::Format(_("Assertion failure on line %d:\n expected: "), line) + input.getSourceCode(start,end);
expectToken(input, _(")"), &token);
if (script.getInstructions().back().instr == I_BINARY && script.getInstructions().back().instr2 == I_EQ) {
// compile "assert(x == y)" into
// warning_if_neq("condition", _1: x, _2: y)
message += _("\n found: ");
script.getConstants()[function_pos] = script_warning_if_neq;
script.getInstructions().pop_back(); // POP == instruction
script.addInstruction(I_PUSH_CONST, message); // push "condition"
script.addInstruction(I_CALL, 3); // call
script.addInstruction(I_NOP, SCRIPT_VAR__1); // (_1:)
script.addInstruction(I_NOP, SCRIPT_VAR__2); // (_2:)
script.addInstruction(I_NOP, SCRIPT_VAR_input); // (input:)
} else {
// compile into: warning("condition", condition: not condition)
script.addInstruction(I_UNARY, I_NOT); // not
script.addInstruction(I_PUSH_CONST, message); // push "condition"
script.addInstruction(I_CALL, 2); // call
script.addInstruction(I_NOP, SCRIPT_VAR_condition); // (condition:)
script.addInstruction(I_NOP, SCRIPT_VAR_input); // (input:)
}
return EXPR_STATEMENT;
} else {
// variable
Variable var = string_to_variable(token.value);
script.addInstruction(I_GET_VAR, var);
return EXPR_VAR;
}
} else if (token == TOK_INT) {
long l = 0;
//l = lexical_cast<long>(token.value);
token.value.ToLong(&l);
script.addInstruction(I_PUSH_CONST, to_script(l));
} else if (token == TOK_DOUBLE) {
double d = 0;
//d = lexical_cast<double>(token.value);
token.value.ToDouble(&d);
script.addInstruction(I_PUSH_CONST, to_script(d));
} else if (token == TOK_STRING) {
script.addInstruction(I_PUSH_CONST, to_script(token.value));
} else {
script.addInstruction(I_PUSH_CONST, script_nil);
input.expected(_("expression"));
return EXPR_FAILED;
}
return EXPR_OTHER;
}
ExprType parseOper(TokenIterator& input, Script& script, Precedence minPrec, InstructionType closeWith, int closeWithData) {
ExprType type = parseExpr(input, script, minPrec); // first argument
// read any operators after an expression
// EBNF: expr = expr | expr oper expr
// without left recursion: expr = expr (oper expr)*
while (true) {
Token token = input.read();
if (token != TOK_OPER && token != TOK_NAME && token!=TOK_LPAREN &&
!((token == TOK_STRING || token == TOK_INT || token == TOK_DOUBLE) && minPrec <= PREC_NEWLINE && token.newline)) {
// not an operator-like token
input.putBack();
break;
}
if (minPrec <= PREC_SEQ && token==_(";")) {
Token next = input.peek(1);
if (next == TOK_RPAREN || next == TOK_EOF) {
// allow ; at end of expression without errors
break;
}
script.addInstruction(I_POP); // discard result of first expression
type = parseOper(input, script, PREC_SET);
} else if (minPrec <= PREC_SET && token==_(":=")) {
// We made a mistake, the part before the := should be a variable name,
// not an expression. Remove that instruction.
Instruction& instr = script.getInstructions().back();
if (type != EXPR_VAR || instr.instr != I_GET_VAR) {
input.add_error(_("Can only assign to variables"));
return EXPR_FAILED;
}
script.getInstructions().pop_back();
type = parseOper(input, script, PREC_SET, I_SET_VAR, instr.data);
if (type == EXPR_STATEMENT) {
input.add_error(_("Warning: the right hand side of an assignment should always yield a value."));
}
}
else if (minPrec <= PREC_AND && token==_("orelse"))parseOper(input, script, PREC_ADD, I_BINARY, I_OR_ELSE);
else if (minPrec <= PREC_AND && token==_("and")) {
// short-circuiting and:
// "XXX and YYY"
// becomes
// XXX
// I_JUMP_SC_AND after # if top==false then goto after else pop
// YYY
// after:
unsigned jmpSC = script.addInstruction(I_JUMP_SC_AND);
parseOper(input, script, PREC_CMP);
script.comeFrom(jmpSC);
}
else if (minPrec <= PREC_AND && token==_("or" )) {
Token t = input.peek();
if (t == _("else")) {// or else
input.read(); // skip else
// TODO: deprecate "or else" in favor of "orelse"
parseOper(input, script, PREC_ADD, I_BINARY, I_OR_ELSE);
} else {
// short-circuiting or
unsigned jmpSC = script.addInstruction(I_JUMP_SC_OR);
parseOper(input, script, PREC_CMP);
script.comeFrom(jmpSC);
}
}
else if (minPrec <= PREC_AND && token==_("xor")) parseOper(input, script, PREC_CMP, I_BINARY, I_XOR);
else if (minPrec <= PREC_CMP && token==_("=")) {
if (minPrec <= PREC_SET) {
input.add_error(_("Use of '=', did you mean ':=' or '=='? I will assume '=='"));
}
parseOper(input, script, PREC_ADD, I_BINARY, I_EQ);
}
else if (minPrec <= PREC_CMP && token==_("==")) parseOper(input, script, PREC_ADD, I_BINARY, I_EQ);
else if (minPrec <= PREC_CMP && token==_("!=")) parseOper(input, script, PREC_ADD, I_BINARY, I_NEQ);
else if (minPrec <= PREC_CMP && token==_("<")) parseOper(input, script, PREC_ADD, I_BINARY, I_LT);
else if (minPrec <= PREC_CMP && token==_(">")) parseOper(input, script, PREC_ADD, I_BINARY, I_GT);
else if (minPrec <= PREC_CMP && token==_("<=")) parseOper(input, script, PREC_ADD, I_BINARY, I_LE);
else if (minPrec <= PREC_CMP && token==_(">=")) parseOper(input, script, PREC_ADD, I_BINARY, I_GE);
else if (minPrec <= PREC_ADD && token==_("+")) parseOper(input, script, PREC_MUL, I_BINARY, I_ADD);
else if (minPrec <= PREC_ADD && token==_("-")) parseOper(input, script, PREC_MUL, I_BINARY, I_SUB);
else if (minPrec <= PREC_MUL && token==_("*")) parseOper(input, script, PREC_POW, I_BINARY, I_MUL);
else if (minPrec <= PREC_MUL && token==_("/")) parseOper(input, script, PREC_POW, I_BINARY, I_FDIV);
else if (minPrec <= PREC_MUL && token==_("div")) parseOper(input, script, PREC_POW, I_BINARY, I_DIV);
else if (minPrec <= PREC_MUL && token==_("mod")) parseOper(input, script, PREC_POW, I_BINARY, I_MOD);
else if (minPrec <= PREC_POW && token==_("^")) parseOper(input, script, PREC_POW, I_BINARY, I_POW);
else if (minPrec <= PREC_FUN && token==_(".")) { // get member by name
input.peek(1); // peek ahead, so the next token can see the preceding "."
// that forces the next token to always be a TOK_NAME instead of TOK_INT/TOK_DOUBLE
// (this is a bit of a hack)
const Token& token = input.read();
if (token == TOK_NAME || token == TOK_STRING) {
script.addInstruction(I_MEMBER_C, token.value);
} else {
input.expected(_("name"));
}
} else if (minPrec <= PREC_FUN && token==_("[") && !token.newline) { // get member by expr
size_t before = script.getInstructions().size();
parseOper(input, script, PREC_SET);
if (script.getInstructions().size() == before + 1 && script.getInstructions().back().instr == I_PUSH_CONST) {
// optimize:
// PUSH_CONST x
// MEMBER
// becomes
// MEMBER_CONST x
script.getInstructions().back().instr = I_MEMBER_C;
} else {
script.addInstruction(I_BINARY, I_MEMBER);
}
expectToken(input, _("]"), &token);
} else if (minPrec <= PREC_FUN && token==_("(") && !token.newline) {
// function call, read arguments
vector<Variable> arguments;
parseCallArguments(input, script, arguments);
expectToken(input, _(")"), &token);
// generate instruction
script.addInstruction(I_CALL, (unsigned int)arguments.size());
for(auto& arg :arguments) {
script.addInstruction(I_NOP, arg);
}
} else if (minPrec <= PREC_FUN && token==_("@")) {
// closure call, read arguments
vector<Variable> arguments;
expectToken(input, _("("));
parseCallArguments(input, script, arguments);
expectToken(input, _(")"), &token);
// generate instruction
script.addInstruction(I_CLOSURE, (unsigned int)arguments.size());
for(auto& arg :arguments) {
script.addInstruction(I_NOP, arg);
}
} else if (minPrec <= PREC_STRING && token==_("\"{")) {
// for smart strings: "x" {{ e }} "y"
// optimize: "" + e -> e
Instruction i = script.getInstructions().back();
if (i.instr == I_PUSH_CONST && script.getConstants()[i.data]->toString().empty()) {
script.getInstructions().pop_back();
parseOper(input, script, PREC_ALL); // e
} else {
parseOper(input, script, PREC_ALL, I_BINARY, I_ADD); // e
}
if (expectToken(input, _("}\""), &token, _("}"))) {
parseOper(input, script, PREC_NONE); // y
// optimize: e + "" -> e
i = script.getInstructions().back();
if (i.instr == I_PUSH_CONST && script.getConstants()[i.data]->toString().empty()) {
script.getInstructions().pop_back();
} else {
script.addInstruction(I_BINARY, I_ADD);
}
}
} else if (minPrec <= PREC_NEWLINE && token.newline) {
// newline functions as ;
// only if we don't match another token!
input.putBack();
script.addInstruction(I_POP);
type = parseOper(input, script, PREC_SET);
} else {
input.putBack();
break;
}
if (type == EXPR_VAR) type = EXPR_OTHER; // var only applies to single variables, not to things with operators
}
// add closing instruction
if (closeWith != I_NOP) {
script.addInstruction(closeWith, closeWithData);
}
return type;
}
void parseCallArguments(TokenIterator& input, Script& script, vector<Variable>& arguments) {
Token t = input.peek();
while (t != _(")") && t != TOK_EOF) {
if (input.peek(2) == _(":") && t.type == TOK_NAME) {
// name: ...
arguments.push_back(string_to_variable(t.value));
input.read(); // skip the name
input.read(); // and the :
parseOper(input, script, PREC_SEQ);
} else {
// implicit "input" argument
arguments.push_back(SCRIPT_VAR_input);
parseOper(input, script, PREC_SEQ);
}
t = input.peek();
if (t == _(",")) {
// Comma separating the arguments
input.read();
t = input.peek();
}
}
}
// ----------------------------------------------------------------------------- : Parsing values
ScriptValueP script_local_image_file(LocalFileName const& filename);
ScriptValueP script_local_symbol_file(LocalFileName const& filename);
ScriptValueP parse_value(TokenIterator& input, Packaged* package) {
Token token = input.read();
if (token == _("(")) {
// Parentheses = grouping, i.e. ignore it
ScriptValueP a = parse_value(input, package);
expectToken(input, _(")"), &token);
return a;
} else if (token == _("[")) {
// [] = list or map literal
ScriptCustomCollectionP col(new ScriptCustomCollection);
Token t = input.peek();
while (t != _("]") && t != TOK_EOF) {
if (input.peek(2) == _(":") && (t.type == TOK_NAME || t.type == TOK_INT || t.type == TOK_STRING)) {
// name: ...
String key = t.value;
input.read(); // skip the name
input.read(); // and the :
ScriptValueP v = parse_value(input, package);
if (v) col->key_value[key] = v;
} else {
// implicit numbered element
ScriptValueP v = parse_value(input, package);
if (v) col->value.push_back(v);
}
t = input.peek();
if (t == _(",")) {
// Comma separating the elements
input.read();
t = input.peek();
}
}
expectToken(input, _("]"), &token);
return col;
} else if (token == TOK_NAME) {
if (token == _("true")) {
return script_true;
} else if (token == _("false")) {
return script_false;
} else if (token == _("nil")) {
return script_nil;
} else if (token == _("rgb")) {
// rgb(r, g, b)
expectToken(input, _("("));
ScriptValueP r = parse_value(input, package);
expectToken(input, _(","));
ScriptValueP g = parse_value(input, package);
expectToken(input, _(","));
ScriptValueP b = parse_value(input, package);
expectToken(input, _(")"));
if (!r || !g || !b) return ScriptValueP();
return to_script(wxColour(r->toInt(),g->toInt(),b->toInt()));
} else if (token == _("rgba")) {
// rgba(r, g, b, a)
expectToken(input, _("("));
ScriptValueP r = parse_value(input, package);
expectToken(input, _(","));
ScriptValueP g = parse_value(input, package);
expectToken(input, _(","));
ScriptValueP b = parse_value(input, package);
expectToken(input, _(","));
ScriptValueP a = parse_value(input, package);
expectToken(input, _(")"));
if (!r || !g || !b) return ScriptValueP();
return to_script(AColor(r->toInt(),g->toInt(),b->toInt(),a->toInt()));
} else if (token == _("mark_default")) {
expectToken(input, _("("));
ScriptValueP x = parse_value(input, package);
expectToken(input, _(")"));
if (!x) return ScriptValueP();
return intrusive(new ScriptDefault(x));
} else if (token == _("local_image_file")) {
expectToken(input, _("("));
ScriptValueP x = parse_value(input, package);
expectToken(input, _(")"));
if (!x) return ScriptValueP();
return script_local_image_file(LocalFileName::fromReadString(x->toString()));
} else if (token == _("local_symbol_file")) {
expectToken(input, _("("));
ScriptValueP x = parse_value(input, package);
expectToken(input, _(")"));
if (!x) return ScriptValueP();
return script_local_symbol_file(LocalFileName::fromReadString(x->toString()));
}
} else if (token == TOK_INT) {
long l = 0;
token.value.ToLong(&l);
return to_script(l);
} else if (token == TOK_DOUBLE) {
double d = 0;
token.value.ToDouble(&d);
return to_script(d);
} else if (token == TOK_STRING) {
return to_script(token.value);
/*} else if (token == _("-")) {
// TODO?
throw InternalError("TODO");*/
}
// parse error
input.expected(_("value (string/number/boolean/color/list)"));
return ScriptValueP();
}
ScriptValueP parse_value(const String& s, Packaged* package, vector<ScriptParseError>& errors_out) {
errors_out.clear();
// parse
TokenIterator input(s, package, false, errors_out);
ScriptValueP value = parse_value(input, package);
if (input.read() != TOK_EOF) {
input.expected(_("end of input"));
}
return value;
}
ScriptValueP parse_value(const String& s, Packaged* package) {
vector<ScriptParseError> errors;
ScriptValueP value = parse_value(s, package, errors);
if (!errors.empty() || !value) {
throw ScriptParseErrors(errors);
}
return value;
}