xref: /openbsd-src/gnu/usr.bin/perl/parser.h (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1850e2753Smillert /*    parser.h
2850e2753Smillert  *
3898184e3Ssthen  *    Copyright (c) 2006, 2007, 2009, 2010, 2011 Larry Wall and others
4850e2753Smillert  *
5850e2753Smillert  *    You may distribute under the terms of either the GNU General Public
6850e2753Smillert  *    License or the Artistic License, as specified in the README file.
7850e2753Smillert  *
8850e2753Smillert  * This file defines the layout of the parser object used by the parser
9898184e3Ssthen  * and lexer (perly.c, toke.c).
10850e2753Smillert  */
11850e2753Smillert 
12850e2753Smillert #define YYEMPTY		(-2)
13850e2753Smillert 
14850e2753Smillert typedef struct {
15850e2753Smillert     YYSTYPE val;    /* semantic value */
16850e2753Smillert     short   state;
17850e2753Smillert     I32     savestack_ix;	/* size of savestack at this state */
18b39c5158Smillert     CV	    *compcv; /* value of PL_compcv when this value was created */
19850e2753Smillert #ifdef DEBUGGING
20850e2753Smillert     const char  *name; /* token/rule name for -Dpv */
21850e2753Smillert #endif
22850e2753Smillert } yy_stack_frame;
23850e2753Smillert 
2491f110e0Safresh1 /* Fields that need to be shared with (i.e., visible to) inner lex-
2591f110e0Safresh1    ing scopes. */
2691f110e0Safresh1 typedef struct yy_lexshared {
2791f110e0Safresh1     struct yy_lexshared	*ls_prev;
2891f110e0Safresh1     SV			*ls_linestr;	/* mirrors PL_parser->linestr */
2991f110e0Safresh1     char		*ls_bufptr;	/* mirrors PL_parser->bufptr */
3091f110e0Safresh1     char		*re_eval_start;	/* start of "(?{..." text */
3191f110e0Safresh1     SV			*re_eval_str;	/* "(?{...})" text */
3291f110e0Safresh1 } LEXSHARED;
3391f110e0Safresh1 
34850e2753Smillert typedef struct yy_parser {
35850e2753Smillert 
36850e2753Smillert     /* parser state */
37850e2753Smillert 
38850e2753Smillert     struct yy_parser *old_parser; /* previous value of PL_parser */
39850e2753Smillert     YYSTYPE	    yylval;	/* value of lookahead symbol, set by yylex() */
40850e2753Smillert     int		    yychar;	/* The lookahead symbol.  */
41850e2753Smillert 
42850e2753Smillert     /* Number of tokens to shift before error messages enabled.  */
43850e2753Smillert     int		    yyerrstatus;
44850e2753Smillert 
45850e2753Smillert     yy_stack_frame  *stack;	/* base of stack */
469f11ffb7Safresh1     yy_stack_frame  *stack_max1;/* (top-1)th element of allocated stack */
47850e2753Smillert     yy_stack_frame  *ps;	/* current stack frame */
4856d68f1eSafresh1     int		    yylen;	/* length of active reduction */
49850e2753Smillert 
50850e2753Smillert     /* lexer state */
51850e2753Smillert 
5256d68f1eSafresh1     I32		lex_formbrack;	/* bracket count at outer format level */
53898184e3Ssthen     I32		lex_brackets;	/* square and curly bracket count */
54850e2753Smillert     I32		lex_casemods;	/* casemod count */
55850e2753Smillert     char	*lex_brackstack;/* what kind of brackets to pop */
56850e2753Smillert     char	*lex_casestack;	/* what kind of case mods in effect */
57850e2753Smillert     U8		lex_defer;	/* state after determined token */
586fb12b70Safresh1     U8		lex_dojoin;	/* doing an array interpolation
596fb12b70Safresh1                                    1 = @{...}  2 = ->@ */
60850e2753Smillert     U8		expect;		/* how to interpret ambiguous tokens */
619f11ffb7Safresh1     bool	preambled;
629f11ffb7Safresh1     bool        sub_no_recover; /* can't recover from a sublex error */
6356d68f1eSafresh1     U8		sub_error_count; /* the number of errors before sublexing */
64850e2753Smillert     OP		*lex_inpat;	/* in pattern $) and $| are special */
65850e2753Smillert     OP		*lex_op;	/* extra info to pass back on op */
66850e2753Smillert     SV		*lex_repl;	/* runtime replacement from s/// */
67850e2753Smillert     U16		lex_inwhat;	/* what kind of quoting are we in */
6891f110e0Safresh1     OPCODE	last_lop_op;	/* last named list or unary operator */
69850e2753Smillert     I32		lex_starts;	/* how many interps done on level */
70850e2753Smillert     SV		*lex_stuff;	/* runtime pattern from m// or s/// */
71850e2753Smillert     I32		multi_start;	/* 1st line of multi-line string */
72850e2753Smillert     I32		multi_end;	/* last line of multi-line string */
73*eac174f2Safresh1     UV		multi_open;	/* delimiter code point of said string */
74*eac174f2Safresh1     UV		multi_close;	/* delimiter code point of said string */
7591f110e0Safresh1     bool        lex_re_reparsing; /* we're doing G_RE_REPARSING */
769f11ffb7Safresh1     U8		lex_super_state;/* lexer state to save */
779f11ffb7Safresh1     U16		lex_sub_inwhat;	/* "lex_inwhat" to use in sublex_push */
78898184e3Ssthen     I32		lex_allbrackets;/* (), [], {}, ?: bracket count */
799f11ffb7Safresh1     OP		*lex_sub_op;	/* current op in y/// or pattern */
809f11ffb7Safresh1     SV		*lex_sub_repl;	/* repl of s/// used in sublex_push */
8191f110e0Safresh1     LEXSHARED	*lex_shared;
82850e2753Smillert     SV		*linestr;	/* current chunk of src text */
8391f110e0Safresh1     char	*bufptr;	/* carries the cursor (current parsing
8491f110e0Safresh1                                    position) from one invocation of yylex
8591f110e0Safresh1                                    to the next */
8691f110e0Safresh1     char	*oldbufptr;	/* in yylex, beginning of current token */
8791f110e0Safresh1     char	*oldoldbufptr;	/* in yylex, beginning of previous token */
88850e2753Smillert     char	*bufend;
89850e2753Smillert     char	*linestart;	/* beginning of most recently read line */
90850e2753Smillert     char	*last_uni;	/* position of last named-unary op */
91850e2753Smillert     char	*last_lop;	/* position of last list operator */
9291f110e0Safresh1     /* copline is used to pass a specific line number to newSTATEOP.  It
9391f110e0Safresh1        is a one-time line number, as newSTATEOP invalidates it (sets it to
9491f110e0Safresh1        NOLINE) after using it.  The purpose of this is to report line num-
9591f110e0Safresh1        bers in multiline constructs using the number of the first line. */
9691f110e0Safresh1     line_t	copline;
97850e2753Smillert     U16		in_my;		/* we're compiling a "my"/"our" declaration */
98850e2753Smillert     U8		lex_state;	/* next token is determined */
99850e2753Smillert     U8		error_count;	/* how many compile errors so far, max 10 */
100850e2753Smillert     HV		*in_my_stash;	/* declared class of this "my" declaration */
101850e2753Smillert     PerlIO	*rsfp;		/* current source file pointer */
102850e2753Smillert     AV		*rsfp_filters;	/* holds chain of active source filters */
103850e2753Smillert 
104850e2753Smillert     YYSTYPE	nextval[5];	/* value of next token, if any */
105850e2753Smillert     I32		nexttype[5];	/* type of next token */
1069f11ffb7Safresh1     U8		nexttoke;
1079f11ffb7Safresh1     U8		form_lex_state;	/* remember lex_state when parsing fmt */
1089f11ffb7Safresh1     U8		lex_fakeeof;	/* precedence at which to fake EOF */
1099f11ffb7Safresh1     U8		lex_flags;
110850e2753Smillert     COP		*saved_curcop;	/* the previous PL_curcop */
111850e2753Smillert     char	tokenbuf[256];
1126fb12b70Safresh1     line_t	herelines;	/* number of lines in here-doc */
1136fb12b70Safresh1     line_t	preambling;	/* line # when processing $ENV{PERL5DB} */
1149f11ffb7Safresh1 
1159f11ffb7Safresh1     /* these are valid while parsing a subroutine signature */
11656d68f1eSafresh1     UV          sig_elems;      /* number of signature elements seen so far */
11756d68f1eSafresh1     UV          sig_optelems;   /* number of optional signature elems seen */
1189f11ffb7Safresh1     char        sig_slurpy;     /* the sigil of the slurpy var (or null) */
11956d68f1eSafresh1     bool        sig_seen;       /* the currently parsing sub has a signature */
1209f11ffb7Safresh1 
1219f11ffb7Safresh1     bool        recheck_utf8_validity;
1229f11ffb7Safresh1 
123898184e3Ssthen     PERL_BITFIELD16	in_pod:1;      /* lexer is within a =pod section */
124898184e3Ssthen     PERL_BITFIELD16	filtered:1;    /* source filters in evalbytes */
1256fb12b70Safresh1     PERL_BITFIELD16	saw_infix_sigil:1; /* saw & or * or % operator */
126b8851fccSafresh1     PERL_BITFIELD16	parsed_sub:1;  /* last thing parsed was a sub */
127850e2753Smillert } yy_parser;
128850e2753Smillert 
129b39c5158Smillert /* flags for lexer API */
130b39c5158Smillert #define LEX_STUFF_UTF8		0x00000001
131b39c5158Smillert #define LEX_KEEP_PREVIOUS	0x00000002
132b39c5158Smillert 
133898184e3Ssthen #ifdef PERL_CORE
134898184e3Ssthen # define LEX_START_SAME_FILTER	0x00000001
135898184e3Ssthen # define LEX_IGNORE_UTF8_HINTS	0x00000002
136898184e3Ssthen # define LEX_EVALBYTES		0x00000004
137898184e3Ssthen # define LEX_START_COPIED	0x00000008
138898184e3Ssthen # define LEX_DONT_CLOSE_RSFP	0x00000010
139898184e3Ssthen # define LEX_START_FLAGS \
140898184e3Ssthen         (LEX_START_SAME_FILTER|LEX_START_COPIED \
141898184e3Ssthen         |LEX_IGNORE_UTF8_HINTS|LEX_EVALBYTES|LEX_DONT_CLOSE_RSFP)
142898184e3Ssthen #endif
143898184e3Ssthen 
144898184e3Ssthen /* flags for parser API */
145898184e3Ssthen #define PARSE_OPTIONAL          0x00000001
146898184e3Ssthen 
147898184e3Ssthen /* values for lex_fakeeof */
148898184e3Ssthen enum {
149898184e3Ssthen     LEX_FAKEEOF_NEVER,      /* don't fake EOF */
150898184e3Ssthen     LEX_FAKEEOF_CLOSING,    /* fake EOF at unmatched closing punctuation */
151898184e3Ssthen     LEX_FAKEEOF_NONEXPR,    /* ... and at token that can't be in expression */
152898184e3Ssthen     LEX_FAKEEOF_LOWLOGIC,   /* ... and at low-precedence logic operator */
153898184e3Ssthen     LEX_FAKEEOF_COMMA,      /* ... and at comma */
154898184e3Ssthen     LEX_FAKEEOF_ASSIGN,     /* ... and at assignment operator */
155898184e3Ssthen     LEX_FAKEEOF_IFELSE,     /* ... and at ?: operator */
156898184e3Ssthen     LEX_FAKEEOF_RANGE,      /* ... and at range operator */
157898184e3Ssthen     LEX_FAKEEOF_LOGIC,      /* ... and at logic operator */
158898184e3Ssthen     LEX_FAKEEOF_BITWISE,    /* ... and at bitwise operator */
159898184e3Ssthen     LEX_FAKEEOF_COMPARE,    /* ... and at comparison operator */
160898184e3Ssthen     LEX_FAKEEOF_MAX
161898184e3Ssthen };
162898184e3Ssthen 
16343003dfeSmillert /*
16491f110e0Safresh1  * ex: set ts=8 sts=4 sw=4 et:
16543003dfeSmillert  */
166