2014-05-03 22:27:38 +00:00
|
|
|
/*
|
2017-06-30 07:22:17 +00:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 22:27:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 21:14:58 +00:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_LEXER_H
|
|
|
|
#define MICROPY_INCLUDED_PY_LEXER_H
|
2014-05-03 22:27:38 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/qstr.h"
|
2016-11-16 07:27:20 +00:00
|
|
|
#include "py/reader.h"
|
2015-01-01 20:27:54 +00:00
|
|
|
|
2017-06-30 07:22:17 +00:00
|
|
|
/* lexer.h -- simple tokeniser for MicroPython
|
2013-12-21 18:17:45 +00:00
|
|
|
*
|
|
|
|
* Uses (byte) length instead of null termination.
|
|
|
|
* Tokens are the same - UTF-8 with (byte) length.
|
2013-10-04 18:53:11 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef enum _mp_token_kind_t {
|
2017-02-17 00:30:14 +00:00
|
|
|
MP_TOKEN_END,
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
MP_TOKEN_INVALID,
|
|
|
|
MP_TOKEN_DEDENT_MISMATCH,
|
|
|
|
MP_TOKEN_LONELY_STRING_OPEN,
|
2021-08-12 15:44:08 +00:00
|
|
|
#if MICROPY_PY_FSTRINGS
|
|
|
|
MP_TOKEN_MALFORMED_FSTRING,
|
|
|
|
MP_TOKEN_FSTRING_RAW,
|
|
|
|
#endif
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2017-02-17 00:30:14 +00:00
|
|
|
MP_TOKEN_NEWLINE,
|
|
|
|
MP_TOKEN_INDENT,
|
|
|
|
MP_TOKEN_DEDENT,
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2017-02-17 00:30:14 +00:00
|
|
|
MP_TOKEN_NAME,
|
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
|
|
|
|
2017-02-17 00:30:14 +00:00
|
|
|
MP_TOKEN_KW_FALSE,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_NONE,
|
|
|
|
MP_TOKEN_KW_TRUE,
|
2017-02-17 00:10:35 +00:00
|
|
|
MP_TOKEN_KW___DEBUG__,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_AND,
|
|
|
|
MP_TOKEN_KW_AS,
|
|
|
|
MP_TOKEN_KW_ASSERT,
|
2016-01-27 20:23:11 +00:00
|
|
|
#if MICROPY_PY_ASYNC_AWAIT
|
|
|
|
MP_TOKEN_KW_ASYNC,
|
|
|
|
MP_TOKEN_KW_AWAIT,
|
|
|
|
#endif
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_BREAK,
|
|
|
|
MP_TOKEN_KW_CLASS,
|
|
|
|
MP_TOKEN_KW_CONTINUE,
|
2017-02-17 00:10:35 +00:00
|
|
|
MP_TOKEN_KW_DEF,
|
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,
|
2017-02-17 00:10:35 +00:00
|
|
|
MP_TOKEN_KW_IMPORT,
|
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,
|
2017-02-17 00:10:35 +00:00
|
|
|
MP_TOKEN_KW_TRY,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_KW_WHILE,
|
|
|
|
MP_TOKEN_KW_WITH,
|
|
|
|
MP_TOKEN_KW_YIELD,
|
|
|
|
|
2020-06-16 11:42:44 +00:00
|
|
|
MP_TOKEN_OP_ASSIGN,
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_OP_TILDE,
|
|
|
|
|
|
|
|
// Order of these 6 matches corresponding mp_binary_op_t operator
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_OP_LESS,
|
|
|
|
MP_TOKEN_OP_MORE,
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_OP_DBL_EQUAL,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_OP_LESS_EQUAL,
|
|
|
|
MP_TOKEN_OP_MORE_EQUAL,
|
|
|
|
MP_TOKEN_OP_NOT_EQUAL,
|
|
|
|
|
2019-07-25 03:15:54 +00:00
|
|
|
// Order of these 13 matches corresponding mp_binary_op_t operator
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_OP_PIPE,
|
|
|
|
MP_TOKEN_OP_CARET,
|
|
|
|
MP_TOKEN_OP_AMPERSAND,
|
|
|
|
MP_TOKEN_OP_DBL_LESS,
|
|
|
|
MP_TOKEN_OP_DBL_MORE,
|
|
|
|
MP_TOKEN_OP_PLUS,
|
|
|
|
MP_TOKEN_OP_MINUS,
|
|
|
|
MP_TOKEN_OP_STAR,
|
2019-07-25 03:15:54 +00:00
|
|
|
MP_TOKEN_OP_AT,
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_OP_DBL_SLASH,
|
|
|
|
MP_TOKEN_OP_SLASH,
|
|
|
|
MP_TOKEN_OP_PERCENT,
|
|
|
|
MP_TOKEN_OP_DBL_STAR,
|
|
|
|
|
2019-07-25 03:15:54 +00:00
|
|
|
// Order of these 13 matches corresponding mp_binary_op_t operator
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_DEL_PIPE_EQUAL,
|
|
|
|
MP_TOKEN_DEL_CARET_EQUAL,
|
|
|
|
MP_TOKEN_DEL_AMPERSAND_EQUAL,
|
|
|
|
MP_TOKEN_DEL_DBL_LESS_EQUAL,
|
|
|
|
MP_TOKEN_DEL_DBL_MORE_EQUAL,
|
|
|
|
MP_TOKEN_DEL_PLUS_EQUAL,
|
|
|
|
MP_TOKEN_DEL_MINUS_EQUAL,
|
|
|
|
MP_TOKEN_DEL_STAR_EQUAL,
|
2019-07-25 03:15:54 +00:00
|
|
|
MP_TOKEN_DEL_AT_EQUAL,
|
2019-07-25 02:10:45 +00:00
|
|
|
MP_TOKEN_DEL_DBL_SLASH_EQUAL,
|
|
|
|
MP_TOKEN_DEL_SLASH_EQUAL,
|
|
|
|
MP_TOKEN_DEL_PERCENT_EQUAL,
|
|
|
|
MP_TOKEN_DEL_DBL_STAR_EQUAL,
|
|
|
|
|
2017-02-17 00:10:35 +00:00
|
|
|
MP_TOKEN_DEL_PAREN_OPEN,
|
2013-12-21 18:17:45 +00:00
|
|
|
MP_TOKEN_DEL_PAREN_CLOSE,
|
|
|
|
MP_TOKEN_DEL_BRACKET_OPEN,
|
|
|
|
MP_TOKEN_DEL_BRACKET_CLOSE,
|
|
|
|
MP_TOKEN_DEL_BRACE_OPEN,
|
|
|
|
MP_TOKEN_DEL_BRACE_CLOSE,
|
|
|
|
MP_TOKEN_DEL_COMMA,
|
|
|
|
MP_TOKEN_DEL_COLON,
|
|
|
|
MP_TOKEN_DEL_PERIOD,
|
|
|
|
MP_TOKEN_DEL_SEMICOLON,
|
|
|
|
MP_TOKEN_DEL_EQUAL,
|
|
|
|
MP_TOKEN_DEL_MINUS_MORE,
|
|
|
|
} mp_token_kind_t;
|
|
|
|
|
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
|
2016-11-16 07:27:20 +00:00
|
|
|
mp_reader_t reader; // stream source
|
2014-12-05 19:35:18 +00:00
|
|
|
|
|
|
|
unichar chr0, chr1, chr2; // current cached characters from source
|
2021-08-12 15:44:08 +00:00
|
|
|
#if MICROPY_PY_FSTRINGS
|
|
|
|
unichar chr0_saved, chr1_saved, chr2_saved; // current cached characters from alt source
|
|
|
|
#endif
|
2014-12-05 19:35:18 +00:00
|
|
|
|
2017-02-17 01:44:24 +00:00
|
|
|
size_t line; // current source line
|
|
|
|
size_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
|
|
|
|
|
2017-02-17 01:44:24 +00:00
|
|
|
size_t alloc_indent_level;
|
|
|
|
size_t num_indent_level;
|
2014-12-05 19:35:18 +00:00
|
|
|
uint16_t *indent_level;
|
|
|
|
|
2017-02-17 01:44:24 +00:00
|
|
|
size_t tok_line; // token source line
|
|
|
|
size_t tok_column; // token source column
|
2014-12-05 19:35:18 +00:00
|
|
|
mp_token_kind_t tok_kind; // token kind
|
|
|
|
vstr_t vstr; // token data
|
2021-08-12 15:44:08 +00:00
|
|
|
#if MICROPY_PY_FSTRINGS
|
|
|
|
vstr_t fstring_args; // extracted arguments to pass to .format()
|
|
|
|
size_t fstring_args_idx; // how many bytes of fstring_args have been read
|
|
|
|
#endif
|
2014-12-05 19:35:18 +00:00
|
|
|
} mp_lexer_t;
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2016-11-16 07:27:20 +00:00
|
|
|
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader);
|
2017-02-17 01:44:24 +00:00
|
|
|
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len);
|
2014-01-08 15:28:26 +00:00
|
|
|
|
2022-05-25 02:04:27 +00:00
|
|
|
// If MICROPY_READER_POSIX or MICROPY_READER_VFS aren't enabled then
|
|
|
|
// this function must be implemented by the port.
|
2023-09-27 03:43:50 +00:00
|
|
|
mp_lexer_t *mp_lexer_new_from_file(qstr filename);
|
2014-06-02 16:37:55 +00:00
|
|
|
|
2015-06-04 22:42:45 +00:00
|
|
|
#if MICROPY_HELPER_LEXER_UNIX
|
|
|
|
mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd);
|
|
|
|
#endif
|
|
|
|
|
2022-05-25 02:04:27 +00:00
|
|
|
void mp_lexer_free(mp_lexer_t *lex);
|
|
|
|
void mp_lexer_to_next(mp_lexer_t *lex);
|
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 21:14:58 +00:00
|
|
|
#endif // MICROPY_INCLUDED_PY_LEXER_H
|