Numworks Epsilon  1.4.1
Graphing Calculator Operating System
lexer.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 #ifndef MICROPY_INCLUDED_PY_LEXER_H
27 #define MICROPY_INCLUDED_PY_LEXER_H
28 
29 #include <stdint.h>
30 
31 #include "py/mpconfig.h"
32 #include "py/qstr.h"
33 #include "py/reader.h"
34 
35 /* lexer.h -- simple tokeniser for MicroPython
36  *
37  * Uses (byte) length instead of null termination.
38  * Tokens are the same - UTF-8 with (byte) length.
39  */
40 
41 typedef enum _mp_token_kind_t {
43 
47 
51 
57 
59 
67  #if MICROPY_PY_ASYNC_AWAIT
68  MP_TOKEN_KW_ASYNC,
69  MP_TOKEN_KW_AWAIT,
70  #endif
98 
118 
145 
146 // this data structure is exposed for efficiency
147 // public members are: source_name, tok_line, tok_column, tok_kind, vstr
148 typedef struct _mp_lexer_t {
149  qstr source_name; // name of source
150  mp_reader_t reader; // stream source
151 
152  unichar chr0, chr1, chr2; // current cached characters from source
153 
154  size_t line; // current source line
155  size_t column; // current source column
156 
157  mp_int_t emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
158  mp_int_t nested_bracket_level; // >0 when there are nested brackets over multiple lines
159 
163 
164  size_t tok_line; // token source line
165  size_t tok_column; // token source column
166  mp_token_kind_t tok_kind; // token kind
167  vstr_t vstr; // token data
168 } mp_lexer_t;
169 
170 mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader);
171 mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len);
172 
173 void mp_lexer_free(mp_lexer_t *lex);
174 void mp_lexer_to_next(mp_lexer_t *lex);
175 
176 /******************************************************************/
177 // platform specific import function; must be implemented for a specific port
178 // TODO tidy up, rename, or put elsewhere
179 
180 //mp_lexer_t *mp_import_open_file(qstr mod_name);
181 
182 typedef enum {
187 
188 mp_import_stat_t mp_import_stat(const char *path);
189 mp_lexer_t *mp_lexer_new_from_file(const char *filename);
190 
191 #if MICROPY_HELPER_LEXER_UNIX
192 mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd);
193 #endif
194 
195 #endif // MICROPY_INCLUDED_PY_LEXER_H
size_t alloc_indent_level
Definition: lexer.h:160
unichar chr0
Definition: lexer.h:152
intptr_t mp_int_t
Definition: mpconfigport.h:73
void mp_lexer_to_next(mp_lexer_t *lex)
Definition: misc.h:142
void mp_lexer_free(mp_lexer_t *lex)
vstr_t vstr
Definition: lexer.h:167
size_t tok_column
Definition: lexer.h:165
mp_lexer_t * mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len)
unsigned short uint16_t
Definition: stdint.h:5
mp_lexer_t * mp_lexer_new(qstr src_name, mp_reader_t reader)
mp_token_kind_t tok_kind
Definition: lexer.h:166
enum _mp_token_kind_t mp_token_kind_t
uint16_t * indent_level
Definition: lexer.h:162
size_t tok_line
Definition: lexer.h:164
size_t qstr
Definition: qstr.h:48
_mp_token_kind_t
Definition: lexer.h:41
size_t line
Definition: lexer.h:154
unichar chr1
Definition: lexer.h:152
mp_reader_t reader
Definition: lexer.h:150
mp_int_t nested_bracket_level
Definition: lexer.h:158
size_t num_indent_level
Definition: lexer.h:161
unichar chr2
Definition: lexer.h:152
struct _mp_lexer_t mp_lexer_t
size_t column
Definition: lexer.h:155
mp_int_t emit_dent
Definition: lexer.h:157
qstr source_name
Definition: lexer.h:149
mp_import_stat_t mp_import_stat(const char *path)
Definition: port.cpp:159
uint unichar
Definition: misc.h:119
mp_import_stat_t
Definition: lexer.h:182
mp_lexer_t * mp_lexer_new_from_file(const char *filename)
Definition: port.cpp:146