My Project
Loading...
Searching...
No Matches
pparg.c
Go to the documentation of this file.
1#include "gwion_util.h"
2#include "gwion_ast.h"
3
4ANN static Macro pparg_def(PPArg *ppa, const m_str str) {
5 const ssize_t sz = strlen(str);
6 char base[sz + 1];
7 m_str def = strchr(str, '(') ?: strchr(str, '=');
8 const ssize_t idx = def ? (def - str) : sz;
9 strncpy(base, str, idx);
10 base[idx] = '\0';
11 Macro m = macro_add(&ppa->hash, base);
12 if (m) {
13 m->pos = (pos_t){ 1, 1};
14 return m;
15 }
16 gw_err(MACRO_DEFINED);
17 return NULL;
18}
19
20ANN static MacroArg pparg_arg(PPArg *ppa, m_str src) {
21 const size_t sz = strlen(src);
22 char buf[sz + 1];
23 for (m_uint i = 0; i < sz; ++i) {
24 const char c = src[i];
25 if (c == ')') {
26 buf[i] = '\0';
27 return new_macroarg(ppa->hash.p, buf);
28 }
29 if (c == ',') {
30 buf[i] = '\0';
31 MacroArg arg = new_macroarg(ppa->hash.p, buf);
32 arg->next = pparg_arg(ppa, src + i + 1);
33 return arg;
34 }
35 buf[i] = c;
36 }
37 return NULL;
38}
39
40ANN static GwText *pparg_body(PPArg *ppa, const m_str str) {
41 GwText *text = new_text(ppa->hash.p);
42 text_add(text, str);
43 return text;
44}
45
46ANN2(1) bool pparg_add(PPArg *ppa, const m_str str) {
47 if (!ppa->hash.table) hini(&ppa->hash, 127);
48 DECL_B(const Macro, m, = pparg_def(ppa, str));
49 const m_str arg = strchr(str, '(');
50 if (arg) m->base = pparg_arg(ppa, arg + 1);
51 const m_str body = strchr(str, '=');
52 if (body) m->text = pparg_body(ppa, body + 1);
53 m->file = "command line";
54 return true;
55}
56
57ANN void pparg_ini(MemPool mp, PPArg *a) {
58 a->hash.p = mp;
59 vector_init(&a->path);
60}
61
62ANN void pparg_end(PPArg *a) {
63 vector_release(&a->path);
64 if (a->hash.table) {
65 macro_del(&a->hash);
66 hend(&a->hash);
67 }
68}
include this file to use gwion-ast library
struct pos_t pos_t
return NULL
Definition macro.c:41
MacroArg new_macroarg(MemPool p, const m_str name)
Definition macro.c:4
void macro_del(const Hash)
#define MACRO_DEFINED
Definition macro.h:7
Macro macro_add(const Hash, m_str)
static ANN GwText * pparg_body(PPArg *ppa, const m_str str)
Definition pparg.c:40
ANN void pparg_ini(MemPool mp, PPArg *a)
Definition pparg.c:57
static ANN Macro pparg_def(PPArg *ppa, const m_str str)
Definition pparg.c:4
ANN2(1)
Definition pparg.c:46
static ANN MacroArg pparg_arg(PPArg *ppa, m_str src)
Definition pparg.c:20
ANN void pparg_end(PPArg *a)
Definition pparg.c:62
const m_str str
Definition pparg.h:27
return macro_rem & ppa
Definition pparg.h:30
MacroArg next
Definition macro.h:14
Definition macro.h:22
struct pos_t pos
Definition macro.h:29
Definition pparg.h:7
struct Vector_ path
Definition pparg.h:8
struct Hash_ hash
Definition pparg.h:9