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