| 1 |
|
| #include <perl.h> |
| 2 |
|
| |
| 3 |
|
| #include "try-catch-constants.h" |
| 4 |
|
| #include "try-catch-parser.h" |
| 5 |
|
| #include "try-catch-op.h" |
| 6 |
|
| |
| 7 |
|
| /*** debug ***/ |
| 8 |
|
| |
| 9 |
|
| #ifdef TRY_PARSER_DEBUG |
| 10 |
|
| #include <perlio.h> |
| 11 |
|
| #define DEBUG_MSG(fmt...) PerlIO_printf(PerlIO_stderr(), "TRY_PARSER_DEBUG: " fmt) |
| 12 |
|
| #else |
| 13 |
|
| #define DEBUG_MSG(fmt...) |
| 14 |
|
| #endif |
| 15 |
|
| |
| 16 |
|
| /*** error reporting ***/ |
| 17 |
|
| #define syntax_error(msg) croak("syntax error: %s", msg) |
| 18 |
|
| |
| 19 |
|
| #define lex_buf_ptr ( PL_parser->bufptr ) |
| 20 |
|
| #define lex_buf_end ( PL_parser->bufend ) |
| 21 |
|
| #define lex_buf_len ( lex_buf_end - lex_buf_ptr ) |
| 22 |
|
| #define lex_next_char ( lex_buf_len > 0 ? lex_buf_ptr[0] : 0 ) |
| 23 |
|
| #define lex_read(n) lex_read_to(lex_buf_ptr + (n)) |
| 24 |
|
| |
| 25 |
|
| #define parse_char(c) my_parse_char(aTHX_ c) |
| 26 |
344 |
| static int my_parse_char(pTHX_ const char c) { |
| 27 |
344 |
▶ | if (lex_next_char != c) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (lex_next_char != c) { |
344 |
0 |
344 |
| if (lex_next_char != c) { |
27 |
317 |
344 |
|
| 28 |
27 |
| return 0; // different character found |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
317 |
| lex_read(1); |
| 32 |
|
| DEBUG_MSG("char: %c\n", c); |
| 33 |
317 |
| return 1; |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| #define parse_keyword(keyword) my_parse_keyword(aTHX_ keyword) |
| 37 |
313 |
| static int my_parse_keyword(pTHX_ char *keyword) |
| 38 |
|
| { |
| 39 |
|
| char *b_ptr, *kw_ptr; |
| 40 |
|
| |
| 41 |
313 |
| b_ptr = lex_buf_ptr; |
| 42 |
1373 |
▶ | for (kw_ptr = keyword; *kw_ptr; kw_ptr++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (kw_ptr = keyword; *kw_ptr; kw_ptr++) { |
1187 |
186 |
1373 |
|
| 43 |
1187 |
▶ | if ( (lex_buf_end <= b_ptr) || (*kw_ptr != *b_ptr) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (lex_buf_end <= b_ptr) || (*kw_ptr != *b_ptr) ) { |
1187 |
0 |
1187 |
| if ( (lex_buf_end <= b_ptr) || (*kw_ptr != *b_ptr) ) { |
127 |
1060 |
1187 |
|
| 44 |
127 |
| return 0; // expected keyword does not match |
| 45 |
|
| } |
| 46 |
1060 |
| b_ptr++; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
186 |
▶ | if ( (lex_buf_end > b_ptr) && (isWORDCHAR(*b_ptr) || (*b_ptr == ':')) ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ( (lex_buf_end > b_ptr) && (isWORDCHAR(*b_ptr) || (*b_ptr == ':')) ) { |
186 |
0 |
186 |
| if ( (lex_buf_end > b_ptr) && (isWORDCHAR(*b_ptr) || (*b_ptr == ':')) ) { |
186 |
0 |
186 |
| if ( (lex_buf_end > b_ptr) && (isWORDCHAR(*b_ptr) || (*b_ptr == ':')) ) { |
0 |
186 |
186 |
|
| 50 |
0 |
| return 0; // there is not end of scanned keyword |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
186 |
| lex_read_to(b_ptr); |
| 54 |
|
| DEBUG_MSG("keyword: %s\n", keyword); |
| 55 |
186 |
| return 1; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| #define parse_identifier(allow_ns) my_parse_identifier(aTHX_ allow_ns) |
| 59 |
209 |
| static SV *my_parse_identifier(pTHX_ int allow_namespace) { |
| 60 |
|
| SV *ident; |
| 61 |
|
| char *end_ptr; |
| 62 |
|
| |
| 63 |
209 |
| end_ptr = lex_buf_ptr; |
| 64 |
1084 |
▶ | while (end_ptr < lex_buf_end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (end_ptr < lex_buf_end) { |
1084 |
0 |
1084 |
|
| 65 |
1084 |
▶ | if ( (*end_ptr == ':') && allow_namespace |
Branch: 2 branches
| Branch | True | False | Total |
| if ( (*end_ptr == ':') && allow_namespace |
61 |
1023 |
1084 |
| if ( (*end_ptr == ':') && allow_namespace |
61 |
0 |
61 |
|
| 66 |
61 |
▶ | && (end_ptr+1 < lex_buf_end) && (end_ptr[1] == ':') |
Branch: 2 branches
| Branch | True | False | Total |
| && (end_ptr+1 < lex_buf_end) && (end_ptr[1] == ':') |
61 |
0 |
61 |
| && (end_ptr+1 < lex_buf_end) && (end_ptr[1] == ':') |
60 |
1 |
61 |
|
| 67 |
|
| ) { |
| 68 |
|
| // namespace separator "::" in identifier |
| 69 |
60 |
| end_ptr += 2; |
| 70 |
60 |
| continue; |
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
1024 |
▶ | if (!isWORDCHAR(*end_ptr)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isWORDCHAR(*end_ptr)) { |
209 |
815 |
1024 |
|
| 74 |
209 |
| break; // end of identifier found |
| 75 |
|
| } |
| 76 |
815 |
| end_ptr++; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
209 |
▶ | if (end_ptr == lex_buf_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (end_ptr == lex_buf_ptr) { |
28 |
181 |
209 |
|
| 80 |
28 |
| return 0; // perl-identifier not found |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
181 |
| ident = newSVpvn(lex_buf_ptr, end_ptr - lex_buf_ptr); |
| 84 |
181 |
| lex_read_to(end_ptr); |
| 85 |
181 |
| return ident; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| #define parse_code_block(inj_code) my_parse_code_block(aTHX_ inj_code) |
| 89 |
285 |
| static OP *my_parse_code_block(pTHX_ char *inject_code) { |
| 90 |
|
| I32 floor; |
| 91 |
|
| OP *content_op, *ret_op; |
| 92 |
|
| |
| 93 |
285 |
| lex_read_space(0); |
| 94 |
285 |
▶ | if (lex_next_char != '{') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (lex_next_char != '{') { |
285 |
0 |
285 |
| if (lex_next_char != '{') { |
4 |
281 |
285 |
|
| 95 |
4 |
| return 0; |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
281 |
| lex_read(1); |
| 99 |
|
| // TODO better might be inject OPcode tree - instead of source-code |
| 100 |
281 |
▶ | if (inject_code) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (inject_code) { |
116 |
165 |
281 |
|
| 101 |
|
| DEBUG_MSG("Inject into block: %s\n", inject_code); |
| 102 |
116 |
| lex_stuff_pvn(inject_code, strlen(inject_code), 0); |
| 103 |
|
| } |
| 104 |
281 |
| lex_stuff_pvs("{ local $" MAIN_PKG "::is_end_of_block;", 0); |
| 105 |
|
| |
| 106 |
281 |
| floor = start_subparse(0, CVf_ANON); |
| 107 |
281 |
| content_op = build_block_content_op(parse_block(0)); |
| 108 |
281 |
| ret_op = newANONSUB(floor, NULL, content_op); |
| 109 |
|
| |
| 110 |
|
| DEBUG_MSG("{ ... }\n"); |
| 111 |
281 |
| lex_read_space(0); |
| 112 |
281 |
| return ret_op; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
|
| #define warn_on_unusual_class_name(name) my_warn_on_unusual_class_name(aTHX_ name) |
| 116 |
85 |
| static void my_warn_on_unusual_class_name(pTHX_ char *name) { |
| 117 |
|
| char *c; |
| 118 |
|
| |
| 119 |
|
| // do not warn if class-name contains ':' or any upper char |
| 120 |
105 |
▶ | for (c=name; *c; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c=name; *c; c++) { |
103 |
2 |
105 |
|
| 121 |
103 |
▶ | if ((*c == ':') || isUPPER(*c)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((*c == ':') || isUPPER(*c)) { |
100 |
3 |
103 |
| if ((*c == ':') || isUPPER(*c)) { |
80 |
20 |
100 |
|
| 122 |
83 |
| return; |
| 123 |
|
| } |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
2 |
| warn("catch: lower case class-name '%s' may lead to confusion" |
| 127 |
|
| " with perl keywords", name); |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| #define parse_catch_args() my_parse_catch_args(aTHX) |
| 131 |
121 |
| static OP *my_parse_catch_args(pTHX) { |
| 132 |
|
| SV *class_name_sv, *var_name_sv; |
| 133 |
|
| OP *block_op; |
| 134 |
121 |
| char *prepend_code = "local $@ = shift;"; |
| 135 |
|
| |
| 136 |
121 |
| class_name_sv = var_name_sv = NULL; |
| 137 |
|
| |
| 138 |
121 |
| lex_read_space(0); |
| 139 |
121 |
▶ | if (parse_char('(')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_char('(')) { |
112 |
9 |
121 |
|
| 140 |
|
| |
| 141 |
|
| // exception class-name |
| 142 |
112 |
| lex_read_space(0); |
| 143 |
112 |
| class_name_sv = parse_identifier(1); |
| 144 |
112 |
▶ | if (class_name_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (class_name_sv) { |
85 |
27 |
112 |
|
| 145 |
|
| DEBUG_MSG("class-name: %s\n", SvPVbyte_nolen(class_name_sv)); |
| 146 |
85 |
| warn_on_unusual_class_name(SvPVbyte_nolen(class_name_sv)); |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| // exception variable-name |
| 150 |
112 |
| lex_read_space(0); |
| 151 |
112 |
▶ | if (parse_char('$')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_char('$')) { |
97 |
15 |
112 |
|
| 152 |
97 |
| var_name_sv = sv_2mortal(parse_identifier(0)); |
| 153 |
97 |
▶ | if (!var_name_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!var_name_sv) { |
1 |
96 |
97 |
|
| 154 |
1 |
| syntax_error("invalid catch syntax"); |
| 155 |
|
| } |
| 156 |
|
| DEBUG_MSG("varname: %s\n", SvPVbyte_nolen(var_name_sv)); |
| 157 |
96 |
| prepend_code = form("local $@ = my $%s=shift;", SvPVbyte_nolen(var_name_sv)); |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
111 |
| lex_read_space(0); |
| 161 |
111 |
▶ | if (!parse_char(')')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_char(')')) { |
3 |
108 |
111 |
|
| 162 |
3 |
| syntax_error("invalid catch syntax"); |
| 163 |
|
| } |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
117 |
| block_op = parse_code_block(prepend_code); |
| 167 |
117 |
▶ | if (!block_op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!block_op) { |
1 |
116 |
117 |
|
| 168 |
1 |
| syntax_error("expected block after 'catch()'"); |
| 169 |
|
| } |
| 170 |
116 |
| return build_catch_args_optree(block_op, class_name_sv); |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
|
| #define parse_all_catch_blocks() my_parse_all_catch_blocks(aTHX) |
| 174 |
101 |
| static OP *my_parse_all_catch_blocks(pTHX) { |
| 175 |
|
| OP *catch_list_op; |
| 176 |
|
| |
| 177 |
101 |
| catch_list_op = NULL; |
| 178 |
217 |
▶ | while (parse_keyword("catch")) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (parse_keyword("catch")) { |
121 |
96 |
217 |
|
| 179 |
121 |
▶ | if (!catch_list_op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!catch_list_op) { |
77 |
44 |
121 |
|
| 180 |
77 |
| catch_list_op = newNULLLIST(); |
| 181 |
|
| } |
| 182 |
121 |
| catch_list_op = op_append_elem(OP_LIST, |
| 183 |
|
| catch_list_op, |
| 184 |
|
| newANONLIST( parse_catch_args() ) |
| 185 |
|
| ); |
| 186 |
|
| } |
| 187 |
96 |
| return catch_list_op; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| #define parse_finally_block() my_parse_finally_block(aTHX) |
| 191 |
96 |
| static OP *my_parse_finally_block(pTHX) { |
| 192 |
|
| OP *finally_block; |
| 193 |
|
| |
| 194 |
96 |
▶ | if (!parse_keyword("finally")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_keyword("finally")) { |
31 |
65 |
96 |
|
| 195 |
31 |
| return NULL; |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
65 |
| finally_block = parse_code_block(NULL); |
| 199 |
65 |
▶ | if (!finally_block) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!finally_block) { |
1 |
64 |
65 |
|
| 200 |
1 |
| syntax_error("expected block after 'finally'"); |
| 201 |
|
| } |
| 202 |
64 |
| return finally_block; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
103 |
| static OP *my_parse_try_statement(pTHX) |
| 206 |
|
| { |
| 207 |
|
| OP *try_block_op, *catch_list_op, *finally_block_op, *ret_op; |
| 208 |
|
| |
| 209 |
103 |
| try_block_op = parse_code_block(NULL); |
| 210 |
103 |
▶ | if (!try_block_op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!try_block_op) { |
2 |
101 |
103 |
|
| 211 |
2 |
| syntax_error("expected block after 'try'"); |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
101 |
| catch_list_op = parse_all_catch_blocks(); |
| 215 |
96 |
| finally_block_op = parse_finally_block(); |
| 216 |
|
| |
| 217 |
95 |
▶ | if (!catch_list_op && !finally_block_op) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!catch_list_op && !finally_block_op) { |
23 |
72 |
95 |
| if (!catch_list_op && !finally_block_op) { |
1 |
22 |
23 |
|
| 218 |
1 |
| syntax_error("expected catch/finally after try block"); |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
94 |
| ret_op = build_statement_optree( |
| 222 |
|
| try_block_op, catch_list_op, finally_block_op); |
| 223 |
|
| #ifdef TRY_PARSER_DUMP |
| 224 |
|
| op_dump(ret_op); |
| 225 |
|
| #endif |
| 226 |
94 |
| return ret_op; |
| 227 |
|
| } |
| 228 |
|
| |