X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=perly.y;h=99493cec440009ef596d1a004a830c1384fece0b;hb=0448a0bdbfbd5e00696d6b3361b97fb1190434c1;hp=48049eb83e632f8ac1395a7b2caf3951a9dfdad4;hpb=eb1102fcca2230364ceadea29bd8e87ee51b15fa;p=p5sagit%2Fp5-mst-13.2.git diff --git a/perly.y b/perly.y index 48049eb..99493ce 100644 --- a/perly.y +++ b/perly.y @@ -1,6 +1,6 @@ /* perly.y * - * Copyright (c) 1991-2002, Larry Wall + * Copyright (c) 1991-2002, 2003, 2004 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. @@ -12,62 +12,31 @@ * All that is gold does not glitter, not all those who wander are lost.' */ -%{ -#include "EXTERN.h" -#define PERL_IN_PERLY_C -#include "perl.h" -#ifdef EBCDIC -#undef YYDEBUG -#endif -#define dep() deprecate("\"do\" to call subroutines") - -/* stuff included here to make perly_c.diff apply better */ - -#define yydebug PL_yydebug -#define yynerrs PL_yynerrs -#define yyerrflag PL_yyerrflag -#define yychar PL_yychar -#define yyval PL_yyval -#define yylval PL_yylval - -struct ysv { - short* yyss; - YYSTYPE* yyvs; - int oldyydebug; - int oldyynerrs; - int oldyyerrflag; - int oldyychar; - YYSTYPE oldyyval; - YYSTYPE oldyylval; -}; +/* This file holds the grammar for the Perl language. If edited, you need + * to run regen_perly.pl, which re-creates the files perly.h, perly.tab + * and perly.act which are derived from this. + * + * The main job of of this grammar is to call the various newFOO() + * functions in op.c to build a syntax tree of OP structs. + * It relies on the lexer in toke.c to do the tokenizing. + */ -static void yydestruct(pTHX_ void *ptr); +/* Make the parser re-entrant. */ -%} +%pure_parser %start prog -%{ -#if 0 /* get this from perly.h instead */ -%} - %union { I32 ival; char *pval; +#ifdef PERL_MAD + TOKEN* tkval; +#endif OP *opval; GV *gvval; } -%{ -#endif /* 0 */ - -#ifdef USE_PURE_BISON -#define YYLEX_PARAM (&yychar) -#define yylex yylex_r -#endif - -%} - %token '{' %token WORD METHOD FUNCMETH THING PMFUNC PRIVATEREF @@ -75,28 +44,30 @@ static void yydestruct(pTHX_ void *ptr); %token LABEL %token FORMAT SUB ANONSUB PACKAGE USE %token WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR +%token GIVEN WHEN DEFAULT %token LOOPEX DOTDOT %token FUNC0 FUNC1 FUNC UNIOP LSTOP %token RELOP EQOP MULOP ADDOP %token DOLSHARP DO HASHBRACK NOAMP -%token LOCAL MY MYSUB +%token LOCAL MY MYSUB REQUIRE %token COLONATTR -%type prog decl format startsub startanonsub startformsub -%type progstart remember mremember '&' +%type prog decl format startsub startanonsub startformsub mintro +%type progstart remember mremember '&' savescope mydefsv %type block mblock lineseq line loop cond else %type expr term subscripted scalar ary hsh arylen star amper sideff -%type argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr +%type argexpr nexpr texpr iexpr mexpr mnexpr miexpr %type listexpr listexprcom indirob listop method %type formname subname proto subbody cont my_scalar %type subattrlist myattrlist mysubrout myattrterm myterm %type termbinop termunop anonymous termdo +%type switch case %type label %nonassoc PREC_LOW %nonassoc LOOPEX -%left OROP +%left OROP DOROP %left ANDOP %right NOTOP %nonassoc LSTOP LSTOPSUB @@ -104,13 +75,14 @@ static void yydestruct(pTHX_ void *ptr); %right ASSIGNOP %right '?' ':' %nonassoc DOTDOT -%left OROR +%left OROR DORDOR %left ANDAND %left BITOROP %left BITANDOP %nonassoc EQOP %nonassoc RELOP %nonassoc UNIOP UNIOPSUB +%nonassoc REQUIRE %left SHIFTOP %left ADDOP %left MULOP @@ -123,6 +95,8 @@ static void yydestruct(pTHX_ void *ptr); %left '(' %left '[' '{' +%token PEG + %% /* RULES */ /* The whole program */ @@ -134,7 +108,7 @@ prog : progstart /* An ordinary block */ block : '{' remember lineseq '}' { if (PL_copline > (line_t)$1) - PL_copline = $1; + PL_copline = (line_t)$1; $$ = block_end($2, $3); } ; @@ -142,11 +116,12 @@ remember: /* NULL */ /* start a full lexical scope */ { $$ = block_start(TRUE); } ; +mydefsv: /* NULL */ /* lexicalize $_ */ + { $$ = (I32) allocmy("$_"); } + ; + progstart: { -#if defined(YYDEBUG) && defined(DEBUGGING) - yydebug = (DEBUG_p_TEST); -#endif PL_expect = XSTATE; $$ = block_start(TRUE); } ; @@ -154,7 +129,7 @@ progstart: mblock : '{' mremember lineseq '}' { if (PL_copline > (line_t)$1) - PL_copline = $1; + PL_copline = (line_t)$1; $$ = block_end($2, $3); } ; @@ -162,22 +137,30 @@ mremember: /* NULL */ /* start a partial lexical scope */ { $$ = block_start(FALSE); } ; +savescope: /* NULL */ /* remember stack pos in case of error */ + { $$ = PL_savestack_ix; } + /* A collection of "lines" in the program */ lineseq : /* NULL */ { $$ = Nullop; } | lineseq decl { $$ = $1; } - | lineseq line - { $$ = append_list(OP_LINESEQ, - (LISTOP*)$1, (LISTOP*)$2); + | lineseq savescope line + { LEAVE_SCOPE($2); + $$ = append_list(OP_LINESEQ, + (LISTOP*)$1, (LISTOP*)$3); PL_pad_reset_pending = TRUE; - if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; } + if ($1 && $3) PL_hints |= HINT_BLOCK_SCOPE; } ; /* A "line" in the program */ line : label cond { $$ = newSTATEOP(0, $1, $2); } | loop /* loops add their own labels */ + | switch /* ... and so do switches */ + { $$ = $1; } + | label case + { $$ = newSTATEOP(0, $1, $2); } | label ';' { if ($1 != Nullch) { $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0)); @@ -206,7 +189,7 @@ sideff : error | expr UNTIL iexpr { $$ = newLOOPOP(OPf_PARENS, 1, $3, $1);} | expr FOR expr - { $$ = newFOROP(0, Nullch, $2, + { $$ = newFOROP(0, Nullch, (line_t)$2, Nullop, $3, $1, Nullop); } ; @@ -216,22 +199,30 @@ else : /* NULL */ | ELSE mblock { ($2)->op_flags |= OPf_PARENS; $$ = scope($2); } | ELSIF '(' mexpr ')' mblock else - { PL_copline = $1; + { PL_copline = (line_t)$1; $$ = newCONDOP(0, $3, scope($5), $6); PL_hints |= HINT_BLOCK_SCOPE; } ; /* Real conditional expressions */ cond : IF '(' remember mexpr ')' mblock else - { PL_copline = $1; + { PL_copline = (line_t)$1; $$ = block_end($3, newCONDOP(0, $4, scope($6), $7)); } | UNLESS '(' remember miexpr ')' mblock else - { PL_copline = $1; + { PL_copline = (line_t)$1; $$ = block_end($3, newCONDOP(0, $4, scope($6), $7)); } ; +/* Cases for a switch statement */ +case : WHEN '(' remember mexpr ')' mblock + { $$ = block_end($3, + newWHENOP($4, scope($6))); } + | DEFAULT block + { $$ = newWHENOP(0, scope($2)); } + ; + /* Continue blocks */ cont : /* NULL */ { $$ = Nullop; } @@ -240,36 +231,37 @@ cont : /* NULL */ ; /* Loops: while, until, for, and a bare block */ -loop : label WHILE '(' remember mtexpr ')' mblock cont - { PL_copline = $2; +loop : label WHILE '(' remember texpr ')' mintro mblock cont + { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } - | label UNTIL '(' remember miexpr ')' mblock cont - { PL_copline = $2; + $2, $5, $8, $9, $7))); } + | label UNTIL '(' remember iexpr ')' mintro mblock cont + { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } + $2, $5, $8, $9, $7))); } | label FOR MY remember my_scalar '(' mexpr ')' mblock cont { $$ = block_end($4, - newFOROP(0, $1, $2, $5, $7, $9, $10)); } + newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); } | label FOR scalar '(' remember mexpr ')' mblock cont { $$ = block_end($5, - newFOROP(0, $1, $2, mod($3, OP_ENTERLOOP), + newFOROP(0, $1, (line_t)$2, mod($3, OP_ENTERLOOP), $6, $8, $9)); } | label FOR '(' remember mexpr ')' mblock cont { $$ = block_end($4, - newFOROP(0, $1, $2, Nullop, $5, $7, $8)); } - | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock + newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); } + | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')' + mblock /* basically fake up an initialize-while lineseq */ { OP *forop; - PL_copline = $2; + PL_copline = (line_t)$2; forop = newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, $2, scalar($7), - $11, $9)); + $12, $10, $9)); if ($5) { forop = append_elem(OP_LINESEQ, newSTATEOP(0, ($1?savepv($1):Nullch), @@ -281,9 +273,24 @@ loop : label WHILE '(' remember mtexpr ')' mblock cont | label block cont /* a block is a loop that happens once */ { $$ = newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - NOLINE, Nullop, $2, $3)); } + NOLINE, Nullop, $2, $3, 0)); } ; +/* Switch blocks */ +switch : label GIVEN '(' remember mydefsv mexpr ')' mblock + { PL_copline = (line_t) $2; + $$ = block_end($4, + newSTATEOP(0, $1, + newGIVENOP($6, scope($8), + (PADOFFSET) $5) )); } + ; + +/* determine whether there are any new my declarations */ +mintro : /* NULL */ + { $$ = (PL_min_intro_pending && + PL_max_intro_pending >= PL_min_intro_pending); + intro_my(); } + /* Normal expression */ nexpr : /* NULL */ { $$ = Nullop; } @@ -310,10 +317,6 @@ mnexpr : nexpr { $$ = $1; intro_my(); } ; -mtexpr : texpr - { $$ = $1; intro_my(); } - ; - miexpr : iexpr { $$ = $1; intro_my(); } ; @@ -338,7 +341,8 @@ decl : format ; format : FORMAT startformsub formname block - { newFORM($2, $3, $4); } + { SvREFCNT_inc(PL_compcv); + newFORM($2, $3, $4); } ; formname: WORD { $$ = $1; } @@ -347,30 +351,36 @@ formname: WORD { $$ = $1; } /* Unimplemented "my sub foo { }" */ mysubrout: MYSUB startsub subname proto subattrlist subbody - { newMYSUB($2, $3, $4, $5, $6); } + { SvREFCNT_inc(PL_compcv); + newMYSUB($2, $3, $4, $5, $6); } ; /* Subroutine definition */ subrout : SUB startsub subname proto subattrlist subbody - { newATTRSUB($2, $3, $4, $5, $6); } + { SvREFCNT_inc(PL_compcv); + newATTRSUB($2, $3, $4, $5, $6); } ; startsub: /* NULL */ /* start a regular subroutine scope */ - { $$ = start_subparse(FALSE, 0); } + { $$ = start_subparse(FALSE, 0); + SAVEFREESV(PL_compcv); } ; startanonsub: /* NULL */ /* start an anonymous subroutine scope */ - { $$ = start_subparse(FALSE, CVf_ANON); } + { $$ = start_subparse(FALSE, CVf_ANON); + SAVEFREESV(PL_compcv); } ; startformsub: /* NULL */ /* start a format subroutine scope */ - { $$ = start_subparse(TRUE, 0); } + { $$ = start_subparse(TRUE, 0); + SAVEFREESV(PL_compcv); } ; /* Name of a subroutine - must be a bareword, could be special */ -subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a); +subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv); if (strEQ(name, "BEGIN") || strEQ(name, "END") - || strEQ(name, "INIT") || strEQ(name, "CHECK")) + || strEQ(name, "INIT") || strEQ(name, "CHECK") + || strEQ(name, "UNITCHECK")) CvSPECIAL_on(PL_compcv); $$ = $1; } ; @@ -404,14 +414,13 @@ subbody : block { $$ = $1; } package : PACKAGE WORD ';' { package($2); } - | PACKAGE ';' - { package(Nullop); } ; use : USE startsub { CvSPECIAL_on(PL_compcv); /* It's a BEGIN {} */ } WORD WORD listexpr ';' - { utilize($1, $2, $4, $5, $6); } + { SvREFCNT_inc(PL_compcv); + utilize($1, $2, $4, $5, $6); } ; /* Ordinary expressions; logical combinations */ @@ -419,6 +428,8 @@ expr : expr ANDOP expr { $$ = newLOGOP(OP_AND, 0, $1, $3); } | expr OROP expr { $$ = newLOGOP($2, 0, $1, $3); } + | expr DOROP expr + { $$ = newLOGOP(OP_DOR, 0, $1, $3); } | argexpr %prec PREC_LOW ; @@ -431,7 +442,7 @@ argexpr : argexpr ',' ; /* List operators */ -listop : LSTOP indirob argexpr /* print $fh @args */ +listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */ { $$ = convert($1, OPf_STACKED, prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); } | FUNC '(' indirob expr ')' /* print ($fh @args */ @@ -460,8 +471,9 @@ listop : LSTOP indirob argexpr /* print $fh @args */ { $$ = convert($1, 0, $2); } | FUNC '(' listexprcom ')' /* print (@args) */ { $$ = convert($1, 0, $3); } - | LSTOPSUB startanonsub block /* map { foo } ... */ - { $3 = newANONATTRSUB($2, 0, Nullop, $3); } + | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ + { SvREFCNT_inc(PL_compcv); + $3 = newANONATTRSUB($2, 0, Nullop, $3); } listexpr %prec LSTOP /* ... @bar */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, @@ -477,7 +489,8 @@ method : METHOD subscripted: star '{' expr ';' '}' /* *main::{something} */ /* In this and all the hash accessors, ';' is * provided by the tokeniser */ - { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); } + { $$ = newBINOP(OP_GELEM, 0, $1, scalar($3)); + PL_expect = XOPERATOR; } | scalar '[' expr ']' /* $array[$element] */ { $$ = newBINOP(OP_AELEM, 0, oopsAV($1), scalar($3)); } | term ARROW '[' expr ']' /* somearef->[$element] */ @@ -516,6 +529,10 @@ subscripted: star '{' expr ';' '}' /* *main::{something} */ | subscripted '(' ')' /* $foo->{bar}->() */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, newCVREF(0, scalar($1))); } + | '(' expr ')' '[' expr ']' /* list slice */ + { $$ = newSLICEOP(0, $5, $2); } + | '(' ')' '[' expr ']' /* empty list slice! */ + { $$ = newSLICEOP(0, $4, Nullop); } ; /* Binary operators between terms */ @@ -545,6 +562,8 @@ termbinop : term ASSIGNOP term /* $x = $y */ { $$ = newLOGOP(OP_AND, 0, $1, $3); } | term OROR term /* $x || $y */ { $$ = newLOGOP(OP_OR, 0, $1, $3); } + | term DORDOR term /* $x // $y */ + { $$ = newLOGOP(OP_DOR, 0, $1, $3); } | term MATCHOP term /* $x =~ /$y/ */ { $$ = bind_match($2, $1, $3); } ; @@ -583,13 +602,14 @@ anonymous: '[' expr ']' | HASHBRACK ';' '}' %prec '(' /* { } (';' by tokener) */ { $$ = newANONHASH(Nullop); } | ANONSUB startanonsub proto subattrlist block %prec '(' - { $$ = newANONATTRSUB($2, $3, $4, $5); } + { SvREFCNT_inc(PL_compcv); + $$ = newANONATTRSUB($2, $3, $4, $5); } ; /* Things called with "do" */ termdo : DO term %prec UNIOP /* do $filename */ - { $$ = dofile($2); } + { $$ = dofile($2, $1); } | DO block %prec '(' /* do { code */ { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); } | DO WORD '(' ')' /* do somesub() */ @@ -649,10 +669,6 @@ term : termbinop { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} | subscripted { $$ = $1; } - | '(' expr ')' '[' expr ']' /* list slice */ - { $$ = newSLICEOP(0, $5, $2); } - | '(' ')' '[' expr ']' /* empty list slice! */ - { $$ = newSLICEOP(0, $4, Nullop); } | ary '[' expr ']' /* array slice */ { $$ = prepend_elem(OP_ASLICE, newOP(OP_PUSHMARK, 0), @@ -687,10 +703,14 @@ term : termbinop { $$ = newUNOP(OP_NOT, 0, scalar($2)); } | UNIOP /* Unary op, $_ implied */ { $$ = newOP($1, 0); } - | UNIOP block /* eval { foo }, I *think* */ + | UNIOP block /* eval { foo } */ { $$ = newUNOP($1, 0, $2); } | UNIOP term /* Unary op */ { $$ = newUNOP($1, 0, $2); } + | REQUIRE /* require, $_ implied */ + { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } + | REQUIRE term /* require Foo */ + { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } | UNIOPSUB term /* Sub treated as unop */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, $2, scalar($1))); } @@ -702,13 +722,12 @@ term : termbinop { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } | FUNC1 '(' ')' /* not () */ - { $$ = newOP($1, OPf_SPECIAL); } + { $$ = $1 == OP_NOT ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0))) + : newOP($1, OPf_SPECIAL); } | FUNC1 '(' expr ')' /* not($foo) */ { $$ = newUNOP($1, 0, $3); } - | PMFUNC '(' term ')' /* split (/foo/) */ - { $$ = pmruntime($1, $3, Nullop); } - | PMFUNC '(' term ',' term ')' /* split (/foo/,$bar) */ - { $$ = pmruntime($1, $3, $5); } + | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */ + { $$ = pmruntime($1, $3, 1); } | WORD | listop ; @@ -789,13 +808,3 @@ indirob : WORD | PRIVATEREF { $$ = $1; } ; - -%% /* PROGRAM */ - -/* more stuff added to make perly_c.diff easier to apply */ - -#ifdef yyparse -#undef yyparse -#endif -#define yyparse() Perl_yyparse(pTHX) -