hilite.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 c_next = get_at(bp, pt + 1);
  30. state = next_state;
  31. if (state == ID_DEFAULT &&
  32. bp->b_keywords != NULL &&
  33. bp->b_keywords->mlc != NULL &&
  34. c_now == bp->b_keywords->mlc[0] &&
  35. c_next == bp->b_keywords->mlc[1]) {
  36. skip_count = 1;
  37. return (next_state = state = ID_BLOCK_COMMENT);
  38. }
  39. if (state == ID_BLOCK_COMMENT &&
  40. bp->b_keywords != NULL &&
  41. bp->b_keywords->emlc != NULL &&
  42. c_now == bp->b_keywords->emlc[0] &&
  43. c_next == bp->b_keywords->emlc[1]) {
  44. skip_count = 1;
  45. next_state = ID_DEFAULT;
  46. return ID_BLOCK_COMMENT;
  47. }
  48. if (state == ID_DEFAULT &&
  49. bp->b_keywords != NULL &&
  50. c_now == bp->b_keywords->slc[0] &&
  51. c_next == bp->b_keywords->slc[1]) {
  52. skip_count = 1;
  53. return (next_state = state = ID_LINE_COMMENT);
  54. }
  55. if (state == ID_LINE_COMMENT && c_now == '\n')
  56. return (next_state = ID_DEFAULT);
  57. if (state == ID_DEFAULT && c_now == '"') {
  58. int enable = FALSE;
  59. char_t z = get_at(bp, pt+1);
  60. point_t end = pos(bp, bp->b_ebuf);
  61. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  62. if(z == '"') {
  63. enable = TRUE;
  64. break;
  65. }
  66. if(z == '\\' && get_at(bp, i+1) == '\n') {
  67. enable = TRUE;
  68. break;
  69. }
  70. }
  71. if(enable)
  72. return (next_state = ID_DOUBLE_STRING);
  73. }
  74. if (state == ID_DEFAULT &&
  75. bp->b_keywords != NULL &&
  76. bp->b_keywords->bqas &&
  77. c_now == '`')
  78. return (next_state = ID_BACK_STRING);
  79. if (state == ID_DEFAULT &&
  80. bp->b_keywords != NULL &&
  81. bp->b_keywords->sqas &&
  82. c_now == '\'') {
  83. int enable = FALSE;
  84. char_t z = get_at(bp, pt+1);
  85. point_t end = pos(bp, bp->b_ebuf);
  86. for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
  87. if(z == '\'') {
  88. enable = TRUE;
  89. break;
  90. }
  91. }
  92. if(enable)
  93. return (next_state = ID_SINGLE_STRING);
  94. }
  95. if (state == ID_DOUBLE_STRING && c_now == '\\') {
  96. skip_count = 1;
  97. return (next_state = ID_DOUBLE_STRING);
  98. }
  99. if (state == ID_DOUBLE_STRING && c_now == '"') {
  100. next_state = ID_DEFAULT;
  101. return ID_DOUBLE_STRING;
  102. }
  103. if (state == ID_SINGLE_STRING && c_now == '\\') {
  104. skip_count = 1;
  105. return (next_state = ID_SINGLE_STRING);
  106. }
  107. if (state == ID_DEFAULT &&
  108. bp->b_keywords != NULL &&
  109. bp->b_keywords->bqas &&
  110. c_now == '`')
  111. return (next_state = ID_BACK_STRING);
  112. if (state == ID_BACK_STRING && c_now == '\\') {
  113. skip_count = 1;
  114. return (next_state = ID_BACK_STRING);
  115. }
  116. if (state == ID_SINGLE_STRING && c_now == '\'') {
  117. next_state = ID_DEFAULT;
  118. return ID_SINGLE_STRING;
  119. }
  120. if (state == ID_BACK_STRING && c_now == '`') {
  121. next_state = ID_DEFAULT;
  122. return ID_BACK_STRING;
  123. }
  124. if (state != ID_DEFAULT)
  125. return (next_state = state);
  126. // if (state == ID_DEFAULT && c_now >= '0' && c_now <= '9') {
  127. // next_state = ID_DEFAULT;
  128. // return (state = ID_DIGITS);
  129. // }
  130. // if (state == ID_DEFAULT && 1 == is_symbol(c_now)) {
  131. // next_state = ID_DEFAULT;
  132. // return (state = ID_SYMBOL);
  133. // }
  134. return (next_state = state);
  135. }