parser.c (2162B)
1 #include "nvim/func_attr.h" 2 #include "nvim/mbyte.h" 3 #include "nvim/memory.h" 4 #include "nvim/viml/parser/parser.h" 5 6 #include "viml/parser/parser.c.generated.h" // IWYU pragma: export 7 8 void parser_simple_get_line(void *cookie, ParserLine *ret_pline) 9 FUNC_ATTR_NONNULL_ALL 10 { 11 ParserLine **plines_p = (ParserLine **)cookie; 12 *ret_pline = **plines_p; 13 (*plines_p)++; 14 } 15 16 /// Get currently parsed line, shifted to pstate->pos.col 17 /// 18 /// @param pstate Parser state to operate on. 19 /// 20 /// @return True if there is a line, false in case of EOF. 21 bool viml_parser_get_remaining_line(ParserState *const pstate, ParserLine *const ret_pline) 22 FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 23 { 24 const size_t num_lines = kv_size(pstate->reader.lines); 25 if (pstate->pos.line == num_lines) { 26 viml_preader_get_line(&pstate->reader, ret_pline); 27 } else { 28 *ret_pline = kv_last(pstate->reader.lines); 29 } 30 assert(pstate->pos.line == kv_size(pstate->reader.lines) - 1); 31 if (ret_pline->data != NULL) { 32 ret_pline->data += pstate->pos.col; 33 ret_pline->size -= pstate->pos.col; 34 } 35 return ret_pline->data != NULL; 36 } 37 38 /// Get one line from ParserInputReader 39 static void viml_preader_get_line(ParserInputReader *const preader, ParserLine *const ret_pline) 40 FUNC_ATTR_NONNULL_ALL 41 { 42 ParserLine pline; 43 preader->get_line(preader->cookie, &pline); 44 if (preader->conv.vc_type != CONV_NONE && pline.size) { 45 ParserLine cpline = { 46 .allocated = true, 47 .size = pline.size, 48 }; 49 cpline.data = string_convert(&preader->conv, (char *)pline.data, &cpline.size); 50 if (pline.allocated) { 51 xfree((void *)pline.data); 52 } 53 pline = cpline; 54 } 55 kvi_push(preader->lines, pline); 56 *ret_pline = pline; 57 } 58 59 /// Free all memory allocated by the parser on heap 60 /// 61 /// @param pstate Parser state to free. 62 void viml_parser_destroy(ParserState *const pstate) 63 FUNC_ATTR_NONNULL_ALL 64 { 65 for (size_t i = 0; i < kv_size(pstate->reader.lines); i++) { 66 ParserLine pline = kv_A(pstate->reader.lines, i); 67 if (pline.allocated) { 68 xfree((void *)pline.data); 69 } 70 } 71 kvi_destroy(pstate->reader.lines); 72 kvi_destroy(pstate->stack); 73 }