summaryrefslogtreecommitdiff
path: root/src/laylex.l
blob: e357207a92c98030ccf127996b4a2c157cb46659 (plain)
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
%option prefix="LayYY"
%option outfile="lex.yy.c"
%{
#ifndef FLEX_SCANNER
#undef input
#undef unput
#endif

#include    <X11/Xlib.h>
#include    <X11/Xresource.h>
#include    <X11/IntrinsicP.h>
#include    <X11/StringDefs.h>

#include    <X11/Xaw3d/LayoutP.h>
#include    "laygram.h"

#define yylval LayYYlval
#define yyerror LayYYerror
#define yysetsource LayYYsetsource

static char *yysourcebase, *yysource;

#ifndef FLEX_SCANNER
#define input() (*yysource++)
#define unput(c)    (--yysource)
#else
#include <string.h>
static void my_yyinput(char *buf, int *result, int max_size);
#define YY_INPUT(buf, res, max) my_yyinput(buf, &(res), max)
#endif

static int count (char *s, char c);
%}
%%
vertical		return VERTICAL;
horizontal		return HORIZONTAL;
"{"			return OC;
"}"			return CC;
"("			return OP;
")"			return CP;
"<"			return OA;
">"			return CA;
infinity		{ yylval.ival = 1; return INFINITY; }
inff*			{ yylval.ival = count(yytext, 'f'); return INFINITY; }
[0-9][0-9]*		{ yylval.ival = atoi(yytext); return NUMBER; }
"="			{ return EQUAL; }
"$"			{ return DOLLAR; }
"+"			{ yylval.oval = Plus; return PLUS; }
"-"			{ yylval.oval = Minus; return MINUS; }
"*"			{ yylval.oval = Times; return TIMES; }
"/"			{ yylval.oval = Divide; return DIVIDE; }
"%"			{ yylval.oval = Percent; return PERCENT; }
%[ \t\n]*of		{ yylval.oval = Percent; return PERCENTOF; }
width			return WIDTH;
height			return HEIGHT;
\\[a-zA-Z_][a-zA-Z0-9_]* {
#ifdef FLEX_SCANNER
			    yytext[yyleng] = '\0';
#else
			    yytext[yyleng-1] = '\0';
#endif
			    yylval.qval = XrmStringToQuark (yytext+1);
 			    return NAME;
			}

[a-zA-Z_][a-zA-Z0-9_]*	{
#ifdef FLEX_SCANNER
			    yytext[yyleng] = '\0';
#else
			    yytext[yyleng-1] = '\0';
#endif
			    yylval.qval = XrmStringToQuark (yytext);
 			    return NAME;
			}
" "			;
"\t"			;
"\n"			;
.			fprintf (stderr, "ignoring %c\n", *yytext);
%%

static int
count (char *s, char c)
{
    int	i = 0;
    while (*s)
	if (*s++ == c)
	    i++;
    return i;
}

void yysetsource(char *s)
{
    yysourcebase = yysource = s;
}

void yyerror(char *s)
{
    char    *t;

    fprintf (stderr, "%s\n", s);
    t = yysource - 50;
    if (t < yysourcebase)
	t = yysourcebase;
    while (*t && t < yysource + 50) {
	if (t == yysource)
	    putc ('@', stderr);
	putc (*t++, stderr);
    }
    if (t == yysource)
	putc ('@', stderr);
    if (!*t)
	fprintf (stderr, "<EOF>");
    fprintf (stderr, "\n");
}

#ifdef FLEX_SCANNER
static void
my_yyinput(char *buf, int *result, int max_size)
{
    int size = max_size < strlen(yysource) ? max_size : strlen(yysource);

    strncpy(buf, yysource, size);
    yysource += size;
    *result = size;
}
#endif