aboutsummaryrefslogtreecommitdiff
path: root/lex.l
blob: 92aadc8434cd61b0831ea6b5c8170b9d8713e985 (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
/* -*- mode:c; indent-tabs-mode: t; -*- */
%{

/*
 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, 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 "star-platinum.h"

#include <X11/Xlib.h>
#include <X11/keysymdef.h>

#include <err.h>
#include <string.h>

#include "y.tab.h"

int state  = 0;
KeySym key = 0;

%}

%x COMMENT
%x KEY
%x STRING

%%

<INITIAL>#	BEGIN(COMMENT);
<COMMENT>.*\n	yylineno++; BEGIN(INITIAL); return '\n';

<INITIAL>\"	BEGIN(KEY);
<KEY>\"		{
	BEGIN(INITIAL);
	yylval.key = (struct key){ state, key };
	state = 0;
	key = 0;
	return TKEY;
 }

<KEY>C-		state |= ControlMask;
<KEY>S-		state |= ShiftMask;
<KEY>M-		state |= Mod1Mask;
<KEY>s-		state |= Mod4Mask;

<KEY><[^>]+>	{
	char *c;

	if ((c = strdup(yytext)) == NULL)
		err(1, "strdup");

	c++;			/* skip the < */
	c[strlen(c)-1] = '\0';	/* trim the > */

	if ((key = XStringToKeysym(c)) == NoSymbol)
		yyerror("invalid keysym %s", c);

	free(--c);
 }

<KEY>SPC	key = XK_space;
<KEY>RET	key = XK_Return;

<KEY>"("	key = XK_parenleft;
<KEY>")"	key = XK_parenright;

<KEY>.		{
	if ((key = XStringToKeysym(yytext)) == NoSymbol)
		yyerror("invalid key %s", yytext);
 }

<INITIAL>\'	BEGIN(STRING);
<STRING>[^']*\'	{
	if ((yylval.str = strdup(yytext)) == NULL)
		err(1, "strdup");
	yylval.str[strlen(yylval.str)-1] = '\0'; /* remove the closing quote */
	BEGIN(INITIAL);
	return TSTRING;
 }

match		return TMATCH;
class		return TCLASS;
on		return TON;
do		return TDO;
toggle		return TTOGGLE;
activate	return TACTIVATE;
deactivate	return TDEACTIVATE;
ignore		return TIGNORE;
exec		return TEXEC;
all		return TALL;

\n		yylineno++; return '\n';
[ \r\t\f]+

.		return TERR;

%%

int
yywrap(void)
{
	return 1;
}