2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2014-12-25 21:29:19 +00:00
|
|
|
#ifndef __MICROPY_INCLUDED_PY_LEXER_H__
|
|
|
|
#define __MICROPY_INCLUDED_PY_LEXER_H__
|
2014-05-03 23:27:38 +01:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/qstr.h"
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
/* lexer.h -- simple tokeniser for Micro Python
|
|
|
|
*
|
|
|
|
* Uses (byte) length instead of null termination.
|
|
|
|
* Tokens are the same - UTF-8 with (byte) length.
|
2013-10-04 19:53:11 +01:00
|
|
|
*/
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef enum _mp_token_kind_t {
|
|
|
|
MP_TOKEN_END, // 0
|
|
|
|
|
|
|
|
MP_TOKEN_INVALID,
|
|
|
|
MP_TOKEN_DEDENT_MISMATCH,
|
|
|
|
MP_TOKEN_LONELY_STRING_OPEN,
|
2014-01-12 13:55:24 +00:00
|
|
|
MP_TOKEN_BAD_LINE_CONTINUATION,
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-01-12 13:55:24 +00:00
|
|
|
MP_TOKEN_NEWLINE, // 5
|
|
|
|
MP_TOKEN_INDENT, // 6
|
|
|
|
MP_TOKEN_DEDENT, // 7
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-01-12 13:55:24 +00:00
|
|
|
MP_TOKEN_NAME, // 8
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_INTEGER,
|
|
|
|
MP_TOKEN_FLOAT_OR_IMAG,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_STRING,
|
|
|
|
MP_TOKEN_BYTES,
|
|
|
|
|
2014-01-04 18:44:46 +00:00
|
|
|
MP_TOKEN_ELLIPSIS,
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_KW_FALSE, // 14
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_NONE,
|
|
|
|
MP_TOKEN_KW_TRUE,
|
|
|
|
MP_TOKEN_KW_AND,
|
|
|
|
MP_TOKEN_KW_AS,
|
|
|
|
MP_TOKEN_KW_ASSERT,
|
|
|
|
MP_TOKEN_KW_BREAK,
|
|
|
|
MP_TOKEN_KW_CLASS,
|
|
|
|
MP_TOKEN_KW_CONTINUE,
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_KW_DEF, // 23
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_DEL,
|
|
|
|
MP_TOKEN_KW_ELIF,
|
|
|
|
MP_TOKEN_KW_ELSE,
|
|
|
|
MP_TOKEN_KW_EXCEPT,
|
|
|
|
MP_TOKEN_KW_FINALLY,
|
|
|
|
MP_TOKEN_KW_FOR,
|
|
|
|
MP_TOKEN_KW_FROM,
|
|
|
|
MP_TOKEN_KW_GLOBAL,
|
|
|
|
MP_TOKEN_KW_IF,
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_KW_IMPORT, // 33
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_IN,
|
|
|
|
MP_TOKEN_KW_IS,
|
|
|
|
MP_TOKEN_KW_LAMBDA,
|
|
|
|
MP_TOKEN_KW_NONLOCAL,
|
|
|
|
MP_TOKEN_KW_NOT,
|
|
|
|
MP_TOKEN_KW_OR,
|
|
|
|
MP_TOKEN_KW_PASS,
|
|
|
|
MP_TOKEN_KW_RAISE,
|
|
|
|
MP_TOKEN_KW_RETURN,
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_KW_TRY, // 43
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_WHILE,
|
|
|
|
MP_TOKEN_KW_WITH,
|
|
|
|
MP_TOKEN_KW_YIELD,
|
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_OP_PLUS, // 47
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_OP_MINUS,
|
|
|
|
MP_TOKEN_OP_STAR,
|
|
|
|
MP_TOKEN_OP_DBL_STAR,
|
|
|
|
MP_TOKEN_OP_SLASH,
|
|
|
|
MP_TOKEN_OP_DBL_SLASH,
|
|
|
|
MP_TOKEN_OP_PERCENT,
|
|
|
|
MP_TOKEN_OP_LESS,
|
|
|
|
MP_TOKEN_OP_DBL_LESS,
|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DEL_PIPE_EQUAL,
|
2015-02-08 01:57:40 +00:00
|
|
|
MP_TOKEN_DEL_CARET_EQUAL, // 86
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_DEL_DBL_MORE_EQUAL,
|
|
|
|
MP_TOKEN_DEL_DBL_LESS_EQUAL,
|
|
|
|
MP_TOKEN_DEL_DBL_STAR_EQUAL,
|
|
|
|
MP_TOKEN_DEL_MINUS_MORE,
|
|
|
|
} mp_token_kind_t;
|
|
|
|
|
2014-07-30 11:46:05 +01:00
|
|
|
// the next-byte function must return the next byte in the stream
|
|
|
|
// it must return MP_LEXER_EOF if end of stream
|
|
|
|
// it can be called again after returning MP_LEXER_EOF, and in that case must return MP_LEXER_EOF
|
2015-03-19 00:21:29 +00:00
|
|
|
#define MP_LEXER_EOF ((unichar)(-1))
|
|
|
|
|
2014-07-30 11:46:05 +01:00
|
|
|
typedef mp_uint_t (*mp_lexer_stream_next_byte_t)(void*);
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef void (*mp_lexer_stream_close_t)(void*);
|
|
|
|
|
2014-12-05 19:35:18 +00:00
|
|
|
// this data structure is exposed for efficiency
|
|
|
|
// public members are: source_name, tok_line, tok_column, tok_kind, vstr
|
|
|
|
typedef struct _mp_lexer_t {
|
|
|
|
qstr source_name; // name of source
|
|
|
|
void *stream_data; // data for stream
|
|
|
|
mp_lexer_stream_next_byte_t stream_next_byte; // stream callback to get next byte
|
|
|
|
mp_lexer_stream_close_t stream_close; // stream callback to free
|
|
|
|
|
|
|
|
unichar chr0, chr1, chr2; // current cached characters from source
|
|
|
|
|
|
|
|
mp_uint_t line; // current source line
|
|
|
|
mp_uint_t column; // current source column
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-12-05 19:35:18 +00:00
|
|
|
mp_int_t emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
|
|
|
|
mp_int_t nested_bracket_level; // >0 when there are nested brackets over multiple lines
|
|
|
|
|
|
|
|
mp_uint_t alloc_indent_level;
|
|
|
|
mp_uint_t num_indent_level;
|
|
|
|
uint16_t *indent_level;
|
|
|
|
|
|
|
|
mp_uint_t tok_line; // token source line
|
|
|
|
mp_uint_t tok_column; // token source column
|
|
|
|
mp_token_kind_t tok_kind; // token kind
|
|
|
|
vstr_t vstr; // token data
|
|
|
|
} mp_lexer_t;
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-07-30 11:46:05 +01:00
|
|
|
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_byte_t stream_next_byte, mp_lexer_stream_close_t stream_close);
|
2014-07-03 13:47:47 +01:00
|
|
|
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
|
2014-01-08 15:28:26 +00:00
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
void mp_lexer_free(mp_lexer_t *lex);
|
|
|
|
void mp_lexer_to_next(mp_lexer_t *lex);
|
2014-12-05 19:35:18 +00:00
|
|
|
void mp_lexer_show_token(const mp_lexer_t *lex);
|
2014-01-03 14:03:48 +00:00
|
|
|
|
2014-02-05 23:57:48 +00:00
|
|
|
/******************************************************************/
|
|
|
|
// platform specific import function; must be implemented for a specific port
|
|
|
|
// TODO tidy up, rename, or put elsewhere
|
|
|
|
|
|
|
|
//mp_lexer_t *mp_import_open_file(qstr mod_name);
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MP_IMPORT_STAT_NO_EXIST,
|
|
|
|
MP_IMPORT_STAT_DIR,
|
|
|
|
MP_IMPORT_STAT_FILE,
|
|
|
|
} mp_import_stat_t;
|
|
|
|
|
|
|
|
mp_import_stat_t mp_import_stat(const char *path);
|
|
|
|
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
|
2014-06-02 17:37:55 +01:00
|
|
|
|
2014-12-25 21:29:19 +00:00
|
|
|
#endif // __MICROPY_INCLUDED_PY_LEXER_H__
|