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