123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- /* hlite.c, generic syntax highlighting, Ait Emacs, Kevin Bloom, BSD 3-Clause, 2023-2024 */
- #include "header.h"
- #include "util.h"
- int state = ID_DEFAULT;
- int next_state = ID_DEFAULT;
- int skip_count = 0;
- int exclude_state = ID_DEFAULT;
- int exclude_count = 0;
- char_t get_at(buffer_t *bp, point_t pt)
- {
- return (*ptr(bp, pt));
- }
- void set_parse_state(buffer_t *bp, point_t pt, window_t *wp, int loop)
- {
- register point_t po;
- state = ID_DEFAULT;
- next_state = ID_DEFAULT;
- skip_count = 0;
- if(bp->b_keywords != NULL &&
- bp->b_keywords->mlc != NULL && bp->b_keywords->emlc != NULL &&
- loop) {
- for (po =0; po < pt; po++)
- parse_text(bp, po);
- wp->w_hilite = state;
- }
- }
- void write_parse_state(window_t *wp)
- {
- state = wp->w_hilite;
- next_state = wp->w_hilite;
- skip_count = 0;
- }
- int parse_text(buffer_t *bp, point_t pt)
- {
- // if(bp->b_keywords == NULL)
- // return state;
- if (skip_count-- > 0) {
- if(exclude_count != 0)
- exclude_count--;
- if(exclude_state != ID_DEFAULT &&
- exclude_count == 0) {
- state = exclude_state;
- exclude_state = ID_DEFAULT;
- }
- return state;
- }
- char_t c_now = get_at(bp, pt);
- char_t c_prev = get_at(bp, pt-1);
- char_t next = c_now;
- int valid = TRUE, k = 0;
- state = next_state;
- if (state == ID_DEFAULT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->mlc != NULL) {
- next = c_now;
- for(int i = 0; bp->b_keywords->mlc[i] != '\0'; i++) {
- next = get_at(bp, pt + i);
- if(next != bp->b_keywords->mlc[i]) {
- valid = FALSE;
- break;
- }
- }
- if(valid) {
- skip_count = 1;
- return (next_state = state = ID_BLOCK_COMMENT);
- }
- valid = TRUE;
- }
- if (state == ID_BLOCK_COMMENT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->emlc != NULL) {
- next = c_now;
- for(int i = 0; bp->b_keywords->emlc[i] != '\0'; i++) {
- next = get_at(bp, pt + i);
- if(next != bp->b_keywords->emlc[i]) {
- valid = FALSE;
- break;
- }
- }
- if(valid) {
- skip_count = strlen(bp->b_keywords->emlc) - 1;
- next_state = ID_DEFAULT;
- return ID_BLOCK_COMMENT;
- }
- valid = TRUE;
- }
- if (state == ID_DEFAULT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->slc[0] != '\0') {
- next = c_now;
- for(int i = 0; bp->b_keywords->slc[i] != '\0'; i++) {
- next = get_at(bp, pt + i);
- if(next != bp->b_keywords->slc[i]) {
- valid = FALSE;
- break;
- }
- }
- if(valid) {
- skip_count = 1;
- return (next_state = state = ID_LINE_COMMENT);
- }
- valid = TRUE;
- }
- if (state == ID_LINE_COMMENT && c_now == '\n')
- return (next_state = ID_DEFAULT);
- if (state == ID_DEFAULT && c_now == '"') {
- int enable = FALSE;
- char_t z = get_at(bp, pt+1);
- point_t end = pos(bp, bp->b_ebuf);
- for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
- if(z == '"') {
- enable = TRUE;
- break;
- }
- if(z == '\\' && get_at(bp, i+1) == '\n') {
- enable = TRUE;
- break;
- }
- }
- if(enable)
- return (next_state = ID_DOUBLE_STRING);
- }
- if (state == ID_DEFAULT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->bqas &&
- c_now == '`')
- return (next_state = ID_BACK_STRING);
- if (state == ID_DEFAULT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->sqas &&
- c_now == '\'') {
- int enable = FALSE;
- char_t z = get_at(bp, pt+1);
- point_t end = pos(bp, bp->b_ebuf);
- for(point_t i = pt+1; z != '\n' && i <= end; i++, z = get_at(bp, i)) {
- if(z == '\'') {
- enable = TRUE;
- break;
- }
- }
- if(enable)
- return (next_state = ID_SINGLE_STRING);
- }
- if (state == ID_DOUBLE_STRING && c_now == '\\') {
- skip_count = 1;
- return (next_state = ID_DOUBLE_STRING);
- }
- if (state == ID_DOUBLE_STRING && c_now == '"') {
- next_state = ID_DEFAULT;
- return ID_DOUBLE_STRING;
- }
- if (state == ID_SINGLE_STRING && c_now == '\\') {
- skip_count = 1;
- return (next_state = ID_SINGLE_STRING);
- }
- if (state == ID_DEFAULT &&
- bp->b_keywords != NULL &&
- bp->b_keywords->bqas &&
- c_now == '`')
- return (next_state = ID_BACK_STRING);
- if (state == ID_BACK_STRING && c_now == '\\') {
- skip_count = 1;
- return (next_state = ID_BACK_STRING);
- }
- if (state == ID_SINGLE_STRING && c_now == '\'') {
- next_state = ID_DEFAULT;
- return ID_SINGLE_STRING;
- }
- if (state == ID_BACK_STRING && c_now == '`') {
- next_state = ID_DEFAULT;
- return ID_BACK_STRING;
- }
- if(bp->b_keywords != NULL &&
- bp->b_keywords->keywords != NULL &&
- state == ID_DEFAULT &&
- (pt == 0 ||
- (is_symbol(c_prev) &&
- (c_prev != '-' && c_prev != '_'))
- || isspace(c_prev))) {
- for(int i = 0; bp->b_keywords->keywords[i].word != NULL; i++) {
- int l = 0, t = 0;
- exclude_count = 0;
- exclude_state = ID_DEFAULT;
- for(k = 0; bp->b_keywords->keywords[i].word[l] != '\0'; k++, l++) {
- c_now = get_at(bp, pt+k);
- /* at the end */
- if(bp->b_keywords->keywords[i].word[l] == '') {
- if(bp->b_keywords->keywords[i].word[l+1] == '\0') {
- for(; !isspace(c_now); k++) {
- c_now = get_at(bp, pt+k);
- }
- k--;
- break;
- } else {
- l++;
- for(; !isspace(c_now) && (bp->b_keywords->keywords[i].word[l] < 32 ||
- !is_symboli(c_now, bp->b_keywords->keywords[i].word[l])); k++) {
- if(bp->b_keywords->keywords[i].word[l] == c_now) {
- t = 1;
- break;
- }
- if(bp->b_keywords->keywords[i].word[l] == '' &&
- bp->b_keywords->keywords[i].word[l+1] == c_now) {
- t = 2;
- break;
- }
- if(pt+k == pos(bp, bp->b_ebuf))
- break;
- c_now = get_at(bp, pt+k);
- }
- if(t == 0) {
- k = 0;
- break;
- }
- if(t == 1) {
- k--;
- continue;
- }
- if(t == 2) {
- l++;
- k--;
- break;
- }
- }
- }
- if(bp->b_keywords->keywords[i].word[l] == '') {
- if(bp->b_keywords->keywords[i].word[l+1] == c_now
- || bp->b_keywords->keywords[i].word[l+1] == '\0') {
- exclude_state = bp->b_keywords->keywords[i].color;
- exclude_count++;
- k--;
- continue;
- } else {
- k = 0;
- exclude_count = 0;
- break;
- }
- }
- if(bp->b_keywords->keywords[i].word[l] != c_now) {
- k = 0;
- break;
- }
- }
- c_now = get_at(bp, pt+k);
- if(k > 0 && (isspace(c_now) ||
- (is_symbol(c_now) && (c_now != '-' && (
- bp->b_keywords->keywords[i].word[l] == '_' || c_now != '_')))) &&
- (bp->b_keywords->keywords[i].word[l] == '\0' ||
- bp->b_keywords->keywords[i].word[l+1] == '\0')) {
- skip_count = k-1;
- next_state = ID_DEFAULT;
- if(exclude_state != ID_DEFAULT) {
- return (state = ID_DEFAULT);
- }
- return (state = bp->b_keywords->keywords[i].color);
- }
- }
- }
- if (state != ID_DEFAULT)
- return (next_state = state);
- // if (state == ID_DEFAULT && c_now >= '0' && c_now <= '9') {
- // next_state = ID_DEFAULT;
- // return (state = ID_DIGITS);
- // }
- // if (state == ID_DEFAULT && 1 == is_symbol(c_now)) {
- // next_state = ID_DEFAULT;
- // return (state = ID_SYMBOL);
- // }
- return (next_state = state);
- }
|