ncd_tokenizer_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file ncd_tokenizer_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <misc/debug.h>
  26. #include <base/BLog.h>
  27. #include <ncd/NCDConfigTokenizer.h>
  28. int error;
  29. static int tokenizer_output (void *user, int token, char *value, size_t line, size_t line_char)
  30. {
  31. if (token == NCD_ERROR) {
  32. printf("line %zu, character %zu: tokenizer error\n", line, line_char);
  33. error = 1;
  34. return 0;
  35. }
  36. switch (token) {
  37. case NCD_EOF:
  38. printf("eof\n");
  39. break;
  40. case NCD_TOKEN_CURLY_OPEN:
  41. printf("curly_open\n");
  42. break;
  43. case NCD_TOKEN_CURLY_CLOSE:
  44. printf("curly_close\n");
  45. break;
  46. case NCD_TOKEN_ROUND_OPEN:
  47. printf("round_open\n");
  48. break;
  49. case NCD_TOKEN_ROUND_CLOSE:
  50. printf("round_close\n");
  51. break;
  52. case NCD_TOKEN_SEMICOLON:
  53. printf("semicolon\n");
  54. break;
  55. case NCD_TOKEN_DOT:
  56. printf("dot\n");
  57. break;
  58. case NCD_TOKEN_COMMA:
  59. printf("comma\n");
  60. break;
  61. case NCD_TOKEN_PROCESS:
  62. printf("process\n");
  63. break;
  64. case NCD_TOKEN_NAME:
  65. printf("name %s\n", value);
  66. free(value);
  67. break;
  68. case NCD_TOKEN_STRING:
  69. printf("string %s\n", value);
  70. free(value);
  71. break;
  72. default:
  73. ASSERT(0);
  74. }
  75. return 1;
  76. }
  77. int main (int argc, char **argv)
  78. {
  79. if (argc < 1) {
  80. return 1;
  81. }
  82. if (argc != 2) {
  83. printf("Usage: %s <string>\n", argv[0]);
  84. return 1;
  85. }
  86. BLog_InitStdout();
  87. error = 0;
  88. NCDConfigTokenizer_Tokenize(argv[1], strlen(argv[1]), tokenizer_output, NULL);
  89. if (error) {
  90. return 1;
  91. }
  92. return 0;
  93. }