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
{
42
MP_TOKEN_END
,
43
44
MP_TOKEN_INVALID
,
45
MP_TOKEN_DEDENT_MISMATCH
,
46
MP_TOKEN_LONELY_STRING_OPEN
,
47
48
MP_TOKEN_NEWLINE
,
49
MP_TOKEN_INDENT
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KEN_OP_SLASH
,
104
MP_TOKEN_OP_DBL_SLASH
,
105
MP_TOKEN_OP_PERCENT
,
106
MP_TOKEN_OP_LESS
,
107
MP_TOKEN_OP_DBL_LESS
,
108
MP_TOKEN_OP_MORE
,
109
MP_TOKEN_OP_DBL_MORE
,
110
MP_TOKEN_OP_AMPERSAND
,
111
MP_TOKEN_OP_PIPE
,
112
MP_TOKEN_OP_CARET
,
113
MP_TOKEN_OP_TILDE
,
114
MP_TOKEN_OP_LESS_EQUAL
,
115
MP_TOKEN_OP_MORE_EQUAL
,
116
MP_TOKEN_OP_DBL_EQUAL
,
117
MP_TOKEN_OP_NOT_EQUAL
,
118
119
MP_TOKEN_DEL_PAREN_OPEN
,
120
MP_TOKEN_DEL_PAREN_CLOSE
,
121
MP_TOKEN_DEL_BRACKET_OPEN
,
122
MP_TOKEN_DEL_BRACKET_CLOSE
,
123
MP_TOKEN_DEL_BRACE_OPEN
,
124
MP_TOKEN_DEL_BRACE_CLOSE
,
125
MP_TOKEN_DEL_COMMA
,
126
MP_TOKEN_DEL_COLON
,
127
MP_TOKEN_DEL_PERIOD
,
128
MP_TOKEN_DEL_SEMICOLON
,
129
MP_TOKEN_DEL_AT
,
130
MP_TOKEN_DEL_EQUAL
,
131
MP_TOKEN_DEL_PLUS_EQUAL
,
132
MP_TOKEN_DEL_MINUS_EQUAL
,
133
MP_TOKEN_DEL_STAR_EQUAL
,
134
MP_TOKEN_DEL_SLASH_EQUAL
,
135
MP_TOKEN_DEL_DBL_SLASH_EQUAL
,
136
MP_TOKEN_DEL_PERCENT_EQUAL
,
137
MP_TOKEN_DEL_AMPERSAND_EQUAL
,
138
MP_TOKEN_DEL_PIPE_EQUAL
,
139
MP_TOKEN_DEL_CARET_EQUAL
,
140
MP_TOKEN_DEL_DBL_MORE_EQUAL
,
141
MP_TOKEN_DEL_DBL_LESS_EQUAL
,
142
MP_TOKEN_DEL_DBL_STAR_EQUAL
,
143
MP_TOKEN_DEL_MINUS_MORE
,
144
}
mp_token_kind_t
;
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
160
size_t
alloc_indent_level
;
161
size_t
num_indent_level
;
162
uint16_t
*
indent_level
;
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
{
183
MP_IMPORT_STAT_NO_EXIST
,
184
MP_IMPORT_STAT_DIR
,
185
MP_IMPORT_STAT_FILE
,
186
}
mp_import_stat_t
;
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
MP_TOKEN_OP_LESS
Definition:
lexer.h:106
_mp_lexer_t::alloc_indent_level
size_t alloc_indent_level
Definition:
lexer.h:160
MP_TOKEN_OP_MINUS
Definition:
lexer.h:100
_mp_lexer_t::chr0
unichar chr0
Definition:
lexer.h:152
MP_TOKEN_KW_PASS
Definition:
lexer.h:91
MP_TOKEN_DEDENT_MISMATCH
Definition:
lexer.h:45
MP_TOKEN_OP_MORE
Definition:
lexer.h:108
MP_IMPORT_STAT_FILE
Definition:
lexer.h:185
MP_TOKEN_KW_AND
Definition:
lexer.h:64
mp_int_t
intptr_t mp_int_t
Definition:
mpconfigport.h:73
mp_lexer_to_next
void mp_lexer_to_next(mp_lexer_t *lex)
MP_TOKEN_KW_CLASS
Definition:
lexer.h:72
_vstr_t
Definition:
misc.h:142
MP_TOKEN_DEL_BRACE_CLOSE
Definition:
lexer.h:124
MP_TOKEN_KW_LAMBDA
Definition:
lexer.h:87
MP_TOKEN_KW_AS
Definition:
lexer.h:65
mp_lexer_free
void mp_lexer_free(mp_lexer_t *lex)
MP_TOKEN_OP_TILDE
Definition:
lexer.h:113
MP_TOKEN_DEL_MINUS_MORE
Definition:
lexer.h:143
MP_TOKEN_DEL_DBL_LESS_EQUAL
Definition:
lexer.h:141
MP_TOKEN_DEL_PLUS_EQUAL
Definition:
lexer.h:131
MP_TOKEN_KW_FALSE
Definition:
lexer.h:60
MP_TOKEN_KW_CONTINUE
Definition:
lexer.h:73
MP_TOKEN_OP_LESS_EQUAL
Definition:
lexer.h:114
reader.h
MP_TOKEN_OP_CARET
Definition:
lexer.h:112
MP_TOKEN_DEL_BRACE_OPEN
Definition:
lexer.h:123
_mp_lexer_t::vstr
vstr_t vstr
Definition:
lexer.h:167
MP_TOKEN_NAME
Definition:
lexer.h:52
MP_TOKEN_KW_DEF
Definition:
lexer.h:74
_mp_lexer_t::tok_column
size_t tok_column
Definition:
lexer.h:165
MP_TOKEN_KW_YIELD
Definition:
lexer.h:97
MP_TOKEN_OP_DBL_STAR
Definition:
lexer.h:102
mp_lexer_new_from_str_len
mp_lexer_t * mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len)
MP_TOKEN_DEL_PAREN_CLOSE
Definition:
lexer.h:120
MP_TOKEN_OP_PERCENT
Definition:
lexer.h:105
MP_TOKEN_BYTES
Definition:
lexer.h:56
MP_TOKEN_KW_ELIF
Definition:
lexer.h:76
uint16_t
unsigned short uint16_t
Definition:
stdint.h:5
MP_TOKEN_OP_STAR
Definition:
lexer.h:101
MP_TOKEN_OP_DBL_EQUAL
Definition:
lexer.h:116
mp_lexer_new
mp_lexer_t * mp_lexer_new(qstr src_name, mp_reader_t reader)
MP_TOKEN_OP_SLASH
Definition:
lexer.h:103
MP_TOKEN_OP_AMPERSAND
Definition:
lexer.h:110
MP_TOKEN_FLOAT_OR_IMAG
Definition:
lexer.h:54
_mp_lexer_t::tok_kind
mp_token_kind_t tok_kind
Definition:
lexer.h:166
_mp_reader_t
Definition:
reader.h:36
MP_TOKEN_OP_MORE_EQUAL
Definition:
lexer.h:115
MP_TOKEN_NEWLINE
Definition:
lexer.h:48
mp_token_kind_t
enum _mp_token_kind_t mp_token_kind_t
MP_TOKEN_OP_DBL_MORE
Definition:
lexer.h:109
MP_TOKEN_KW_TRY
Definition:
lexer.h:94
_mp_lexer_t::indent_level
uint16_t * indent_level
Definition:
lexer.h:162
MP_TOKEN_KW_BREAK
Definition:
lexer.h:71
MP_TOKEN_KW_NONE
Definition:
lexer.h:61
MP_TOKEN_KW_RETURN
Definition:
lexer.h:93
MP_TOKEN_DEL_DBL_MORE_EQUAL
Definition:
lexer.h:140
_mp_lexer_t::tok_line
size_t tok_line
Definition:
lexer.h:164
MP_TOKEN_KW_NONLOCAL
Definition:
lexer.h:88
MP_TOKEN_STRING
Definition:
lexer.h:55
MP_TOKEN_OP_NOT_EQUAL
Definition:
lexer.h:117
MP_TOKEN_DEL_PIPE_EQUAL
Definition:
lexer.h:138
MP_TOKEN_KW_IN
Definition:
lexer.h:85
MP_TOKEN_KW_EXCEPT
Definition:
lexer.h:78
MP_TOKEN_DEL_COMMA
Definition:
lexer.h:125
MP_TOKEN_DEL_CARET_EQUAL
Definition:
lexer.h:139
qstr
size_t qstr
Definition:
qstr.h:48
MP_TOKEN_DEL_STAR_EQUAL
Definition:
lexer.h:133
MP_TOKEN_OP_PIPE
Definition:
lexer.h:111
MP_TOKEN_DEL_PAREN_OPEN
Definition:
lexer.h:119
MP_TOKEN_DEL_COLON
Definition:
lexer.h:126
MP_TOKEN_KW_ASSERT
Definition:
lexer.h:66
MP_TOKEN_LONELY_STRING_OPEN
Definition:
lexer.h:46
MP_TOKEN_KW_IMPORT
Definition:
lexer.h:84
qstr.h
MP_TOKEN_END
Definition:
lexer.h:42
_mp_token_kind_t
_mp_token_kind_t
Definition:
lexer.h:41
_mp_lexer_t::line
size_t line
Definition:
lexer.h:154
MP_TOKEN_KW_WITH
Definition:
lexer.h:96
MP_TOKEN_KW_IS
Definition:
lexer.h:86
_mp_lexer_t::chr1
unichar chr1
Definition:
lexer.h:152
MP_TOKEN_DEL_MINUS_EQUAL
Definition:
lexer.h:132
mpconfig.h
_mp_lexer_t::reader
mp_reader_t reader
Definition:
lexer.h:150
MP_TOKEN_KW_FINALLY
Definition:
lexer.h:79
MP_TOKEN_KW_NOT
Definition:
lexer.h:89
MP_TOKEN_OP_DBL_LESS
Definition:
lexer.h:107
MP_TOKEN_DEL_PERIOD
Definition:
lexer.h:127
MP_IMPORT_STAT_NO_EXIST
Definition:
lexer.h:183
stdint.h
MP_TOKEN_KW___DEBUG__
Definition:
lexer.h:63
MP_TOKEN_ELLIPSIS
Definition:
lexer.h:58
_mp_lexer_t::nested_bracket_level
mp_int_t nested_bracket_level
Definition:
lexer.h:158
MP_TOKEN_KW_GLOBAL
Definition:
lexer.h:82
MP_TOKEN_DEL_SEMICOLON
Definition:
lexer.h:128
MP_TOKEN_KW_DEL
Definition:
lexer.h:75
MP_TOKEN_DEL_PERCENT_EQUAL
Definition:
lexer.h:136
MP_TOKEN_DEL_AMPERSAND_EQUAL
Definition:
lexer.h:137
MP_IMPORT_STAT_DIR
Definition:
lexer.h:184
_mp_lexer_t::num_indent_level
size_t num_indent_level
Definition:
lexer.h:161
MP_TOKEN_INTEGER
Definition:
lexer.h:53
_mp_lexer_t::chr2
unichar chr2
Definition:
lexer.h:152
MP_TOKEN_DEL_AT
Definition:
lexer.h:129
MP_TOKEN_DEL_DBL_SLASH_EQUAL
Definition:
lexer.h:135
MP_TOKEN_DEDENT
Definition:
lexer.h:50
MP_TOKEN_KW_TRUE
Definition:
lexer.h:62
MP_TOKEN_INVALID
Definition:
lexer.h:44
mp_lexer_t
struct _mp_lexer_t mp_lexer_t
MP_TOKEN_KW_OR
Definition:
lexer.h:90
_mp_lexer_t::column
size_t column
Definition:
lexer.h:155
_mp_lexer_t::emit_dent
mp_int_t emit_dent
Definition:
lexer.h:157
MP_TOKEN_DEL_DBL_STAR_EQUAL
Definition:
lexer.h:142
MP_TOKEN_KW_FOR
Definition:
lexer.h:80
_mp_lexer_t
Definition:
lexer.h:148
_mp_lexer_t::source_name
qstr source_name
Definition:
lexer.h:149
MP_TOKEN_DEL_BRACKET_CLOSE
Definition:
lexer.h:122
MP_TOKEN_KW_RAISE
Definition:
lexer.h:92
MP_TOKEN_KW_WHILE
Definition:
lexer.h:95
mp_import_stat
mp_import_stat_t mp_import_stat(const char *path)
Definition:
port.cpp:159
MP_TOKEN_OP_DBL_SLASH
Definition:
lexer.h:104
unichar
uint unichar
Definition:
misc.h:119
MP_TOKEN_KW_FROM
Definition:
lexer.h:81
mp_import_stat_t
mp_import_stat_t
Definition:
lexer.h:182
MP_TOKEN_OP_PLUS
Definition:
lexer.h:99
MP_TOKEN_KW_ELSE
Definition:
lexer.h:77
MP_TOKEN_DEL_BRACKET_OPEN
Definition:
lexer.h:121
mp_lexer_new_from_file
mp_lexer_t * mp_lexer_new_from_file(const char *filename)
Definition:
port.cpp:146
MP_TOKEN_DEL_EQUAL
Definition:
lexer.h:130
MP_TOKEN_INDENT
Definition:
lexer.h:49
MP_TOKEN_KW_IF
Definition:
lexer.h:83
MP_TOKEN_DEL_SLASH_EQUAL
Definition:
lexer.h:134
epsilon
python
src
py
lexer.h
Generated by
1.8.14