bim-syntax.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef _BIM_SYNTAX_H
  2. #define _BIM_SYNTAX_H
  3. #define BIM_SYNTAX(name, spaces) \
  4. __attribute__((constructor)) static void _load_ ## name (void) { \
  5. add_syntax((struct syntax_definition){#name, syn_ ## name ## _ext, syn_ ## name ## _calculate, spaces, NULL, NULL}); \
  6. } \
  7. #define BIM_SYNTAX_EXT(name, spaces, matcher) \
  8. __attribute__((constructor)) static void _load_ ## name (void) { \
  9. add_syntax((struct syntax_definition){#name, syn_ ## name ## _ext, syn_ ## name ## _calculate, spaces, matcher, _match_completions_ ## name}); \
  10. } \
  11. #define BIM_SYNTAX_COMPLETER(name) \
  12. static int _match_completions_ ## name ( \
  13. uint32_t * comp __attribute__((unused)), \
  14. struct completion_match **matches __attribute__((unused)), \
  15. int * matches_count __attribute__((unused)), \
  16. int complete_match __attribute__((unused)), \
  17. int *matches_len __attribute__((unused)))
  18. #define paint(length, flag) do { for (int i = 0; i < (length) && state->i < state->line->actual; i++, state->i++) { state->line->text[state->i].flags = (flag); } } while (0)
  19. #define charat() (state->i < state->line->actual ? state->line->text[(state->i)].codepoint : -1)
  20. #define nextchar() (state->i + 1 < state->line->actual ? state->line->text[(state->i+1)].codepoint : -1)
  21. #define lastchar() (state->i - 1 >= 0 ? state->line->text[(state->i-1)].codepoint : -1)
  22. #define skip() (state->i++)
  23. #define charrel(x) (state->i + (x) < state->line->actual ? state->line->text[(state->i+(x))].codepoint : -1)
  24. extern int find_keywords(struct syntax_state * state, char ** keywords, int flag, int (*keyword_qualifier)(int c));
  25. extern int match_and_paint(struct syntax_state * state, const char * keyword, int flag, int (*keyword_qualifier)(int c));
  26. extern void paint_single_string(struct syntax_state * state);
  27. extern void paint_simple_string(struct syntax_state * state);
  28. extern int common_comment_buzzwords(struct syntax_state * state);
  29. extern int paint_comment(struct syntax_state * state);
  30. extern int match_forward(struct syntax_state * state, char * c);
  31. extern struct syntax_definition * find_syntax_calculator(const char * name);
  32. #define nest(lang, low) \
  33. do { \
  34. state->state = (state->state < 1 ? 0 : state->state - low); \
  35. do { state->state = lang(state); } while (state->state == 0); \
  36. if (state->state == -1) return low; \
  37. return state->state + low; \
  38. } while (0)
  39. /* Some of the C stuff is widely used */
  40. extern int c_keyword_qualifier(int c);
  41. extern int paint_c_numeral(struct syntax_state * state);
  42. extern int paint_c_comment(struct syntax_state * state);
  43. extern void paint_c_char(struct syntax_state * state);
  44. /* Hacky workaround for isdigit not really accepting Unicode stuff */
  45. static __attribute__((used)) int _isdigit(int c) { if (c > 128) return 0; return isdigit(c); }
  46. static __attribute__((used)) int _isxdigit(int c) { if (c > 128) return 0; return isxdigit(c); }
  47. #undef isdigit
  48. #undef isxdigit
  49. #define isdigit(c) _isdigit(c)
  50. #define isxdigit(c) _isxdigit(c)
  51. #endif /* _BIM_SYNTAX_H */