ncd_parser_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @file ncd_tokenizer_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/debug.h>
  34. #include <base/BLog.h>
  35. #include <ncd/NCDConfigParser.h>
  36. #include <ncd/NCDValueGenerator.h>
  37. #include <ncd/NCDSugar.h>
  38. int error;
  39. static void print_indent (unsigned int indent)
  40. {
  41. while (indent > 0) {
  42. printf(" ");
  43. indent--;
  44. }
  45. }
  46. static void print_value (NCDValue *v, unsigned int indent)
  47. {
  48. char *str = NCDValueGenerator_Generate(v);
  49. if (!str) {
  50. DEBUG("NCDValueGenerator_Generate failed");
  51. exit(1);
  52. }
  53. print_indent(indent);
  54. printf("%s\n", str);
  55. free(str);
  56. }
  57. static void print_block (NCDBlock *block, unsigned int indent)
  58. {
  59. for (NCDStatement *st = NCDBlock_FirstStatement(block); st; st = NCDBlock_NextStatement(block, st)) {
  60. const char *name = NCDStatement_Name(st) ? NCDStatement_Name(st) : "";
  61. switch (NCDStatement_Type(st)) {
  62. case NCDSTATEMENT_REG: {
  63. const char *objname = NCDStatement_RegObjName(st) ? NCDStatement_RegObjName(st) : "";
  64. const char *cmdname = NCDStatement_RegCmdName(st);
  65. print_indent(indent);
  66. printf("reg name=%s objname=%s cmdname=%s args:\n", name, objname, cmdname);
  67. print_value(NCDStatement_RegArgs(st), indent + 2);
  68. } break;
  69. case NCDSTATEMENT_IF: {
  70. print_indent(indent);
  71. printf("if name=%s\n", name);
  72. NCDIfBlock *ifb = NCDStatement_IfBlock(st);
  73. for (NCDIf *ifc = NCDIfBlock_FirstIf(ifb); ifc; ifc = NCDIfBlock_NextIf(ifb, ifc)) {
  74. print_indent(indent + 2);
  75. printf("if\n");
  76. print_value(NCDIf_Cond(ifc), indent + 4);
  77. print_indent(indent + 2);
  78. printf("then\n");
  79. print_block(NCDIf_Block(ifc), indent + 4);
  80. }
  81. if (NCDStatement_IfElse(st)) {
  82. print_indent(indent + 2);
  83. printf("else\n");
  84. print_block(NCDStatement_IfElse(st), indent + 4);
  85. }
  86. } break;
  87. case NCDSTATEMENT_FOREACH: {
  88. const char *name1 = NCDStatement_ForeachName1(st);
  89. const char *name2 = NCDStatement_ForeachName2(st) ? NCDStatement_ForeachName2(st) : "";
  90. print_indent(indent);
  91. printf("foreach name=%s name1=%s name2=%s\n", name, name1, name2);
  92. print_block(NCDStatement_ForeachBlock(st), indent + 2);
  93. } break;
  94. default: ASSERT(0);
  95. }
  96. }
  97. }
  98. int main (int argc, char **argv)
  99. {
  100. int res = 1;
  101. if (argc != 3) {
  102. printf("Usage: %s <desugar=0/1> <string>\n", (argc > 0 ? argv[0] : ""));
  103. goto fail0;
  104. }
  105. int desugar = atoi(argv[1]);
  106. char *text = argv[2];
  107. BLog_InitStdout();
  108. // parse
  109. NCDProgram prog;
  110. if (!NCDConfigParser_Parse(text, strlen(text), &prog)) {
  111. DEBUG("NCDConfigParser_Parse failed");
  112. goto fail1;
  113. }
  114. // desugar
  115. if (desugar) {
  116. if (!NCDSugar_Desugar(&prog)) {
  117. DEBUG("NCDSugar_Desugar failed");
  118. goto fail2;
  119. }
  120. }
  121. // print
  122. for (NCDProcess *p = NCDProgram_FirstProcess(&prog); p; p = NCDProgram_NextProcess(&prog, p)) {
  123. printf("process name=%s is_template=%d\n", NCDProcess_Name(p), NCDProcess_IsTemplate(p));
  124. print_block(NCDProcess_Block(p), 2);
  125. }
  126. res = 0;
  127. fail2:
  128. NCDProgram_Free(&prog);
  129. fail1:
  130. BLog_Free();
  131. fail0:
  132. return res;
  133. }