Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 27 | // 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 George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 40 | #define STR_MEMORY "parser could not allocate enough memory" |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 41 | #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 | |
| 45 | void 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 George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 48 | case MP_PARSE_ERROR_MEMORY: |
| 49 | printf("MemoryError: %s\n", STR_MEMORY); |
| 50 | break; |
| 51 | |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 52 | 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 George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 67 | mp_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 George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 70 | switch (parse_error_kind) { |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 71 | case MP_PARSE_ERROR_MEMORY: |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 72 | exc = mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY); |
| 73 | break; |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 74 | |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 75 | case MP_PARSE_ERROR_UNEXPECTED_INDENT: |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 76 | exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT); |
| 77 | break; |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 78 | |
| 79 | case MP_PARSE_ERROR_UNMATCHED_UNINDENT: |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 80 | exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT); |
| 81 | break; |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 82 | |
| 83 | case MP_PARSE_ERROR_INVALID_SYNTAX: |
| 84 | default: |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 85 | exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX); |
| 86 | break; |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 87 | } |
Damien George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 88 | |
| 89 | // add traceback to give info about file name and location |
Damien George | 0e4ba25 | 2014-04-13 15:01:28 +0100 | [diff] [blame] | 90 | // 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 George | 4b01de4 | 2014-04-13 11:56:02 +0100 | [diff] [blame] | 92 | |
| 93 | return exc; |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 94 | } |