LCOV - code coverage report
Current view: top level - py - parse.h (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 6 6 100.0 %
Date: 2024-04-25 17:00:48 Functions: 0 0 -
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       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_PARSE_H
      27                 :            : #define MICROPY_INCLUDED_PY_PARSE_H
      28                 :            : 
      29                 :            : #include <stddef.h>
      30                 :            : #include <stdint.h>
      31                 :            : 
      32                 :            : #include "py/obj.h"
      33                 :            : 
      34                 :            : struct _mp_lexer_t;
      35                 :            : 
      36                 :            : // a mp_parse_node_t is:
      37                 :            : //  - 0000...0000: no node
      38                 :            : //  - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement
      39                 :            : //  - xxxx...xx00: pointer to mp_parse_node_struct_t
      40                 :            : //  - xx...xx0010: an identifier; bits 4 and above are the qstr
      41                 :            : //  - xx...xx0110: a string; bits 4 and above are the qstr holding the value
      42                 :            : //  - xx...xx1010: a token; bits 4 and above are mp_token_kind_t
      43                 :            : 
      44                 :            : #define MP_PARSE_NODE_NULL      (0)
      45                 :            : #define MP_PARSE_NODE_SMALL_INT (0x1)
      46                 :            : #define MP_PARSE_NODE_ID        (0x02)
      47                 :            : #define MP_PARSE_NODE_STRING    (0x06)
      48                 :            : #define MP_PARSE_NODE_TOKEN     (0x0a)
      49                 :            : 
      50                 :            : typedef uintptr_t mp_parse_node_t; // must be pointer size
      51                 :            : 
      52                 :            : typedef struct _mp_parse_node_struct_t {
      53                 :            :     uint32_t source_line;       // line number in source file
      54                 :            :     uint32_t kind_num_nodes;    // parse node kind, and number of nodes
      55                 :            :     mp_parse_node_t nodes[];    // nodes
      56                 :            : } mp_parse_node_struct_t;
      57                 :            : 
      58                 :            : // macros for mp_parse_node_t usage
      59                 :            : // some of these evaluate their argument more than once
      60                 :            : 
      61                 :            : #define MP_PARSE_NODE_IS_NULL(pn) ((pn) == MP_PARSE_NODE_NULL)
      62                 :            : #define MP_PARSE_NODE_IS_LEAF(pn) ((pn) & 3)
      63                 :            : #define MP_PARSE_NODE_IS_STRUCT(pn) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0)
      64                 :            : #define MP_PARSE_NODE_IS_STRUCT_KIND(pn, k) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0 && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)(pn)) == (k))
      65                 :            : 
      66                 :            : #define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
      67                 :            : #define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x0f) == MP_PARSE_NODE_ID)
      68                 :            : #define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x0f) == MP_PARSE_NODE_TOKEN)
      69                 :            : #define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 4)))
      70                 :            : 
      71                 :            : #define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x0f)
      72                 :            : #define MP_PARSE_NODE_LEAF_ARG(pn) (((uintptr_t)(pn)) >> 4)
      73                 :            : #define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(intptr_t)(pn)) >> 1)
      74                 :            : #define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
      75                 :            : #define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
      76                 :            : 
      77                 :      28545 : static inline mp_parse_node_t mp_parse_node_new_small_int(mp_int_t val) {
      78                 :      28545 :     return (mp_parse_node_t)(MP_PARSE_NODE_SMALL_INT | ((mp_uint_t)val << 1));
      79                 :            : }
      80                 :            : 
      81                 :     150369 : static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg) {
      82                 :     150369 :     return (mp_parse_node_t)(kind | ((mp_uint_t)arg << 4));
      83                 :            : }
      84                 :            : 
      85                 :      32247 : static inline mp_obj_t mp_parse_node_extract_const_object(mp_parse_node_struct_t *pns) {
      86                 :            :     #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
      87                 :            :     // nodes are 32-bit pointers, but need to extract 64-bit object
      88                 :            :     return (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32);
      89                 :            :     #else
      90         [ +  - ]:      32247 :     return (mp_obj_t)pns->nodes[0];
      91                 :            :     #endif
      92                 :            : }
      93                 :            : 
      94                 :            : bool mp_parse_node_is_const_false(mp_parse_node_t pn);
      95                 :            : bool mp_parse_node_is_const_true(mp_parse_node_t pn);
      96                 :            : bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
      97                 :            : size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
      98                 :            : void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t indent);
      99                 :            : 
     100                 :            : typedef enum {
     101                 :            :     MP_PARSE_SINGLE_INPUT,
     102                 :            :     MP_PARSE_FILE_INPUT,
     103                 :            :     MP_PARSE_EVAL_INPUT,
     104                 :            : } mp_parse_input_kind_t;
     105                 :            : 
     106                 :            : typedef struct _mp_parse_t {
     107                 :            :     mp_parse_node_t root;
     108                 :            :     struct _mp_parse_chunk_t *chunk;
     109                 :            : } mp_parse_tree_t;
     110                 :            : 
     111                 :            : // the parser will raise an exception if an error occurred
     112                 :            : // the parser will free the lexer before it returns
     113                 :            : mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);
     114                 :            : void mp_parse_tree_clear(mp_parse_tree_t *tree);
     115                 :            : 
     116                 :            : #endif // MICROPY_INCLUDED_PY_PARSE_H

Generated by: LCOV version 1.15-5-g462f71d