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; /* 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_MY(kind) {.type = XS_PARSE_KEYWORD_LEXVAR|XPK_TYPEFLAG_SPECIAL, .u.c = kind}
137              
138             #define XPK_ATTRIBUTES {.type = XS_PARSE_KEYWORD_ATTRS}
139              
140             #define XPK_VSTRING {.type = XS_PARSE_KEYWORD_VSTRING}
141             #define XPK_VSTRING_OPT {.type = XS_PARSE_KEYWORD_VSTRING|XPK_TYPEFLAG_OPT}
142              
143             #define XPK_COMMA {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ','}
144             #define XPK_COLON {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ':'}
145             #define XPK_EQUALS {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = '='}
146              
147             #define XPK_LITERAL(s) {.type = XS_PARSE_KEYWORD_LITERALSTR, .u.str = (const char *)s}
148             #define XPK_STRING(s) XPK_LITERAL(s)
149             #define XPK_AUTOSEMI {.type = XS_PARSE_KEYWORD_AUTOSEMI}
150             #define XPK_KEYWORD(s) {.type = XS_PARSE_KEYWORD_LITERALSTR|XPK_TYPEFLAG_SPECIAL, .u.str = (const char *)s}
151              
152             #define XPK_INFIX(select) {.type = XS_PARSE_KEYWORD_INFIX, .u.c = select}
153             #define XPK_INFIX_RELATION XPK_INFIX(XPI_SELECT_RELATION)
154             #define XPK_INFIX_EQUALITY XPK_INFIX(XPI_SELECT_EQUALITY)
155             #define XPK_INFIX_MATCH_NOSMART XPK_INFIX(XPI_SELECT_MATCH_NOSMART)
156             #define XPK_INFIX_MATCH_SMART XPK_INFIX(XPI_SELECT_MATCH_SMART)
157              
158             #define XPK_SEQUENCE(...) \
159             {.type = XS_PARSE_KEYWORD_SEQUENCE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
160             /* First piece of these must be something probe-able */
161             #define XPK_OPTIONAL(...) \
162             {.type = XS_PARSE_KEYWORD_SEQUENCE|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
163             #define XPK_REPEATED(...) \
164             {.type = XS_PARSE_KEYWORD_REPEATED, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
165             /* Every piece must be probeable */
166             #define XPK_CHOICE(...) \
167             {.type = XS_PARSE_KEYWORD_CHOICE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
168             /* Every piece must be probeable, and followed by XPK_TAG */
169             #define XPK_TAGGEDCHOICE(...) \
170             {.type = XS_PARSE_KEYWORD_TAGGEDCHOICE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0}, {0} }}
171             #define XPK_TAG(val) \
172             {.type = val}
173             #define XPK_COMMALIST(...) \
174             {.type = XS_PARSE_KEYWORD_SEPARATEDLIST, .u.pieces = (const struct XSParseKeywordPieceType []){ \
175             {.type = XS_PARSE_KEYWORD_LITERALCHAR, .u.c = ','}, __VA_ARGS__, {0}}}
176              
177             #define XPK_FAILURE(s) {.type = XS_PARSE_KEYWORD_FAILURE, .u.str = (const char *)s}
178              
179             #define XPK_PARENSCOPE(...) \
180             {.type = XS_PARSE_KEYWORD_PARENSCOPE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
181             #define XPK_PARENSCOPE_OPT(...) \
182             {.type = XS_PARSE_KEYWORD_PARENSCOPE|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
183              
184             #define XPK_ARGSCOPE(...) \
185             {.type = XS_PARSE_KEYWORD_PARENSCOPE|XPK_TYPEFLAG_MAYBEPARENS, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
186              
187             #define XPK_BRACKETSCOPE(...) \
188             {.type = XS_PARSE_KEYWORD_BRACKETSCOPE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
189             #define XPK_BRACKETSCOPE_OPT(...) \
190             {.type = XS_PARSE_KEYWORD_BRACKETSCOPE|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
191              
192             #define XPK_BRACESCOPE(...) \
193             {.type = XS_PARSE_KEYWORD_BRACESCOPE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
194             #define XPK_BRACESCOPE_OPT(...) \
195             {.type = XS_PARSE_KEYWORD_BRACESCOPE|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
196              
197             #define XPK_CHEVRONSCOPE(...) \
198             {.type = XS_PARSE_KEYWORD_CHEVRONSCOPE, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
199             #define XPK_CHEVRONSCOPE_OPT(...) \
200             {.type = XS_PARSE_KEYWORD_CHEVRONSCOPE|XPK_TYPEFLAG_OPT, .u.pieces = (const struct XSParseKeywordPieceType []){ __VA_ARGS__, {0} }}
201              
202             /* This type defined in XSParseInfix.h */
203             typedef struct XSParseInfixInfo XSParseInfixInfo;
204              
205             typedef struct {
206             union {
207             OP *op;
208             CV *cv;
209             SV *sv;
210             int i;
211             struct { SV *name; SV *value; } attr;
212             PADOFFSET padix;
213             XSParseInfixInfo *infix;
214             };
215             int line;
216             } XSParseKeywordPiece;
217              
218             struct XSParseKeywordHooks {
219             U32 flags;
220              
221             /* used by build1 */
222             struct XSParseKeywordPieceType piece1;
223             /* alternatively, used by build */
224             const struct XSParseKeywordPieceType *pieces;
225              
226             /* These two hooks are ANDed together; both must pass, if present */
227             const char *permit_hintkey;
228             bool (*permit) (pTHX_ void *hookdata);
229              
230             void (*check)(pTHX_ void *hookdata);
231              
232             /* These are alternatives; the first one defined is used */
233             int (*parse)(pTHX_ OP **opp, void *hookdata);
234             int (*build)(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata);
235             int (*build1)(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata);
236             };
237              
238             static void (*register_xs_parse_keyword_func)(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata);
239             #define register_xs_parse_keyword(kwname, hooks, hookdata) S_register_xs_parse_keyword(aTHX_ kwname, hooks, hookdata)
240             static void S_register_xs_parse_keyword(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata)
241             {
242 90           if(!register_xs_parse_keyword_func)
243 0           croak("Must call boot_xs_parse_keyword() first");
244              
245 90           (*register_xs_parse_keyword_func)(aTHX_ kwname, hooks, hookdata);
246             }
247              
248             #define boot_xs_parse_keyword(ver) S_boot_xs_parse_keyword(aTHX_ ver)
249 45           static void S_boot_xs_parse_keyword(pTHX_ double ver) {
250             SV **svp;
251 45 50         SV *versv = ver ? newSVnv(ver) : NULL;
252              
253 45           load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("XS::Parse::Keyword"), versv, NULL);
254              
255 45           svp = hv_fetchs(PL_modglobal, "XS::Parse::Keyword/ABIVERSION_MIN", 0);
256 45 50         if(!svp)
257 0           croak("XS::Parse::Keyword ABI minimum version missing");
258 45 50         int abi_ver = SvIV(*svp);
259 45 50         if(abi_ver > XSPARSEKEYWORD_ABI_VERSION)
260 0           croak("XS::Parse::Keyword ABI version mismatch - library supports >= %d, compiled for %d",
261             abi_ver, XSPARSEKEYWORD_ABI_VERSION);
262              
263 45           svp = hv_fetchs(PL_modglobal, "XS::Parse::Keyword/ABIVERSION_MAX", 0);
264 45 50         abi_ver = SvIV(*svp);
265 45 50         if(abi_ver < XSPARSEKEYWORD_ABI_VERSION)
266 0           croak("XS::Parse::Keyword ABI version mismatch - library supports <= %d, compiled for %d",
267             abi_ver, XSPARSEKEYWORD_ABI_VERSION);
268              
269 45 50         register_xs_parse_keyword_func = INT2PTR(void (*)(pTHX_ const char *, const struct XSParseKeywordHooks *, void *),
270             SvUV(*hv_fetchs(PL_modglobal, "XS::Parse::Keyword/register()@2", 0)));
271 45           }
272              
273             #endif