all repos — openbox @ b1fe0dbbc2ef4472ac8893d9b3b20e9cb149af6d

openbox fork - make it a bit more like ryudo

obcl/parse.y (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
%{
#include "obcl.h"

int yylex(void);
void yyerror(char *msg, ...);

extern int yylineno;
extern char *yytext;
GList *config; /* this is what we parse into */

%}

%union {
    double num;
    gchar *string;
    CLNode *node;
    GList *glist;
};

%token <num> TOK_NUM
%token <string> TOK_ID TOK_STRING
%token TOK_SEP

%type <glist> config
%type <glist> stmts
%type <node> stmt
%type <glist> list
%type <glist> block
%type <node> value

%expect 2 /* for now */

%%

config: stmts
    {
        config = $$ = $1;
    }
    ;

stmts:
    { $$ = NULL; }
    | stmt
    { $$ = g_list_append(NULL, $1); }
    | stmts stmt
    { $$ = g_list_append($1, $2); }
    ;

stmt: TOK_ID list ';'
    {
        CLNode *s = g_new(CLNode,1);
        s->type = CL_LIST;
        s->u.lb.list = $2;
        s->u.lb.block = NULL;
        s->u.lb.id = $1;
        s->lineno = yylineno;
        $$ = s;
    }
    | TOK_ID list block
    {
        CLNode *s = g_new(CLNode,1);
        s->type = CL_LISTBLOCK;
        s->u.lb.list = $2;
        s->u.lb.block = $3;
        s->u.lb.id = $1;
        s->lineno = yylineno;
        $$ = s;
    }
    | TOK_ID block
    { 
        CLNode *s = g_new(CLNode,1);
        s->type = CL_BLOCK;
        s->u.lb.block = $2;
        s->u.lb.list = NULL;
        s->u.lb.id = $1;
        s->lineno = yylineno;
        $$ = s;
    }
    ;

list: value
    {
        $$ = g_list_append(NULL, $1);
    }
    | list ',' value
    {
        $$ = g_list_append($1, $3);
    }
    ;

block: '{' stmts '}'
    {
        $$ = $2;
    }
    ;

value: TOK_ID
    {
        CLNode *node = g_new(CLNode,1);
        node->type = CL_ID;
        node->u.str = $1;
        node->lineno = yylineno;
        $$ = node;
    }
    | TOK_STRING
    { 
        CLNode *node = g_new(CLNode,1);
        node->type = CL_STR;
        node->u.str = $1;
        node->lineno = yylineno;
        $$ = node;
    }
    | TOK_NUM
    {
        CLNode *node = g_new(CLNode,1);
        node->type = CL_NUM;
        node->u.num = $1;
        node->lineno = yylineno;
        $$ = node;
    }
    ;

%%

int yywrap()
{
    return 1;
}

/* void yyerror(const char *err) */
/* { */
/*     fprintf(stderr, "Parse error on line %d, near '%s': %s\n", */
/*             yylineno, yytext, err); */
/* } */

void yyerror(char *msg, ...)
{
    va_list args;
    va_start(args,msg);

    fprintf(stderr, "Error on line %d, near '%s': ", yylineno, yytext);
    vfprintf(stderr, msg, args);
    fprintf(stderr,"\n");

    va_end(args);
}

GList *cl_parse_fh(FILE *fh)
{
    extern FILE *yyin;
    yyin = fh;
    yyparse();
    return config;
}