-
Notifications
You must be signed in to change notification settings - Fork 0
/
toke.c
7502 lines (6774 loc) · 182 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-2000, Larry Wall
*
* 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
*/
/*
* 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.
*/
#include "EXTERN.h"
#define PERL_IN_TOKE_C
#include "perl.h"
#define yychar PL_yychar
#define yylval PL_yylval
static char ident_too_long[] = "Identifier too long";
static void restore_rsfp(pTHXo_ void *f);
#ifndef PERL_NO_UTF16_FILTER
static I32 utf16_textfilter(pTHXo_ int idx, SV *sv, int maxlen);
static I32 utf16rev_textfilter(pTHXo_ int idx, SV *sv, int maxlen);
#endif
#define XFAKEBRACK 128
#define XENUMMASK 127
/*#define UTF (SvUTF8(PL_linestr) && !(PL_hints & HINT_BYTE))*/
#define UTF (PL_hints & HINT_UTF8)
/* In variables name $^X, these are the legal values for X.
* 1999-02-27 [email protected] */
#define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x)))
/* On MacOS, respect nonbreaking spaces */
#ifdef MACOS_TRADITIONAL
#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t')
#else
#define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t')
#endif
/* 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).
*/
/* #define LEX_NOTPARSING 11 is done in perl.h. */
#define LEX_NORMAL 10
#define LEX_INTERPNORMAL 9
#define LEX_INTERPCASEMOD 8
#define LEX_INTERPPUSH 7
#define LEX_INTERPSTART 6
#define LEX_INTERPEND 5
#define LEX_INTERPENDMAYBE 4
#define LEX_INTERPCONCAT 3
#define LEX_INTERPCONST 2
#define LEX_FORMLINE 1
#define LEX_KNOWNEXT 0
/* XXX If this causes problems, set i_unistd=undef in the hint file. */
#ifdef I_UNISTD
# include <unistd.h> /* Needed for execv() */
#endif
#ifdef ff_next
#undef ff_next
#endif
#ifdef USE_PURE_BISON
YYSTYPE* yylval_pointer = NULL;
int* yychar_pointer = NULL;
# undef yylval
# undef yychar
# define yylval (*yylval_pointer)
# define yychar (*yychar_pointer)
# define PERL_YYLEX_PARAM yylval_pointer,yychar_pointer
# undef yylex
# define yylex() Perl_yylex(aTHX_ yylval_pointer, yychar_pointer)
#endif
#include "keywords.h"
/* CLINE is a macro that ensures PL_copline has a sane value */
#ifdef CLINE
#undef CLINE
#endif
#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
* LOOPX : loop exiting command (goto, last, dump, etc)
* FTST : file test operator
* FUN0 : zero-argument function
* FUN1 : not used, except for not, which isn't a UNIOP
* BOop : bitwise or or xor
* BAop : bitwise and
* SHop : shift operator
* PWop : power operator
* PMop : pattern-matching operator
* Aop : addition-level operator
* Mop : multiplication-level operator
* Eop : equality-testing operator
* Rop : relational operator <= != gt
*
* Also see LOP and lop() below.
*/
#define TOKEN(retval) return (PL_bufptr = s,(int)retval)
#define OPERATOR(retval) return (PL_expect = XTERM,PL_bufptr = s,(int)retval)
#define AOPERATOR(retval) return ao((PL_expect = XTERM,PL_bufptr = s,(int)retval))
#define PREBLOCK(retval) return (PL_expect = XBLOCK,PL_bufptr = s,(int)retval)
#define PRETERMBLOCK(retval) return (PL_expect = XTERMBLOCK,PL_bufptr = s,(int)retval)
#define PREREF(retval) return (PL_expect = XREF,PL_bufptr = s,(int)retval)
#define TERM(retval) return (CLINE, PL_expect = XOPERATOR,PL_bufptr = s,(int)retval)
#define LOOPX(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LOOPEX)
#define FTST(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)UNIOP)
#define FUN0(f) return(yylval.ival = f,PL_expect = XOPERATOR,PL_bufptr = s,(int)FUNC0)
#define FUN1(f) return(yylval.ival = f,PL_expect = XOPERATOR,PL_bufptr = s,(int)FUNC1)
#define BOop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)BITOROP))
#define BAop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)BITANDOP))
#define SHop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)SHIFTOP))
#define PWop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)POWOP))
#define PMop(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)MATCHOP)
#define Aop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)ADDOP))
#define Mop(f) return ao((yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)MULOP))
#define Eop(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)EQOP)
#define Rop(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)RELOP)
/* This bit of chicanery makes a unary function followed by
* a parenthesis into a function with one argument, highest precedence.
*/
#define UNI(f) return(yylval.ival = f, \
PL_expect = XTERM, \
PL_bufptr = s, \
PL_last_uni = PL_oldbufptr, \
PL_last_lop_op = f, \
(*s == '(' || (s = skipspace(s), *s == '(') ? (int)FUNC1 : (int)UNIOP) )
#define UNIBRACK(f) return(yylval.ival = f, \
PL_bufptr = s, \
PL_last_uni = PL_oldbufptr, \
(*s == '(' || (s = skipspace(s), *s == '(') ? (int)FUNC1 : (int)UNIOP) )
/* grandfather return to old style */
#define OLDLOP(f) return(yylval.ival=f,PL_expect = XTERM,PL_bufptr = s,(int)LSTOP)
/*
* S_ao
*
* This subroutine detects &&= and ||= and turns an ANDAND or OROR
* into an OP_ANDASSIGN or OP_ORASSIGN
*/
STATIC int
S_ao(pTHX_ int toketype)
{
if (*PL_bufptr == '=') {
PL_bufptr++;
if (toketype == ANDAND)
yylval.ival = OP_ANDASSIGN;
else if (toketype == OROR)
yylval.ival = OP_ORASSIGN;
toketype = ASSIGNOP;
}
return toketype;
}
/*
* S_no_op
* 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.
*/
STATIC void
S_no_op(pTHX_ char *what, char *s)
{
char *oldbp = PL_bufptr;
bool is_first = (PL_oldbufptr == PL_linestart);
if (!s)
s = oldbp;
else {
assert(s >= oldbp);
PL_bufptr = s;
}
yywarn(Perl_form(aTHX_ "%s found where operator expected", what));
if (is_first)
Perl_warn(aTHX_ "\t(Missing semicolon on previous line?)\n");
else if (PL_oldoldbufptr && isIDFIRST_lazy_if(PL_oldoldbufptr,UTF)) {
char *t;
for (t = PL_oldoldbufptr; *t && (isALNUM_lazy_if(t,UTF) || *t == ':'); t++) ;
if (t < PL_bufptr && isSPACE(*t))
Perl_warn(aTHX_ "\t(Do you need to predeclare %.*s?)\n",
t - PL_oldoldbufptr, PL_oldoldbufptr);
}
else
Perl_warn(aTHX_ "\t(Missing operator before %.*s?)\n", s - oldbp, oldbp);
PL_bufptr = oldbp;
}
/*
* S_missingterm
* Complain about missing quote/regexp/heredoc terminator.
* If it's called with (char *)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)
{
char tmpbuf[3];
char q;
if (s) {
char *nl = strrchr(s,'\n');
if (nl)
*nl = '\0';
}
else if (
#ifdef EBCDIC
iscntrl(PL_multi_close)
#else
PL_multi_close < 32 || PL_multi_close == 127
#endif
) {
*tmpbuf = '^';
tmpbuf[1] = toCTRL(PL_multi_close);
s = "\\n";
tmpbuf[2] = '\0';
s = tmpbuf;
}
else {
*tmpbuf = PL_multi_close;
tmpbuf[1] = '\0';
s = tmpbuf;
}
q = strchr(s,'"') ? '\'' : '"';
Perl_croak(aTHX_ "Can't find string terminator %c%s%c anywhere before EOF",q,s,q);
}
/*
* Perl_deprecate
*/
void
Perl_deprecate(pTHX_ char *s)
{
dTHR;
if (ckWARN(WARN_DEPRECATED))
Perl_warner(aTHX_ WARN_DEPRECATED, "Use of %s is deprecated", s);
}
/*
* depcom
* Deprecate a comma-less variable list.
*/
STATIC void
S_depcom(pTHX)
{
deprecate("comma-less variable list");
}
/*
* experimental text filters for win32 carriage-returns, utf16-to-utf8 and
* utf16-to-utf8-reversed.
*/
#ifdef PERL_CR_FILTER
static void
strip_return(SV *sv)
{
register char *s = SvPVX(sv);
register char *e = s + SvCUR(sv);
/* outer loop optimized to do nothing if there are no CR-LFs */
while (s < e) {
if (*s++ == '\r' && *s == '\n') {
/* hit a CR-LF, need to copy the rest */
register char *d = s - 1;
*d++ = *s++;
while (s < e) {
if (*s == '\r' && s[1] == '\n')
s++;
*d++ = *s++;
}
SvCUR(sv) -= s - d;
return;
}
}
}
STATIC I32
S_cr_textfilter(pTHX_ int idx, SV *sv, int maxlen)
{
I32 count = FILTER_READ(idx+1, sv, maxlen);
if (count > 0 && !maxlen)
strip_return(sv);
return count;
}
#endif
/*
* Perl_lex_start
* Initialize variables. Uses the Perl save_stack to save its state (for
* recursive calls to the parser).
*/
void
Perl_lex_start(pTHX_ SV *line)
{
dTHR;
char *s;
STRLEN len;
SAVEI32(PL_lex_dojoin);
SAVEI32(PL_lex_brackets);
SAVEI32(PL_lex_casemods);
SAVEI32(PL_lex_starts);
SAVEI32(PL_lex_state);
SAVEVPTR(PL_lex_inpat);
SAVEI32(PL_lex_inwhat);
if (PL_lex_state == LEX_KNOWNEXT) {
I32 toke = PL_nexttoke;
while (--toke >= 0) {
SAVEI32(PL_nexttype[toke]);
SAVEVPTR(PL_nextval[toke]);
}
SAVEI32(PL_nexttoke);
PL_nexttoke = 0;
}
SAVECOPLINE(PL_curcop);
SAVEPPTR(PL_bufptr);
SAVEPPTR(PL_bufend);
SAVEPPTR(PL_oldbufptr);
SAVEPPTR(PL_oldoldbufptr);
SAVEPPTR(PL_linestart);
SAVESPTR(PL_linestr);
SAVEPPTR(PL_lex_brackstack);
SAVEPPTR(PL_lex_casestack);
SAVEDESTRUCTOR_X(restore_rsfp, PL_rsfp);
SAVESPTR(PL_lex_stuff);
SAVEI32(PL_lex_defer);
SAVEI32(PL_sublex_info.sub_inwhat);
SAVESPTR(PL_lex_repl);
SAVEINT(PL_expect);
SAVEINT(PL_lex_expect);
PL_lex_state = LEX_NORMAL;
PL_lex_defer = 0;
PL_expect = XSTATE;
PL_lex_brackets = 0;
New(899, PL_lex_brackstack, 120, char);
New(899, PL_lex_casestack, 12, char);
SAVEFREEPV(PL_lex_brackstack);
SAVEFREEPV(PL_lex_casestack);
PL_lex_casemods = 0;
*PL_lex_casestack = '\0';
PL_lex_dojoin = 0;
PL_lex_starts = 0;
PL_lex_stuff = Nullsv;
PL_lex_repl = Nullsv;
PL_lex_inpat = 0;
PL_lex_inwhat = 0;
PL_sublex_info.sub_inwhat = 0;
PL_linestr = line;
if (SvREADONLY(PL_linestr))
PL_linestr = sv_2mortal(newSVsv(PL_linestr));
s = SvPV(PL_linestr, len);
if (len && s[len-1] != ';') {
if (!(SvFLAGS(PL_linestr) & SVs_TEMP))
PL_linestr = sv_2mortal(newSVsv(PL_linestr));
sv_catpvn(PL_linestr, "\n;", 2);
}
SvTEMP_off(PL_linestr);
PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = PL_linestart = SvPVX(PL_linestr);
PL_bufend = PL_bufptr + SvCUR(PL_linestr);
SvREFCNT_dec(PL_rs);
PL_rs = newSVpvn("\n", 1);
PL_rsfp = 0;
}
/*
* Perl_lex_end
* Finalizer for lexing operations. Must be called when the parser is
* done with the lexer.
*/
void
Perl_lex_end(pTHX)
{
PL_doextract = FALSE;
}
/*
* S_incline
* This subroutine has nothing to do with tilting, whether at windmills
* or pinball tables. Its name is short for "increment line". It
* increments the current line number in CopLINE(PL_curcop) and checks
* to see whether the line starts with a comment of the form
* # line 500 "foo.pm"
* If so, it sets the current line number and file to the values in the comment.
*/
STATIC void
S_incline(pTHX_ char *s)
{
dTHR;
char *t;
char *n;
char *e;
char ch;
CopLINE_inc(PL_curcop);
if (*s++ != '#')
return;
while (SPACE_OR_TAB(*s)) s++;
if (strnEQ(s, "line", 4))
s += 4;
else
return;
if (*s == ' ' || *s == '\t')
s++;
else
return;
while (SPACE_OR_TAB(*s)) s++;
if (!isDIGIT(*s))
return;
n = s;
while (isDIGIT(*s))
s++;
while (SPACE_OR_TAB(*s))
s++;
if (*s == '"' && (t = strchr(s+1, '"'))) {
s++;
e = t + 1;
}
else {
for (t = s; !isSPACE(*t); t++) ;
e = t;
}
while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f')
e++;
if (*e != '\n' && *e != '\0')
return; /* false alarm */
ch = *t;
*t = '\0';
if (t - s > 0) {
#ifdef USE_ITHREADS
Safefree(CopFILE(PL_curcop));
#else
SvREFCNT_dec(CopFILEGV(PL_curcop));
#endif
CopFILE_set(PL_curcop, s);
}
*t = ch;
CopLINE_set(PL_curcop, atoi(n)-1);
}
/*
* S_skipspace
* Called to gobble the appropriate amount and type of whitespace.
* Skips comments as well.
*/
STATIC char *
S_skipspace(pTHX_ register char *s)
{
dTHR;
if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) {
while (s < PL_bufend && SPACE_OR_TAB(*s))
s++;
return s;
}
for (;;) {
STRLEN prevlen;
SSize_t oldprevlen, oldoldprevlen;
SSize_t oldloplen, oldunilen;
while (s < PL_bufend && isSPACE(*s)) {
if (*s++ == '\n' && PL_in_eval && !PL_rsfp)
incline(s);
}
/* comment */
if (s < PL_bufend && *s == '#') {
while (s < PL_bufend && *s != '\n')
s++;
if (s < PL_bufend) {
s++;
if (PL_in_eval && !PL_rsfp) {
incline(s);
continue;
}
}
}
/* only continue to recharge the buffer if we're at the end
* of the buffer, we're not reading from a source filter, and
* we're in normal lexing mode
*/
if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat ||
PL_lex_state == LEX_FORMLINE)
return s;
/* try to recharge the buffer */
if ((s = filter_gets(PL_linestr, PL_rsfp,
(prevlen = SvCUR(PL_linestr)))) == Nullch)
{
/* end of file. Add on the -p or -n magic */
if (PL_minus_n || PL_minus_p) {
sv_setpv(PL_linestr,PL_minus_p ?
";}continue{print or die qq(-p destination: $!\\n)" :
"");
sv_catpv(PL_linestr,";}");
PL_minus_n = PL_minus_p = 0;
}
else
sv_setpv(PL_linestr,";");
/* reset variables for next time we lex */
PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart
= SvPVX(PL_linestr);
PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
/* Close the filehandle. Could be from -P preprocessor,
* STDIN, or a regular file. If we were reading code from
* STDIN (because the commandline held no -e or filename)
* then we don't close it, we reset it so the code can
* read from STDIN too.
*/
if (PL_preprocess && !PL_in_eval)
(void)PerlProc_pclose(PL_rsfp);
else if ((PerlIO*)PL_rsfp == PerlIO_stdin())
PerlIO_clearerr(PL_rsfp);
else
(void)PerlIO_close(PL_rsfp);
PL_rsfp = Nullfp;
return s;
}
/* not at end of file, so we only read another line */
/* make corresponding updates to old pointers, for yyerror() */
oldprevlen = PL_oldbufptr - PL_bufend;
oldoldprevlen = PL_oldoldbufptr - PL_bufend;
if (PL_last_uni)
oldunilen = PL_last_uni - PL_bufend;
if (PL_last_lop)
oldloplen = PL_last_lop - PL_bufend;
PL_linestart = PL_bufptr = s + prevlen;
PL_bufend = s + SvCUR(PL_linestr);
s = PL_bufptr;
PL_oldbufptr = s + oldprevlen;
PL_oldoldbufptr = s + oldoldprevlen;
if (PL_last_uni)
PL_last_uni = s + oldunilen;
if (PL_last_lop)
PL_last_lop = s + oldloplen;
incline(s);
/* debugger active and we're not compiling the debugger code,
* so store the line into the debugger's array of lines
*/
if (PERLDB_LINE && PL_curstash != PL_debstash) {
SV *sv = NEWSV(85,0);
sv_upgrade(sv, SVt_PVMG);
sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv);
}
}
}
/*
* S_check_uni
* Check the unary operators to ensure there's no ambiguity in how they're
* used. An ambiguous piece of code would be:
* rand + 5
* This doesn't mean rand() + 5. Because rand() is a unary operator,
* the +5 is its argument.
*/
STATIC void
S_check_uni(pTHX)
{
char *s;
char *t;
dTHR;
if (PL_oldoldbufptr != PL_last_uni)
return;
while (isSPACE(*PL_last_uni))
PL_last_uni++;
for (s = PL_last_uni; isALNUM_lazy_if(s,UTF) || *s == '-'; s++) ;
if ((t = strchr(s, '(')) && t < PL_bufptr)
return;
if (ckWARN_d(WARN_AMBIGUOUS)){
char ch = *s;
*s = '\0';
Perl_warner(aTHX_ WARN_AMBIGUOUS,
"Warning: Use of \"%s\" without parens is ambiguous",
PL_last_uni);
*s = ch;
}
}
/* workaround to replace the UNI() macro with a function. Only the
* hints/uts.sh file mentions this. Other comments elsewhere in the
* source indicate Microport Unix might need it too.
*/
#ifdef CRIPPLED_CC
#undef UNI
#define UNI(f) return uni(f,s)
STATIC int
S_uni(pTHX_ I32 f, char *s)
{
yylval.ival = f;
PL_expect = XTERM;
PL_bufptr = s;
PL_last_uni = PL_oldbufptr;
PL_last_lop_op = f;
if (*s == '(')
return FUNC1;
s = skipspace(s);
if (*s == '(')
return FUNC1;
else
return UNIOP;
}
#endif /* CRIPPLED_CC */
/*
* LOP : macro to build a list operator. Its behaviour has been replaced
* with a subroutine, S_lop() for which LOP is just another name.
*/
#define LOP(f,x) return lop(f,x,s)
/*
* S_lop
* Build a list operator (or something that might be one). The rules:
* - if we have a next token, then it's a list operator [why?]
* - if the next thing is an opening paren, then it's a function
* - else it's a list operator
*/
STATIC I32
S_lop(pTHX_ I32 f, int x, char *s)
{
dTHR;
yylval.ival = f;
CLINE;
PL_expect = x;
PL_bufptr = s;
PL_last_lop = PL_oldbufptr;
PL_last_lop_op = f;
if (PL_nexttoke)
return LSTOP;
if (*s == '(')
return FUNC;
s = skipspace(s);
if (*s == '(')
return FUNC;
else
return LSTOP;
}
/*
* S_force_next
* When the lexer realizes it knows the next token (for instance,
* it is reordering tokens for the parser) then it can call S_force_next
* to know what token to return the next time the lexer is called. Caller
* will need to set PL_nextval[], and possibly PL_expect to ensure the lexer
* handles the token correctly.
*/
STATIC void
S_force_next(pTHX_ I32 type)
{
PL_nexttype[PL_nexttoke] = type;
PL_nexttoke++;
if (PL_lex_state != LEX_KNOWNEXT) {
PL_lex_defer = PL_lex_state;
PL_lex_expect = PL_expect;
PL_lex_state = LEX_KNOWNEXT;
}
}
/*
* S_force_word
* When the lexer knows the next thing is a word (for instance, it has
* just seen -> and it knows that the next char is a word char, then
* it calls S_force_word to stick the next word into the PL_next lookahead.
*
* Arguments:
* char *start : buffer position (must be within PL_linestr)
* int token : PL_next will be this type of bare word (e.g., METHOD,WORD)
* int check_keyword : if true, Perl checks to make sure the word isn't
* a keyword (do this if the word is a label, e.g. goto FOO)
* int allow_pack : if true, : characters will also be allowed (require,
* use, etc. do this)
* int allow_initial_tick : used by the "sub" lexer only.
*/
STATIC char *
S_force_word(pTHX_ register char *start, int token, int check_keyword, int allow_pack, int allow_initial_tick)
{
register char *s;
STRLEN len;
start = skipspace(start);
s = start;
if (isIDFIRST_lazy_if(s,UTF) ||
(allow_pack && *s == ':') ||
(allow_initial_tick && *s == '\'') )
{
s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, allow_pack, &len);
if (check_keyword && keyword(PL_tokenbuf, len))
return start;
if (token == METHOD) {
s = skipspace(s);
if (*s == '(')
PL_expect = XTERM;
else {
PL_expect = XOPERATOR;
}
}
PL_nextval[PL_nexttoke].opval = (OP*)newSVOP(OP_CONST,0, newSVpv(PL_tokenbuf,0));
PL_nextval[PL_nexttoke].opval->op_private |= OPpCONST_BARE;
force_next(token);
}
return s;
}
/*
* S_force_ident
* Called when the lexer wants $foo *foo &foo etc, but the program
* text only contains the "foo" portion. The first argument is a pointer
* to the "foo", and the second argument is the type symbol to prefix.
* Forces the next token to be a "WORD".
* Creates the symbol if it didn't already exist (via gv_fetchpv()).
*/
STATIC void
S_force_ident(pTHX_ register char *s, int kind)
{
if (s && *s) {
OP* o = (OP*)newSVOP(OP_CONST, 0, newSVpv(s,0));
PL_nextval[PL_nexttoke].opval = o;
force_next(WORD);
if (kind) {
dTHR; /* just for in_eval */
o->op_private = OPpCONST_ENTERED;
/* XXX see note in pp_entereval() for why we forgo typo
warnings if the symbol must be introduced in an eval.
GSAR 96-10-12 */
gv_fetchpv(s, PL_in_eval ? (GV_ADDMULTI | GV_ADDINEVAL) : TRUE,
kind == '$' ? SVt_PV :
kind == '@' ? SVt_PVAV :
kind == '%' ? SVt_PVHV :
SVt_PVGV
);
}
}
}
NV
Perl_str_to_version(pTHX_ SV *sv)
{
NV retval = 0.0;
NV nshift = 1.0;
STRLEN len;
char *start = SvPVx(sv,len);
bool utf = SvUTF8(sv) ? TRUE : FALSE;
char *end = start + len;
while (start < end) {
I32 skip;
UV n;
if (utf)
n = utf8_to_uv((U8*)start, &skip);
else {
n = *(U8*)start;
skip = 1;
}
retval += ((NV)n)/nshift;
start += skip;
nshift *= 1000;
}
return retval;
}
/*
* S_force_version
* Forces the next token to be a version number.
*/
STATIC char *
S_force_version(pTHX_ char *s)
{
OP *version = Nullop;
char *d;
s = skipspace(s);
d = s;
if (*d == 'v')
d++;
if (isDIGIT(*d)) {
for (; isDIGIT(*d) || *d == '_' || *d == '.'; d++);
if (*d == ';' || isSPACE(*d) || *d == '}' || !*d) {
SV *ver;
s = scan_num(s);
version = yylval.opval;
ver = cSVOPx(version)->op_sv;
if (SvPOK(ver) && !SvNIOK(ver)) {
(void)SvUPGRADE(ver, SVt_PVNV);
SvNVX(ver) = str_to_version(ver);
SvNOK_on(ver); /* hint that it is a version */
}
}
}
/* NOTE: The parser sees the package name and the VERSION swapped */
PL_nextval[PL_nexttoke].opval = version;
force_next(WORD);
return (s);
}
/*
* S_tokeq
* Tokenize a quoted string passed in as an SV. It finds the next
* chunk, up to end of string or a backslash. It may make a new
* SV containing that chunk (if HINT_NEW_STRING is on). It also
* turns \\ into \.
*/
STATIC SV *
S_tokeq(pTHX_ SV *sv)
{
register char *s;
register char *send;
register char *d;
STRLEN len = 0;
SV *pv = sv;
if (!SvLEN(sv))
goto finish;
s = SvPV_force(sv, len);
if (SvTYPE(sv) >= SVt_PVIV && SvIVX(sv) == -1)
goto finish;
send = s + len;
while (s < send && *s != '\\')
s++;
if (s == send)
goto finish;
d = s;
if ( PL_hints & HINT_NEW_STRING )
pv = sv_2mortal(newSVpvn(SvPVX(pv), len));
while (s < send) {
if (*s == '\\') {
if (s + 1 < send && (s[1] == '\\'))
s++; /* all that, just for this */
}
*d++ = *s++;
}
*d = '\0';
SvCUR_set(sv, d - SvPVX(sv));
finish:
if ( PL_hints & HINT_NEW_STRING )
return new_constant(NULL, 0, "q", sv, pv, "q");
return sv;
}
/*
* Now come three functions related to double-quote context,
* S_sublex_start, S_sublex_push, and S_sublex_done. They're used when
* converting things like "\u\Lgnat" into ucfirst(lc("gnat")). They
* interact with PL_lex_state, and create fake ( ... ) argument lists
* to handle functions and concatenation.
* They assume that whoever calls them will be setting up a fake
* join call, because each subthing puts a ',' after it. This lets
* "lower \luPpEr"
* become
* join($, , 'lower ', lcfirst( 'uPpEr', ) ,)
*
* (I'm not sure whether the spurious commas at the end of lcfirst's
* arguments and join's arguments are created or not).
*/
/*
* S_sublex_start
* Assumes that yylval.ival is the op we're creating (e.g. OP_LCFIRST).
*
* Pattern matching will set PL_lex_op to the pattern-matching op to
* make (we return THING if yylval.ival is OP_NULL, PMFUNC otherwise).
*
* OP_CONST and OP_READLINE are easy--just make the new op and return.
*
* Everything else becomes a FUNC.
*
* Sets PL_lex_state to LEX_INTERPPUSH unless (ival was OP_NULL or we
* had an OP_CONST or OP_READLINE). This just sets us up for a
* call to S_sublex_push().
*/
STATIC I32
S_sublex_start(pTHX)
{
register I32 op_type = yylval.ival;
if (op_type == OP_NULL) {
yylval.opval = PL_lex_op;
PL_lex_op = Nullop;
return THING;
}
if (op_type == OP_CONST || op_type == OP_READLINE) {
SV *sv = tokeq(PL_lex_stuff);
if (SvTYPE(sv) == SVt_PVIV) {
/* Overloaded constants, nothing fancy: Convert to SVt_PV: */
STRLEN len;
char *p;
SV *nsv;
p = SvPV(sv, len);
nsv = newSVpvn(p, len);
if (SvUTF8(sv))
SvUTF8_on(nsv);
SvREFCNT_dec(sv);
sv = nsv;
}
yylval.opval = (OP*)newSVOP(op_type, 0, sv);
PL_lex_stuff = Nullsv;
return THING;
}
PL_sublex_info.super_state = PL_lex_state;
PL_sublex_info.sub_inwhat = op_type;
PL_sublex_info.sub_op = PL_lex_op;
PL_lex_state = LEX_INTERPPUSH;
PL_expect = XTERM;
if (PL_lex_op) {
yylval.opval = PL_lex_op;
PL_lex_op = Nullop;
return PMFUNC;
}
else
return FUNC;
}
/*
* S_sublex_push
* Create a new scope to save the lexing state. The scope will be
* ended in S_sublex_done. Returns a '(', starting the function arguments
* to the uc, lc, etc. found before.
* Sets PL_lex_state to LEX_INTERPCONCAT.
*/
STATIC I32
S_sublex_push(pTHX)
{
dTHR;