blob: 5481b1032dea9a061bd41d0c5efaf0524bd54e4b [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 */
Damien George9193f892014-01-08 15:28:26 +000026
Damien George51dfcb42015-01-01 20:27:54 +000027#include "py/lexer.h"
Damien George9193f892014-01-08 15:28:26 +000028
29typedef struct _mp_lexer_str_buf_t {
Damien George54eb4e72014-07-03 13:47:47 +010030 mp_uint_t free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
Damien George9193f892014-01-08 15:28:26 +000031 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 George94fbe972014-07-30 11:46:05 +010036STATIC mp_uint_t str_buf_next_byte(mp_lexer_str_buf_t *sb) {
Damien George9193f892014-01-08 15:28:26 +000037 if (sb->src_cur < sb->src_end) {
38 return *sb->src_cur++;
39 } else {
Damien George94fbe972014-07-30 11:46:05 +010040 return MP_LEXER_EOF;
Damien George9193f892014-01-08 15:28:26 +000041 }
42}
43
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020044STATIC void str_buf_free(mp_lexer_str_buf_t *sb) {
Damien George9193f892014-01-08 15:28:26 +000045 if (sb->free_len > 0) {
Damien George4d77e1a2015-02-27 09:34:51 +000046 m_del(char, (char*)sb->src_beg, sb->free_len);
Damien George9193f892014-01-08 15:28:26 +000047 }
Paul Sokolovsky39763c62014-01-24 00:38:31 +020048 m_del_obj(mp_lexer_str_buf_t, sb);
Damien George9193f892014-01-08 15:28:26 +000049}
50
Damien George54eb4e72014-07-03 13:47:47 +010051mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
Damien George9bf5f282014-10-09 16:53:37 +010052 mp_lexer_str_buf_t *sb = m_new_obj_maybe(mp_lexer_str_buf_t);
Dave Hylandse20cbbe2014-10-08 23:17:35 -070053 if (sb == NULL) {
54 return NULL;
55 }
Damien George9193f892014-01-08 15:28:26 +000056 sb->free_len = free_len;
57 sb->src_beg = str;
58 sb->src_cur = str;
59 sb->src_end = str + len;
Damien George94fbe972014-07-30 11:46:05 +010060 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 George9193f892014-01-08 15:28:26 +000061}