| 1 |
/* You may distribute under the terms of either the GNU General Public License |
| 2 |
* or the Artistic License (the same terms as Perl itself) |
| 3 |
* |
| 4 |
* (C) Paul Evans, 2021 -- [email protected] |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "EXTERN.h" |
| 8 |
#include "perl.h" |
| 9 |
#include "XSUB.h" |
| 10 |
|
| 11 |
#include "XSParseKeyword.h" |
| 12 |
|
| 13 |
static const char hintkey[] = "t::structures/permit"; |
| 14 |
|
| 15 |
static int build_op(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata) |
| 16 |
{ |
| 17 |
*out = args[0]->op; |
| 18 |
return KEYWORD_PLUGIN_EXPR; |
| 19 |
} |
| 20 |
|
| 21 |
static int build_constiv(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata) |
| 22 |
{ |
| 23 |
/* npieces should always be 1 because XPK_LITERAL() does not yield args */ |
| 24 |
*out = newSVOP(OP_CONST, 0, newSViv(args[0]->i)); |
| 25 |
return KEYWORD_PLUGIN_EXPR; |
| 26 |
} |
| 27 |
|
| 28 |
static int build_constsv(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata) |
| 29 |
{ |
| 30 |
*out = newSVOP(OP_CONST, 0, args[0]->sv); |
| 31 |
return KEYWORD_PLUGIN_EXPR; |
| 32 |
} |
| 33 |
|
| 34 |
static const struct XSParseKeywordHooks hooks_sequence = { |
| 35 |
.permit_hintkey = hintkey, |
| 36 |
|
| 37 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 38 |
XPK_SEQUENCE( |
| 39 |
XPK_LITERAL("part"), |
| 40 |
XPK_TERMEXPR |
| 41 |
), |
| 42 |
{0} |
| 43 |
}, |
| 44 |
.build = &build_op, |
| 45 |
}; |
| 46 |
|
| 47 |
static const struct XSParseKeywordHooks hooks_optional = { |
| 48 |
.permit_hintkey = hintkey, |
| 49 |
|
| 50 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 51 |
XPK_OPTIONAL( |
| 52 |
XPK_LITERAL("part") |
| 53 |
), |
| 54 |
{0} |
| 55 |
}, |
| 56 |
.build = &build_constiv, |
| 57 |
}; |
| 58 |
|
| 59 |
static const struct XSParseKeywordHooks hooks_repeated = { |
| 60 |
.permit_hintkey = hintkey, |
| 61 |
|
| 62 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 63 |
XPK_REPEATED( |
| 64 |
XPK_LITERAL("part") |
| 65 |
), |
| 66 |
{0} |
| 67 |
}, |
| 68 |
.build = &build_constiv, |
| 69 |
}; |
| 70 |
|
| 71 |
static const struct XSParseKeywordHooks hooks_choice = { |
| 72 |
.permit_hintkey = hintkey, |
| 73 |
|
| 74 |
.pieces = (const struct XSParseKeywordPieceType []) { |
| 75 |
XPK_CHOICE( |
| 76 |
XPK_LITERAL("zero"), |
| 77 |
XPK_LITERAL("one"), |
| 78 |
XPK_LITERAL("two"), |
| 79 |
XPK_BLOCK |
| 80 |
), |
| 81 |
{0} |
| 82 |
}, |
| 83 |
.build = &build_constiv, |
| 84 |
}; |
| 85 |
|
| 86 |
static const struct XSParseKeywordHooks hooks_tagged = { |
| 87 |
.permit_hintkey = hintkey, |
| 88 |
|
| 89 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 90 |
XPK_TAGGEDCHOICE( |
| 91 |
XPK_LITERAL("one"), XPK_TAG(1), |
| 92 |
XPK_LITERAL("two"), XPK_TAG(2), |
| 93 |
XPK_LITERAL("three"), XPK_TAG(3) |
| 94 |
), |
| 95 |
{0} |
| 96 |
}, |
| 97 |
.build = &build_constiv, |
| 98 |
}; |
| 99 |
|
| 100 |
static const struct XSParseKeywordHooks hooks_commalist = { |
| 101 |
.permit_hintkey = hintkey, |
| 102 |
|
| 103 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 104 |
XPK_COMMALIST( XPK_LITERAL("item") ), |
| 105 |
{0} |
| 106 |
}, |
| 107 |
.build = &build_constiv, |
| 108 |
}; |
| 109 |
|
| 110 |
static const struct XSParseKeywordHooks hooks_parens = { |
| 111 |
.permit_hintkey = hintkey, |
| 112 |
|
| 113 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 114 |
XPK_PARENS( XPK_TERMEXPR ), |
| 115 |
{0} |
| 116 |
}, |
| 117 |
.build = &build_op, |
| 118 |
}; |
| 119 |
|
| 120 |
static const struct XSParseKeywordHooks hooks_args = { |
| 121 |
.permit_hintkey = hintkey, |
| 122 |
|
| 123 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 124 |
XPK_ARGS( XPK_TERMEXPR ), |
| 125 |
{0} |
| 126 |
}, |
| 127 |
.build = &build_op, |
| 128 |
}; |
| 129 |
|
| 130 |
static const struct XSParseKeywordHooks hooks_brackets = { |
| 131 |
.permit_hintkey = hintkey, |
| 132 |
|
| 133 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 134 |
XPK_BRACKETS( XPK_TERMEXPR ), |
| 135 |
{0} |
| 136 |
}, |
| 137 |
.build = &build_op, |
| 138 |
}; |
| 139 |
|
| 140 |
static const struct XSParseKeywordHooks hooks_braces = { |
| 141 |
.permit_hintkey = hintkey, |
| 142 |
|
| 143 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 144 |
XPK_BRACES( XPK_TERMEXPR ), |
| 145 |
{0} |
| 146 |
}, |
| 147 |
.build = &build_op, |
| 148 |
}; |
| 149 |
|
| 150 |
static const struct XSParseKeywordHooks hooks_chevrons = { |
| 151 |
.permit_hintkey = hintkey, |
| 152 |
|
| 153 |
.pieces = (const struct XSParseKeywordPieceType []){ |
| 154 |
/* A TERMEXPR inside chevrons is ambiguous, because of the < 2 > 1 > problem */ |
| 155 |
XPK_CHEVRONS( XPK_IDENT ), |
| 156 |
{0} |
| 157 |
}, |
| 158 |
.build = &build_constsv, |
| 159 |
}; |
| 160 |
|
| 161 |
MODULE = t::structures PACKAGE = t::structures |
| 162 |
|
| 163 |
BOOT: |
| 164 |
boot_xs_parse_keyword(0); |
| 165 |
|
| 166 |
register_xs_parse_keyword("structsequence", &hooks_sequence, NULL); |
| 167 |
register_xs_parse_keyword("structoptional", &hooks_optional, NULL); |
| 168 |
register_xs_parse_keyword("structrepeat", &hooks_repeated, NULL); |
| 169 |
register_xs_parse_keyword("structchoice", &hooks_choice, NULL); |
| 170 |
register_xs_parse_keyword("structtagged", &hooks_tagged, NULL); |
| 171 |
register_xs_parse_keyword("structcommalist", &hooks_commalist, NULL); |
| 172 |
|
| 173 |
register_xs_parse_keyword("parens", &hooks_parens, NULL); |
| 174 |
register_xs_parse_keyword("args", &hooks_args, NULL); |
| 175 |
register_xs_parse_keyword("brackets", &hooks_brackets, NULL); |
| 176 |
register_xs_parse_keyword("braces", &hooks_braces, NULL); |
| 177 |
register_xs_parse_keyword("chevrons", &hooks_chevrons, NULL); |