xref: /Universal-ctags/dsl/qualifier.c (revision d742dcb0e13d8d69a234a11405ffa12adc217b8f)
1 /*
2 *   Copyright (c) 2016, Masatake YAMATO
3 *   Copyright (c) 2016, Red Hat, Inc.
4 *
5 *   This source code is released for free distribution under the terms of the
6 *   GNU General Public License version 2 or (at your option) any later version.
7 */
8 
9 /*
10  * INCLUDES
11  */
12 #include "qualifier.h"
13 #include "dsl.h"
14 #include "es.h"
15 
16 #include <stdlib.h>
17 
18 /*
19  * TYPES
20  */
21 struct sQCode
22 {
23 	DSLCode *dsl;
24 };
25 
26 
27 /*
28  * DATA DEFINITIONS
29  */
30 
31 
32 /*
33  * FUNCTION DEFINITIONS
34  */
initialize(void)35 static int initialize (void)
36 {
37 	static int initialized;
38 
39 	if (initialized)
40 		return 1;
41 
42 	if (!dsl_init (DSL_QUALIFIER, NULL, 0))
43 	{
44 		fprintf(stderr, "MEMORY EXHAUSTED\n");
45 		return 0;
46 	}
47 
48 	initialized = 1;
49 	return 1;
50 }
51 
q_compile(EsObject * exp)52 QCode  *q_compile (EsObject *exp)
53 {
54 	QCode *code;
55 
56 	if (!initialize ())
57 		exit (1);
58 
59 	code = malloc (sizeof (QCode));
60 	if (code == NULL)
61 	{
62 		fprintf(stderr, "MEMORY EXHAUSTED\n");
63 		return NULL;
64 	}
65 
66 	code->dsl = dsl_compile (DSL_QUALIFIER, exp);
67 	if (code->dsl == NULL)
68 	{
69 		fprintf(stderr, "MEMORY EXHAUSTED or SYNTAX ERROR\n");
70 		free (code);
71 		return NULL;
72 	}
73 	return code;
74 }
75 
q_is_acceptable(QCode * code,tagEntry * entry)76 enum QRESULT q_is_acceptable  (QCode *code, tagEntry *entry)
77 {
78 	EsObject *r;
79 	int i;
80 
81 	DSLEnv env = {
82 		.engine = DSL_QUALIFIER,
83 		.entry  = entry,
84 	};
85 	es_autounref_pool_push ();
86 	r = dsl_eval (code->dsl, &env);
87 	if (es_object_equal (r, es_false))
88 		i = Q_REJECT;
89 	else if (es_error_p (r))
90 	{
91 		dsl_report_error ("GOT ERROR in QUALIFYING", r);
92 		i = Q_ERROR;
93 	}
94 	else
95 		i = Q_ACCEPT;
96 	es_autounref_pool_pop ();
97 
98 	dsl_cache_reset (DSL_QUALIFIER);
99 
100 	return i;
101 }
102 
q_destroy(QCode * code)103 void q_destroy (QCode *code)
104 {
105 	dsl_release (DSL_QUALIFIER, code->dsl);
106 	free (code);
107 }
108 
q_help(FILE * fp)109 void q_help (FILE *fp)
110 {
111 	if (!initialize ())
112 		exit (1);
113 	dsl_help (DSL_QUALIFIER, fp);
114 }
115