hilite.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* hlite.c, generic syntax highlighting, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023 */
  2. #include "header.h"
  3. #include "util.h"
  4. int state = ID_DEFAULT;
  5. int next_state = ID_DEFAULT;
  6. int skip_count = 0;
  7. char_t get_at(buffer_t *bp, point_t pt)
  8. {
  9. return (*ptr(bp, pt));
  10. }
  11. void set_parse_state(buffer_t *bp, point_t pt)
  12. {
  13. register point_t po;
  14. state = ID_DEFAULT;
  15. next_state = ID_DEFAULT;
  16. skip_count = 0;
  17. for (po =0; po < pt; po++)
  18. parse_text(bp, po);
  19. }
  20. int parse_text(buffer_t *bp, point_t pt)
  21. {
  22. // if(bp->b_keywords == NULL)
  23. // return state;
  24. if (skip_count-- > 0)
  25. return state;
  26. char_t c_now = get_at(bp, pt);
  27. char_t c_next = get_at(bp, pt + 1);
  28. state = next_state;
  29. if (state == ID_DEFAULT &&
  30. bp->b_keywords != NULL &&
  31. bp->b_keywords->mlc != NULL &&
  32. c_now == bp->b_keywords->mlc[0] &&
  33. c_next == bp->b_keywords->mlc[1]) {
  34. skip_count = 1;
  35. return (next_state = state = ID_BLOCK_COMMENT);
  36. }
  37. if (state == ID_BLOCK_COMMENT &&
  38. bp->b_keywords != NULL &&
  39. bp->b_keywords->emlc != NULL &&
  40. c_now == bp->b_keywords->emlc[0] &&
  41. c_next == bp->b_keywords->emlc[1]) {
  42. skip_count = 1;
  43. next_state = ID_DEFAULT;
  44. return ID_BLOCK_COMMENT;
  45. }
  46. if (state == ID_DEFAULT &&
  47. bp->b_keywords != NULL &&
  48. c_now == bp->b_keywords->slc[0] &&
  49. c_next == bp->b_keywords->slc[1]) {
  50. skip_count = 1;
  51. return (next_state = state = ID_LINE_COMMENT);
  52. }
  53. if (state == ID_LINE_COMMENT && c_now == '\n')
  54. return (next_state = ID_DEFAULT);
  55. if (state == ID_DEFAULT && c_now == '"') {
  56. int enable = FALSE;
  57. char_t z = get_at(bp, pt+1);
  58. point_t end = pos(bp, bp->b_ebuf);
  59. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  60. if(z == '"') {
  61. enable = TRUE;
  62. break;
  63. }
  64. if(z == '\\' && get_at(bp, i+1) == '\n') {
  65. enable = TRUE;
  66. break;
  67. }
  68. }
  69. if(enable)
  70. return (next_state = ID_DOUBLE_STRING);
  71. }
  72. if (state == ID_DEFAULT &&
  73. bp->b_keywords != NULL &&
  74. bp->b_keywords->bqas &&
  75. c_now == '`')
  76. return (next_state = ID_BACK_STRING);
  77. if (state == ID_DEFAULT &&
  78. bp->b_keywords != NULL &&
  79. bp->b_keywords->sqas &&
  80. c_now == '\'') {
  81. int enable = FALSE;
  82. char_t z = get_at(bp, pt+1);
  83. point_t end = pos(bp, bp->b_ebuf);
  84. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  85. if(z == '\'') {
  86. enable = TRUE;
  87. break;
  88. }
  89. }
  90. if(enable)
  91. return (next_state = ID_SINGLE_STRING);
  92. }
  93. if (state == ID_DOUBLE_STRING && c_now == '\\') {
  94. skip_count = 1;
  95. return (next_state = ID_DOUBLE_STRING);
  96. }
  97. if (state == ID_DOUBLE_STRING && c_now == '"') {
  98. next_state = ID_DEFAULT;
  99. return ID_DOUBLE_STRING;
  100. }
  101. if (state == ID_SINGLE_STRING && c_now == '\\') {
  102. skip_count = 1;
  103. return (next_state = ID_SINGLE_STRING);
  104. }
  105. if (state == ID_DEFAULT &&
  106. bp->b_keywords != NULL &&
  107. bp->b_keywords->bqas &&
  108. c_now == '`')
  109. return (next_state = ID_BACK_STRING);
  110. if (state == ID_BACK_STRING && c_now == '\\') {
  111. skip_count = 1;
  112. return (next_state = ID_BACK_STRING);
  113. }
  114. if (state == ID_SINGLE_STRING && c_now == '\'') {
  115. next_state = ID_DEFAULT;
  116. return ID_SINGLE_STRING;
  117. }
  118. if (state == ID_BACK_STRING && (c_now == '`' || c_now == '\'')) {
  119. next_state = ID_DEFAULT;
  120. return ID_BACK_STRING;
  121. }
  122. if (state != ID_DEFAULT)
  123. return (next_state = state);
  124. // if (state == ID_DEFAULT && c_now >= '0' && c_now <= '9') {
  125. // next_state = ID_DEFAULT;
  126. // return (state = ID_DIGITS);
  127. // }
  128. // if (state == ID_DEFAULT && 1 == is_symbol(c_now)) {
  129. // next_state = ID_DEFAULT;
  130. // return (state = ID_SYMBOL);
  131. // }
  132. return (next_state = state);
  133. }