Debian Codegraph @

src/fcml_parser.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2020 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "fcml_parser.h"
21
22 #include <fcml_errors.h>
23 #include <fcml_common.h>
24 #include <fcml_symbols.h>
25
26 #include "fcml_dialect_int.h"
27 #include "fcml_apc_ast.h"
28 #include "fcml_env_int.h"
29 #include "fcml_trace.h"
30 #include "fcml_parser_int.h"
31 #include "fcml_utils.h"
32
33 fcml_ceh_error LIB_CALL fcml_fn_parse(fcml_st_parser_context *context,
34 const fcml_string instruction, fcml_st_parser_result *result_out) {
35
36 fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR;
37 fcml_st_dialect_context_int *dialect_context_int =
38 (fcml_st_dialect_context_int*) context->dialect;
39
40 if (dialect_context_int->instruction_parser) {
41 /* Call parser instance associated with given dialect. */
42 error = fcml_fn_parse_to_cif(context, instruction, result_out);
43 } else {
44 /* Dialect not initialized correctly.*/
45 FCML_TRACE_MSG("Parsing not supported by current dialect.");
46 error = FCML_CEH_GEC_FEATURE_NOT_SUPPORTED;
47 }
48 if (error) {
49 // Try to convert error code to error message if there is such need.
50 fcml_fn_utils_conv_gec_to_error_info(
51 context->configuration.enable_error_messages,
52 &(result_out->errors), error);
53 }
54 return error;
55 }
56
57 void LIB_CALL fcml_fn_parser_result_free(fcml_st_parser_result *result) {
58 if (result) {
59 /* Frees parsed instruction, potential errors and warnings and result structure itself.*/
60 if (result->instruction) {
61 fcml_fn_ast_free_converted_cif(result->instruction);
62 result->instruction = NULL;
63 }
64 /* Symbol can not be free, because it is managed by symbols
65 * table, but of course we have to zero it.
66 */
67 result->symbol = NULL;
68 fcml_fn_ceh_free_errors_only(&(result->errors));
69 }
70 }
71
72 void LIB_CALL fcml_fn_parser_result_prepare(fcml_st_parser_result *result) {
73 if (result) {
74 result->errors.errors = NULL;
75 result->errors.last_error = NULL;
76 result->instruction = NULL;
77 }
78 }