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 | */ |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 26 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 27 | #include "py/lexer.h" |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 28 | |
| 29 | typedef struct _mp_lexer_str_buf_t { |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 30 | mp_uint_t free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len) |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 31 | const char *src_beg; // beginning of source |
| 32 | const char *src_cur; // current location in source |
| 33 | const char *src_end; // end (exclusive) of source |
| 34 | } mp_lexer_str_buf_t; |
| 35 | |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 36 | STATIC mp_uint_t str_buf_next_byte(mp_lexer_str_buf_t *sb) { |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 37 | if (sb->src_cur < sb->src_end) { |
| 38 | return *sb->src_cur++; |
| 39 | } else { |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 40 | return MP_LEXER_EOF; |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 44 | STATIC void str_buf_free(mp_lexer_str_buf_t *sb) { |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 45 | if (sb->free_len > 0) { |
Damien George | 4d77e1a | 2015-02-27 09:34:51 +0000 | [diff] [blame] | 46 | m_del(char, (char*)sb->src_beg, sb->free_len); |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 47 | } |
Paul Sokolovsky | 39763c6 | 2014-01-24 00:38:31 +0200 | [diff] [blame] | 48 | m_del_obj(mp_lexer_str_buf_t, sb); |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Damien George | 54eb4e7 | 2014-07-03 13:47:47 +0100 | [diff] [blame] | 51 | mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) { |
Damien George | 9bf5f28 | 2014-10-09 16:53:37 +0100 | [diff] [blame] | 52 | mp_lexer_str_buf_t *sb = m_new_obj_maybe(mp_lexer_str_buf_t); |
Dave Hylands | e20cbbe | 2014-10-08 23:17:35 -0700 | [diff] [blame] | 53 | if (sb == NULL) { |
| 54 | return NULL; |
| 55 | } |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 56 | sb->free_len = free_len; |
| 57 | sb->src_beg = str; |
| 58 | sb->src_cur = str; |
| 59 | sb->src_end = str + len; |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame] | 60 | return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_byte_t)str_buf_next_byte, (mp_lexer_stream_close_t)str_buf_free); |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 61 | } |