viml_expressions_parser.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifdef USE_KLEE
  2. # include <klee/klee.h>
  3. #else
  4. # include <string.h>
  5. #endif
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <assert.h>
  9. #include "nvim/viml/parser/expressions.h"
  10. #include "nvim/viml/parser/parser.h"
  11. #include "nvim/mbyte.h"
  12. #include "nvim/memory.c"
  13. #include "nvim/mbyte.c"
  14. #include "nvim/charset.c"
  15. #include "nvim/garray.c"
  16. #include "nvim/gettext.c"
  17. #include "nvim/viml/parser/expressions.c"
  18. #include "nvim/keycodes.c"
  19. #define INPUT_SIZE 50
  20. uint8_t avoid_optimizing_out;
  21. void simple_get_line(void *cookie, ParserLine *ret_pline)
  22. {
  23. ParserLine **plines_p = (ParserLine **)cookie;
  24. *ret_pline = **plines_p;
  25. (*plines_p)++;
  26. }
  27. int main(const int argc, const char *const *const argv,
  28. const char *const *const environ)
  29. {
  30. char input[INPUT_SIZE];
  31. uint8_t shift;
  32. unsigned flags;
  33. const bool peek = false;
  34. avoid_optimizing_out = argc;
  35. #ifndef USE_KLEE
  36. sscanf(argv[2], "%d", &flags);
  37. #endif
  38. #ifdef USE_KLEE
  39. klee_make_symbolic(input, sizeof(input), "input");
  40. klee_make_symbolic(&shift, sizeof(shift), "shift");
  41. klee_make_symbolic(&flags, sizeof(flags), "flags");
  42. klee_assume(shift < INPUT_SIZE);
  43. klee_assume(
  44. flags <= (kExprFlagsMulti|kExprFlagsDisallowEOC|kExprFlagsParseLet));
  45. #endif
  46. ParserLine plines[] = {
  47. {
  48. #ifdef USE_KLEE
  49. .data = &input[shift],
  50. .size = sizeof(input) - shift,
  51. #else
  52. .data = argv[1],
  53. .size = strlen(argv[1]),
  54. #endif
  55. .allocated = false,
  56. },
  57. {
  58. .data = NULL,
  59. .size = 0,
  60. .allocated = false,
  61. },
  62. };
  63. #ifdef USE_KLEE
  64. assert(plines[0].size <= INPUT_SIZE);
  65. assert((plines[0].data[0] != 5) | (plines[0].data[0] != argc));
  66. #endif
  67. ParserLine *cur_pline = &plines[0];
  68. ParserHighlight colors;
  69. kvi_init(colors);
  70. ParserState pstate = {
  71. .reader = {
  72. .get_line = simple_get_line,
  73. .cookie = &cur_pline,
  74. .lines = KV_INITIAL_VALUE,
  75. .conv.vc_type = CONV_NONE,
  76. },
  77. .pos = { 0, 0 },
  78. .colors = &colors,
  79. .can_continuate = false,
  80. };
  81. kvi_init(pstate.reader.lines);
  82. const ExprAST ast = viml_pexpr_parse(&pstate, (int)flags);
  83. assert(ast.root != NULL || ast.err.msg);
  84. if (flags & kExprFlagsParseLet) {
  85. assert(ast.err.msg != NULL
  86. || ast.root->type == kExprNodeAssignment
  87. || (ast.root->type == kExprNodeListLiteral
  88. && ast.root->children != NULL)
  89. || ast.root->type == kExprNodeComplexIdentifier
  90. || ast.root->type == kExprNodeCurlyBracesIdentifier
  91. || ast.root->type == kExprNodePlainIdentifier
  92. || ast.root->type == kExprNodeRegister
  93. || ast.root->type == kExprNodeEnvironment
  94. || ast.root->type == kExprNodeOption
  95. || ast.root->type == kExprNodeSubscript
  96. || ast.root->type == kExprNodeConcatOrSubscript);
  97. }
  98. // Can’t possibly have more highlight tokens then there are bytes in string.
  99. assert(kv_size(colors) <= INPUT_SIZE - shift);
  100. kvi_destroy(colors);
  101. // Not destroying pstate.reader.lines because there is no way it could exceed
  102. // its limits in the current circumstances.
  103. viml_pexpr_free_ast(ast);
  104. assert(allocated_memory == 0);
  105. }