File Coverage

XSParseKeyword.h
Criterion Covered Total %
statement 14 18 77.7
branch 8 16 50.0
condition n/a
subroutine n/a
pod n/a
total 22 34 64.7


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