hilite.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* hlite.c, generic syntax highlighting, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2024 */
  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. if(bp->b_keywords != NULL &&
  18. bp->b_keywords->mlc != NULL && bp->b_keywords->emlc != NULL)
  19. for (po =0; po < pt; po++)
  20. parse_text(bp, po);
  21. }
  22. int parse_text(buffer_t *bp, point_t pt)
  23. {
  24. // if(bp->b_keywords == NULL)
  25. // return state;
  26. if (skip_count-- > 0)
  27. return state;
  28. char_t c_now = get_at(bp, pt);
  29. char_t next = c_now;
  30. int valid = TRUE;
  31. state = next_state;
  32. if (state == ID_DEFAULT &&
  33. bp->b_keywords != NULL &&
  34. bp->b_keywords->mlc != NULL) {
  35. next = c_now;
  36. for(int i = 0; bp->b_keywords->mlc[i] != '\0'; i++) {
  37. next = get_at(bp, pt + i);
  38. if(next != bp->b_keywords->mlc[i]) {
  39. valid = FALSE;
  40. break;
  41. }
  42. }
  43. if(valid) {
  44. skip_count = 1;
  45. return (next_state = state = ID_BLOCK_COMMENT);
  46. }
  47. valid = TRUE;
  48. }
  49. if (state == ID_BLOCK_COMMENT &&
  50. bp->b_keywords != NULL &&
  51. bp->b_keywords->emlc != NULL) {
  52. next = c_now;
  53. for(int i = 0; bp->b_keywords->emlc[i] != '\0'; i++) {
  54. next = get_at(bp, pt + i);
  55. if(next != bp->b_keywords->emlc[i]) {
  56. valid = FALSE;
  57. break;
  58. }
  59. }
  60. if(valid) {
  61. skip_count = strlen(bp->b_keywords->emlc) - 1;
  62. next_state = ID_DEFAULT;
  63. return ID_BLOCK_COMMENT;
  64. }
  65. valid = TRUE;
  66. }
  67. if (state == ID_DEFAULT &&
  68. bp->b_keywords != NULL &&
  69. bp->b_keywords->slc[0] != '\0') {
  70. next = c_now;
  71. for(int i = 0; bp->b_keywords->slc[i] != '\0'; i++) {
  72. next = get_at(bp, pt + i);
  73. if(next != bp->b_keywords->slc[i]) {
  74. valid = FALSE;
  75. break;
  76. }
  77. }
  78. if(valid) {
  79. skip_count = 1;
  80. return (next_state = state = ID_LINE_COMMENT);
  81. }
  82. valid = TRUE;
  83. }
  84. if (state == ID_LINE_COMMENT && c_now == '\n')
  85. return (next_state = ID_DEFAULT);
  86. if (state == ID_DEFAULT && c_now == '"') {
  87. int enable = FALSE;
  88. char_t z = get_at(bp, pt+1);
  89. point_t end = pos(bp, bp->b_ebuf);
  90. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  91. if(z == '"') {
  92. enable = TRUE;
  93. break;
  94. }
  95. if(z == '\\' && get_at(bp, i+1) == '\n') {
  96. enable = TRUE;
  97. break;
  98. }
  99. }
  100. if(enable)
  101. return (next_state = ID_DOUBLE_STRING);
  102. }
  103. if (state == ID_DEFAULT &&
  104. bp->b_keywords != NULL &&
  105. bp->b_keywords->bqas &&
  106. c_now == '`')
  107. return (next_state = ID_BACK_STRING);
  108. if (state == ID_DEFAULT &&
  109. bp->b_keywords != NULL &&
  110. bp->b_keywords->sqas &&
  111. c_now == '\'') {
  112. int enable = FALSE;
  113. char_t z = get_at(bp, pt+1);
  114. point_t end = pos(bp, bp->b_ebuf);
  115. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  116. if(z == '\'') {
  117. enable = TRUE;
  118. break;
  119. }
  120. }
  121. if(enable)
  122. return (next_state = ID_SINGLE_STRING);
  123. }
  124. if (state == ID_DOUBLE_STRING && c_now == '\\') {
  125. skip_count = 1;
  126. return (next_state = ID_DOUBLE_STRING);
  127. }
  128. if (state == ID_DOUBLE_STRING && c_now == '"') {
  129. next_state = ID_DEFAULT;
  130. return ID_DOUBLE_STRING;
  131. }
  132. if (state == ID_SINGLE_STRING && c_now == '\\') {
  133. skip_count = 1;
  134. return (next_state = ID_SINGLE_STRING);
  135. }
  136. if (state == ID_DEFAULT &&
  137. bp->b_keywords != NULL &&
  138. bp->b_keywords->bqas &&
  139. c_now == '`')
  140. return (next_state = ID_BACK_STRING);
  141. if (state == ID_BACK_STRING && c_now == '\\') {
  142. skip_count = 1;
  143. return (next_state = ID_BACK_STRING);
  144. }
  145. if (state == ID_SINGLE_STRING && c_now == '\'') {
  146. next_state = ID_DEFAULT;
  147. return ID_SINGLE_STRING;
  148. }
  149. if (state == ID_BACK_STRING && c_now == '`') {
  150. next_state = ID_DEFAULT;
  151. return ID_BACK_STRING;
  152. }
  153. if (state != ID_DEFAULT)
  154. return (next_state = state);
  155. // if (state == ID_DEFAULT && c_now >= '0' && c_now <= '9') {
  156. // next_state = ID_DEFAULT;
  157. // return (state = ID_DIGITS);
  158. // }
  159. // if (state == ID_DEFAULT && 1 == is_symbol(c_now)) {
  160. // next_state = ID_DEFAULT;
  161. // return (state = ID_SYMBOL);
  162. // }
  163. return (next_state = state);
  164. }