blob: 76e90671bea03a6325f9742d2f29df610687c205 [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
27#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000028#include "mpconfig.h"
29#include "qstr.h"
Damien George9193f892014-01-08 15:28:26 +000030#include "lexer.h"
31
32typedef struct _mp_lexer_str_buf_t {
33 uint free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
34 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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020039STATIC unichar str_buf_next_char(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 {
43 return MP_LEXER_CHAR_EOF;
44 }
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 Georgeb829b5c2014-01-25 13:51:19 +000054mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint 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;
60 return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_char_t)str_buf_next_char, (mp_lexer_stream_close_t)str_buf_free);
61}