NCDValueParser.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file NCDValueParser.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 <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <base/BLog.h>
  33. #include <ncd/NCDConfigTokenizer.h>
  34. #include <ncd/NCDValCompat.h>
  35. #include "NCDValueParser.h"
  36. #include <generated/blog_channel_NCDValueParser.h>
  37. struct token {
  38. char *str;
  39. size_t len;
  40. };
  41. struct value {
  42. int have;
  43. NCDValue v;
  44. };
  45. struct parser_state {
  46. int have_value;
  47. NCDValue value;
  48. int out_of_memory;
  49. int syntax_error;
  50. int error;
  51. void *parser;
  52. };
  53. static void free_token (struct token o)
  54. {
  55. free(o.str);
  56. };
  57. static void free_value (struct value o)
  58. {
  59. if (o.have) {
  60. NCDValue_Free(&o.v);
  61. }
  62. }
  63. // rename non-static functions defined by our Lemon parser
  64. // to avoid clashes with other Lemon parsers
  65. #define ParseTrace ParseTrace_NCDValueParser
  66. #define ParseAlloc ParseAlloc_NCDValueParser
  67. #define ParseFree ParseFree_NCDValueParser
  68. #define Parse Parse_NCDValueParser
  69. #include "../generated/NCDValueParser_parse.c"
  70. #include "../generated/NCDValueParser_parse.h"
  71. static int tokenizer_output (void *user, int token, char *value, size_t value_len, size_t line, size_t line_char)
  72. {
  73. struct parser_state *state = user;
  74. ASSERT(!state->out_of_memory)
  75. ASSERT(!state->syntax_error)
  76. ASSERT(!state->error)
  77. if (token == NCD_ERROR) {
  78. BLog(BLOG_ERROR, "line %zu, character %zu: tokenizer error", line, line_char);
  79. goto fail;
  80. }
  81. struct token minor;
  82. minor.str = value;
  83. minor.len = value_len;
  84. switch (token) {
  85. case NCD_EOF: {
  86. Parse(state->parser, 0, minor, state);
  87. } break;
  88. case NCD_TOKEN_CURLY_OPEN: {
  89. Parse(state->parser, CURLY_OPEN, minor, state);
  90. } break;
  91. case NCD_TOKEN_CURLY_CLOSE: {
  92. Parse(state->parser, CURLY_CLOSE, minor, state);
  93. } break;
  94. case NCD_TOKEN_COMMA: {
  95. Parse(state->parser, COMMA, minor, state);
  96. } break;
  97. case NCD_TOKEN_STRING: {
  98. Parse(state->parser, STRING, minor, state);
  99. } break;
  100. case NCD_TOKEN_COLON: {
  101. Parse(state->parser, COLON, minor, state);
  102. } break;
  103. case NCD_TOKEN_BRACKET_OPEN: {
  104. Parse(state->parser, BRACKET_OPEN, minor, state);
  105. } break;
  106. case NCD_TOKEN_BRACKET_CLOSE: {
  107. Parse(state->parser, BRACKET_CLOSE, minor, state);
  108. } break;
  109. default:
  110. BLog(BLOG_ERROR, "line %zu, character %zu: invalid token", line, line_char);
  111. free_token(minor);
  112. goto fail;
  113. }
  114. if (state->syntax_error) {
  115. BLog(BLOG_ERROR, "line %zu, character %zu: syntax error", line, line_char);
  116. goto fail;
  117. }
  118. if (state->out_of_memory) {
  119. BLog(BLOG_ERROR, "line %zu, character %zu: out of memory", line, line_char);
  120. goto fail;
  121. }
  122. return 1;
  123. fail:
  124. state->error = 1;
  125. return 0;
  126. }
  127. int NCDValueParser_Parse (const char *str, size_t str_len, NCDValue *out_value)
  128. {
  129. ASSERT(str_len == 0 || str)
  130. ASSERT(out_value)
  131. struct parser_state state;
  132. state.have_value = 0;
  133. state.out_of_memory = 0;
  134. state.syntax_error = 0;
  135. state.error = 0;
  136. if (!(state.parser = ParseAlloc(malloc))) {
  137. BLog(BLOG_ERROR, "ParseAlloc failed");
  138. return 0;
  139. }
  140. NCDConfigTokenizer_Tokenize((char *)str, str_len, tokenizer_output, &state);
  141. ParseFree(state.parser, free);
  142. if (state.error) {
  143. if (state.have_value) {
  144. NCDValue_Free(&state.value);
  145. }
  146. return 0;
  147. }
  148. ASSERT(state.have_value)
  149. *out_value = state.value;
  150. return 1;
  151. }
  152. int NCDValParser_Parse (const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out_value)
  153. {
  154. ASSERT(str_len == 0 || str)
  155. ASSERT(mem)
  156. ASSERT(out_value)
  157. NCDValue value;
  158. if (!NCDValueParser_Parse(str, str_len, &value)) {
  159. return 0;
  160. }
  161. if (!NCDValCompat_ValueToVal(&value, mem, out_value)) {
  162. BLog(BLOG_ERROR, "NCDValCompat_ValueToVal failed");
  163. NCDValue_Free(&value);
  164. return 0;
  165. }
  166. NCDValue_Free(&value);
  167. return 1;
  168. }