ncd_tokenizer_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <ncd/NCDConfigTokenizer.h>
  27. int error;
  28. static int tokenizer_output (void *user, int token, char *value, size_t pos)
  29. {
  30. if (token == NCD_ERROR) {
  31. printf("error at %zd\n", pos);
  32. error = 1;
  33. return 0;
  34. }
  35. switch (token) {
  36. case NCD_EOF:
  37. printf("eof\n");
  38. break;
  39. case NCD_TOKEN_CURLY_OPEN:
  40. printf("curly_open\n");
  41. break;
  42. case NCD_TOKEN_CURLY_CLOSE:
  43. printf("curly_close\n");
  44. break;
  45. case NCD_TOKEN_ROUND_OPEN:
  46. printf("round_open\n");
  47. break;
  48. case NCD_TOKEN_ROUND_CLOSE:
  49. printf("round_close\n");
  50. break;
  51. case NCD_TOKEN_SEMICOLON:
  52. printf("semicolon\n");
  53. break;
  54. case NCD_TOKEN_DOT:
  55. printf("dot\n");
  56. break;
  57. case NCD_TOKEN_COMMA:
  58. printf("comma\n");
  59. break;
  60. case NCD_TOKEN_PROCESS:
  61. printf("process\n");
  62. break;
  63. case NCD_TOKEN_NAME:
  64. printf("name %s\n", value);
  65. break;
  66. case NCD_TOKEN_STRING:
  67. printf("string %s\n", value);
  68. break;
  69. default:
  70. ASSERT(0);
  71. }
  72. return 1;
  73. }
  74. int main (int argc, char **argv)
  75. {
  76. if (argc < 1) {
  77. return 1;
  78. }
  79. if (argc != 2) {
  80. printf("Usage: %s <string>\n", argv[0]);
  81. return 1;
  82. }
  83. error = 0;
  84. NCDConfigTokenizer_Tokenize(argv[1], strlen(argv[1]), tokenizer_output, NULL);
  85. if (error) {
  86. return 1;
  87. }
  88. return 0;
  89. }