123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /* 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;
- char_t get_at(buffer_t *bp, point_t pt)
- {
- return (*ptr(bp, pt));
- }
- void set_parse_state(buffer_t *bp, point_t pt)
- {
- 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)
- for (po =0; po < pt; po++)
- parse_text(bp, po);
- }
- int parse_text(buffer_t *bp, point_t pt)
- {
- // if(bp->b_keywords == NULL)
- // return state;
- if (skip_count-- > 0)
- return state;
- char_t c_now = get_at(bp, pt);
- char_t next = c_now;
- int valid = TRUE;
- 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 (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);
- }
|