keyword.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: g0tsu
  12. * Email: g0tsu at dnmx.0rg
  13. */
  14. #include <libhighlight.h>
  15. #include <string.h>
  16. #include <lang/hl_c.h>
  17. #include <lang/hl_sh.h>
  18. static int hl_cmp(char *a, const char *b, int len) {
  19. return (strncmp(a, b, len) == 0);
  20. }
  21. static int hl_isspecial(int c) {
  22. switch (c) {
  23. case ',':
  24. case ';':
  25. case '[':
  26. case ']':
  27. case '(':
  28. case ')':
  29. case '\0':
  30. case ' ':
  31. return 1;
  32. default:
  33. break;
  34. }
  35. return 0;
  36. }
  37. int hl_keyword_expr(char *word, int len) {
  38. for (int i = 0; i < HL_C_EXPR_SIZ; i++) {
  39. if (hl_cmp(word, HL_C_EXPR[i], len))
  40. return 1;
  41. }
  42. for (int i = 0; i < HL_SH_EXPR_SIZ; i++) {
  43. if (hl_cmp(word, HL_SH_EXPR[i], len))
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. int hl_keyword_is_number(char *str, int len) {
  49. for (int i = 0; i < len; i++) {
  50. if (hl_isspecial(str[i]))
  51. continue;
  52. if (str[i] < '0' || str[i] > '9') {
  53. return 0;
  54. }
  55. }
  56. return 1;
  57. }
  58. int hl_keyword_type(char *word, int len) {
  59. for (int i = 0; i < HL_C_TYPE_SIZ; i++) {
  60. if (hl_cmp(word, HL_C_TYPE[i], len))
  61. return 1;
  62. }
  63. for (int i = 0; i < HL_SH_TYPE_SIZ; i++) {
  64. if (hl_cmp(word, HL_SH_TYPE[i], len))
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. int hl_keyword_decl(char *word, int len) {
  70. for (int i = 0; i < HL_C_DECL_SIZ; i++) {
  71. if (hl_cmp(word, HL_C_DECL[i], len))
  72. return 1;
  73. }
  74. for (int i = 0; i < HL_SH_DECL_SIZ; i++) {
  75. if (hl_cmp(word, HL_SH_DECL[i], len))
  76. return 1;
  77. }
  78. return 0;
  79. }