blob: e069657b1d1c46965e72e8f9e6e0af992fcf35df [file] [log] [blame]
Damien Georgec5966122014-02-15 16:10:44 +00001// these functions are separate from parse.c to keep parser independent of mp_obj_t
2
3#include <stdint.h>
4#include <stdio.h>
5
6#include "misc.h"
7#include "mpconfig.h"
8#include "qstr.h"
9#include "lexer.h"
10#include "parse.h"
11#include "obj.h"
12#include "parsehelper.h"
13
Damien George58ba4c32014-04-10 14:27:31 +000014#define STR_MEMORY "parser could not allocate enough memory"
Damien Georgec5966122014-02-15 16:10:44 +000015#define STR_UNEXPECTED_INDENT "unexpected indent"
16#define STR_UNMATCHED_UNINDENT "unindent does not match any outer indentation level"
17#define STR_INVALID_SYNTAX "invalid syntax"
18
19void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
20 printf(" File \"%s\", line %d, column %d\n", qstr_str(mp_lexer_source_name(lex)), mp_lexer_cur(lex)->src_line, mp_lexer_cur(lex)->src_column);
21 switch (parse_error_kind) {
Damien George58ba4c32014-04-10 14:27:31 +000022 case MP_PARSE_ERROR_MEMORY:
23 printf("MemoryError: %s\n", STR_MEMORY);
24 break;
25
Damien Georgec5966122014-02-15 16:10:44 +000026 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
27 printf("IndentationError: %s\n", STR_UNEXPECTED_INDENT);
28 break;
29
30 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
31 printf("IndentationError: %s\n", STR_UNMATCHED_UNINDENT);
32 break;
33
34 case MP_PARSE_ERROR_INVALID_SYNTAX:
35 default:
36 printf("SyntaxError: %s\n", STR_INVALID_SYNTAX);
37 break;
38 }
39}
40
41mp_obj_t mp_parse_make_exception(mp_parse_error_kind_t parse_error_kind) {
42 // TODO add source file and line number to exception?
43 switch (parse_error_kind) {
Damien George58ba4c32014-04-10 14:27:31 +000044 case MP_PARSE_ERROR_MEMORY:
45 return mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY);
46
Damien Georgec5966122014-02-15 16:10:44 +000047 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
48 return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
49
50 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
51 return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
52
53 case MP_PARSE_ERROR_INVALID_SYNTAX:
54 default:
55 return mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
56 }
57}