File Coverage

XSParseKeyword.h
Criterion Covered Total %
statement 14 18 77.7
branch 7 14 50.0
condition n/a
subroutine n/a
pod n/a
total 21 32 65.6


line stmt bran cond sub pod time code
1             #ifndef __XS_PARSE_KEYWORD_H__
2             #define __XS_PARSE_KEYWORD_H__
3              
4             #define XSPARSEKEYWORD_ABI_VERSION 2
5              
6             struct XSParseKeywordPieceType;
7             struct XSParseKeywordPieceType {
8             int type;
9             union {
10             char c; /* LITERALCHAR */
11             const char *str; /* LITERALSTR */
12             const struct XSParseKeywordPieceType *pieces; /* containers */
13             void (*callback)(pTHX_ void *hookdata); /* SETUP, ANONSUB PREPARE+START */
14              
15             OP *(*op_wrap_callback)(pTHX_ OP *o, void *hookdata);
16             } u;
17             };
18              
19             enum {
20             XPK_FLAG_EXPR = (1<<0),
21             XPK_FLAG_STMT = (1<<1),
22             XPK_FLAG_AUTOSEMI = (1<<2),
23             XPK_FLAG_BLOCKSCOPE = (1<<3),
24             };
25              
26             enum {
27             /* skip zero */
28              
29             /* emits */
30             XS_PARSE_KEYWORD_LITERALCHAR = 1, /* nothing */
31             XS_PARSE_KEYWORD_LITERALSTR, /* nothing */
32             XS_PARSE_KEYWORD_AUTOSEMI, /* nothing */
33             XS_PARSE_KEYWORD_WARNING = 0x0e, /* nothing */
34             XS_PARSE_KEYWORD_FAILURE, /* nothing */
35              
36             XS_PARSE_KEYWORD_BLOCK = 0x10, /* op */
37             XS_PARSE_KEYWORD_ANONSUB, /* cv */
38             XS_PARSE_KEYWORD_ARITHEXPR, /* op */
39             XS_PARSE_KEYWORD_TERMEXPR, /* op */
40             XS_PARSE_KEYWORD_LISTEXPR, /* op */
41             /* TODO: XS_PARSE_KEYWORD_FULLEXPR = 0x15 */
42             XS_PARSE_KEYWORD_IDENT = 0x16, /* sv */
43             XS_PARSE_KEYWORD_PACKAGENAME, /* sv */
44             XS_PARSE_KEYWORD_LEXVARNAME, /* sv */
45             XS_PARSE_KEYWORD_LEXVAR, /* padix */
46             XS_PARSE_KEYWORD_ATTRS, /* i / {attr.name + attr.val} */
47             XS_PARSE_KEYWORD_VSTRING, /* sv */
48              
49             XS_PARSE_KEYWORD_INFIX = 0x40, /* infix */
50              
51             XS_PARSE_KEYWORD_INTRO_MY = 0x60, /* emits nothing */
52              
53             XS_PARSE_KEYWORD_SETUP = 0x70, /* invokes callback, emits nothing */
54              
55             XS_PARSE_KEYWORD_ANONSUB_PREPARE, /* invokes callback, emits nothing */
56             XS_PARSE_KEYWORD_ANONSUB_START, /* invokes callback, emits nothing */
57             XS_PARSE_KEYWORD_ANONSUB_END, /* invokes op_wrap_callback, emits nothing */
58             XS_PARSE_KEYWORD_ANONSUB_WRAP, /* invokes op_wrap_callback, emits nothing */
59              
60             XS_PARSE_KEYWORD_SEQUENCE = 0x80, /* contained */
61             XS_PARSE_KEYWORD_REPEATED, /* i, contained */
62             XS_PARSE_KEYWORD_CHOICE, /* i, contained */
63             XS_PARSE_KEYWORD_TAGGEDCHOICE, /* i, contained */
64             XS_PARSE_KEYWORD_SEPARATEDLIST, /* i, contained */
65             XS_PARSE_KEYWORD_PARENS = 0xb0, /* contained */
66             XS_PARSE_KEYWORD_BRACKETS, /* contained */
67             XS_PARSE_KEYWORD_BRACES, /* contained */
68             XS_PARSE_KEYWORD_CHEVRONS, /* contained */
69             };
70              
71             enum {
72             XPK_LEXVAR_SCALAR = (1<<0),
73             XPK_LEXVAR_ARRAY = (1<<1),
74             XPK_LEXVAR_HASH = (1<<2),
75             XPK_LEXVAR_ANY = XPK_LEXVAR_SCALAR|XPK_LEXVAR_ARRAY|XPK_LEXVAR_HASH,
76             };
77              
78             enum {
79             XPK_TYPEFLAG_OPT = (1<<16),
80             XPK_TYPEFLAG_SPECIAL = (1<<17), /* on XPK_LITERALSTR: keyword
81             on XPK_BLOCK: scoped
82             on XPK_LEXVAR: my */
83              
84             /* These three are shifted versions of perl's G_VOID, G_SCALAR, G_LIST */
85             XPK_TYPEFLAG_G_VOID = (1<<18),
86             XPK_TYPEFLAG_G_SCALAR = (2<<18),
87             XPK_TYPEFLAG_G_LIST = (3<<18),
88              
89             XPK_TYPEFLAG_ENTERLEAVE = (1<<20), /* wrap ENTER/LEAVE pair around the item */
90              
91             XPK_TYPEFLAG_MAYBEPARENS = (1<<21), /* parens themselves are optional on PARENS */
92             };
93              
94             #define XPK_BLOCK_flags(flags) {.type = XS_PARSE_KEYWORD_BLOCK|(flags), .u.pieces = NULL}
95             #define XPK_BLOCK XPK_BLOCK_flags(0)
96             #define XPK_BLOCK_VOIDCTX XPK_BLOCK_flags(XPK_TYPEFLAG_SPECIAL|XPK_TYPEFLAG_G_VOID)
97             #define XPK_BLOCK_SCALARCTX XPK_BLOCK_flags(XPK_TYPEFLAG_SPECIAL|XPK_TYPEFLAG_G_SCALAR)
98             #define XPK_BLOCK_LISTCTX XPK_BLOCK_flags(XPK_TYPEFLAG_SPECIAL|XPK_TYPEFLAG_G_LIST)
99              
100             #define XPK_PREFIXED_BLOCK_flags(flags,...) \
101             {.type = XS_PARSE_KEYWORD_BLOCK|flags, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
102             #define XPK_PREFIXED_BLOCK(...) XPK_PREFIXED_BLOCK_flags(0, __VA_ARGS__)
103             #define XPK_PREFIXED_BLOCK_ENTERLEAVE(...) XPK_PREFIXED_BLOCK_flags(XPK_TYPEFLAG_ENTERLEAVE, __VA_ARGS__)
104              
105             #define XPK_SETUP(setup) {.type = XS_PARSE_KEYWORD_SETUP, .u.callback = setup}
106              
107             #define XPK_ANONSUB {.type = XS_PARSE_KEYWORD_ANONSUB}
108              
109             #define XPK_ANONSUB_PREPARE(func) {.type = XS_PARSE_KEYWORD_ANONSUB_PREPARE, .u.callback = func}
110             #define XPK_ANONSUB_START(func) {.type = XS_PARSE_KEYWORD_ANONSUB_START, .u.callback = func}
111             #define XPK_ANONSUB_END(func) {.type = XS_PARSE_KEYWORD_ANONSUB_END, .u.op_wrap_callback = func}
112             #define XPK_ANONSUB_WRAP(func) {.type = XS_PARSE_KEYWORD_ANONSUB_WRAP, .u.op_wrap_callback = func}
113              
114             #define XPK_STAGED_ANONSUB(...) \
115             {.type = XS_PARSE_KEYWORD_ANONSUB, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
116              
117             #define XPK_ARITHEXPR_flags(flags) {.type = XS_PARSE_KEYWORD_ARITHEXPR|(flags)}
118             #define XPK_ARITHEXPR XPK_ARITHEXPR_flags(0)
119             #define XPK_ARITHEXPR_VOIDCTX XPK_ARITHEXPR_flags(XPK_TYPEFLAG_G_VOID)
120             #define XPK_ARITHEXPR_SCALARCTX XPK_ARITHEXPR_flags(XPK_TYPEFLAG_G_SCALAR)
121             #define XPK_TERMEXPR_flags(flags) {.type = XS_PARSE_KEYWORD_TERMEXPR|(flags)}
122             #define XPK_TERMEXPR XPK_TERMEXPR_flags(0)
123             #define XPK_TERMEXPR_VOIDCTX XPK_TERMEXPR_flags(XPK_TYPEFLAG_G_VOID)
124             #define XPK_TERMEXPR_SCALARCTX XPK_TERMEXPR_flags(XPK_TYPEFLAG_G_SCALAR)
125             #define XPK_LISTEXPR_flags(flags) {.type = XS_PARSE_KEYWORD_LISTEXPR|(flags)}
126             #define XPK_LISTEXPR XPK_LISTEXPR_flags(0)
127             #define XPK_LISTEXPR_LISTCTX XPK_LISTEXPR_flags(XPK_TYPEFLAG_G_LIST)
128              
129             #define XPK_PREFIXED_TERMEXPR_flags(flags,...) \
130             {.type = XS_PARSE_KEYWORD_TERMEXPR|(flags), .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
131             #define XPK_PREFIXED_TERMEXPR_ENTERLEAVE(...) XPK_PREFIXED_TERMEXPR_flags(XPK_TYPEFLAG_ENTERLEAVE, __VA_ARGS__)
132              
133             #define XPK_IDENT {.type = XS_PARSE_KEYWORD_IDENT }
134             #define XPK_IDENT_OPT {.type = XS_PARSE_KEYWORD_IDENT |XPK_TYPEFLAG_OPT}
135             #define XPK_PACKAGENAME {.type = XS_PARSE_KEYWORD_PACKAGENAME }
136             #define XPK_PACKAGENAME_OPT {.type = XS_PARSE_KEYWORD_PACKAGENAME|XPK_TYPEFLAG_OPT}
137              
138             #define XPK_LEXVARNAME(kind) {.type = XS_PARSE_KEYWORD_LEXVARNAME, .u.c = kind}
139              
140             #define XPK_LEXVAR(kind) {.type = XS_PARSE_KEYWORD_LEXVAR, .u.c = kind}
141             #define XPK_LEXVAR_MY(kind) {.type = XS_PARSE_KEYWORD_LEXVAR|XPK_TYPEFLAG_SPECIAL, .u.c = kind}
142              
143             #define XPK_ATTRIBUTES {.type = XS_PARSE_KEYWORD_ATTRS}
144              
145             #define XPK_VSTRING {.type = XS_PARSE_KEYWORD_VSTRING}
146             #define XPK_VSTRING_OPT {.type = XS_PARSE_KEYWORD_VSTRING|XPK_TYPEFLAG_OPT}
147              
148             #define XPK_COMMA {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ','}
149             #define XPK_COLON {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ':'}
150             #define XPK_EQUALS {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = '='}
151              
152             #define XPK_LITERAL(s) {.type = XS_PARSE_KEYWORD_LITERALSTR, .u.str = (const char *)s}
153             #define XPK_STRING(s) XPK_LITERAL(s)
154             #define XPK_AUTOSEMI {.type = XS_PARSE_KEYWORD_AUTOSEMI}
155             #define XPK_KEYWORD(s) {.type = XS_PARSE_KEYWORD_LITERALSTR|XPK_TYPEFLAG_SPECIAL, .u.str = (const char *)s}
156              
157             #define XPK_INFIX(select) {.type = XS_PARSE_KEYWORD_INFIX, .u.c = select}
158             #define XPK_INFIX_RELATION XPK_INFIX(XPI_SELECT_RELATION)
159             #define XPK_INFIX_EQUALITY XPK_INFIX(XPI_SELECT_EQUALITY)
160             #define XPK_INFIX_MATCH_NOSMART XPK_INFIX(XPI_SELECT_MATCH_NOSMART)
161             #define XPK_INFIX_MATCH_SMART XPK_INFIX(XPI_SELECT_MATCH_SMART)
162              
163             #define XPK_INTRO_MY {.type = XS_PARSE_KEYWORD_INTRO_MY}
164              
165             #define XPK_SEQUENCE_pieces(p) {.type = XS_PARSE_KEYWORD_SEQUENCE, .u.pieces = p}
166             #define XPK_SEQUENCE(...) XPK_SEQUENCE_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
167              
168             /* First piece of these must be something probe-able */
169             #define XPK_OPTIONAL_pieces(p) {.type = XS_PARSE_KEYWORD_SEQUENCE|XPK_TYPEFLAG_OPT, .u.pieces = p}
170             #define XPK_OPTIONAL(...) XPK_OPTIONAL_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
171              
172             #define XPK_REPEATED_pieces(p) {.type = XS_PARSE_KEYWORD_REPEATED, .u.pieces = p}
173             #define XPK_REPEATED(...) XPK_REPEATED_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
174              
175             /* Every piece must be probeable */
176             #define XPK_CHOICE_pieces(p) {.type = XS_PARSE_KEYWORD_CHOICE, .u.pieces = p}
177             #define XPK_CHOICE(...) XPK_CHOICE_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
178              
179             /* Every piece must be probeable, and followed by XPK_TAG */
180             #define XPK_TAGGEDCHOICE_pieces(p) {.type = XS_PARSE_KEYWORD_TAGGEDCHOICE, .u.pieces = p}
181             #define XPK_TAGGEDCHOICE(...) XPK_TAGGEDCHOICE_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0}, {0} }))
182             #define XPK_TAG(val) {.type = val}
183              
184             #define XPK_COMMALIST(...) \
185             {.type = XS_PARSE_KEYWORD_SEPARATEDLIST, .u.pieces = (const struct XSParseKeywordPieceType []){ \
186             {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ','}, __VA_ARGS__, {0}}}
187              
188             #define XPK_WARNING_bit(bit,s) {.type = (XS_PARSE_KEYWORD_WARNING|(bit << 24)), .u.str = (const char *)s}
189             #define XPK_WARNING(s) XPK_WARNING_bit(0,s)
190             #define XPK_WARNING_AMBIGUOUS(s) XPK_WARNING_bit(WARN_AMBIGUOUS, s)
191             #define XPK_WARNING_DEPRECATED(s) XPK_WARNING_bit(WARN_DEPRECATED, s)
192             #define XPK_WARNING_EXPERIMENTAL(s) XPK_WARNING_bit(WARN_EXPERIMENTAL,s)
193             #define XPK_WARNING_PRECEDENCE(s) XPK_WARNING_bit(WARN_PRECEDENCE, s)
194             #define XPK_WARNING_SYNTAX(s) XPK_WARNING_bit(WARN_SYNTAX, s)
195              
196             #define XPK_FAILURE(s) {.type = XS_PARSE_KEYWORD_FAILURE, .u.str = (const char *)s}
197              
198             #define XPK_PARENS_pieces(p) {.type = XS_PARSE_KEYWORD_PARENS, .u.pieces = p}
199             #define XPK_PARENS(...) XPK_PARENS_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
200             #define XPK_PARENS_OPT(...) \
201             {.type = XS_PARSE_KEYWORD_PARENS|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
202              
203             #define XPK_ARGS_pieces(p) {.type = XS_PARSE_KEYWORD_PARENS|XPK_TYPEFLAG_MAYBEPARENS, .u.pieces = p}
204             #define XPK_ARGS(...) XPK_ARGS_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
205              
206             #define XPK_BRACKETS_pieces(p) {.type = XS_PARSE_KEYWORD_BRACKETS, .u.pieces = p}
207             #define XPK_BRACKETS(...) XPK_BRACKETS_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
208             #define XPK_BRACKETS_OPT(...) \
209             {.type = XS_PARSE_KEYWORD_BRACKETS|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
210              
211             #define XPK_BRACES_pieces(p) {.type = XS_PARSE_KEYWORD_BRACES, .u.pieces = p}
212             #define XPK_BRACES(...) XPK_BRACES_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
213             #define XPK_BRACES_OPT(...) \
214             {.type = XS_PARSE_KEYWORD_BRACES|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
215              
216             #define XPK_CHEVRONS_pieces(p) {.type = XS_PARSE_KEYWORD_CHEVRONS, .u.pieces = p}
217             #define XPK_CHEVRONS(...) XPK_CHEVRONS_pieces(((const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }))
218             #define XPK_CHEVRONS_OPT(...) \
219             {.type = XS_PARSE_KEYWORD_CHEVRONS|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
220              
221             /* back-compat for older names */
222             #define XPK_PARENSCOPE_pieces XPK_PARENS_pieces
223             #define XPK_PARENSCOPE XPK_PARENS
224             #define XPK_PARENSCOPE_OPT XPK_PARENS_OPT
225             #define XPK_ARGSCOPE_pieces XPK_ARGS_pieces
226             #define XPK_ARGSCOPE XPK_ARGS
227             #define XPK_BRACKETSCOPE_pieces XPK_BRACKETS_pieces
228             #define XPK_BRACKETSCOPE XPK_BRACKETS
229             #define XPK_BRACKETSCOPE_OPT XPK_BRACKETS_OPT
230             #define XPK_BRACESCOPE_pieces XPK_BRACES_pieces
231             #define XPK_BRACESCOPE XPK_BRACES
232             #define XPK_BRACESCOPE_OPT XPK_BRACES_OPT
233             #define XPK_CHEVRONSCOPE_pieces XPK_CHEVRONS_pieces
234             #define XPK_CHEVRONSCOPE XPK_CHEVRONS
235             #define XPK_CHEVRONSCOPE_OPT XPK_CHEVRONS_OPT
236              
237             /* This type defined in XSParseInfix.h */
238             typedef struct XSParseInfixInfo XSParseInfixInfo;
239              
240             typedef struct {
241             union {
242             OP *op;
243             CV *cv;
244             SV *sv;
245             int i;
246             struct { SV *name; SV *value; } attr;
247             PADOFFSET padix;
248             XSParseInfixInfo *infix;
249             };
250             int line;
251             } XSParseKeywordPiece;
252              
253             struct XSParseKeywordHooks {
254             U32 flags;
255              
256             /* used by build1 */
257             struct XSParseKeywordPieceType piece1;
258             /* alternatively, used by build */
259             const struct XSParseKeywordPieceType *pieces;
260              
261             /* These two hooks are ANDed together; both must pass, if present */
262             const char *permit_hintkey;
263             bool (*permit) (pTHX_ void *hookdata);
264              
265             void (*check)(pTHX_ void *hookdata);
266              
267             /* These are alternatives; the first one defined is used */
268             int (*parse)(pTHX_ OP **opp, void *hookdata);
269             int (*build)(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata);
270             int (*build1)(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata);
271             };
272              
273             static void (*register_xs_parse_keyword_func)(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata);
274             #define register_xs_parse_keyword(kwname, hooks, hookdata) S_register_xs_parse_keyword(aTHX_ kwname, hooks, hookdata)
275             static void S_register_xs_parse_keyword(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata)
276             {
277 90           if(!register_xs_parse_keyword_func)
278 0           croak("Must call boot_xs_parse_keyword() first");
279              
280 90           (*register_xs_parse_keyword_func)(aTHX_ kwname, hooks, hookdata);
281             }
282              
283             #define boot_xs_parse_keyword(ver) S_boot_xs_parse_keyword(aTHX_ ver)
284 45           static void S_boot_xs_parse_keyword(pTHX_ double ver) {
285             SV **svp;
286 45 50         SV *versv = ver ? newSVnv(ver) : NULL;
287              
288 45           load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("XS::Parse::Keyword"), versv, NULL);
289              
290 45           svp = hv_fetchs(PL_modglobal, "XS::Parse::Keyword/ABIVERSION_MIN", 0);
291 45 50         if(!svp)
292 0           croak("XS::Parse::Keyword ABI minimum version missing");
293 45 50         int abi_ver = SvIV(*svp);
294 45 50         if(abi_ver > XSPARSEKEYWORD_ABI_VERSION)
295 0           croak("XS::Parse::Keyword ABI version mismatch - library supports >= %d, compiled for %d",
296             abi_ver, XSPARSEKEYWORD_ABI_VERSION);
297              
298 45           svp = hv_fetchs(PL_modglobal, "XS::Parse::Keyword/ABIVERSION_MAX", 0);
299 45 50         abi_ver = SvIV(*svp);
300 45 50         if(abi_ver < XSPARSEKEYWORD_ABI_VERSION)
301 0           croak("XS::Parse::Keyword ABI version mismatch - library supports <= %d, compiled for %d",
302             abi_ver, XSPARSEKEYWORD_ABI_VERSION);
303              
304 45 50         register_xs_parse_keyword_func = INT2PTR(void (*)(pTHX_ const char *, const struct XSParseKeywordHooks *, void *),
305             SvUV(*hv_fetchs(PL_modglobal, "XS::Parse::Keyword/register()@2", 0)));
306 45           }
307              
308             #endif