isl_printer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #include <string.h>
  2. #include <isl_int.h>
  3. #include <isl/id.h>
  4. #include <isl/id_to_id.h>
  5. #include <isl_printer_private.h>
  6. static __isl_give isl_printer *file_start_line(__isl_take isl_printer *p)
  7. {
  8. fprintf(p->file, "%s%*s%s", p->indent_prefix ? p->indent_prefix : "",
  9. p->indent, "", p->prefix ? p->prefix : "");
  10. return p;
  11. }
  12. static __isl_give isl_printer *file_end_line(__isl_take isl_printer *p)
  13. {
  14. fprintf(p->file, "%s\n", p->suffix ? p->suffix : "");
  15. return p;
  16. }
  17. static __isl_give isl_printer *file_flush(__isl_take isl_printer *p)
  18. {
  19. fflush(p->file);
  20. return p;
  21. }
  22. static __isl_give isl_printer *file_print_str(__isl_take isl_printer *p,
  23. const char *s)
  24. {
  25. fprintf(p->file, "%s", s);
  26. return p;
  27. }
  28. static __isl_give isl_printer *file_print_double(__isl_take isl_printer *p,
  29. double d)
  30. {
  31. fprintf(p->file, "%g", d);
  32. return p;
  33. }
  34. static __isl_give isl_printer *file_print_int(__isl_take isl_printer *p, int i)
  35. {
  36. fprintf(p->file, "%d", i);
  37. return p;
  38. }
  39. static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
  40. {
  41. isl_int_print(p->file, i, p->width);
  42. return p;
  43. }
  44. static int grow_buf(__isl_keep isl_printer *p, int extra)
  45. {
  46. int new_size;
  47. char *new_buf;
  48. if (p->buf_size == 0)
  49. return -1;
  50. new_size = ((p->buf_n + extra + 1) * 3) / 2;
  51. new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
  52. if (!new_buf) {
  53. p->buf_size = 0;
  54. return -1;
  55. }
  56. p->buf = new_buf;
  57. p->buf_size = new_size;
  58. return 0;
  59. }
  60. static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
  61. const char *s, int len)
  62. {
  63. if (p->buf_n + len + 1 >= p->buf_size && grow_buf(p, len))
  64. goto error;
  65. memcpy(p->buf + p->buf_n, s, len);
  66. p->buf_n += len;
  67. p->buf[p->buf_n] = '\0';
  68. return p;
  69. error:
  70. isl_printer_free(p);
  71. return NULL;
  72. }
  73. static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
  74. int indent)
  75. {
  76. int i;
  77. if (p->buf_n + indent + 1 >= p->buf_size && grow_buf(p, indent))
  78. goto error;
  79. for (i = 0; i < indent; ++i)
  80. p->buf[p->buf_n++] = ' ';
  81. p->buf[p->buf_n] = '\0';
  82. return p;
  83. error:
  84. isl_printer_free(p);
  85. return NULL;
  86. }
  87. static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
  88. {
  89. if (p->indent_prefix)
  90. p = str_print(p, p->indent_prefix, strlen(p->indent_prefix));
  91. p = str_print_indent(p, p->indent);
  92. if (p->prefix)
  93. p = str_print(p, p->prefix, strlen(p->prefix));
  94. return p;
  95. }
  96. static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
  97. {
  98. if (p->suffix)
  99. p = str_print(p, p->suffix, strlen(p->suffix));
  100. p = str_print(p, "\n", strlen("\n"));
  101. return p;
  102. }
  103. static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
  104. {
  105. p->buf_n = 0;
  106. p->buf[p->buf_n] = '\0';
  107. return p;
  108. }
  109. static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
  110. const char *s)
  111. {
  112. return str_print(p, s, strlen(s));
  113. }
  114. static __isl_give isl_printer *str_print_double(__isl_take isl_printer *p,
  115. double d)
  116. {
  117. int left = p->buf_size - p->buf_n;
  118. int need = snprintf(p->buf + p->buf_n, left, "%g", d);
  119. if (need >= left) {
  120. if (grow_buf(p, need))
  121. goto error;
  122. left = p->buf_size - p->buf_n;
  123. need = snprintf(p->buf + p->buf_n, left, "%g", d);
  124. }
  125. p->buf_n += need;
  126. return p;
  127. error:
  128. isl_printer_free(p);
  129. return NULL;
  130. }
  131. static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
  132. {
  133. int left = p->buf_size - p->buf_n;
  134. int need = snprintf(p->buf + p->buf_n, left, "%d", i);
  135. if (need >= left) {
  136. if (grow_buf(p, need))
  137. goto error;
  138. left = p->buf_size - p->buf_n;
  139. need = snprintf(p->buf + p->buf_n, left, "%d", i);
  140. }
  141. p->buf_n += need;
  142. return p;
  143. error:
  144. isl_printer_free(p);
  145. return NULL;
  146. }
  147. static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
  148. isl_int i)
  149. {
  150. char *s;
  151. int len;
  152. s = isl_int_get_str(i);
  153. len = strlen(s);
  154. if (len < p->width)
  155. p = str_print_indent(p, p->width - len);
  156. p = str_print(p, s, len);
  157. isl_int_free_str(s);
  158. return p;
  159. }
  160. struct isl_printer_ops {
  161. __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
  162. __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
  163. __isl_give isl_printer *(*print_double)(__isl_take isl_printer *p,
  164. double d);
  165. __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
  166. __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
  167. isl_int i);
  168. __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
  169. const char *s);
  170. __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
  171. };
  172. static struct isl_printer_ops file_ops = {
  173. file_start_line,
  174. file_end_line,
  175. file_print_double,
  176. file_print_int,
  177. file_print_isl_int,
  178. file_print_str,
  179. file_flush
  180. };
  181. static struct isl_printer_ops str_ops = {
  182. str_start_line,
  183. str_end_line,
  184. str_print_double,
  185. str_print_int,
  186. str_print_isl_int,
  187. str_print_str,
  188. str_flush
  189. };
  190. __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
  191. {
  192. struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
  193. if (!p)
  194. return NULL;
  195. p->ctx = ctx;
  196. isl_ctx_ref(p->ctx);
  197. p->ops = &file_ops;
  198. p->file = file;
  199. p->buf = NULL;
  200. p->buf_n = 0;
  201. p->buf_size = 0;
  202. p->indent = 0;
  203. p->output_format = ISL_FORMAT_ISL;
  204. p->indent_prefix = NULL;
  205. p->prefix = NULL;
  206. p->suffix = NULL;
  207. p->width = 0;
  208. p->yaml_style = ISL_YAML_STYLE_FLOW;
  209. return p;
  210. }
  211. __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
  212. {
  213. struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
  214. if (!p)
  215. return NULL;
  216. p->ctx = ctx;
  217. isl_ctx_ref(p->ctx);
  218. p->ops = &str_ops;
  219. p->file = NULL;
  220. p->buf = isl_alloc_array(ctx, char, 256);
  221. if (!p->buf)
  222. goto error;
  223. p->buf_n = 0;
  224. p->buf[0] = '\0';
  225. p->buf_size = 256;
  226. p->indent = 0;
  227. p->output_format = ISL_FORMAT_ISL;
  228. p->indent_prefix = NULL;
  229. p->prefix = NULL;
  230. p->suffix = NULL;
  231. p->width = 0;
  232. p->yaml_style = ISL_YAML_STYLE_FLOW;
  233. return p;
  234. error:
  235. isl_printer_free(p);
  236. return NULL;
  237. }
  238. __isl_null isl_printer *isl_printer_free(__isl_take isl_printer *p)
  239. {
  240. if (!p)
  241. return NULL;
  242. free(p->buf);
  243. free(p->indent_prefix);
  244. free(p->prefix);
  245. free(p->suffix);
  246. free(p->yaml_state);
  247. isl_id_to_id_free(p->notes);
  248. isl_ctx_deref(p->ctx);
  249. free(p);
  250. return NULL;
  251. }
  252. isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
  253. {
  254. return printer ? printer->ctx : NULL;
  255. }
  256. FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
  257. {
  258. if (!printer)
  259. return NULL;
  260. if (!printer->file)
  261. isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
  262. "not a file printer", return NULL);
  263. return printer->file;
  264. }
  265. __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
  266. int width)
  267. {
  268. if (!p)
  269. return NULL;
  270. p->width = width;
  271. return p;
  272. }
  273. __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
  274. int indent)
  275. {
  276. if (!p)
  277. return NULL;
  278. p->indent = indent;
  279. return p;
  280. }
  281. __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
  282. int indent)
  283. {
  284. if (!p)
  285. return NULL;
  286. p->indent += indent;
  287. if (p->indent < 0)
  288. p->indent = 0;
  289. return p;
  290. }
  291. /* Replace the indent prefix of "p" by "prefix".
  292. */
  293. __isl_give isl_printer *isl_printer_set_indent_prefix(__isl_take isl_printer *p,
  294. const char *prefix)
  295. {
  296. if (!p)
  297. return NULL;
  298. free(p->indent_prefix);
  299. p->indent_prefix = prefix ? strdup(prefix) : NULL;
  300. return p;
  301. }
  302. __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
  303. const char *prefix)
  304. {
  305. if (!p)
  306. return NULL;
  307. free(p->prefix);
  308. p->prefix = prefix ? strdup(prefix) : NULL;
  309. return p;
  310. }
  311. __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
  312. const char *suffix)
  313. {
  314. if (!p)
  315. return NULL;
  316. free(p->suffix);
  317. p->suffix = suffix ? strdup(suffix) : NULL;
  318. return p;
  319. }
  320. __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
  321. int output_format)
  322. {
  323. if (!p)
  324. return NULL;
  325. p->output_format = output_format;
  326. return p;
  327. }
  328. int isl_printer_get_output_format(__isl_keep isl_printer *p)
  329. {
  330. if (!p)
  331. return -1;
  332. return p->output_format;
  333. }
  334. /* Does "p" have a note with identifier "id"?
  335. */
  336. isl_bool isl_printer_has_note(__isl_keep isl_printer *p,
  337. __isl_keep isl_id *id)
  338. {
  339. if (!p || !id)
  340. return isl_bool_error;
  341. if (!p->notes)
  342. return isl_bool_false;
  343. return isl_id_to_id_has(p->notes, id);
  344. }
  345. /* Retrieve the note identified by "id" from "p".
  346. * The note is assumed to exist.
  347. */
  348. __isl_give isl_id *isl_printer_get_note(__isl_keep isl_printer *p,
  349. __isl_take isl_id *id)
  350. {
  351. isl_bool has_note;
  352. has_note = isl_printer_has_note(p, id);
  353. if (has_note < 0)
  354. goto error;
  355. if (!has_note)
  356. isl_die(isl_printer_get_ctx(p), isl_error_invalid,
  357. "no such note", goto error);
  358. return isl_id_to_id_get(p->notes, id);
  359. error:
  360. isl_id_free(id);
  361. return NULL;
  362. }
  363. /* Associate "note" to the identifier "id" in "p",
  364. * replacing the previous note associated to the identifier, if any.
  365. */
  366. __isl_give isl_printer *isl_printer_set_note(__isl_take isl_printer *p,
  367. __isl_take isl_id *id, __isl_take isl_id *note)
  368. {
  369. if (!p || !id || !note)
  370. goto error;
  371. if (!p->notes) {
  372. p->notes = isl_id_to_id_alloc(isl_printer_get_ctx(p), 1);
  373. if (!p->notes)
  374. goto error;
  375. }
  376. p->notes = isl_id_to_id_set(p->notes, id, note);
  377. if (!p->notes)
  378. return isl_printer_free(p);
  379. return p;
  380. error:
  381. isl_printer_free(p);
  382. isl_id_free(id);
  383. isl_id_free(note);
  384. return NULL;
  385. }
  386. /* Keep track of whether the printing to "p" is being performed from
  387. * an isl_*_dump function as specified by "dump".
  388. */
  389. __isl_give isl_printer *isl_printer_set_dump(__isl_take isl_printer *p,
  390. int dump)
  391. {
  392. if (!p)
  393. return NULL;
  394. p->dump = dump;
  395. return p;
  396. }
  397. /* Set the YAML style of "p" to "yaml_style" and return the updated printer.
  398. */
  399. __isl_give isl_printer *isl_printer_set_yaml_style(__isl_take isl_printer *p,
  400. int yaml_style)
  401. {
  402. if (!p)
  403. return NULL;
  404. p->yaml_style = yaml_style;
  405. return p;
  406. }
  407. /* Return the YAML style of "p" or -1 on error.
  408. */
  409. int isl_printer_get_yaml_style(__isl_keep isl_printer *p)
  410. {
  411. if (!p)
  412. return -1;
  413. return p->yaml_style;
  414. }
  415. /* Push "state" onto the stack of currently active YAML elements and
  416. * return the updated printer.
  417. */
  418. static __isl_give isl_printer *push_state(__isl_take isl_printer *p,
  419. enum isl_yaml_state state)
  420. {
  421. if (!p)
  422. return NULL;
  423. if (p->yaml_size < p->yaml_depth + 1) {
  424. enum isl_yaml_state *state;
  425. state = isl_realloc_array(p->ctx, p->yaml_state,
  426. enum isl_yaml_state, p->yaml_depth + 1);
  427. if (!state)
  428. return isl_printer_free(p);
  429. p->yaml_state = state;
  430. p->yaml_size = p->yaml_depth + 1;
  431. }
  432. p->yaml_state[p->yaml_depth] = state;
  433. p->yaml_depth++;
  434. return p;
  435. }
  436. /* Remove the innermost active YAML element from the stack and
  437. * return the updated printer.
  438. */
  439. static __isl_give isl_printer *pop_state(__isl_take isl_printer *p)
  440. {
  441. if (!p)
  442. return NULL;
  443. p->yaml_depth--;
  444. return p;
  445. }
  446. /* Set the state of the innermost active YAML element to "state" and
  447. * return the updated printer.
  448. */
  449. static __isl_give isl_printer *update_state(__isl_take isl_printer *p,
  450. enum isl_yaml_state state)
  451. {
  452. if (!p)
  453. return NULL;
  454. if (p->yaml_depth < 1)
  455. isl_die(isl_printer_get_ctx(p), isl_error_invalid,
  456. "not in YAML construct", return isl_printer_free(p));
  457. p->yaml_state[p->yaml_depth - 1] = state;
  458. return p;
  459. }
  460. /* Return the state of the innermost active YAML element.
  461. * Return isl_yaml_none if we are not inside any YAML element.
  462. */
  463. static enum isl_yaml_state current_state(__isl_keep isl_printer *p)
  464. {
  465. if (!p)
  466. return isl_yaml_none;
  467. if (p->yaml_depth < 1)
  468. return isl_yaml_none;
  469. return p->yaml_state[p->yaml_depth - 1];
  470. }
  471. /* If we are printing a YAML document and we are at the start of an element,
  472. * print whatever is needed before we can print the actual element and
  473. * keep track of the fact that we are now printing the element.
  474. * If "eol" is set, then whatever we print is going to be the last
  475. * thing that gets printed on this line.
  476. *
  477. * If we are about the print the first key of a mapping, then nothing
  478. * extra needs to be printed. For any other key, however, we need
  479. * to either move to the next line (in block format) or print a comma
  480. * (in flow format).
  481. * Before printing a value in a mapping, we need to print a colon.
  482. *
  483. * For sequences, in flow format, we only need to print a comma
  484. * for each element except the first.
  485. * In block format, before the first element in the sequence,
  486. * we move to a new line, print a dash and increase the indentation.
  487. * Before any other element, we print a dash on a new line,
  488. * temporarily moving the indentation back.
  489. */
  490. static __isl_give isl_printer *enter_state(__isl_take isl_printer *p,
  491. int eol)
  492. {
  493. enum isl_yaml_state state;
  494. if (!p)
  495. return NULL;
  496. state = current_state(p);
  497. if (state == isl_yaml_mapping_val_start) {
  498. if (eol)
  499. p = p->ops->print_str(p, ":");
  500. else
  501. p = p->ops->print_str(p, ": ");
  502. p = update_state(p, isl_yaml_mapping_val);
  503. } else if (state == isl_yaml_mapping_first_key_start) {
  504. p = update_state(p, isl_yaml_mapping_key);
  505. } else if (state == isl_yaml_mapping_key_start) {
  506. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  507. p = p->ops->print_str(p, ", ");
  508. else {
  509. p = p->ops->end_line(p);
  510. p = p->ops->start_line(p);
  511. }
  512. p = update_state(p, isl_yaml_mapping_key);
  513. } else if (state == isl_yaml_sequence_first_start) {
  514. if (p->yaml_style != ISL_YAML_STYLE_FLOW) {
  515. p = p->ops->end_line(p);
  516. p = p->ops->start_line(p);
  517. p = p->ops->print_str(p, "- ");
  518. p = isl_printer_indent(p, 2);
  519. }
  520. p = update_state(p, isl_yaml_sequence);
  521. } else if (state == isl_yaml_sequence_start) {
  522. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  523. p = p->ops->print_str(p, ", ");
  524. else {
  525. p = p->ops->end_line(p);
  526. p = isl_printer_indent(p, -2);
  527. p = p->ops->start_line(p);
  528. p = p->ops->print_str(p, "- ");
  529. p = isl_printer_indent(p, 2);
  530. }
  531. p = update_state(p, isl_yaml_sequence);
  532. }
  533. return p;
  534. }
  535. __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
  536. const char *s)
  537. {
  538. if (!p)
  539. return NULL;
  540. if (!s)
  541. return isl_printer_free(p);
  542. p = enter_state(p, 0);
  543. if (!p)
  544. return NULL;
  545. return p->ops->print_str(p, s);
  546. }
  547. __isl_give isl_printer *isl_printer_print_double(__isl_take isl_printer *p,
  548. double d)
  549. {
  550. p = enter_state(p, 0);
  551. if (!p)
  552. return NULL;
  553. return p->ops->print_double(p, d);
  554. }
  555. __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
  556. {
  557. p = enter_state(p, 0);
  558. if (!p)
  559. return NULL;
  560. return p->ops->print_int(p, i);
  561. }
  562. __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
  563. isl_int i)
  564. {
  565. p = enter_state(p, 0);
  566. if (!p)
  567. return NULL;
  568. return p->ops->print_isl_int(p, i);
  569. }
  570. __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
  571. {
  572. if (!p)
  573. return NULL;
  574. return p->ops->start_line(p);
  575. }
  576. __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
  577. {
  578. if (!p)
  579. return NULL;
  580. return p->ops->end_line(p);
  581. }
  582. /* Return a copy of the string constructed by the string printer "printer".
  583. */
  584. __isl_give char *isl_printer_get_str(__isl_keep isl_printer *printer)
  585. {
  586. if (!printer)
  587. return NULL;
  588. if (printer->ops != &str_ops)
  589. isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
  590. "isl_printer_get_str can only be called on a string "
  591. "printer", return NULL);
  592. if (!printer->buf)
  593. return NULL;
  594. return strdup(printer->buf);
  595. }
  596. __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
  597. {
  598. if (!p)
  599. return NULL;
  600. return p->ops->flush(p);
  601. }
  602. /* Start a YAML mapping and push a new state to reflect that we
  603. * are about to print the first key in a mapping.
  604. *
  605. * In flow style, print the opening brace.
  606. * In block style, move to the next line with an increased indentation,
  607. * except if this is the outer mapping or if we are inside a sequence
  608. * (in which case we have already increased the indentation and we want
  609. * to print the first key on the same line as the dash).
  610. */
  611. __isl_give isl_printer *isl_printer_yaml_start_mapping(
  612. __isl_take isl_printer *p)
  613. {
  614. enum isl_yaml_state state;
  615. if (!p)
  616. return NULL;
  617. p = enter_state(p, p->yaml_style == ISL_YAML_STYLE_BLOCK);
  618. if (!p)
  619. return NULL;
  620. state = current_state(p);
  621. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  622. p = p->ops->print_str(p, "{ ");
  623. else if (state != isl_yaml_none && state != isl_yaml_sequence) {
  624. p = p->ops->end_line(p);
  625. p = isl_printer_indent(p, 2);
  626. p = p->ops->start_line(p);
  627. }
  628. p = push_state(p, isl_yaml_mapping_first_key_start);
  629. return p;
  630. }
  631. /* Finish a YAML mapping and pop it from the state stack.
  632. *
  633. * In flow style, print the closing brace.
  634. *
  635. * In block style, first check if we are still in the
  636. * isl_yaml_mapping_first_key_start state. If so, we have not printed
  637. * anything yet, so print "{}" to indicate an empty mapping.
  638. * If we increased the indentation in isl_printer_yaml_start_mapping,
  639. * then decrease it again.
  640. * If this is the outer mapping then print a newline.
  641. */
  642. __isl_give isl_printer *isl_printer_yaml_end_mapping(
  643. __isl_take isl_printer *p)
  644. {
  645. enum isl_yaml_state state;
  646. state = current_state(p);
  647. p = pop_state(p);
  648. if (!p)
  649. return NULL;
  650. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  651. return p->ops->print_str(p, " }");
  652. if (state == isl_yaml_mapping_first_key_start)
  653. p = p->ops->print_str(p, "{}");
  654. if (!p)
  655. return NULL;
  656. state = current_state(p);
  657. if (state != isl_yaml_none && state != isl_yaml_sequence)
  658. p = isl_printer_indent(p, -2);
  659. if (state == isl_yaml_none)
  660. p = p->ops->end_line(p);
  661. return p;
  662. }
  663. /* Start a YAML sequence and push a new state to reflect that we
  664. * are about to print the first element in a sequence.
  665. *
  666. * In flow style, print the opening bracket.
  667. */
  668. __isl_give isl_printer *isl_printer_yaml_start_sequence(
  669. __isl_take isl_printer *p)
  670. {
  671. if (!p)
  672. return NULL;
  673. p = enter_state(p, p->yaml_style == ISL_YAML_STYLE_BLOCK);
  674. p = push_state(p, isl_yaml_sequence_first_start);
  675. if (!p)
  676. return NULL;
  677. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  678. p = p->ops->print_str(p, "[ ");
  679. return p;
  680. }
  681. /* Finish a YAML sequence and pop it from the state stack.
  682. *
  683. * In flow style, print the closing bracket.
  684. *
  685. * In block style, check if we are still in the
  686. * isl_yaml_sequence_first_start state. If so, we have not printed
  687. * anything yet, so print "[]" or " []" to indicate an empty sequence.
  688. * We print the extra space when we instructed enter_state not
  689. * to print a space at the end of the line.
  690. * Otherwise, undo the increase in indentation performed by
  691. * enter_state when moving away from the isl_yaml_sequence_first_start
  692. * state.
  693. * If this is the outer sequence then print a newline.
  694. */
  695. __isl_give isl_printer *isl_printer_yaml_end_sequence(
  696. __isl_take isl_printer *p)
  697. {
  698. enum isl_yaml_state state, up;
  699. state = current_state(p);
  700. p = pop_state(p);
  701. if (!p)
  702. return NULL;
  703. if (p->yaml_style == ISL_YAML_STYLE_FLOW)
  704. return p->ops->print_str(p, " ]");
  705. up = current_state(p);
  706. if (state == isl_yaml_sequence_first_start) {
  707. if (up == isl_yaml_mapping_val)
  708. p = p->ops->print_str(p, " []");
  709. else
  710. p = p->ops->print_str(p, "[]");
  711. } else {
  712. p = isl_printer_indent(p, -2);
  713. }
  714. if (!p)
  715. return NULL;
  716. state = current_state(p);
  717. if (state == isl_yaml_none)
  718. p = p->ops->end_line(p);
  719. return p;
  720. }
  721. /* Mark the fact that the current element is finished and that
  722. * the next output belongs to the next element.
  723. * In particular, if we are printing a key, then prepare for
  724. * printing the subsequent value. If we are printing a value,
  725. * prepare for printing the next key. If we are printing an
  726. * element in a sequence, prepare for printing the next element.
  727. */
  728. __isl_give isl_printer *isl_printer_yaml_next(__isl_take isl_printer *p)
  729. {
  730. enum isl_yaml_state state;
  731. if (!p)
  732. return NULL;
  733. if (p->yaml_depth < 1)
  734. isl_die(isl_printer_get_ctx(p), isl_error_invalid,
  735. "not in YAML construct", return isl_printer_free(p));
  736. state = current_state(p);
  737. if (state == isl_yaml_mapping_key)
  738. state = isl_yaml_mapping_val_start;
  739. else if (state == isl_yaml_mapping_val)
  740. state = isl_yaml_mapping_key_start;
  741. else if (state == isl_yaml_sequence)
  742. state = isl_yaml_sequence_start;
  743. p = update_state(p, state);
  744. return p;
  745. }