Skip to content

Commit 9528cd6

Browse files
committed
Convert parse errors to exceptions.
Parser no longer prints an error, but instead returns an exception ID and message.
1 parent 24224d7 commit 9528cd6

9 files changed

Lines changed: 74 additions & 42 deletions

File tree

py/builtinimport.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
4848
rt_globals_set(mp_obj_module_get_globals(module_obj));
4949

5050
// parse the imported script
51-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
51+
qstr parse_exc_id;
52+
const char *parse_exc_msg;
53+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
5254
mp_lexer_free(lex);
5355

5456
if (pn == MP_PARSE_NODE_NULL) {
55-
// TODO handle parse error correctly
57+
// parse error; clean up and raise exception
5658
rt_locals_set(old_locals);
5759
rt_globals_set(old_globals);
58-
return mp_const_none;
60+
nlr_jump(mp_obj_new_exception_msg(parse_exc_id, parse_exc_msg));
5961
}
6062

63+
// compile the imported script
6164
mp_obj_t module_fun = mp_compile(pn, false);
6265

6366
if (module_fun == mp_const_none) {

py/lexer.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct _mp_lexer_t {
3535
mp_token_t tok_cur;
3636
};
3737

38+
// TODO replace with a call to a standard function
3839
bool str_strn_equal(const char *str, const char *strn, int len) {
3940
uint i = 0;
4041

@@ -66,15 +67,6 @@ void mp_token_show(const mp_token_t *tok) {
6667
printf("\n");
6768
}
6869

69-
void mp_token_show_error_prefix(const mp_token_t *tok) {
70-
printf("(%s:%d:%d) ", tok->src_name, tok->src_line, tok->src_column);
71-
}
72-
73-
bool mp_token_show_error(const mp_token_t *tok, const char *msg) {
74-
printf("(%s:%d:%d) %s\n", tok->src_name, tok->src_line, tok->src_column, msg);
75-
return false;
76-
}
77-
7870
#define CUR_CHAR(lex) ((lex)->chr0)
7971

8072
static bool is_end(mp_lexer_t *lex) {
@@ -684,8 +676,9 @@ bool mp_lexer_opt_str(mp_lexer_t *lex, const char *str) {
684676
}
685677
*/
686678

687-
bool mp_lexer_show_error(mp_lexer_t *lex, const char *msg) {
688-
return mp_token_show_error(&lex->tok_cur, msg);
679+
bool mp_lexer_show_error_pythonic_prefix(mp_lexer_t *lex) {
680+
printf(" File \"%s\", line %d column %d\n", lex->tok_cur.src_name, lex->tok_cur.src_line, lex->tok_cur.src_column);
681+
return false;
689682
}
690683

691684
bool mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg) {

py/lexer.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ typedef void (*mp_lexer_stream_close_t)(void*);
124124
typedef struct _mp_lexer_t mp_lexer_t;
125125

126126
void mp_token_show(const mp_token_t *tok);
127-
void mp_token_show_error_prefix(const mp_token_t *tok);
128-
bool mp_token_show_error(const mp_token_t *tok, const char *msg);
129127

130128
mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close);
131129
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, uint free_len);
@@ -134,12 +132,8 @@ void mp_lexer_free(mp_lexer_t *lex);
134132
void mp_lexer_to_next(mp_lexer_t *lex);
135133
const mp_token_t *mp_lexer_cur(const mp_lexer_t *lex);
136134
bool mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind);
137-
/* unused
138-
bool mp_lexer_is_str(mp_lexer_t *lex, const char *str);
139-
bool mp_lexer_opt_kind(mp_lexer_t *lex, mp_token_kind_t kind);
140-
bool mp_lexer_opt_str(mp_lexer_t *lex, const char *str);
141-
*/
142-
bool mp_lexer_show_error(mp_lexer_t *lex, const char *msg);
135+
136+
bool mp_lexer_show_error_pythonic_prefix(mp_lexer_t *lex);
143137
bool mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg);
144138

145139
// used to import a module; must be implemented for a specific port

py/mpqstrraw.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ Q(StopIteration)
2424

2525
Q(AssertionError)
2626
Q(AttributeError)
27+
Q(IndentationError)
2728
Q(IndexError)
2829
Q(KeyError)
2930
Q(NameError)
31+
Q(OSError)
3032
Q(SyntaxError)
3133
Q(TypeError)
3234
Q(ValueError)
33-
Q(OSError)
3435

3536
Q(abs)
3637
Q(all)

py/parse.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "misc.h"
1010
#include "mpconfig.h"
11+
#include "mpqstr.h"
1112
#include "lexer.h"
1213
#include "parse.h"
1314

@@ -265,7 +266,7 @@ static void push_result_rule(parser_t *parser, const rule_t *rule, int num_args)
265266
push_result_node(parser, (mp_parse_node_t)pn);
266267
}
267268

268-
mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
269+
mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr *exc_id_out, const char **exc_msg_out) {
269270

270271
// allocate memory for the parser and its stacks
271272

@@ -598,17 +599,20 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
598599
return result;
599600

600601
syntax_error:
601-
// TODO these should raise a proper exception
602602
if (mp_lexer_is_kind(lex, MP_TOKEN_INDENT)) {
603-
mp_lexer_show_error_pythonic(lex, "IndentationError: unexpected indent");
603+
*exc_id_out = MP_QSTR_IndentationError;
604+
*exc_msg_out = "unexpected indent";
604605
} else if (mp_lexer_is_kind(lex, MP_TOKEN_DEDENT_MISMATCH)) {
605-
mp_lexer_show_error_pythonic(lex, "IndentationError: unindent does not match any outer indentation level");
606+
*exc_id_out = MP_QSTR_IndentationError;
607+
*exc_msg_out = "unindent does not match any outer indentation level";
606608
} else {
607-
mp_lexer_show_error_pythonic(lex, "syntax error:");
609+
*exc_id_out = MP_QSTR_SyntaxError;
610+
*exc_msg_out = "invalid syntax";
608611
#ifdef USE_RULE_NAME
609-
mp_lexer_show_error(lex, rule->rule_name);
610-
#endif
612+
// debugging: print the rule name that failed and the token
613+
mp_lexer_show_error_pythonic(lex, rule->rule_name);
611614
mp_token_show(mp_lexer_cur(lex));
615+
#endif
612616
}
613617
result = MP_PARSE_NODE_NULL;
614618
goto finished;

py/parse.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ typedef enum {
6262
MP_PARSE_EVAL_INPUT,
6363
} mp_parse_input_kind_t;
6464

65-
mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);
65+
// returns MP_PARSE_NODE_NULL on error, and then exc_id_out and exc_msg_out are valid
66+
mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr *exc_id_out, const char **exc_msg_out);

stm/main.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,18 @@ void do_repl(void) {
418418
}
419419

420420
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), 0);
421-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
422-
mp_lexer_free(lex);
423-
424-
if (pn != MP_PARSE_NODE_NULL) {
421+
qstr parse_exc_id;
422+
const char *parse_exc_msg;
423+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_exc_id, &parse_exc_msg);
424+
425+
if (pn == MP_PARSE_NODE_NULL) {
426+
// parse error
427+
mp_lexer_show_error_pythonic_prefix(lex);
428+
printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
429+
mp_lexer_free(lex);
430+
} else {
431+
// parse okay
432+
mp_lexer_free(lex);
425433
mp_obj_t module_fun = mp_compile(pn, true);
426434
if (module_fun != mp_const_none) {
427435
nlr_buf_t nlr;
@@ -455,13 +463,20 @@ bool do_file(const char *filename) {
455463
return false;
456464
}
457465

458-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
459-
mp_lexer_free(lex);
466+
qstr parse_exc_id;
467+
const char *parse_exc_msg;
468+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
460469

461470
if (pn == MP_PARSE_NODE_NULL) {
471+
// parse error
472+
mp_lexer_show_error_pythonic_prefix(lex);
473+
printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
474+
mp_lexer_free(lex);
462475
return false;
463476
}
464477

478+
mp_lexer_free(lex);
479+
465480
mp_obj_t module_fun = mp_compile(pn, false);
466481
if (module_fun == mp_const_none) {
467482
return false;
@@ -1073,7 +1088,9 @@ int main(void) {
10731088
// nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
10741089
printf("lex; al=%u\n", m_get_total_bytes_allocated());
10751090
sys_tick_delay_ms(1000);
1076-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
1091+
qstr parse_exc_id;
1092+
const char *parse_exc_msg;
1093+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
10771094
mp_lexer_free(lex);
10781095
if (pn != MP_PARSE_NODE_NULL) {
10791096
printf("pars;al=%u\n", m_get_total_bytes_allocated());

unix-cpy/main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@ void do_file(const char *file) {
2727
mp_lexer_free(lex);
2828

2929
} else {
30-
// compile
30+
// parse
31+
qstr parse_exc_id;
32+
const char *parse_exc_msg;
33+
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
34+
35+
if (pn == MP_PARSE_NODE_NULL) {
36+
// parse error
37+
mp_lexer_show_error_pythonic_prefix(lex);
38+
printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
39+
mp_lexer_free(lex);
40+
return;
41+
}
3142

32-
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
3343
mp_lexer_free(lex);
3444

3545
if (pn != MP_PARSE_NODE_NULL) {
3646
//printf("----------------\n");
3747
//parse_node_show(pn, 0);
3848
//printf("----------------\n");
49+
50+
// compile
3951
mp_obj_t module_fun = mp_compile(pn, false);
52+
4053
//printf("----------------\n");
4154

4255
if (module_fun == mp_const_none) {

unix/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
3737
return;
3838
}
3939

40-
mp_parse_node_t pn = mp_parse(lex, input_kind);
41-
mp_lexer_free(lex);
40+
qstr parse_exc_id;
41+
const char *parse_exc_msg;
42+
mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_exc_id, &parse_exc_msg);
4243

4344
if (pn == MP_PARSE_NODE_NULL) {
4445
// parse error
46+
mp_lexer_show_error_pythonic_prefix(lex);
47+
printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg);
48+
mp_lexer_free(lex);
4549
return;
4650
}
4751

52+
mp_lexer_free(lex);
53+
4854
//printf("----------------\n");
4955
//parse_node_show(pn, 0);
5056
//printf("----------------\n");

0 commit comments

Comments
 (0)