1 /*
2  * libvc - vCard library
3  * Copyright (C) 2003  Andrew Hsu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * $Id: vc_parse.y,v 1.3 2003/06/04 10:56:55 ahsu Rel $
20  */
21 
22 %{
23 
24 #include "vc.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #define YYSTYPE char*
30 
31 extern FILE *yyin;
32 
33 extern int yylex ();
34 void yyerror (char *s);
35 
36 vc_component *current_vcard = NULL;
37 vc_component *current_vc = NULL;
38 char *current_vc_param_name = NULL;
39 
40 %}
41 
42 /*-----------------------
43    create a y.tab.h file
44   -----------------------*/
45 %defines
46 
47 %start vcard
48 
49 %token TOK_BEGIN_VCARD
50 %token TOK_END_VCARD
51 %token TOK_GROUP
52 %token TOK_NAME
53 %token TOK_PARAM_NAME
54 %token TOK_PARAM_VALUE
55 %token TOK_VALUE
56 
57 %%
58 
59 vcard
60         : begin_line contentlines end_line { YYACCEPT; }
61         | error '\n' { yyerrok; }
62         ;
63 
64 begin_line
65         : filler first_line
66         | first_line
67         ;
68 
69 filler
70         : filler '\n'
71         | '\n'
72         ;
73 
74 first_line
75         : TOK_BEGIN_VCARD '\n' { current_vcard = vc_new (); }
76         | TOK_GROUP '.' TOK_BEGIN_VCARD '\n' {
77             current_vcard = vc_new ();
78             vc_set_group (current_vcard, $1); }
79         ;
80 
81 end_line
82         : TOK_END_VCARD '\n'
83         | TOK_GROUP '.' TOK_END_VCARD '\n'
84         ;
85 
86 contentlines
87         : contentlines group_contentline
88         | group_contentline
89         ;
90 
91 group_contentline
92         : TOK_GROUP '.' contentline { vc_set_group (current_vc, $1); }
93         | contentline
94         ;
95 
96 contentline
97         : name params ':' value '\n'
98         | name ':' value '\n'
99         ;
100 
101 name
102         : TOK_NAME { current_vc = vc_append_with_name (current_vcard, $1); }
103         ;
104 
105 params
106         : ';' param
107         | params ';' param
108         ;
109 
110 param
111         : param_name '=' param_values
112         | param_name {
113             vc_component_param *tmp_vc_param = NULL;
114             tmp_vc_param = vc_param_new ();
115             vc_param_set_name (tmp_vc_param, "TYPE");
116             vc_param_set_value (tmp_vc_param, current_vc_param_name);
117             vc_add_param (current_vc, tmp_vc_param); }
118         ;
119 
120 param_name
121         : TOK_PARAM_NAME {
122             if (NULL != current_vc_param_name)
123               {
124                 free (current_vc_param_name);
125                 current_vc_param_name = NULL;
126               }
127             current_vc_param_name = strdup ($1); }
128         ;
129 
130 param_values
131         : param_value
132         | param_values ',' param_value
133         ;
134 
135 param_value
136         : TOK_PARAM_VALUE {
137             vc_component_param *tmp_vc_param = NULL;
138 
139             tmp_vc_param = vc_param_new ();
140             vc_param_set_name (tmp_vc_param, current_vc_param_name);
141             vc_param_set_value (tmp_vc_param, $1);
142             vc_add_param (current_vc, tmp_vc_param); }
143         ;
144 
145 value
146         : TOK_VALUE { vc_set_value (current_vc, $1); }
147         ;
148 
149 %%
150 
151 void
152 yyerror (char *s)
153 {
154 }
155 
156 vc_component *
parse_vcard_file(FILE * fp)157 parse_vcard_file (FILE * fp)
158 {
159   vc_component *vc = NULL;
160 
161   yyin = fp;
162 
163   if (0 == yyparse ())
164     {
165       vc = current_vcard;
166     }
167 
168   return vc;
169 }
170