blob: 3177e9a341ce3882f24c991b9aa0a87f8ac6dda2 [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
14#define STR_UNEXPECTED_INDENT "unexpected indent"
15#define STR_UNMATCHED_UNINDENT "unindent does not match any outer indentation level"
16#define STR_INVALID_SYNTAX "invalid syntax"
17
18void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
19 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);
20 switch (parse_error_kind) {
21 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
22 printf("IndentationError: %s\n", STR_UNEXPECTED_INDENT);
23 break;
24
25 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
26 printf("IndentationError: %s\n", STR_UNMATCHED_UNINDENT);
27 break;
28
29 case MP_PARSE_ERROR_INVALID_SYNTAX:
30 default:
31 printf("SyntaxError: %s\n", STR_INVALID_SYNTAX);
32 break;
33 }
34}
35
36mp_obj_t mp_parse_make_exception(mp_parse_error_kind_t parse_error_kind) {
37 // TODO add source file and line number to exception?
38 switch (parse_error_kind) {
39 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
40 return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
41
42 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
43 return mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
44
45 case MP_PARSE_ERROR_INVALID_SYNTAX:
46 default:
47 return mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
48 }
49}