xftgram.y 8.97 KB
Newer Older
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/*
 * $XFree86: xc/lib/Xft/xftgram.y,v 1.5 2001/05/16 10:32:54 keithp Exp $
 *
 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

%{

#include <stdlib.h>
#include <stdio.h>
#include "xftint.h"

static XftMatrix   matrix;
    
%}

%union {
    int		ival;
    double	dval;
    char	*sval;
    XftExpr	*eval;
    XftPattern	*pval;
    XftValue	vval;
    XftEdit	*Eval;
    XftOp	oval;
    XftQual	qval;
    XftTest	*tval;
}

%token <ival>	INTEGER
%token <dval>	DOUBLE
%token <sval>	STRING NAME
%token <ival>	ANY ALL
%token <ival>	DIR CACHE INCLUDE INCLUDEIF MATCH EDIT
%token <ival>	TOK_TRUE TOK_FALSE TOK_NIL
%token <ival>	EQUAL SEMI OS CS

%type  <eval>	expr
%type  <vval>	value
%type  <sval>	field
%type  <Eval>	edit
%type  <Eval>	edits
%type  <oval>	eqop
%type  <qval>	qual
%type  <oval>	compare
%type  <tval>	tests test
%type  <dval>	number

%right <ival>	QUEST COLON
%left <ival>	OROR
%left <ival>	ANDAND
%left <ival>	EQEQ NOTEQ
%left <ival>	LESS LESSEQ MORE MOREEQ
%left <ival>	PLUS MINUS
%left <ival>	TIMES DIVIDE
%right <ival>	NOT

%%
configs	:   configs config
	|
	;
config	:   DIR STRING
		{ XftConfigAddDir ($2); }
	|   CACHE STRING
		{ XftConfigSetCache ($2); }
	|   INCLUDE STRING
		{ XftConfigPushInput ($2, True); }
	|   INCLUDEIF STRING
		{ XftConfigPushInput ($2, False); }
	|   MATCH tests EDIT edits
		{ XftConfigAddEdit ($2, $4); }
	;
tests	:   test tests
		{ $1->next = $2; $$ = $1; }
	|
		{ $$ = 0; }
	;
test	:   qual field compare value
		{ $$ = XftTestCreate ($1, $2, $3, $4); }
	;
qual	:   ANY
	    { $$ = XftQualAny; }
	|   ALL
	    { $$ = XftQualAll; }
	|
	    { $$ = XftQualAny; }
	;
field	:   NAME
		{ 
		    $$ = XftConfigSaveField ($1); 
		}
	;
compare	:   EQUAL
		{ $$ = XftOpEqual; }
	|   EQEQ
		{ $$ = XftOpEqual; }
	|   NOTEQ
		{ $$ = XftOpNotEqual; }
	|   LESS
		{ $$ = XftOpLess; }
	|   LESSEQ
		{ $$ = XftOpLessEqual; }
	|   MORE
		{ $$ = XftOpMore; }
	|   MOREEQ
		{ $$ = XftOpMoreEqual; }
	;
value	:   INTEGER
		{
		    $$.type = XftTypeInteger;
		    $$.u.i = $1;
		}
	|   DOUBLE		
		{
		    $$.type = XftTypeDouble;
		    $$.u.d = $1;
		}
	|   STRING
		{
		    $$.type = XftTypeString;
		    $$.u.s = $1;
		}
	|   TOK_TRUE
		{
		    $$.type = XftTypeBool;
		    $$.u.b = True;
		}
	|   TOK_FALSE
		{
		    $$.type = XftTypeBool;
		    $$.u.b = False;
		}
	|   TOK_NIL
		{
		    $$.type = XftTypeVoid;
		}
	|   matrix
		{
		    $$.type = XftTypeMatrix;
		    $$.u.m = &matrix;
		}
	;
matrix	:   OS number number number number CS
		{
		    matrix.xx = $2;
		    matrix.xy = $3;
		    matrix.yx = $4;
		    matrix.__REALLY_YY__ = $5;
		}
number	:   INTEGER
		{ $$ = (double) $1; }
	|   DOUBLE
	;
edits	:   edit edits
	    { $1->next = $2; $$ = $1; }
	|
	    { $$ = 0; }
	;
edit	:   field eqop expr SEMI
	    { $$ = XftEditCreate ($1, $2, $3); }
	;
eqop	:   EQUAL
	    { $$ = XftOpAssign; }
	|   PLUS EQUAL
	    { $$ = XftOpPrepend; }
	|   EQUAL PLUS
	    { $$ = XftOpAppend; }
	;
expr	:   INTEGER
	    { $$ = XftExprCreateInteger ($1); }
	|   DOUBLE
	    { $$ = XftExprCreateDouble ($1); }
	|   STRING
	    { $$ = XftExprCreateString ($1); }
	|   TOK_TRUE
	    { $$ = XftExprCreateBool (True); }
	|   TOK_FALSE
	    { $$ = XftExprCreateBool (False); }
	|   TOK_NIL
	    { $$ = XftExprCreateNil (); }
	|   matrix
	    { $$ = XftExprCreateMatrix (&matrix); }
	|   NAME
	    { $$ = XftExprCreateField ($1); }
	|   expr OROR expr
	    { $$ = XftExprCreateOp ($1, XftOpOr, $3); }
	|   expr ANDAND expr
	    { $$ = XftExprCreateOp ($1, XftOpAnd, $3); }
	|   expr EQEQ expr
	    { $$ = XftExprCreateOp ($1, XftOpEqual, $3); }
	|   expr NOTEQ expr
	    { $$ = XftExprCreateOp ($1, XftOpNotEqual, $3); }
	|   expr LESS expr
	    { $$ = XftExprCreateOp ($1, XftOpLess, $3); }
	|   expr LESSEQ expr
	    { $$ = XftExprCreateOp ($1, XftOpLessEqual, $3); }
	|   expr MORE expr
	    { $$ = XftExprCreateOp ($1, XftOpMore, $3); }
	|   expr MOREEQ expr
	    { $$ = XftExprCreateOp ($1, XftOpMoreEqual, $3); }
	|   expr PLUS expr
	    { $$ = XftExprCreateOp ($1, XftOpPlus, $3); }
	|   expr MINUS expr
	    { $$ = XftExprCreateOp ($1, XftOpMinus, $3); }
	|   expr TIMES expr
	    { $$ = XftExprCreateOp ($1, XftOpTimes, $3); }
	|   expr DIVIDE expr
	    { $$ = XftExprCreateOp ($1, XftOpDivide, $3); }
	|   NOT expr
	    { $$ = XftExprCreateOp ($2, XftOpNot, (XftExpr *) 0); }
	|   expr QUEST expr COLON expr
	    { $$ = XftExprCreateOp ($1, XftOpQuest, XftExprCreateOp ($3, XftOpQuest, $5)); }
	;
%%

int
XftConfigwrap (void)
{
    return 1;
}

void
XftConfigerror (char *fmt, ...)
{
    va_list	args;

    fprintf (stderr, "\"%s\": line %d, ", XftConfigFile, XftConfigLineno);
    va_start (args, fmt);
    vfprintf (stderr, fmt, args);
    va_end (args);
    fprintf (stderr, "\n");
}

XftTest *
XftTestCreate (XftQual qual, const char *field, XftOp compare, XftValue value)
{
    XftTest	*test = (XftTest *) malloc (sizeof (XftTest));;

    if (test)
    {
	test->next = 0;
	test->qual = qual;
	test->field = field;	/* already saved in grammar */
	test->op = compare;
	if (value.type == XftTypeString)
	    value.u.s = _XftSaveString (value.u.s);
	else if (value.type == XftTypeMatrix)
	    value.u.m = _XftSaveMatrix (value.u.m);
	test->value = value;
    }
    return test;
}

XftExpr *
XftExprCreateInteger (int i)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpInteger;
	e->u.ival = i;
    }
    return e;
}

XftExpr *
XftExprCreateDouble (double d)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpDouble;
	e->u.dval = d;
    }
    return e;
}

XftExpr *
XftExprCreateString (const char *s)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpString;
	e->u.sval = _XftSaveString (s);
    }
    return e;
}

XftExpr *
XftExprCreateMatrix (const XftMatrix *m)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpMatrix;
	e->u.mval = _XftSaveMatrix (m);
    }
    return e;
}

XftExpr *
XftExprCreateBool (Bool b)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpBool;
	e->u.bval = b;
    }
    return e;
}

XftExpr *
XftExprCreateNil (void)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpNil;
    }
    return e;
}

XftExpr *
XftExprCreateField (const char *field)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = XftOpField;
	e->u.field = _XftSaveString (field);
    }
    return e;
}

XftExpr *
XftExprCreateOp (XftExpr *left, XftOp op, XftExpr *right)
{
    XftExpr *e = (XftExpr *) malloc (sizeof (XftExpr));

    if (e)
    {
	e->op = op;
	e->u.tree.left = left;
	e->u.tree.right = right;
    }
    return e;
}

void
XftExprDestroy (XftExpr *e)
{
    switch (e->op) {
    case XftOpInteger:
	break;
    case XftOpDouble:
	break;
    case XftOpString:
	free (e->u.sval);
	break;
    case XftOpMatrix:
	free (e->u.mval);
	break;
    case XftOpBool:
	break;
    case XftOpField:
	free (e->u.field);
	break;
    case XftOpAssign:
    case XftOpPrepend:
    case XftOpAppend:
	break;
    case XftOpOr:
    case XftOpAnd:
    case XftOpEqual:
    case XftOpNotEqual:
    case XftOpLess:
    case XftOpLessEqual:
    case XftOpMore:
    case XftOpMoreEqual:
    case XftOpPlus:
    case XftOpMinus:
    case XftOpTimes:
    case XftOpDivide:
    case XftOpQuest:
	XftExprDestroy (e->u.tree.right);
	/* fall through */
    case XftOpNot:
	XftExprDestroy (e->u.tree.left);
	break;
    case XftOpNil:
	break;
    }
    free (e);
}

XftEdit *
XftEditCreate (const char *field, XftOp op, XftExpr *expr)
{
    XftEdit *e = (XftEdit *) malloc (sizeof (XftEdit));

    if (e)
    {
	e->next = 0;
	e->field = field;   /* already saved in grammar */
	e->op = op;
	e->expr = expr;
    }
    return e;
}

void
XftEditDestroy (XftEdit *e)
{
    if (e->next)
	XftEditDestroy (e->next);
    free ((void *) e->field);
    if (e->expr)
	XftExprDestroy (e->expr);
}

char *
XftConfigSaveField (const char *field)
{
    return _XftSaveString (field);
}