-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathtoke.c
14032 lines (12422 loc) · 469 KB
/
toke.c
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
/* toke.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'It all comes from here, the stench and the peril.' --Frodo
*
* [p.719 of _The Lord of the Rings_, IV/ix: "Shelob's Lair"]
*/
/*
* This file is the lexer for Perl. It's closely linked to the
* parser, perly.y.
*
* The main routine is yylex(), which returns the next token.
*/
/*
=head1 Lexer interface
This is the lower layer of the Perl parser, managing characters and tokens.
=for apidoc AmnU|yy_parser *|PL_parser
Pointer to a structure encapsulating the state of the parsing operation
currently in progress. The pointer can be locally changed to perform
a nested parse without interfering with the state of an outer parse.
Individual members of C<PL_parser> have their own documentation.
=cut
*/
#include "EXTERN.h"
#define PERL_IN_TOKE_C
#include "perl.h"
#include "invlist_inline.h"
#define new_constant(a,b,c,d,e,f,g, h) \
S_new_constant(aTHX_ a,b,STR_WITH_LEN(c),d,e,f, g, h)
#define pl_yylval (PL_parser->yylval)
/* XXX temporary backwards compatibility */
#define PL_lex_brackets (PL_parser->lex_brackets)
#define PL_lex_allbrackets (PL_parser->lex_allbrackets)
#define PL_lex_fakeeof (PL_parser->lex_fakeeof)
#define PL_lex_brackstack (PL_parser->lex_brackstack)
#define PL_lex_casemods (PL_parser->lex_casemods)
#define PL_lex_casestack (PL_parser->lex_casestack)
#define PL_lex_dojoin (PL_parser->lex_dojoin)
#define PL_lex_formbrack (PL_parser->lex_formbrack)
#define PL_lex_inpat (PL_parser->lex_inpat)
#define PL_lex_inwhat (PL_parser->lex_inwhat)
#define PL_lex_op (PL_parser->lex_op)
#define PL_lex_repl (PL_parser->lex_repl)
#define PL_lex_starts (PL_parser->lex_starts)
#define PL_lex_stuff (PL_parser->lex_stuff)
#define PL_multi_start (PL_parser->multi_start)
#define PL_multi_open (PL_parser->multi_open)
#define PL_multi_close (PL_parser->multi_close)
#define PL_preambled (PL_parser->preambled)
#define PL_linestr (PL_parser->linestr)
#define PL_expect (PL_parser->expect)
#define PL_copline (PL_parser->copline)
#define PL_bufptr (PL_parser->bufptr)
#define PL_oldbufptr (PL_parser->oldbufptr)
#define PL_oldoldbufptr (PL_parser->oldoldbufptr)
#define PL_linestart (PL_parser->linestart)
#define PL_bufend (PL_parser->bufend)
#define PL_last_uni (PL_parser->last_uni)
#define PL_last_lop (PL_parser->last_lop)
#define PL_last_lop_op (PL_parser->last_lop_op)
#define PL_lex_state (PL_parser->lex_state)
#define PL_rsfp (PL_parser->rsfp)
#define PL_rsfp_filters (PL_parser->rsfp_filters)
#define PL_in_my (PL_parser->in_my)
#define PL_in_my_stash (PL_parser->in_my_stash)
#define PL_tokenbuf (PL_parser->tokenbuf)
#define PL_multi_end (PL_parser->multi_end)
#define PL_error_count (PL_parser->error_count)
# define PL_nexttoke (PL_parser->nexttoke)
# define PL_nexttype (PL_parser->nexttype)
# define PL_nextval (PL_parser->nextval)
#define SvEVALED(sv) \
(SvTYPE(sv) >= SVt_PVNV \
&& ((XPVIV*)SvANY(sv))->xiv_u.xivu_eval_seen)
static const char ident_too_long[] = "Identifier too long";
static const char ident_var_zero_multi_digit[] = "Numeric variables with more than one digit may not start with '0'";
# define NEXTVAL_NEXTTOKE PL_nextval[PL_nexttoke]
#define XENUMMASK 0x3f
#define XFAKEEOF 0x40
#define XFAKEBRACK 0x80
#ifdef USE_UTF8_SCRIPTS
# define UTF cBOOL(!IN_BYTES)
#else
# define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || ( !(PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS) && (PL_hints & HINT_UTF8)))
#endif
#define ONLY_ASCII (PL_hints & HINT_ASCII_ENCODING)
#define ALLOW_NON_ASCII (! ONLY_ASCII)
/* The maximum number of characters preceding the unrecognized one to display */
#define UNRECOGNIZED_PRECEDE_COUNT 10
/* In variables named $^X, these are the legal values for X.
* 1999-02-27 [email protected] */
#define isCONTROLVAR(x) (isUPPER(x) || memCHRs("[\\]^_?", (x)))
/* Non-identifier plugin infix operators are allowed any printing character
* except spaces, digits, or identifier chars
*/
#define isPLUGINFIX(c) (c && !isSPACE(c) && !isDIGIT(c) && !isALPHA(c))
/* Plugin infix operators may not begin with a quote symbol */
#define isPLUGINFIX_FIRST(c) (isPLUGINFIX(c) && c != '"' && c != '\'')
#define PLUGINFIX_IS_ENABLED UNLIKELY(PL_infix_plugin != &Perl_infix_plugin_standard)
#define SPACE_OR_TAB(c) isBLANK_A(c)
#define HEXFP_PEEK(s) \
(((s[0] == '.') && \
(isXDIGIT(s[1]) || isALPHA_FOLD_EQ(s[1], 'p'))) || \
isALPHA_FOLD_EQ(s[0], 'p'))
/* LEX_* are values for PL_lex_state, the state of the lexer.
* They are arranged oddly so that the guard on the switch statement
* can get by with a single comparison (if the compiler is smart enough).
*
* These values refer to the various states within a sublex parse,
* i.e. within a double quotish string
*/
/* #define LEX_NOTPARSING 11 is done in perl.h. */
#define LEX_NORMAL 10 /* normal code (ie not within "...") */
#define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */
#define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */
#define LEX_INTERPPUSH 7 /* starting a new sublex parse level */
#define LEX_INTERPSTART 6 /* expecting the start of a $var */
/* at end of code, eg "$x" followed by: */
#define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */
#define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */
#define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of
string or after \E, $foo, etc */
#define LEX_INTERPCONST 2 /* NOT USED */
#define LEX_FORMLINE 1 /* expecting a format line */
/* returned to yyl_try() to request it to retry the parse loop, expected to only
be returned directly by yyl_fake_eof(), but functions that call yyl_fake_eof()
can also return it.
yylex (aka Perl_yylex) returns 0 on EOF rather than returning -1,
other token values are 258 or higher (see perly.h), so -1 should be
a safe value here.
*/
#define YYL_RETRY (-1)
#ifdef DEBUGGING
static const char* const lex_state_names[] = {
"KNOWNEXT",
"FORMLINE",
"INTERPCONST",
"INTERPCONCAT",
"INTERPENDMAYBE",
"INTERPEND",
"INTERPSTART",
"INTERPPUSH",
"INTERPCASEMOD",
"INTERPNORMAL",
"NORMAL"
};
#endif
#include "keywords.h"
/* CLINE is a macro that ensures PL_copline has a sane value */
#define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline))
/*
* Convenience functions to return different tokens and prime the
* lexer for the next token. They all take an argument.
*
* TOKEN : generic token (used for '(', DOLSHARP, etc)
* OPERATOR : generic operator
* AOPERATOR : assignment operator
* PREBLOCK : beginning the block after an if, while, foreach, ...
* PRETERMBLOCK : beginning a non-code-defining {} block (eg, hash ref)
* PREREF : *EXPR where EXPR is not a simple identifier
* TERM : expression term
* POSTDEREF : postfix dereference (->$* ->@[...] etc.)
* LOOPX : loop exiting command (goto, last, dump, etc)
* FTST : file test operator
* FUN0 : zero-argument function
* FUN0OP : zero-argument function, with its op created in this file
* FUN1 : not used, except for not, which isn't a UNIOP
* BOop : bitwise or or xor
* BAop : bitwise and
* BCop : bitwise complement
* SHop : shift operator
* PWop : power operator
* PMop : pattern-matching operator
* Aop : addition-level operator
* AopNOASSIGN : addition-level operator that is never part of .=
* Mop : multiplication-level operator
* ChEop : chaining equality-testing operator
* NCEop : non-chaining comparison operator at equality precedence
* ChRop : chaining relational operator <= != gt
* NCRop : non-chaining relational operator isa
*
* Also see LOP and lop() below.
*/
#ifdef DEBUGGING /* Serve -DT. */
# define REPORT(retval) tokereport((I32)retval, &pl_yylval)
#else
# define REPORT(retval) (retval)
#endif
#define TOKEN(retval) return ( PL_bufptr = s, REPORT(retval))
#define OPERATOR(retval) return (PL_expect = XTERM, PL_bufptr = s, REPORT(retval))
#define AOPERATOR(retval) return ao((PL_expect = XTERM, PL_bufptr = s, retval))
#define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s, REPORT(retval))
#define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s, REPORT(retval))
#define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s, REPORT(retval))
#define TERM(retval) return (CLINE, PL_expect = XOPERATOR, PL_bufptr = s, REPORT(retval))
#define PHASERBLOCK(f) return (pl_yylval.ival=f, PL_expect = XBLOCK, PL_bufptr = s, REPORT((int)PHASER))
#define POSTDEREF(f) return (PL_bufptr = s, S_postderef(aTHX_ REPORT(f),s[1]))
#define LOOPX(f) return (PL_bufptr = force_word(s,BAREWORD,TRUE,FALSE), \
pl_yylval.ival=f, \
PL_expect = PL_nexttoke ? XOPERATOR : XTERM, \
REPORT((int)LOOPEX))
#define FTST(f) return (pl_yylval.ival=f, PL_expect=XTERMORDORDOR, PL_bufptr=s, REPORT((int)UNIOP))
#define FUN0(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0))
#define FUN0OP(f) return (pl_yylval.opval=f, CLINE, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC0OP))
#define FUN1(f) return (pl_yylval.ival=f, PL_expect=XOPERATOR, PL_bufptr=s, REPORT((int)FUNC1))
#define BOop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITOROP))
#define BAop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)BITANDOP))
#define BCop(f) return pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr = s, \
REPORT(PERLY_TILDE)
#define SHop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)SHIFTOP))
#define PWop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)POWOP))
#define PMop(f) return(pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)MATCHOP))
#define Aop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)ADDOP))
#define AopNOASSIGN(f) return (pl_yylval.ival=f, PL_bufptr=s, REPORT((int)ADDOP))
#define Mop(f) return ao((pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, (int)MULOP))
#define ChEop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)CHEQOP))
#define NCEop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)NCEQOP))
#define ChRop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)CHRELOP))
#define NCRop(f) return (pl_yylval.ival=f, PL_expect=XTERM, PL_bufptr=s, REPORT((int)NCRELOP))
/* This bit of chicanery makes a unary function followed by
* a parenthesis into a function with one argument, highest precedence.
* The UNIDOR macro is for unary functions that can be followed by the //
* operator (such as C<shift // 0>).
*/
#define UNI3(f,x,have_x) { \
pl_yylval.ival = f; \
if (have_x) PL_expect = x; \
PL_bufptr = s; \
PL_last_uni = PL_oldbufptr; \
PL_last_lop_op = (f) < 0 ? -(f) : (f); \
if (*s == '(') \
return REPORT( (int)FUNC1 ); \
s = skipspace(s); \
return REPORT( *s=='(' ? (int)FUNC1 : (int)UNIOP ); \
}
#define UNI(f) UNI3(f,XTERM,1)
#define UNIDOR(f) UNI3(f,XTERMORDORDOR,1)
#define UNIPROTO(f,optional) { \
if (optional) PL_last_uni = PL_oldbufptr; \
OPERATOR(f); \
}
#define UNIBRACK(f) UNI3(f,0,0)
/* return has special case parsing.
*
* List operators have low precedence. Functions have high precedence.
* Every built in, *except return*, if written with () around its arguments, is
* parsed as a function. Hence every other list built in:
*
* $ perl -lwe 'sub foo { join 2,4,6 * 1.5 } print for foo()' # join 2,4,9
* 429
* $ perl -lwe 'sub foo { join(2,4,6) * 1.5 } print for foo()' # 426 * 1.5
* 639
* $ perl -lwe 'sub foo { join+(2,4,6) * 1.5 } print for foo()'
* Useless use of a constant (2) in void context at -e line 1.
* Useless use of a constant (4) in void context at -e line 1.
*
* $
*
* empty line output because C<(2, 4, 6) * 1.5> is the comma operator, not a
* list. * forces scalar context, 6 * 1.5 is 9, and join(9) is the empty string.
*
* Whereas return:
*
* $ perl -lwe 'sub foo { return 2,4,6 * 1.5 } print for foo()'
* 2
* 4
* 9
* $ perl -lwe 'sub foo { return(2,4,6) * 1.5 } print for foo()'
* Useless use of a constant (2) in void context at -e line 1.
* Useless use of a constant (4) in void context at -e line 1.
* 9
* $ perl -lwe 'sub foo { return+(2,4,6) * 1.5 } print for foo()'
* Useless use of a constant (2) in void context at -e line 1.
* Useless use of a constant (4) in void context at -e line 1.
* 9
* $
*
* and:
* $ perl -lwe 'sub foo { return(2,4,6) } print for foo()'
* 2
* 4
* 6
*
* This last example is what we expect, but it's clearly inconsistent with how
* C<return(2,4,6) * 1.5> *ought* to behave, if the rules were consistently
* followed.
*
*
* Perl 3 attempted to be consistent:
*
* The rules are more consistent about where parens are needed and
* where they are not. In particular, unary operators and list operators now
* behave like functions if they're called like functions.
*
* However, the behaviour for return was reverted to the "old" parsing with
* patches 9-12:
*
* The construct
* return (1,2,3);
* did not do what was expected, since return was swallowing the
* parens in order to consider itself a function. The solution,
* since return never wants any trailing expression such as
* return (1,2,3) + 2;
* is to simply make return an exception to the paren-makes-a-function
* rule, and treat it the way it always was, so that it doesn't
* strip the parens.
*
* To demonstrate the special-case parsing, replace OLDLOP(OP_RETURN); with
* LOP(OP_RETURN, XTERM);
*
* and constructs such as
*
* return (Internals::V())[2]
*
* turn into syntax errors
*/
#define OLDLOP(f) \
do { \
if (!PL_lex_allbrackets && PL_lex_fakeeof > LEX_FAKEEOF_LOWLOGIC) \
PL_lex_fakeeof = LEX_FAKEEOF_LOWLOGIC; \
pl_yylval.ival = (f); \
PL_expect = XTERM; \
PL_bufptr = s; \
return (int)LSTOP; \
} while(0)
#define COPLINE_INC_WITH_HERELINES \
STMT_START { \
CopLINE_inc(PL_curcop); \
if (PL_parser->herelines) \
CopLINE(PL_curcop) += PL_parser->herelines, \
PL_parser->herelines = 0; \
} STMT_END
/* Called after scan_str to update CopLINE(PL_curcop), but only when there
* is no sublex_push to follow. */
#define COPLINE_SET_FROM_MULTI_END \
STMT_START { \
CopLINE_set(PL_curcop, PL_multi_end); \
if (PL_multi_end != PL_multi_start) \
PL_parser->herelines = 0; \
} STMT_END
/* A file-local structure for passing around information about subroutines and
* related definable words */
struct code {
SV *sv;
CV *cv;
GV *gv, **gvp;
OP *rv2cv_op;
PADOFFSET off;
bool lex;
};
static const struct code no_code = { NULL, NULL, NULL, NULL, NULL, 0, FALSE };
#ifdef DEBUGGING
/* how to interpret the pl_yylval associated with the token */
enum token_type {
TOKENTYPE_NONE,
TOKENTYPE_IVAL,
TOKENTYPE_OPNUM, /* pl_yylval.ival contains an opcode number */
TOKENTYPE_PVAL,
TOKENTYPE_OPVAL
};
#define DEBUG_TOKEN(Type, Name) \
{ Name, TOKENTYPE_##Type, #Name }
static struct debug_tokens {
const int token;
enum token_type type;
const char *name;
} const debug_tokens[] =
{
DEBUG_TOKEN (OPNUM, ADDOP),
DEBUG_TOKEN (NONE, ANDAND),
DEBUG_TOKEN (NONE, ANDOP),
DEBUG_TOKEN (NONE, ARROW),
DEBUG_TOKEN (OPNUM, ASSIGNOP),
DEBUG_TOKEN (OPNUM, BITANDOP),
DEBUG_TOKEN (OPNUM, BITOROP),
DEBUG_TOKEN (OPNUM, BLKLSTOP),
DEBUG_TOKEN (OPNUM, CHEQOP),
DEBUG_TOKEN (OPNUM, CHRELOP),
DEBUG_TOKEN (NONE, COLONATTR),
DEBUG_TOKEN (NONE, DOLSHARP),
DEBUG_TOKEN (NONE, DORDOR),
DEBUG_TOKEN (IVAL, DOTDOT),
DEBUG_TOKEN (NONE, FORMLBRACK),
DEBUG_TOKEN (NONE, FORMRBRACK),
DEBUG_TOKEN (OPNUM, FUNC),
DEBUG_TOKEN (OPNUM, FUNC0),
DEBUG_TOKEN (OPVAL, FUNC0OP),
DEBUG_TOKEN (OPVAL, FUNC0SUB),
DEBUG_TOKEN (OPNUM, FUNC1),
DEBUG_TOKEN (NONE, HASHBRACK),
DEBUG_TOKEN (IVAL, KW_CATCH),
DEBUG_TOKEN (IVAL, KW_CLASS),
DEBUG_TOKEN (IVAL, KW_CONTINUE),
DEBUG_TOKEN (IVAL, KW_DEFAULT),
DEBUG_TOKEN (IVAL, KW_DO),
DEBUG_TOKEN (IVAL, KW_ELSE),
DEBUG_TOKEN (IVAL, KW_ELSIF),
DEBUG_TOKEN (IVAL, KW_FIELD),
DEBUG_TOKEN (IVAL, KW_GIVEN),
DEBUG_TOKEN (IVAL, KW_FOR),
DEBUG_TOKEN (IVAL, KW_FORMAT),
DEBUG_TOKEN (IVAL, KW_IF),
DEBUG_TOKEN (IVAL, KW_LOCAL),
DEBUG_TOKEN (IVAL, KW_METHOD_anon),
DEBUG_TOKEN (IVAL, KW_METHOD_named),
DEBUG_TOKEN (IVAL, KW_MY),
DEBUG_TOKEN (IVAL, KW_PACKAGE),
DEBUG_TOKEN (IVAL, KW_REQUIRE),
DEBUG_TOKEN (IVAL, KW_SUB_anon),
DEBUG_TOKEN (IVAL, KW_SUB_anon_sig),
DEBUG_TOKEN (IVAL, KW_SUB_named),
DEBUG_TOKEN (IVAL, KW_SUB_named_sig),
DEBUG_TOKEN (IVAL, KW_TRY),
DEBUG_TOKEN (IVAL, KW_USE_or_NO),
DEBUG_TOKEN (IVAL, KW_UNLESS),
DEBUG_TOKEN (IVAL, KW_UNTIL),
DEBUG_TOKEN (IVAL, KW_WHEN),
DEBUG_TOKEN (IVAL, KW_WHILE),
DEBUG_TOKEN (OPVAL, LABEL),
DEBUG_TOKEN (OPNUM, LOOPEX),
DEBUG_TOKEN (OPNUM, LSTOP),
DEBUG_TOKEN (OPVAL, LSTOPSUB),
DEBUG_TOKEN (OPNUM, MATCHOP),
DEBUG_TOKEN (OPVAL, METHCALL),
DEBUG_TOKEN (OPVAL, METHCALL0),
DEBUG_TOKEN (OPNUM, MULOP),
DEBUG_TOKEN (OPNUM, NCEQOP),
DEBUG_TOKEN (OPNUM, NCRELOP),
DEBUG_TOKEN (NONE, NOAMP),
DEBUG_TOKEN (NONE, NOTOP),
DEBUG_TOKEN (IVAL, OROP),
DEBUG_TOKEN (IVAL, OROR),
DEBUG_TOKEN (IVAL, PERLY_AMPERSAND),
DEBUG_TOKEN (IVAL, PERLY_BRACE_CLOSE),
DEBUG_TOKEN (IVAL, PERLY_BRACE_OPEN),
DEBUG_TOKEN (IVAL, PERLY_BRACKET_CLOSE),
DEBUG_TOKEN (IVAL, PERLY_BRACKET_OPEN),
DEBUG_TOKEN (IVAL, PERLY_COLON),
DEBUG_TOKEN (IVAL, PERLY_COMMA),
DEBUG_TOKEN (IVAL, PERLY_DOT),
DEBUG_TOKEN (IVAL, PERLY_EQUAL_SIGN),
DEBUG_TOKEN (IVAL, PERLY_EXCLAMATION_MARK),
DEBUG_TOKEN (IVAL, PERLY_MINUS),
DEBUG_TOKEN (IVAL, PERLY_PAREN_OPEN),
DEBUG_TOKEN (IVAL, PERLY_PERCENT_SIGN),
DEBUG_TOKEN (IVAL, PERLY_PLUS),
DEBUG_TOKEN (IVAL, PERLY_QUESTION_MARK),
DEBUG_TOKEN (IVAL, PERLY_SEMICOLON),
DEBUG_TOKEN (IVAL, PERLY_SLASH),
DEBUG_TOKEN (IVAL, PERLY_SNAIL),
DEBUG_TOKEN (IVAL, PERLY_STAR),
DEBUG_TOKEN (IVAL, PERLY_TILDE),
DEBUG_TOKEN (OPVAL, PLUGEXPR),
DEBUG_TOKEN (OPVAL, PLUGSTMT),
DEBUG_TOKEN (PVAL, PLUGIN_ADD_OP),
DEBUG_TOKEN (PVAL, PLUGIN_ASSIGN_OP),
DEBUG_TOKEN (PVAL, PLUGIN_HIGH_OP),
DEBUG_TOKEN (PVAL, PLUGIN_LOGICAL_AND_OP),
DEBUG_TOKEN (PVAL, PLUGIN_LOGICAL_OR_OP),
DEBUG_TOKEN (PVAL, PLUGIN_LOGICAL_AND_LOW_OP),
DEBUG_TOKEN (PVAL, PLUGIN_LOGICAL_OR_LOW_OP),
DEBUG_TOKEN (PVAL, PLUGIN_LOW_OP),
DEBUG_TOKEN (PVAL, PLUGIN_MUL_OP),
DEBUG_TOKEN (PVAL, PLUGIN_POW_OP),
DEBUG_TOKEN (PVAL, PLUGIN_REL_OP),
DEBUG_TOKEN (OPVAL, PMFUNC),
DEBUG_TOKEN (NONE, POSTJOIN),
DEBUG_TOKEN (NONE, POSTDEC),
DEBUG_TOKEN (NONE, POSTINC),
DEBUG_TOKEN (OPNUM, POWOP),
DEBUG_TOKEN (NONE, PREDEC),
DEBUG_TOKEN (NONE, PREINC),
DEBUG_TOKEN (OPVAL, PRIVATEREF),
DEBUG_TOKEN (OPVAL, QWLIST),
DEBUG_TOKEN (NONE, REFGEN),
DEBUG_TOKEN (OPNUM, SHIFTOP),
DEBUG_TOKEN (NONE, SUBLEXEND),
DEBUG_TOKEN (NONE, SUBLEXSTART),
DEBUG_TOKEN (OPVAL, THING),
DEBUG_TOKEN (NONE, UMINUS),
DEBUG_TOKEN (OPNUM, UNIOP),
DEBUG_TOKEN (OPVAL, UNIOPSUB),
DEBUG_TOKEN (OPVAL, BAREWORD),
DEBUG_TOKEN (IVAL, YADAYADA),
{ 0, TOKENTYPE_NONE, NULL }
};
#undef DEBUG_TOKEN
/* dump the returned token in rv, plus any optional arg in pl_yylval */
STATIC int
S_tokereport(pTHX_ I32 rv, const YYSTYPE* lvalp)
{
PERL_ARGS_ASSERT_TOKEREPORT;
if (DEBUG_T_TEST) {
const char *name = NULL;
enum token_type type = TOKENTYPE_NONE;
const struct debug_tokens *p;
SV* const report = newSVpvs("<== ");
for (p = debug_tokens; p->token; p++) {
if (p->token == (int)rv) {
name = p->name;
type = p->type;
break;
}
}
if (name)
Perl_sv_catpv(aTHX_ report, name);
else if (isGRAPH(rv))
{
Perl_sv_catpvf(aTHX_ report, "'%c'", (char)rv);
if ((char)rv == 'p')
sv_catpvs(report, " (pending identifier)");
}
else if (!rv)
sv_catpvs(report, "EOF");
else
Perl_sv_catpvf(aTHX_ report, "?? %" IVdf, (IV)rv);
switch (type) {
case TOKENTYPE_NONE:
break;
case TOKENTYPE_IVAL:
Perl_sv_catpvf(aTHX_ report, "(ival=%" IVdf ")", (IV)lvalp->ival);
break;
case TOKENTYPE_OPNUM:
Perl_sv_catpvf(aTHX_ report, "(ival=op_%s)",
PL_op_name[lvalp->ival]);
break;
case TOKENTYPE_PVAL:
Perl_sv_catpvf(aTHX_ report, "(pval=%p)", lvalp->pval);
break;
case TOKENTYPE_OPVAL:
if (lvalp->opval) {
Perl_sv_catpvf(aTHX_ report, "(opval=op_%s)",
PL_op_name[lvalp->opval->op_type]);
if (lvalp->opval->op_type == OP_CONST) {
Perl_sv_catpvf(aTHX_ report, " %s",
SvPEEK(cSVOPx_sv(lvalp->opval)));
}
}
else
sv_catpvs(report, "(opval=null)");
break;
}
PerlIO_printf(Perl_debug_log, "### %s\n\n", SvPV_nolen_const(report));
};
return (int)rv;
}
/* print the buffer with suitable escapes */
STATIC void
S_printbuf(pTHX_ const char *const fmt, const char *const s)
{
SV* const tmp = newSVpvs("");
PERL_ARGS_ASSERT_PRINTBUF;
GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral); /* fmt checked by caller */
PerlIO_printf(Perl_debug_log, fmt, pv_display(tmp, s, strlen(s), 0, 60));
GCC_DIAG_RESTORE_STMT;
SvREFCNT_dec(tmp);
}
#endif
/*
* S_ao
*
* This subroutine looks for an '=' next to the operator that has just been
* parsed and turns it into an ASSIGNOP if it finds one.
*/
STATIC int
S_ao(pTHX_ int toketype)
{
if (*PL_bufptr == '=') {
PL_bufptr++;
switch (toketype) {
case ANDAND: pl_yylval.ival = OP_ANDASSIGN; break;
case OROR: pl_yylval.ival = OP_ORASSIGN; break;
case DORDOR: pl_yylval.ival = OP_DORASSIGN; break;
}
toketype = ASSIGNOP;
}
return REPORT(toketype);
}
/*
* S_warn_expect_operator
* When Perl expects an operator and finds something else, no_op
* prints the warning. It always prints "<something> found where
* operator expected. It prints "Missing semicolon on previous line?"
* if the surprise occurs at the start of the line. "do you need to
* predeclare ..." is printed out for code like "sub bar; foo bar $x"
* where the compiler doesn't know if foo is a method call or a function.
* It prints "Missing operator before end of line" if there's nothing
* after the missing operator, or "... before <...>" if there is something
* after the missing operator.
*
* PL_bufptr is expected to point to the start of the thing that was found,
* and s after the next token or partial token.
*/
#define POP_OLDBUFPTR TRUE
STATIC void
S_warn_expect_operator(pTHX_ const char *const what, char *s, I32 pop_oldbufptr)
{
if (PL_expect != XOPERATOR)
return;
if (pop_oldbufptr && PL_bufptr > s) {
s = PL_bufptr - 1;
PL_bufptr = PL_oldbufptr;
}
char * const oldbp = PL_bufptr;
const bool is_first = (PL_oldbufptr == PL_linestart);
SV *message = sv_2mortal( newSVpvf(
PERL_DIAG_WARN_SYNTAX("%s found where operator expected"),
what
) );
PERL_ARGS_ASSERT_WARN_EXPECT_OPERATOR;
if (!s)
s = oldbp;
else
PL_bufptr = s;
if (ckWARN_d(WARN_SYNTAX)) {
bool has_more = FALSE;
if (is_first) {
has_more = TRUE;
sv_catpvs(message,
" (Missing semicolon on previous line?)");
}
else if (PL_oldoldbufptr) {
/* yyerror (via yywarn) would do this itself, so we should too */
const char *t;
for (t = PL_oldoldbufptr;
t < PL_bufptr && isSPACE(*t);
t += UTF ? UTF8SKIP(t) : 1)
{
NOOP;
}
/* see if we can identify the cause of the warning */
if (isIDFIRST_lazy_if_safe(t,PL_bufend,UTF))
{
const char *t_start= t;
for ( ;
(isWORDCHAR_lazy_if_safe(t, PL_bufend, UTF) || *t == ':');
t += UTF ? UTF8SKIP(t) : 1)
{
NOOP;
}
if (t < PL_bufptr && isSPACE(*t)) {
has_more = TRUE;
sv_catpvf( message,
" (Do you need to predeclare \"%" UTF8f "\"?)",
UTF8fARG(UTF, t - t_start, t_start));
}
}
}
if (!has_more) {
const char *t= oldbp;
assert(s >= oldbp);
while (t < s && isSPACE(*t)) {
t += UTF ? UTF8SKIP(t) : 1;
}
sv_catpvf(message,
" (Missing operator before \"%" UTF8f "\"?)",
UTF8fARG(UTF, s - t, t));
}
}
yywarn(SvPV_nolen(message), UTF ? SVf_UTF8 : 0);
PL_bufptr = oldbp;
}
/*
* S_missingterm
* Complain about missing quote/regexp/heredoc terminator.
* If it's called with NULL then it cauterizes the line buffer.
* If we're in a delimited string and the delimiter is a control
* character, it's reformatted into a two-char sequence like ^C.
* This is fatal.
*/
STATIC void
S_missingterm(pTHX_ char *s, STRLEN len)
{
char tmpbuf[UTF8_MAXBYTES + 1];
char q;
bool uni = FALSE;
if (s) {
char * const nl = (char *) my_memrchr(s, '\n', len);
if (nl) {
*nl = '\0';
len = nl - s;
}
uni = UTF;
}
else if (PL_multi_close < 32) {
*tmpbuf = '^';
tmpbuf[1] = (char)toCTRL(PL_multi_close);
tmpbuf[2] = '\0';
s = tmpbuf;
len = 2;
}
else {
if (! UTF && LIKELY(PL_multi_close < 256)) {
*tmpbuf = (char)PL_multi_close;
tmpbuf[1] = '\0';
len = 1;
}
else {
char *end = (char *) uv_to_utf8((U8 *)tmpbuf, PL_multi_close);
*end = '\0';
len = end - tmpbuf;
uni = TRUE;
}
s = tmpbuf;
}
q = memchr(s, '"', len) ? '\'' : '"';
Perl_croak(aTHX_ "Can't find string terminator %c%" UTF8f "%c"
" anywhere before EOF", q, UTF8fARG(uni, len, s), q);
}
STATIC char *
S_scan_terminated(pTHX_ char *s, I32 ival) {
s = scan_str(s,FALSE,FALSE,FALSE,NULL);
if (!s)
missingterm(NULL, 0);
PL_parser->yylval.ival = ival;
return s;
}
#include "feature.h"
STATIC void
S_yyerror_non_ascii_message(pTHX_ const U8 * const s)
{
PERL_ARGS_ASSERT_YYERROR_NON_ASCII_MESSAGE;
yyerror_pv(Perl_form(aTHX_ "Use of non-ASCII character 0x%02X"
" illegal when 'use source::encoding"
" \"ascii\"' is in effect", *s), 0);
}
#ifndef EBCDIC /* On ASCII platforms, invariants are identical to ASCII; can
use faster method */
# define is_ascii_string_loc(s, len, ep) \
is_utf8_invariant_string_loc(s, len, ep)
#else
STATIC bool
S_is_ascii_string_loc(const U8 * const s0, STRLEN len, const U8 ** ep)
{
const U8 * s = s0;
const U8 * send = s0 + len;
while (s < send) {
if (isASCII(*s)) {
s++;
continue;
}
*ep = s;
return FALSE;
}
return TRUE;
}
# define is_ascii_string_loc(s, len, ep) \
S_is_ascii_string_loc(s, len, ep)
#endif
/*
=for apidoc lex_start
Creates and initialises a new lexer/parser state object, supplying
a context in which to lex and parse from a new source of Perl code.
A pointer to the new state object is placed in L</PL_parser>. An entry
is made on the save stack so that upon unwinding, the new state object
will be destroyed and the former value of L</PL_parser> will be restored.
Nothing else need be done to clean up the parsing context.
The code to be parsed comes from C<line> and C<rsfp>. C<line>, if
non-null, provides a string (in SV form) containing code to be parsed.
A copy of the string is made, so subsequent modification of C<line>
does not affect parsing. C<rsfp>, if non-null, provides an input stream
from which code will be read to be parsed. If both are non-null, the
code in C<line> comes first and must consist of complete lines of input,
and C<rsfp> supplies the remainder of the source.
The C<flags> parameter is reserved for future use. Currently it is only
used by perl internally, so extensions should always pass zero.
=cut
*/
/* LEX_START_SAME_FILTER indicates that this is not a new file, so it
can share filters with the current parser.
LEX_START_DONT_CLOSE indicates that the file handle wasn't opened by the
caller, hence isn't owned by the parser, so shouldn't be closed on parser
destruction. This is used to handle the case of defaulting to reading the
script from the standard input because no filename was given on the command
line (without getting confused by situation where STDIN has been closed, so
the script handle is opened on fd 0) */
void
Perl_lex_start(pTHX_ SV *line, PerlIO *rsfp, U32 flags)
{
const char *s = NULL;
yy_parser *parser, *oparser;
if (flags && flags & ~LEX_START_FLAGS)
Perl_croak(aTHX_ "Lexing code internal error (%s)", "lex_start");
/* create and initialise a parser */
Newxz(parser, 1, yy_parser);
parser->old_parser = oparser = PL_parser;
PL_parser = parser;
parser->stack = NULL;
parser->stack_max1 = NULL;
parser->ps = NULL;
/* on scope exit, free this parser and restore any outer one */
SAVEPARSER(parser);
parser->saved_curcop = PL_curcop;
/* initialise lexer state */
parser->nexttoke = 0;
parser->error_count = oparser ? oparser->error_count : 0;
parser->copline = parser->preambling = NOLINE;
parser->lex_state = LEX_NORMAL;
parser->expect = XSTATE;
parser->rsfp = rsfp;
parser->recheck_charset_validity = TRUE;
parser->rsfp_filters =
!(flags & LEX_START_SAME_FILTER) || !oparser
? NULL
: AvREFCNT_inc(
oparser->rsfp_filters
? oparser->rsfp_filters
: (oparser->rsfp_filters = newAV())
);
Newx(parser->lex_brackstack, 120, char);
Newx(parser->lex_casestack, 12, char);
*parser->lex_casestack = '\0';
Newxz(parser->lex_shared, 1, LEXSHARED);
if (line) {
Size_t len;
const U8* first_bad_char_loc;
s = SvPV_const(line, len);
if ( SvUTF8(line)
&& UNLIKELY(! is_utf8_string_loc((U8 *) s,
SvCUR(line),
&first_bad_char_loc)))
{
force_out_malformed_utf8_message_(first_bad_char_loc,
(U8 *) s + SvCUR(line),
0,
MALFORMED_UTF8_DIE);
NOT_REACHED; /* NOTREACHED */
}
else if (ONLY_ASCII && UNLIKELY(! is_ascii_string_loc(
(const U8 *) s,
SvCUR(line),
&first_bad_char_loc)))
{
yyerror_non_ascii_message(first_bad_char_loc);
}
parser->linestr = flags & LEX_START_COPIED
? SvREFCNT_inc_simple_NN(line)
: newSVpvn_flags(s, len, SvUTF8(line));
if (!rsfp)
sv_catpvs(parser->linestr, "\n;");
} else {
parser->linestr = newSVpvn("\n;", rsfp ? 1 : 2);
}
parser->oldoldbufptr =
parser->oldbufptr =
parser->bufptr =
parser->linestart = SvPVX(parser->linestr);
parser->bufend = parser->bufptr + SvCUR(parser->linestr);
parser->last_lop = parser->last_uni = NULL;
STATIC_ASSERT_STMT(FITS_IN_8_BITS(LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
|LEX_DONT_CLOSE_RSFP));
parser->lex_flags = (U8) (flags & (LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES
|LEX_DONT_CLOSE_RSFP));
parser->in_pod = parser->filtered = 0;
}
/* delete a parser object */
void
Perl_parser_free(pTHX_ const yy_parser *parser)
{
PERL_ARGS_ASSERT_PARSER_FREE;
PL_curcop = parser->saved_curcop;
SvREFCNT_dec(parser->linestr);
if (PL_parser->lex_flags & LEX_DONT_CLOSE_RSFP)
PerlIO_clearerr(parser->rsfp);
else if (parser->rsfp && (!parser->old_parser
|| (parser->old_parser && parser->rsfp != parser->old_parser->rsfp)))
PerlIO_close(parser->rsfp);
SvREFCNT_dec(parser->rsfp_filters);
SvREFCNT_dec(parser->lex_stuff);
SvREFCNT_dec(parser->lex_sub_repl);
Safefree(parser->lex_brackstack);
Safefree(parser->lex_casestack);
Safefree(parser->lex_shared);
PL_parser = parser->old_parser;
Safefree(parser);
}
void