parse.h 4.8 KB

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