ncd_parser_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @file ncd_parser_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. #include <ncd/NCDValCompat.h>
  39. int error;
  40. static void print_indent (unsigned int indent)
  41. {
  42. while (indent > 0) {
  43. printf(" ");
  44. indent--;
  45. }
  46. }
  47. static void print_value (NCDValue *v, unsigned int indent)
  48. {
  49. NCDValMem mem;
  50. NCDValMem_Init(&mem);
  51. NCDValRef vref;
  52. if (!NCDValCompat_ValueToVal(v, &mem, &vref)) {
  53. DEBUG("NCDValCompat_ValueToVal failed");
  54. exit(1);
  55. }
  56. char *str = NCDValGenerator_Generate(vref);
  57. if (!str) {
  58. DEBUG("NCDValGenerator_Generate failed");
  59. exit(1);
  60. }
  61. print_indent(indent);
  62. printf("%s\n", str);
  63. free(str);
  64. NCDValMem_Free(&mem);
  65. }
  66. static void print_block (NCDBlock *block, unsigned int indent)
  67. {
  68. for (NCDStatement *st = NCDBlock_FirstStatement(block); st; st = NCDBlock_NextStatement(block, st)) {
  69. const char *name = NCDStatement_Name(st) ? NCDStatement_Name(st) : "";
  70. switch (NCDStatement_Type(st)) {
  71. case NCDSTATEMENT_REG: {
  72. const char *objname = NCDStatement_RegObjName(st) ? NCDStatement_RegObjName(st) : "";
  73. const char *cmdname = NCDStatement_RegCmdName(st);
  74. print_indent(indent);
  75. printf("reg name=%s objname=%s cmdname=%s args:\n", name, objname, cmdname);
  76. print_value(NCDStatement_RegArgs(st), indent + 2);
  77. } break;
  78. case NCDSTATEMENT_IF: {
  79. print_indent(indent);
  80. printf("if name=%s\n", name);
  81. NCDIfBlock *ifb = NCDStatement_IfBlock(st);
  82. for (NCDIf *ifc = NCDIfBlock_FirstIf(ifb); ifc; ifc = NCDIfBlock_NextIf(ifb, ifc)) {
  83. print_indent(indent + 2);
  84. printf("if\n");
  85. print_value(NCDIf_Cond(ifc), indent + 4);
  86. print_indent(indent + 2);
  87. printf("then\n");
  88. print_block(NCDIf_Block(ifc), indent + 4);
  89. }
  90. if (NCDStatement_IfElse(st)) {
  91. print_indent(indent + 2);
  92. printf("else\n");
  93. print_block(NCDStatement_IfElse(st), indent + 4);
  94. }
  95. } break;
  96. case NCDSTATEMENT_FOREACH: {
  97. const char *name1 = NCDStatement_ForeachName1(st);
  98. const char *name2 = NCDStatement_ForeachName2(st) ? NCDStatement_ForeachName2(st) : "";
  99. print_indent(indent);
  100. printf("foreach name=%s name1=%s name2=%s\n", name, name1, name2);
  101. print_block(NCDStatement_ForeachBlock(st), indent + 2);
  102. } break;
  103. default: ASSERT(0);
  104. }
  105. }
  106. }
  107. int main (int argc, char **argv)
  108. {
  109. int res = 1;
  110. if (argc != 3) {
  111. printf("Usage: %s <desugar=0/1> <string>\n", (argc > 0 ? argv[0] : ""));
  112. goto fail0;
  113. }
  114. int desugar = atoi(argv[1]);
  115. char *text = argv[2];
  116. BLog_InitStdout();
  117. // parse
  118. NCDProgram prog;
  119. if (!NCDConfigParser_Parse(text, strlen(text), &prog)) {
  120. DEBUG("NCDConfigParser_Parse failed");
  121. goto fail1;
  122. }
  123. // desugar
  124. if (desugar) {
  125. if (!NCDSugar_Desugar(&prog)) {
  126. DEBUG("NCDSugar_Desugar failed");
  127. goto fail2;
  128. }
  129. }
  130. // print
  131. for (NCDProcess *p = NCDProgram_FirstProcess(&prog); p; p = NCDProgram_NextProcess(&prog, p)) {
  132. printf("process name=%s is_template=%d\n", NCDProcess_Name(p), NCDProcess_IsTemplate(p));
  133. print_block(NCDProcess_Block(p), 2);
  134. }
  135. res = 0;
  136. fail2:
  137. NCDProgram_Free(&prog);
  138. fail1:
  139. BLog_Free();
  140. fail0:
  141. return res;
  142. }