Add first version of lexer/parser to git, update Makefile

This commit is contained in:
Michael Stapelberg 2009-09-13 14:16:39 +02:00
parent 30ad7e93e7
commit 9dd975e974
3 changed files with 268 additions and 4 deletions

View File

@ -3,7 +3,9 @@ TOPDIR=$(shell pwd)
include $(TOPDIR)/common.mk include $(TOPDIR)/common.mk
# Depend on the object files of all source-files in src/*.c and on all header files # Depend on the object files of all source-files in src/*.c and on all header files
FILES=$(patsubst %.c,%.o,$(wildcard src/*.c)) AUTOGENERATED:=src/cfgparse.tab.c src/cfgparse.yy.c
FILES:=$(filter-out $(AUTOGENERATED),$(wildcard src/*.c))
FILES:=$(FILES:.c=.o)
HEADERS=$(wildcard include/*.h) HEADERS=$(wildcard include/*.h)
# Depend on the specific file (.c for each .o) and on all headers # Depend on the specific file (.c for each .o) and on all headers
@ -11,13 +13,23 @@ src/%.o: src/%.c ${HEADERS}
echo "CC $<" echo "CC $<"
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
all: ${FILES} all: src/cfgparse.y.o src/cfgparse.yy.o ${FILES}
echo "LINK i3" echo "LINK i3"
$(CC) -o i3 ${FILES} $(LDFLAGS) $(CC) -o i3 ${FILES} src/cfgparse.y.o src/cfgparse.yy.o $(LDFLAGS)
echo "" echo ""
echo "SUBDIR i3-msg" echo "SUBDIR i3-msg"
$(MAKE) TOPDIR=$(TOPDIR) -C i3-msg $(MAKE) TOPDIR=$(TOPDIR) -C i3-msg
src/cfgparse.yy.o: src/cfgparse.l
echo "LEX $<"
lex -i -o $(@:.o=.c) $<
$(CC) $(CFLAGS) -c -o $@ $(@:.o=.c)
src/cfgparse.y.o: src/cfgparse.y
echo "YACC $<"
yacc -b $(basename $< .y) -d $<
$(CC) $(CFLAGS) -c -o $@ $(<:.y=.tab.c)
install: all install: all
echo "INSTALL" echo "INSTALL"
$(INSTALL) -d -m 0755 $(DESTDIR)/usr/bin $(INSTALL) -d -m 0755 $(DESTDIR)/usr/bin
@ -46,7 +58,7 @@ dist: distclean
rm -rf i3-${VERSION} rm -rf i3-${VERSION}
clean: clean:
rm -f src/*.o rm -f src/*.o src/cfgparse.tab.{c,h} src/cfgparse.yy.c
$(MAKE) -C docs clean $(MAKE) -C docs clean
$(MAKE) -C man clean $(MAKE) -C man clean
$(MAKE) TOPDIR=$(TOPDIR) -C i3-msg clean $(MAKE) TOPDIR=$(TOPDIR) -C i3-msg clean

59
src/cfgparse.l Normal file
View File

@ -0,0 +1,59 @@
%{
#include <stdio.h>
#include "cfgparse.tab.h"
#include <xcb/xcb.h>
enum {
BIND_NONE = 0,
BIND_SHIFT = XCB_MOD_MASK_SHIFT, /* (1 << 0) */
BIND_CONTROL = XCB_MOD_MASK_CONTROL, /* (1 << 2) */
BIND_MOD1 = XCB_MOD_MASK_1, /* (1 << 3) */
BIND_MOD2 = XCB_MOD_MASK_2, /* (1 << 4) */
BIND_MOD3 = XCB_MOD_MASK_3, /* (1 << 5) */
BIND_MOD4 = XCB_MOD_MASK_4, /* (1 << 6) */
BIND_MOD5 = XCB_MOD_MASK_5, /* (1 << 7) */
BIND_MODE_SWITCH = (1 << 8)
};
%}
%Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND
%%
[0-9]+ yylval.number = atoi(yytext); return NUMBER;
[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; }
bind { BEGIN(BIND_COND); return TOKBIND; }
bindsym { BEGIN(BIND_COND); return TOKBINDSYM; }
floating_modifier return TOKFLOATING_MODIFIER;
workspace return TOKWORKSPACE;
screen return TOKSCREEN;
assign { BEGIN(ASSIGN_COND); return TOKASSIGN; }
set { BEGIN(BIND_COND); return TOKSET; }
ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; }
exec { BEGIN(BIND_AWS_COND); return TOKEXEC; }
client.focused { /* TODO: lval pointer */ return TOKCOLOR; }
Mod1 yylval.number = BIND_MOD1; return MODIFIER;
Mod2 yylval.number = BIND_MOD2; return MODIFIER;
Mod3 yylval.number = BIND_MOD3; return MODIFIER;
Mod4 yylval.number = BIND_MOD4; return MODIFIER;
Mod5 yylval.number = BIND_MOD5; return MODIFIER;
control return TOKCONTROL;
shift return TOKSHIFT;
→ return TOKARROW;
\n /* ignore end of line */;
<BIND_COND>[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; }
<BIND_AWS_COND>[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; }
[ \t]+ return WHITESPACE;
<BIND_A2WS_COND>[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING; }
<ASSIGN_COND>[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING_NG; }
[a-zA-Z]+ yylval.string = strdup(yytext); return WORD;
[a-zA-Z0-9_-]+ yylval.string = strdup(yytext); return VARNAME;
\"[^\"]+\" {
/* yylval will be the string, but without quotes */
char *copy = strdup(yytext+1);
copy[strlen(copy)-1] = '\0';
yylval.string = copy;
return QUOTEDSTRING;
}
. return (int) yytext[0];
%%

193
src/cfgparse.y Normal file
View File

@ -0,0 +1,193 @@
%{
#include <stdio.h>
#include <string.h>
#include <xcb/xcb.h>
enum {
BIND_NONE = 0,
BIND_SHIFT = XCB_MOD_MASK_SHIFT, /* (1 << 0) */
BIND_CONTROL = XCB_MOD_MASK_CONTROL, /* (1 << 2) */
BIND_MOD1 = XCB_MOD_MASK_1, /* (1 << 3) */
BIND_MOD2 = XCB_MOD_MASK_2, /* (1 << 4) */
BIND_MOD3 = XCB_MOD_MASK_3, /* (1 << 5) */
BIND_MOD4 = XCB_MOD_MASK_4, /* (1 << 6) */
BIND_MOD5 = XCB_MOD_MASK_5, /* (1 << 7) */
BIND_MODE_SWITCH = (1 << 8)
};
int yydebug = 1;
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
#if 0
main()
{
yyparse();
printf("parsing done\n");
}
#endif
%}
%union {
int number;
char *string;
}
%token <number>NUMBER
%token <string>WORD
%token <string>STRING
%token <string>STRING_NG
%token <string>VARNAME
%token <string>HEX
%token TOKBIND
%token TOKBINDSYM
%token MODIFIER
%token TOKCONTROL
%token TOKSHIFT
%token WHITESPACE
%token TOKFLOATING_MODIFIER
%token QUOTEDSTRING
%token TOKWORKSPACE
%token TOKSCREEN
%token TOKASSIGN
%token TOKSET
%token TOKIPCSOCKET
%token TOKEXEC
%token TOKCOLOR
%token TOKARROW
%%
lines: /* empty */
| lines WHITESPACE line
| lines line
;
line:
bind
| bindsym
| floating_modifier
| workspace
| assign
| set
| ipcsocket
| exec
| color
;
command:
STRING
;
bind:
TOKBIND WHITESPACE binding_modifiers '+' NUMBER WHITESPACE command
{
printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $5, $<string>7);
}
;
bindsym:
TOKBINDSYM WHITESPACE binding_modifiers '+' WORD WHITESPACE command
{
printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $5, $<string>7);
}
;
floating_modifier:
TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
{
printf("\tfloating modifier %d\n", $<number>3);
}
;
workspace:
TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen
{
printf("\t workspace %d to screen %d\n", $<number>3, $<number>7);
}
| TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen WHITESPACE QUOTEDSTRING
{
printf("\t quoted: %s\n", $<string>9);
}
;
screen:
NUMBER
;
assign:
TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow NUMBER
{
printf("assignment of %s to %d\n", $<string>3, $<number>6);
}
;
window_class:
QUOTEDSTRING
| STRING_NG
;
optional_arrow:
/* NULL */
| TOKARROW WHITESPACE
;
set:
TOKSET WHITESPACE variable WHITESPACE STRING
{
printf("set %s to %s\n", $<string>3, $<string>5);
}
;
variable:
'$' WORD { asprintf(&$<string>$, "$%s", $<string>2); }
| '$' VARNAME { asprintf(&$<string>$, "$%s", $<string>2); }
;
ipcsocket:
TOKIPCSOCKET WHITESPACE STRING
{
printf("ipc %s\n", $<string>3);
}
;
exec:
TOKEXEC WHITESPACE STRING
{
printf("exec %s\n", $<string>3);
}
;
color:
TOKCOLOR WHITESPACE '#' HEX
{
printf("color %s\n", $<string>4);
}
;
binding_modifiers:
binding_modifier
|
binding_modifiers '+' binding_modifier
{
$<number>$ = $<number>1 | $<number>3;
}
;
binding_modifier:
MODIFIER { $<number>$ = $<number>1; }
| TOKCONTROL { $<number>$ = BIND_CONTROL; }
| TOKSHIFT { $<number>$ = BIND_SHIFT; }
;