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