shithub: vcardfs

ref: 071b196a37e0e88ee005c68f84d90753d2a98a5b
dir: /libvcard/vcard.y/

View raw version
%{
#include <u.h>
#include <libc.h>
#include "vcard.h"

Vcard *vcparsecard;

extern int yylex(void);

void
yyerror(char *s)
{
	werrstr("%s (%10s)\n", s, vcstate.s);
}

static Vcard*
mkvcard(Vline *lines)
{
	Vcard *vc;
	vc = mallocz(sizeof(Vcard), 1);
	vc->content = lines;
	return vc;
}

static void
enqueue(Vcard *head, Vcard *tail)
{
	head->next = tail;
	vcparsecard = head;
}

static void
enqueuel(Vline *head, Vline *tail)
{
	head->next = tail;
}

static void
enqueuep(Vparam *head, Vparam *tail)
{
	head->next = tail;
}

static Vline*
mkline(char *name, Vparam *params, char *value, char *group)
{
	Vline *vl;
	vl = mallocz(sizeof(Vline), 1);
	vl->name = name;
	vl->value = value;
	vl->params = params;
	vl->group = group;
	/*
	fprint(2, "new line:\n");
	fprint(2, "  name: %s\n", name);
	fprint(2, "  value: %s\n", value);
	fprint(2, "  params: %p\n", params);
	fprint(2, "  group: %s\n", group);
	*/
	return vl;
}

static Vparam*
addparam(char *name, char *value)
{
	Vparam *vp;
	vp = mallocz(sizeof(Vparam), 1);
	vp->name = name;
	vp->value = value;
	return vp;
}

static char*
xname(char *s)
{
	char *n;
	n = smprint("x-%s", s);
	free(s);
	return n;
}

%}

%union {
	int i;
	Vcard *vc;
	Vline *vl;
	Vparam *vp;
	char *s;
}

%token	<i> 	BEGIN END CRLF

%token	<s> 	WORD SWORD FWORD
%token	<s> 	PROPNAME

%type	<s> 	pvalue

%type	<vc>	vclist vcard
%type	<vl>	cline clinel
%type	<vp>	params param
%type	<s> 	group name value

%%

vclist:
	  vcard vclist { $$ = $1; enqueue($1, $2); }
	| /* empty */ { $$ = nil; }
	;

vcard:
	  BEGIN clinel END { $$ = mkvcard($2); }
	| BEGIN clinel END CRLF { $$ = mkvcard($2); }
	;

clinel:
	  cline
	| cline clinel { $$ = $1; enqueuel($1, $2); }
	;

cline:
	  group '.' name params ':' value CRLF { $$ = mkline($3, $4, $6, $1); }
	| name params ':' value CRLF { $$ = mkline($1, $2, $4, nil); }
	| name ':' value CRLF { $$ = mkline($1, nil, $3, nil); }
	;

params:
	  ';' param { $$ = $2; }
	| ';' param params { $$ = $2; enqueuep($2, $3); }
	;

param: SWORD '=' pvalue { $$ = addparam($1, $3); };

pvalue:
	  SWORD
	| '"' WORD '"' { $$ = $2; }
	| '"' SWORD '"' { $$ = $2; }
	| '"' FWORD '"' { $$ = $2; }
	;

group: SWORD;
name: PROPNAME;

value: WORD | SWORD | FWORD;