blob: 3ead5a3031c8bb019019ab9c6023801ffce92112 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgec5966122014-02-15 16:10:44 +000027// these functions are separate from parse.c to keep parser independent of mp_obj_t
28
29#include <stdint.h>
30#include <stdio.h>
31
32#include "misc.h"
33#include "mpconfig.h"
34#include "qstr.h"
35#include "lexer.h"
36#include "parse.h"
37#include "obj.h"
38#include "parsehelper.h"
39
Damien George58ba4c32014-04-10 14:27:31 +000040#define STR_MEMORY "parser could not allocate enough memory"
Damien Georgec5966122014-02-15 16:10:44 +000041#define STR_UNEXPECTED_INDENT "unexpected indent"
42#define STR_UNMATCHED_UNINDENT "unindent does not match any outer indentation level"
43#define STR_INVALID_SYNTAX "invalid syntax"
44
45void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
46 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);
47 switch (parse_error_kind) {
Damien George58ba4c32014-04-10 14:27:31 +000048 case MP_PARSE_ERROR_MEMORY:
49 printf("MemoryError: %s\n", STR_MEMORY);
50 break;
51
Damien Georgec5966122014-02-15 16:10:44 +000052 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
53 printf("IndentationError: %s\n", STR_UNEXPECTED_INDENT);
54 break;
55
56 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
57 printf("IndentationError: %s\n", STR_UNMATCHED_UNINDENT);
58 break;
59
60 case MP_PARSE_ERROR_INVALID_SYNTAX:
61 default:
62 printf("SyntaxError: %s\n", STR_INVALID_SYNTAX);
63 break;
64 }
65}
66
Damien George4b01de42014-04-13 11:56:02 +010067mp_obj_t mp_parse_make_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
68 // make exception object
69 mp_obj_t exc;
Damien Georgec5966122014-02-15 16:10:44 +000070 switch (parse_error_kind) {
Damien George58ba4c32014-04-10 14:27:31 +000071 case MP_PARSE_ERROR_MEMORY:
Damien George4b01de42014-04-13 11:56:02 +010072 exc = mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY);
73 break;
Damien George58ba4c32014-04-10 14:27:31 +000074
Damien Georgec5966122014-02-15 16:10:44 +000075 case MP_PARSE_ERROR_UNEXPECTED_INDENT:
Damien George4b01de42014-04-13 11:56:02 +010076 exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
77 break;
Damien Georgec5966122014-02-15 16:10:44 +000078
79 case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
Damien George4b01de42014-04-13 11:56:02 +010080 exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
81 break;
Damien Georgec5966122014-02-15 16:10:44 +000082
83 case MP_PARSE_ERROR_INVALID_SYNTAX:
84 default:
Damien George4b01de42014-04-13 11:56:02 +010085 exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
86 break;
Damien Georgec5966122014-02-15 16:10:44 +000087 }
Damien George4b01de42014-04-13 11:56:02 +010088
89 // add traceback to give info about file name and location
Damien George0e4ba252014-04-13 15:01:28 +010090 // we don't have a 'block' name, so just pass the NULL qstr to indicate this
91 mp_obj_exception_add_traceback(exc, mp_lexer_source_name(lex), mp_lexer_cur(lex)->src_line, MP_QSTR_NULL);
Damien George4b01de42014-04-13 11:56:02 +010092
93 return exc;
Damien Georgec5966122014-02-15 16:10:44 +000094}