keyword.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <lang/hl_java.h>
  19. static int hl_cmp(char *a, const char *b, int len) {
  20. return (strncmp(a, b, len) == 0);
  21. }
  22. static int hl_isspecial(int c) {
  23. switch (c) {
  24. case ',':
  25. case ';':
  26. case '[':
  27. case ']':
  28. case '(':
  29. case ')':
  30. case '-':
  31. case '.':
  32. case '\0':
  33. case ' ':
  34. return 1;
  35. default:
  36. break;
  37. }
  38. return 0;
  39. }
  40. int hl_keyword_is_number(char *str, int len) {
  41. for (int i = 0; i < len; i++) {
  42. if (hl_isspecial(str[i]))
  43. continue;
  44. if (str[i] < '0' || str[i] > '9') {
  45. return 0;
  46. }
  47. }
  48. return 1;
  49. }
  50. int hl_keyword_expr(char *word, int len) {
  51. for (int i = 0; i < HL_C_EXPR_SIZ; i++) {
  52. if (hl_cmp(word, HL_C_EXPR[i], len))
  53. return 1;
  54. }
  55. for (int i = 0; i < HL_SH_EXPR_SIZ; i++) {
  56. if (hl_cmp(word, HL_SH_EXPR[i], len))
  57. return 1;
  58. }
  59. for (int i = 0; i < HL_JAVA_EXPR_SIZ; i++) {
  60. if (hl_cmp(word, HL_JAVA_EXPR[i], len))
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. int hl_keyword_type(char *word, int len) {
  66. for (int i = 0; i < HL_C_TYPE_SIZ; i++) {
  67. if (hl_cmp(word, HL_C_TYPE[i], len))
  68. return 1;
  69. }
  70. for (int i = 0; i < HL_SH_TYPE_SIZ; i++) {
  71. if (hl_cmp(word, HL_SH_TYPE[i], len))
  72. return 1;
  73. }
  74. for (int i = 0; i < HL_JAVA_TYPE_SIZ; i++) {
  75. if (hl_cmp(word, HL_JAVA_TYPE[i], len))
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. int hl_keyword_decl(char *word, int len) {
  81. for (int i = 0; i < HL_C_DECL_SIZ; i++) {
  82. if (hl_cmp(word, HL_C_DECL[i], len))
  83. return 1;
  84. }
  85. for (int i = 0; i < HL_SH_DECL_SIZ; i++) {
  86. if (hl_cmp(word, HL_SH_DECL[i], len))
  87. return 1;
  88. }
  89. for (int i = 0; i < HL_JAVA_DECL_SIZ; i++) {
  90. if (hl_cmp(word, HL_JAVA_DECL[i], len))
  91. return 1;
  92. }
  93. return 0;
  94. }