forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
3156 lines (2643 loc) · 87.8 KB
/
parser.cpp
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
/** \file parser.c
The fish parser. Contains functions for parsing and evaluating code.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <pwd.h>
#include <dirent.h>
#include <signal.h>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "proc.h"
#include "parser.h"
#include "parser_keywords.h"
#include "tokenizer.h"
#include "exec.h"
#include "wildcard.h"
#include "function.h"
#include "builtin.h"
#include "env.h"
#include "expand.h"
#include "reader.h"
#include "sanity.h"
#include "env_universal.h"
#include "event.h"
#include "intern.h"
#include "parse_util.h"
#include "path.h"
#include "signal.h"
#include "complete.h"
#include "parse_tree.h"
#include "parse_execution.h"
/**
Error message for unknown builtin
*/
#define UNKNOWN_BUILTIN_ERR_MSG _(L"Unknown builtin '%ls'")
/**
Error message for improper use of the exec builtin
*/
#define EXEC_ERR_MSG _(L"This command can not be used in a pipeline")
/**
Error message for tokenizer error. The tokenizer message is
appended to this message.
*/
#define TOK_ERR_MSG _( L"Tokenizer error: '%ls'")
/**
Error message for short circuit command error.
*/
#define COND_ERR_MSG _( L"An additional command is required" )
/**
Error message on a function that calls itself immediately
*/
#define INFINITE_RECURSION_ERR_MSG _( L"The function calls itself immediately, which would result in an infinite loop.")
/**
Error message used when the end of a block can't be located
*/
#define BLOCK_END_ERR_MSG _( L"Could not locate end of block. The 'end' command is missing, misspelled or a ';' is missing.")
/** Error message when a non-string token is found when expecting a command name */
#define CMD_ERR_MSG _( L"Expected a command name, got token of type '%ls'")
/**
Error message when encountering an illegal command name
*/
#define ILLEGAL_CMD_ERR_MSG _( L"Illegal command name '%ls'")
/**
Error message when encountering an illegal file descriptor
*/
#define ILLEGAL_FD_ERR_MSG _( L"Illegal file descriptor in redirection '%ls'")
/**
Error message for wildcards with no matches
*/
#define WILDCARD_ERR_MSG _( L"No matches for wildcard '%ls'.")
/**
Error when using case builtin outside of switch block
*/
#define INVALID_CASE_ERR_MSG _( L"'case' builtin not inside of switch block")
/**
Error when using loop control builtins (break or continue) outside of loop
*/
#define INVALID_LOOP_ERR_MSG _( L"Loop control command while not inside of loop" )
/**
Error when using return builtin outside of function definition
*/
#define INVALID_RETURN_ERR_MSG _( L"'return' builtin command outside of function definition" )
/**
Error when using else builtin outside of if block
*/
#define INVALID_ELSE_ERR_MSG _( L"'%ls' builtin not inside of if block" )
/**
Error when using 'else if' past a naked 'else'
*/
#define INVALID_ELSEIF_PAST_ELSE_ERR_MSG _( L"'%ls' used past terminating 'else'" )
/**
Error when using end builtin outside of block
*/
#define INVALID_END_ERR_MSG _( L"'end' command outside of block")
/**
Error message for Posix-style assignment: foo=bar
*/
#define COMMAND_ASSIGN_ERR_MSG _( L"Unknown command '%ls'. Did you mean 'set %ls %ls'? See the help section on the set command by typing 'help set'.")
/**
Error for invalid redirection token
*/
#define REDIRECT_TOKEN_ERR_MSG _( L"Expected redirection specification, got token of type '%ls'")
/**
Error when encountering redirection without a command
*/
#define INVALID_REDIRECTION_ERR_MSG _( L"Encountered redirection when expecting a command name. Fish does not allow a redirection operation before a command.")
/**
Error for evaluating in illegal scope
*/
#define INVALID_SCOPE_ERR_MSG _( L"Tried to evaluate commands using invalid block type '%ls'" )
/**
Error for wrong token type
*/
#define UNEXPECTED_TOKEN_ERR_MSG _( L"Unexpected token of type '%ls'")
/**
While block description
*/
#define WHILE_BLOCK N_( L"'while' block" )
/**
For block description
*/
#define FOR_BLOCK N_( L"'for' block" )
/**
Breakpoint block
*/
#define BREAKPOINT_BLOCK N_( L"Block created by breakpoint" )
/**
If block description
*/
#define IF_BLOCK N_( L"'if' conditional block" )
/**
Function definition block description
*/
#define FUNCTION_DEF_BLOCK N_( L"function definition block" )
/**
Function invocation block description
*/
#define FUNCTION_CALL_BLOCK N_( L"function invocation block" )
/**
Function invocation block description
*/
#define FUNCTION_CALL_NO_SHADOW_BLOCK N_( L"function invocation block with no variable shadowing" )
/**
Switch block description
*/
#define SWITCH_BLOCK N_( L"'switch' block" )
/**
Fake block description
*/
#define FAKE_BLOCK N_( L"unexecutable block" )
/**
Top block description
*/
#define TOP_BLOCK N_( L"global root block" )
/**
Command substitution block description
*/
#define SUBST_BLOCK N_( L"command substitution block" )
/**
Begin block description
*/
#define BEGIN_BLOCK N_( L"'begin' unconditional block" )
/**
Source block description
*/
#define SOURCE_BLOCK N_( L"Block created by the . builtin" )
/**
Source block description
*/
#define EVENT_BLOCK N_( L"event handler block" )
/**
Unknown block description
*/
#define UNKNOWN_BLOCK N_( L"unknown/invalid block" )
/**
Datastructure to describe a block type, like while blocks, command substitution blocks, etc.
*/
struct block_lookup_entry
{
/**
The block type id. The legal values are defined in parser.h.
*/
block_type_t type;
/**
The name of the builtin that creates this type of block, if any.
*/
const wchar_t *name;
/**
A description of this block type
*/
const wchar_t *desc;
}
;
/**
List of all legal block types
*/
static const struct block_lookup_entry block_lookup[]=
{
{ WHILE, L"while", WHILE_BLOCK },
{ FOR, L"for", FOR_BLOCK },
{ IF, L"if", IF_BLOCK },
{ FUNCTION_DEF, L"function", FUNCTION_DEF_BLOCK },
{ FUNCTION_CALL, 0, FUNCTION_CALL_BLOCK },
{ FUNCTION_CALL_NO_SHADOW, 0, FUNCTION_CALL_NO_SHADOW_BLOCK },
{ SWITCH, L"switch", SWITCH_BLOCK },
{ FAKE, 0, FAKE_BLOCK },
{ TOP, 0, TOP_BLOCK },
{ SUBST, 0, SUBST_BLOCK },
{ BEGIN, L"begin", BEGIN_BLOCK },
{ SOURCE, L".", SOURCE_BLOCK },
{ EVENT, 0, EVENT_BLOCK },
{ BREAKPOINT, L"breakpoint", BREAKPOINT_BLOCK },
{ (block_type_t)0, 0, 0 }
};
static bool job_should_skip_elseif(const job_t *job, const block_t *current_block);
// Given a file path, return something nicer. Currently we just "unexpand" tildes.
static wcstring user_presentable_path(const wcstring &path)
{
return replace_home_directory_with_tilde(path);
}
parser_t::parser_t(enum parser_type_t type, bool errors) :
parser_type(type),
show_errors(errors),
error_code(0),
err_pos(0),
cancellation_requested(false),
is_within_fish_initialization(false),
current_tokenizer(NULL),
current_tokenizer_pos(0),
job_start_pos(0),
eval_level(-1),
block_io(shared_ptr<io_data_t>())
{
}
/* A pointer to the principal parser (which is a static local) */
static parser_t *s_principal_parser = NULL;
parser_t &parser_t::principal_parser(void)
{
ASSERT_IS_NOT_FORKED_CHILD();
ASSERT_IS_MAIN_THREAD();
static parser_t parser(PARSER_TYPE_GENERAL, true);
if (! s_principal_parser)
{
s_principal_parser = &parser;
}
return parser;
}
void parser_t::set_is_within_fish_initialization(bool flag)
{
is_within_fish_initialization = flag;
}
void parser_t::skip_all_blocks(void)
{
/* Tell all blocks to skip */
if (s_principal_parser)
{
s_principal_parser->cancellation_requested = true;
//write(2, "Cancelling blocks\n", strlen("Cancelling blocks\n"));
for (size_t i=0; i < s_principal_parser->block_count(); i++)
{
s_principal_parser->block_at_index(i)->skip = true;
}
}
}
void parser_t::push_block(block_t *new_current)
{
const enum block_type_t type = new_current->type();
new_current->src_lineno = parser_t::get_lineno();
new_current->src_filename = parser_t::current_filename()?intern(parser_t::current_filename()):0;
const block_t *old_current = this->current_block();
if (old_current && old_current->skip)
new_current->mark_as_fake();
/*
New blocks should be skipped if the outer block is skipped,
except TOP ans SUBST block, which open up new environments. Fake
blocks should always be skipped. Rather complicated... :-(
*/
new_current->skip = old_current ? old_current->skip : 0;
/*
Type TOP and SUBST are never skipped
*/
if (type == TOP || type == SUBST)
{
new_current->skip = 0;
}
/*
Fake blocks and function definition blocks are never executed
*/
if (type == FAKE || type == FUNCTION_DEF)
{
new_current->skip = 1;
}
new_current->job = 0;
new_current->loop_status=LOOP_NORMAL;
this->block_stack.push_back(new_current);
if ((new_current->type() != FUNCTION_DEF) &&
(new_current->type() != FAKE) &&
(new_current->type() != TOP))
{
env_push(type == FUNCTION_CALL);
new_current->wants_pop_env = true;
}
}
void parser_t::pop_block()
{
if (block_stack.empty())
{
debug(1,
L"function %s called on empty block stack.",
__func__);
bugreport();
return;
}
block_t *old = block_stack.back();
block_stack.pop_back();
if (old->wants_pop_env)
env_pop();
delete old;
}
void parser_t::pop_block(const block_t *expected)
{
assert(expected == this->current_block());
this->pop_block();
}
const wchar_t *parser_t::get_block_desc(int block) const
{
for (size_t i=0; block_lookup[i].desc; i++)
{
if (block_lookup[i].type == block)
{
return _(block_lookup[i].desc);
}
}
return _(UNKNOWN_BLOCK);
}
const block_t *parser_t::block_at_index(size_t idx) const
{
/* 0 corresponds to the last element in our vector */
size_t count = block_stack.size();
return idx < count ? block_stack.at(count - idx - 1) : NULL;
}
block_t *parser_t::block_at_index(size_t idx)
{
size_t count = block_stack.size();
return idx < count ? block_stack.at(count - idx - 1) : NULL;
}
const block_t *parser_t::current_block() const
{
return block_stack.empty() ? NULL : block_stack.back();
}
block_t *parser_t::current_block()
{
return block_stack.empty() ? NULL : block_stack.back();
}
/**
Search the text for the end of the current block
*/
static const wchar_t *parser_find_end(const wchar_t * buff)
{
int had_cmd=0;
int count = 0;
int error=0;
int mark=0;
CHECK(buff, 0);
tokenizer_t tok(buff, 0);
for (; tok_has_next(&tok) && !error; tok_next(&tok))
{
int last_type = tok_last_type(&tok);
switch (last_type)
{
case TOK_STRING:
{
if (!had_cmd)
{
if (wcscmp(tok_last(&tok), L"end")==0)
{
count--;
}
else if (parser_keywords_is_block(tok_last(&tok)))
{
count++;
}
if (count < 0)
{
error = 1;
}
had_cmd = 1;
}
break;
}
case TOK_END:
{
had_cmd = 0;
break;
}
case TOK_PIPE:
case TOK_BACKGROUND:
{
if (had_cmd)
{
had_cmd = 0;
}
else
{
error = 1;
}
break;
}
case TOK_ERROR:
error = 1;
break;
default:
break;
}
if (!count)
{
tok_next(&tok);
mark = tok_get_pos(&tok);
break;
}
}
if (!count && !error)
{
return buff+mark;
}
return 0;
}
void parser_t::forbid_function(const wcstring &function)
{
forbidden_function.push_back(function);
}
void parser_t::allow_function()
{
/*
if( al_peek( &forbidden_function) )
debug( 2, L"Allow %ls\n", al_peek( &forbidden_function) );
*/
forbidden_function.pop_back();
}
void parser_t::error(int ec, size_t p, const wchar_t *str, ...)
{
va_list va;
CHECK(str,);
error_code = ec;
// note : p may be -1
err_pos = static_cast<int>(p);
va_start(va, str);
err_buff = vformat_string(str, va);
va_end(va);
}
/**
Print profiling information to the specified stream
*/
static void print_profile(const std::vector<profile_item_t*> &items,
FILE *out)
{
for (size_t pos = 0; pos < items.size(); pos++)
{
const profile_item_t *me, *prev;
size_t i;
int my_time;
me = items.at(pos);
if (!me->skipped)
{
my_time=me->parse+me->exec;
for (i=pos+1; i<items.size(); i++)
{
prev = items.at(i);
if (prev->skipped)
{
continue;
}
if (prev->level <= me->level)
{
break;
}
if (prev->level > me->level+1)
{
continue;
}
my_time -= prev->parse;
my_time -= prev->exec;
}
if (me->cmd.size() > 0)
{
if (fwprintf(out, L"%d\t%d\t", my_time, me->parse+me->exec) < 0)
{
wperror(L"fwprintf");
return;
}
for (i=0; i<me->level; i++)
{
if (fwprintf(out, L"-") < 0)
{
wperror(L"fwprintf");
return;
}
}
if (fwprintf(out, L"> %ls\n", me->cmd.c_str()) < 0)
{
wperror(L"fwprintf");
return;
}
}
}
}
}
void parser_t::emit_profiling(const char *path) const
{
/* Save profiling information. OK to not use CLO_EXEC here because this is called while fish is dying (and hence will not fork) */
FILE *f = fopen(path, "w");
if (!f)
{
debug(1,
_(L"Could not write profiling information to file '%s'"),
path);
}
else
{
if (fwprintf(f,
_(L"Time\tSum\tCommand\n"),
profile_items.size()) < 0)
{
wperror(L"fwprintf");
}
else
{
print_profile(profile_items, f);
}
if (fclose(f))
{
wperror(L"fclose");
}
}
}
/**
Print error message to string if an error has occured while parsing
\param target the buffer to write to
\param prefix: The string token to prefix the each line with. Usually the name of the command trying to parse something.
*/
void parser_t::print_errors(wcstring &target, const wchar_t *prefix)
{
CHECK(prefix,);
if (error_code && ! err_buff.empty())
{
int tmp;
append_format(target, L"%ls: %ls\n", prefix, err_buff.c_str());
tmp = current_tokenizer_pos;
current_tokenizer_pos = err_pos;
append_format(target, L"%ls", this->current_line());
current_tokenizer_pos=tmp;
}
}
/**
Print error message to stderr if an error has occured while parsing
*/
void parser_t::print_errors_stderr()
{
if (error_code && ! err_buff.empty())
{
debug(0, L"%ls", err_buff.c_str());
int tmp;
tmp = current_tokenizer_pos;
current_tokenizer_pos = err_pos;
fwprintf(stderr, L"%ls", this->current_line());
current_tokenizer_pos=tmp;
}
}
void parser_t::eval_args(const wchar_t *line, std::vector<completion_t> &args)
{
expand_flags_t eflags = 0;
if (! show_errors)
eflags |= EXPAND_NO_DESCRIPTIONS;
if (this->parser_type != PARSER_TYPE_GENERAL)
eflags |= EXPAND_SKIP_CMDSUBST;
bool do_loop=1;
if (! line) return;
// PCA we need to suppress calling proc_push_interactive off of the main thread.
if (this->parser_type == PARSER_TYPE_GENERAL)
proc_push_interactive(0);
tokenizer_t tok(line, (show_errors ? 0 : TOK_SQUASH_ERRORS));
/*
eval_args may be called while evaulating another command, so we
save the previous tokenizer and restore it on exit
*/
scoped_push<tokenizer_t*> tokenizer_push(¤t_tokenizer, &tok);
scoped_push<int> tokenizer_pos_push(¤t_tokenizer_pos, 0);
error_code=0;
for (; do_loop && tok_has_next(&tok) ; tok_next(&tok))
{
current_tokenizer_pos = tok_get_pos(&tok);
switch (tok_last_type(&tok))
{
case TOK_STRING:
{
const wcstring tmp = tok_last(&tok);
if (expand_string(tmp, args, eflags) == EXPAND_ERROR)
{
err_pos=tok_get_pos(&tok);
do_loop=0;
}
break;
}
case TOK_END:
{
break;
}
case TOK_ERROR:
{
if (show_errors)
error(SYNTAX_ERROR,
tok_get_pos(&tok),
TOK_ERR_MSG,
tok_last(&tok));
do_loop=0;
break;
}
default:
{
if (show_errors)
error(SYNTAX_ERROR,
tok_get_pos(&tok),
UNEXPECTED_TOKEN_ERR_MSG,
tok_get_desc(tok_last_type(&tok)));
do_loop=0;
break;
}
}
}
if (show_errors)
this->print_errors_stderr();
if (this->parser_type == PARSER_TYPE_GENERAL)
proc_pop_interactive();
}
void parser_t::stack_trace(size_t block_idx, wcstring &buff) const
{
/*
Check if we should end the recursion
*/
if (block_idx >= this->block_count())
return;
const block_t *b = this->block_at_index(block_idx);
if (b->type()==EVENT)
{
/*
This is an event handler
*/
const event_block_t *eb = static_cast<const event_block_t *>(b);
wcstring description = event_get_desc(eb->event);
append_format(buff, _(L"in event handler: %ls\n"), description.c_str());
buff.append(L"\n");
/*
Stop recursing at event handler. No reason to believe that
any other code is relevant.
It might make sense in the future to continue printing the
stack trace of the code that invoked the event, if this is a
programmatic event, but we can't currently detect that.
*/
return;
}
if (b->type() == FUNCTION_CALL || b->type()==SOURCE || b->type()==SUBST)
{
/*
These types of blocks should be printed
*/
int i;
switch (b->type())
{
case SOURCE:
{
const source_block_t *sb = static_cast<const source_block_t*>(b);
const wchar_t *source_dest = sb->source_file;
append_format(buff, _(L"from sourcing file %ls\n"), user_presentable_path(source_dest).c_str());
break;
}
case FUNCTION_CALL:
{
const function_block_t *fb = static_cast<const function_block_t*>(b);
append_format(buff, _(L"in function '%ls'\n"), fb->name.c_str());
break;
}
case SUBST:
{
append_format(buff, _(L"in command substitution\n"));
break;
}
default: /* Can't get here */
break;
}
const wchar_t *file = b->src_filename;
if (file)
{
append_format(buff,
_(L"\tcalled on line %d of file %ls\n"),
b->src_lineno,
user_presentable_path(file).c_str());
}
else if (is_within_fish_initialization)
{
append_format(buff, _(L"\tcalled during startup\n"));
}
else
{
append_format(buff, _(L"\tcalled on standard input\n"));
}
if (b->type() == FUNCTION_CALL)
{
const function_block_t *fb = static_cast<const function_block_t *>(b);
const process_t * const process = fb->process;
if (process->argv(1))
{
wcstring tmp;
for (i=1; process->argv(i); i++)
{
if (i > 1)
tmp.push_back(L' ');
tmp.append(process->argv(i));
}
append_format(buff, _(L"\twith parameter list '%ls'\n"), tmp.c_str());
}
}
append_format(buff, L"\n");
}
/*
Recursively print the next block
*/
parser_t::stack_trace(block_idx + 1, buff);
}
/**
Returns the name of the currently evaluated function if we are
currently evaluating a function, null otherwise. This is tested by
moving down the block-scope-stack, checking every block if it is of
type FUNCTION_CALL.
*/
const wchar_t *parser_t::is_function() const
{
// PCA: Have to make this a string somehow
ASSERT_IS_MAIN_THREAD();
const wchar_t *result = NULL;
for (size_t block_idx = 0; block_idx < this->block_count(); block_idx++)
{
const block_t *b = this->block_at_index(block_idx);
if (b->type() == FUNCTION_CALL)
{
const function_block_t *fb = static_cast<const function_block_t *>(b);
result = fb->name.c_str();
break;
}
}
return result;
}
int parser_t::get_lineno() const
{
int lineno;
if (! current_tokenizer || ! tok_string(current_tokenizer))
return -1;
lineno = current_tokenizer->line_number_of_character_at_offset(current_tokenizer_pos);
const wchar_t *function_name;
if ((function_name = is_function()))
{
lineno += function_get_definition_offset(function_name);
}
return lineno;
}
int parser_t::line_number_of_character_at_offset(size_t idx) const
{
if (! current_tokenizer)
return -1;
int result = current_tokenizer->line_number_of_character_at_offset(idx);
//assert(result == parse_util_lineno(tok_string( current_tokenizer ), idx));
return result;
}
const wchar_t *parser_t::current_filename() const
{
ASSERT_IS_MAIN_THREAD();
for (size_t i=0; i < this->block_count(); i++)
{
const block_t *b = this->block_at_index(i);
if (b->type() == FUNCTION_CALL)
{
const function_block_t *fb = static_cast<const function_block_t *>(b);
return function_get_definition_file(fb->name);
}
}
/* We query a global array for the current file name, but only do that if we are the principal parser */
if (this == &principal_parser())
{
return reader_current_filename();
}
return NULL;
}
/**
Calculates the on-screen width of the specified substring of the
specified string. This function takes into account the width and
alignment of the tab character, but other wise behaves like
repeatedly calling wcwidth.
*/
static int printed_width(const wchar_t *str, int len)
{
int res=0;
int i;
CHECK(str, 0);
for (i=0; str[i] && i<len; i++)
{
if (str[i] == L'\t')
{
res=(res+8)&~7;
}
else
{
res += fish_wcwidth(str[i]);