summaryrefslogtreecommitdiff
path: root/py/parse.c
diff options
context:
space:
mode:
authorJeff Epler <jeff@adafruit.com>2021-03-15 13:50:49 -0500
committerGitHub <noreply@github.com>2021-03-15 13:50:49 -0500
commit05ed179e11599dd45a76dfb14ecf624d0ff96d68 (patch)
treea046c6d1f117814168150484ed1bf7840d3400d7 /py/parse.c
parent3cbff45f9aac5ea2855bbc888083d356a91c80fe (diff)
parentf9b4189b4c640823dabb76de8effd2a836da2eb0 (diff)
Merge pull request #4362 from microDev1/code-formatting
Add code formatting and translations check
Diffstat (limited to 'py/parse.c')
-rw-r--r--py/parse.c122
1 files changed, 67 insertions, 55 deletions
diff --git a/py/parse.c b/py/parse.c
index 28621cf89..c8a975e0d 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -58,13 +58,13 @@
#define RULE_ARG_OPT_RULE (0x3000)
// (un)comment to use rule names; for debugging
-//#define USE_RULE_NAME (1)
+// #define USE_RULE_NAME (1)
enum {
// define rules with a compile function
#define DEF_RULE(rule, comp, kind, ...) RULE_##rule,
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
RULE_const_object, // special node for a constant, generic Python object
@@ -72,7 +72,7 @@ enum {
// define rules without a compile function
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) RULE_##rule,
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
@@ -89,7 +89,7 @@ STATIC const uint8_t rule_act_table[] = {
#define DEF_RULE(rule, comp, kind, ...) kind,
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
@@ -97,7 +97,7 @@ STATIC const uint8_t rule_act_table[] = {
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) kind,
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
@@ -118,13 +118,13 @@ STATIC const uint16_t rule_arg_combined_table[] = {
#define DEF_RULE(rule, comp, kind, ...) __VA_ARGS__,
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) __VA_ARGS__,
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
@@ -144,12 +144,12 @@ STATIC const uint16_t rule_arg_combined_table[] = {
enum {
#define DEF_RULE(rule, comp, kind, ...) RULE_PADDING(rule, __VA_ARGS__)
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) RULE_PADDING(rule, __VA_ARGS__)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
@@ -167,13 +167,13 @@ enum {
STATIC const uint8_t rule_arg_offset_table[] = {
#define DEF_RULE(rule, comp, kind, ...) RULE_ARG_OFFSET(rule, __VA_ARGS__) & 0xff,
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
0, // RULE_const_object
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) RULE_ARG_OFFSET(rule, __VA_ARGS__) & 0xff,
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
@@ -190,20 +190,20 @@ static const size_t FIRST_RULE_WITH_OFFSET_ABOVE_255 =
#include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
-0;
+ 0;
#if defined(USE_RULE_NAME) && USE_RULE_NAME
// Define an array of rule names corresponding to each rule
STATIC const char *const rule_name_table[] = {
#define DEF_RULE(rule, comp, kind, ...) #rule,
#define DEF_RULE_NC(rule, kind, ...)
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
"", // RULE_const_object
#define DEF_RULE(rule, comp, kind, ...)
#define DEF_RULE_NC(rule, kind, ...) #rule,
-#include "py/grammar.h"
+ #include "py/grammar.h"
#undef DEF_RULE
#undef DEF_RULE_NC
};
@@ -261,7 +261,7 @@ STATIC void *parser_alloc(parser_t *parser, size_t num_bytes) {
if (chunk != NULL && chunk->union_.used + num_bytes > chunk->alloc) {
// not enough room at end of previously allocated chunk so try to grow
- mp_parse_chunk_t *new_data = (mp_parse_chunk_t*)m_renew_maybe(byte, chunk,
+ mp_parse_chunk_t *new_data = (mp_parse_chunk_t *)m_renew_maybe(byte, chunk,
sizeof(mp_parse_chunk_t) + chunk->alloc,
sizeof(mp_parse_chunk_t) + chunk->alloc + num_bytes, false);
if (new_data == NULL) {
@@ -284,7 +284,7 @@ STATIC void *parser_alloc(parser_t *parser, size_t num_bytes) {
if (alloc < num_bytes) {
alloc = num_bytes;
}
- chunk = (mp_parse_chunk_t*)m_new(byte, sizeof(mp_parse_chunk_t) + alloc);
+ chunk = (mp_parse_chunk_t *)m_new(byte, sizeof(mp_parse_chunk_t) + alloc);
chunk->alloc = alloc;
chunk->union_.used = 0;
parser->cur_chunk = chunk;
@@ -324,12 +324,12 @@ STATIC uint8_t pop_rule(parser_t *parser, size_t *arg_i, size_t *src_line) {
bool mp_parse_node_is_const_false(mp_parse_node_t pn) {
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE)
- || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
+ || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
}
bool mp_parse_node_is_const_true(mp_parse_node_t pn) {
return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE)
- || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
+ || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0);
}
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
@@ -337,7 +337,7 @@ bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
*o = MP_OBJ_NEW_SMALL_INT(MP_PARSE_NODE_LEAF_SMALL_INT(pn));
return true;
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, RULE_const_object)) {
- mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
+ mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
// nodes are 32-bit pointers, but need to extract 64-bit object
*o = (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
@@ -358,7 +358,7 @@ int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_nod
*nodes = pn;
return 1;
} else {
- mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
+ mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)(*pn);
if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
*nodes = pn;
return 1;
@@ -372,7 +372,7 @@ int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_nod
#if MICROPY_DEBUG_PRINTERS
void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
- printf("[% 4d] ", (int)((mp_parse_node_struct_t*)pn)->source_line);
+ printf("[% 4d] ", (int)((mp_parse_node_struct_t *)pn)->source_line);
} else {
printf(" ");
}
@@ -387,16 +387,23 @@ void mp_parse_node_print(mp_parse_node_t pn, size_t indent) {
} else if (MP_PARSE_NODE_IS_LEAF(pn)) {
uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
- case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
- case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
- case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
+ case MP_PARSE_NODE_ID:
+ printf("id(%s)\n", qstr_str(arg));
+ break;
+ case MP_PARSE_NODE_STRING:
+ printf("str(%s)\n", qstr_str(arg));
+ break;
+ case MP_PARSE_NODE_BYTES:
+ printf("bytes(%s)\n", qstr_str(arg));
+ break;
default:
assert(MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN);
- printf("tok(%u)\n", (uint)arg); break;
+ printf("tok(%u)\n", (uint)arg);
+ break;
}
} else {
// node must be a mp_parse_node_struct_t
- mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
+ mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)pn;
if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
@@ -477,7 +484,7 @@ STATIC void push_result_token(parser_t *parser, uint8_t rule_id) {
mp_parse_node_t pn;
mp_lexer_t *lex = parser->lexer;
if (lex->tok_kind == MP_TOKEN_NAME) {
- if(lex->vstr.len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) {
+ if (lex->vstr.len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) {
mp_raise_msg(&mp_type_SyntaxError, translate("Name too long"));
}
qstr id = qstr_from_strn(lex->vstr.buf, lex->vstr.len);
@@ -526,7 +533,7 @@ STATIC void push_result_token(parser_t *parser, uint8_t rule_id) {
// not interned, make a node holding a pointer to the string/bytes object
mp_obj_t o = mp_obj_new_str_copy(
lex->tok_kind == MP_TOKEN_STRING ? &mp_type_str : &mp_type_bytes,
- (const byte*)lex->vstr.buf, lex->vstr.len);
+ (const byte *)lex->vstr.buf, lex->vstr.len);
pn = make_node_const_object(parser, lex->tok_line, o);
}
} else {
@@ -639,8 +646,8 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
arg0 = mp_binary_op(op, arg0, arg1);
}
} else if (rule_id == RULE_shift_expr
- || rule_id == RULE_arith_expr
- || rule_id == RULE_term) {
+ || rule_id == RULE_arith_expr
+ || rule_id == RULE_term) {
// folding for binary ops: << >> + - * / % //
mp_parse_node_t pn = peek_result(parser, num_args - 1);
if (!mp_parse_node_get_int_maybe(pn, &arg0)) {
@@ -657,13 +664,13 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
MP_BINARY_OP_ADD,
MP_BINARY_OP_SUBTRACT,
MP_BINARY_OP_MULTIPLY,
- 255,//MP_BINARY_OP_POWER,
- 255,//MP_BINARY_OP_TRUE_DIVIDE,
+ 255,// MP_BINARY_OP_POWER,
+ 255,// MP_BINARY_OP_TRUE_DIVIDE,
MP_BINARY_OP_FLOOR_DIVIDE,
MP_BINARY_OP_MODULO,
- 255,//MP_BINARY_OP_LESS
+ 255,// MP_BINARY_OP_LESS
MP_BINARY_OP_LSHIFT,
- 255,//MP_BINARY_OP_MORE
+ 255,// MP_BINARY_OP_MORE
MP_BINARY_OP_RSHIFT,
};
mp_binary_op_t op = token_to_op[tok - MP_TOKEN_OP_PLUS];
@@ -707,14 +714,14 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
mp_parse_node_t pn1 = peek_result(parser, 0);
if (!MP_PARSE_NODE_IS_NULL(pn1)
&& !(MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_augassign)
- || MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
+ || MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_expr_stmt_assign_list))) {
// this node is of the form <x> = <y>
mp_parse_node_t pn0 = peek_result(parser, 1);
if (MP_PARSE_NODE_IS_ID(pn0)
&& MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_atom_expr_normal)
- && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pn1)->nodes[0])
- && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn1)->nodes[0]) == MP_QSTR_const
- && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pn1)->nodes[1], RULE_trailer_paren)
+ && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t *)pn1)->nodes[0])
+ && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t *)pn1)->nodes[0]) == MP_QSTR_const
+ && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t *)pn1)->nodes[1], RULE_trailer_paren)
) {
// code to assign dynamic constants: id = const(value)
@@ -722,13 +729,13 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
qstr id = MP_PARSE_NODE_LEAF_ARG(pn0);
// get the value
- mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pn1)->nodes[1])->nodes[0];
+ mp_parse_node_t pn_value = ((mp_parse_node_struct_t *)((mp_parse_node_struct_t *)pn1)->nodes[1])->nodes[0];
mp_obj_t value;
if (!mp_parse_node_get_int_maybe(pn_value, &value)) {
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
translate("constant must be an integer"));
mp_obj_exception_add_traceback(exc, parser->lexer->source_name,
- ((mp_parse_node_struct_t*)pn1)->source_line, MP_QSTR_NULL);
+ ((mp_parse_node_struct_t *)pn1)->source_line, MP_QSTR_NULL);
nlr_raise(exc);
}
@@ -762,16 +769,16 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
mp_parse_node_t pn0 = peek_result(parser, 1);
mp_parse_node_t pn1 = peek_result(parser, 0);
if (!(MP_PARSE_NODE_IS_ID(pn0)
- && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period))) {
+ && MP_PARSE_NODE_IS_STRUCT_KIND(pn1, RULE_trailer_period))) {
return false;
}
// id1.id2
// look it up in constant table, see if it can be replaced with an integer
- mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pn1;
+ mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pn1;
assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
qstr q_base = MP_PARSE_NODE_LEAF_ARG(pn0);
qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
- mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
+ mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
if (elem == NULL) {
return false;
}
@@ -883,9 +890,14 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
// work out the top-level rule to use, and push it on the stack
size_t top_level_rule;
switch (input_kind) {
- case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
- case MP_PARSE_EVAL_INPUT: top_level_rule = RULE_eval_input; break;
- default: top_level_rule = RULE_file_input;
+ case MP_PARSE_SINGLE_INPUT:
+ top_level_rule = RULE_single_input;
+ break;
+ case MP_PARSE_EVAL_INPUT:
+ top_level_rule = RULE_eval_input;
+ break;
+ default:
+ top_level_rule = RULE_file_input;
}
push_rule(&parser, lex->tok_line, top_level_rule, 0);
@@ -894,7 +906,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
bool backtrack = false;
for (;;) {
- next_rule:
+ next_rule:
if (parser.rule_stack_top == 0) {
break;
}
@@ -924,7 +936,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
backtrack = false;
}
for (; i < n; ++i) {
- //printf("--> inside for @L924\n");
+ // printf("--> inside for @L924\n");
uint16_t kind = rule_arg[i] & RULE_ARG_KIND_MASK;
if (kind == RULE_ARG_TOK) {
if (lex->tok_kind == (rule_arg[i] & RULE_ARG_ARG_MASK)) {
@@ -1067,7 +1079,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
// n=3 is: item (sep item)* [sep]
bool had_trailing_sep;
if (backtrack) {
- list_backtrack:
+ list_backtrack:
had_trailing_sep = false;
if (n == 2) {
if (i == 1) {
@@ -1169,7 +1181,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
) {
syntax_error:;
mp_obj_t exc;
- switch(lex->tok_kind) {
+ switch (lex->tok_kind) {
case MP_TOKEN_INDENT:
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
translate("unexpected indent"));
@@ -1178,8 +1190,8 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
translate("unindent does not match any outer indentation level"));
break;
-#if MICROPY_COMP_FSTRING_LITERAL
-#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
+ #if MICROPY_COMP_FSTRING_LITERAL
+ #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
case MP_TOKEN_FSTRING_BACKSLASH:
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
translate("f-string expression part cannot include a backslash"));
@@ -1204,7 +1216,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
exc = mp_obj_new_exception_msg(&mp_type_NotImplementedError,
translate("raw f-strings are not implemented"));
break;
-#else
+ #else
case MP_TOKEN_FSTRING_BACKSLASH:
case MP_TOKEN_FSTRING_COMMENT:
case MP_TOKEN_FSTRING_UNCLOSED:
@@ -1214,8 +1226,8 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
translate("malformed f-string"));
break;
-#endif
-#endif
+ #endif
+ #endif
default:
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
translate("invalid syntax"));