blob: a919dc7e14d44288d3f5ac6c0dd0d6263870bc67 [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 George55baff42014-01-21 21:40:13 +000027#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030028#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000029#include "qstr.h"
Damien George9193f892014-01-08 15:28:26 +000030#include "lexer.h"
31
32typedef struct _mp_lexer_str_buf_t {
Damien George54eb4e72014-07-03 13:47:47 +010033 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 +000034 const char *src_beg; // beginning of source
35 const char *src_cur; // current location in source
36 const char *src_end; // end (exclusive) of source
37} mp_lexer_str_buf_t;
38
Damien George94fbe972014-07-30 11:46:05 +010039STATIC mp_uint_t str_buf_next_byte(mp_lexer_str_buf_t *sb) {
Damien George9193f892014-01-08 15:28:26 +000040 if (sb->src_cur < sb->src_end) {
41 return *sb->src_cur++;
42 } else {
Damien George94fbe972014-07-30 11:46:05 +010043 return MP_LEXER_EOF;
Damien George9193f892014-01-08 15:28:26 +000044 }
45}
46
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020047STATIC void str_buf_free(mp_lexer_str_buf_t *sb) {
Damien George9193f892014-01-08 15:28:26 +000048 if (sb->free_len > 0) {
49 m_free((char*)sb->src_beg, sb->free_len);
50 }
Paul Sokolovsky39763c62014-01-24 00:38:31 +020051 m_del_obj(mp_lexer_str_buf_t, sb);
Damien George9193f892014-01-08 15:28:26 +000052}
53
Damien George54eb4e72014-07-03 13:47:47 +010054mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
Damien George9193f892014-01-08 15:28:26 +000055 mp_lexer_str_buf_t *sb = m_new_obj(mp_lexer_str_buf_t);
56 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}