~ubuntu-branches/ubuntu/trusty/picviz/trusty

« back to all changes in this revision

Viewing changes to src/libpicviz/parser/lexer.l

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2009-03-30 10:42:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090330104208-j095obwkp574t1lm
Tags: upstream-0.5
ImportĀ upstreamĀ versionĀ 0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Written by Sebastien Tricaud (c) 2008
 
4
 * Part of the PicViz project
 
5
 */
 
6
 
 
7
%x incl
 
8
 
 
9
%{
 
10
 
 
11
#include <stdio.h>
 
12
#include <string.h>
 
13
#include <stdlib.h>
 
14
 
 
15
#include <picviz.h>
 
16
#include "parser.h"
 
17
 
 
18
static char *filename;
 
19
static char *realfile;
 
20
 
 
21
void yyerror (char *s);
 
22
#if defined(__FreeBSD__) || defined(__APPLE__)
 
23
char* strndup(const char* string, size_t n)
 
24
{
 
25
        char* copy_string = 0;
 
26
 
 
27
        if(0 == string || 0 == n)
 
28
                return 0;
 
29
 
 
30
        copy_string = (char*) malloc(n + 1);
 
31
        if(0 == copy_string)
 
32
                return 0;
 
33
 
 
34
        memcpy(copy_string, string, n);
 
35
        *(copy_string + n) = '\0';
 
36
 
 
37
        return copy_string;
 
38
}
 
39
#else
 
40
char * strndup (const char *s, size_t n);
 
41
#endif
 
42
 
 
43
#define MAX_INCLUDE_DEPTH 10
 
44
YY_BUFFER_STATE includes[MAX_INCLUDE_DEPTH];
 
45
char *filenames[MAX_INCLUDE_DEPTH];
 
46
int includes_index = 0;
 
47
 
 
48
extern int axis_position;
 
49
 
 
50
static char *escape_str(char *str)
 
51
{
 
52
        size_t w = 0, i = 0;
 
53
        int escape_next = 0;
 
54
 
 
55
        for ( i = 1; str[i]; i++ ) {
 
56
 
 
57
                if ( ! escape_next && str[i] == '\\' ) {
 
58
                        escape_next = 1;
 
59
                        continue;
 
60
                }
 
61
 
 
62
                str[w++] = str[i];
 
63
                escape_next = 0;
 
64
        }
 
65
 
 
66
        str[w - 1] = '\0';
 
67
        return str;
 
68
}
 
69
 
 
70
#ifdef __FreeBSD__
 
71
void yyset_lineno(int line_number) {
 
72
        yylineno = line_number;
 
73
}
 
74
 
 
75
int yyget_lineno() {
 
76
        return yylineno;
 
77
}
 
78
 
 
79
char * yyget_text() {
 
80
        return yytext;
 
81
}
 
82
#endif
 
83
 
 
84
%}
 
85
%option noyywrap
 
86
 
 
87
SECTION         (header|engine|axes|data)
 
88
OPEN_SECTION    \{
 
89
CLOSE_SECTION   \}
 
90
DATATYPE        (timeline|integer|float|string|short|ipv4|gold|char|years|enum|ln|port)
 
91
OPEN_PROP       \[
 
92
CLOSE_PROP      \]
 
93
PROPERTY        (label|color|penwidth|relative|print)
 
94
SQSTRING        \'([^\\\']|\\.)*\'
 
95
DQSTRING        \"([^\\\"]|\\.)*\"
 
96
STRING          (\"|\')([^\\(\"|\')]|\\.)*(\"|\')
 
97
COMMENT         ^#.*\n
 
98
WORD            ([a-zA-Z0-9_\-]+(\(\-?[0-9\*]+\))?\.?)+
 
99
SEMICOLON       \;
 
100
COMMA           \,
 
101
EQUAL           \=
 
102
VOID            [ \t]+
 
103
NEWLINE         \n
 
104
INCLUDE         @include
 
105
 
 
106
%%
 
107
 
 
108
{INCLUDE}       { BEGIN(incl); }
 
109
 
 
110
{PROPERTY}      { yylval.string = strdup(yytext);
 
111
                  return TOK_PROPERTY; }
 
112
{OPEN_PROP}     { return TOK_OPEN_PROP; }
 
113
{CLOSE_PROP}     { return TOK_CLOSE_PROP; }
 
114
 
 
115
{SEMICOLON}     {
 
116
                        axis_position = 0;
 
117
                        return TOK_SEMICOLON;
 
118
                }
 
119
{COMMA}         { return TOK_COMMA; }
 
120
 
 
121
{SECTION}       {       yylval.string = strdup(yytext);
 
122
                        return TOK_SECTION;
 
123
                }
 
124
{OPEN_SECTION}  {       return TOK_OPEN_SECTION;  }
 
125
{CLOSE_SECTION} {       return TOK_CLOSE_SECTION; }
 
126
 
 
127
{DATATYPE}      {       yylval.string = strdup(yytext);
 
128
                        return TOK_DATATYPE;
 
129
                }
 
130
 
 
131
{DQSTRING}      {
 
132
                        yylval.string = strdup(escape_str(yytext));
 
133
                        return TOK_DQSTRING;
 
134
                }
 
135
 
 
136
{WORD}          {       yylval.string = strdup(yytext);
 
137
                        return TOK_WORD;
 
138
                }
 
139
 
 
140
{EQUAL}         {       return TOK_EQUAL; }
 
141
 
 
142
{STRING}        {
 
143
                        yylval.string = strdup(escape_str(yytext));
 
144
                        return TOK_WORD;
 
145
                }
 
146
 
 
147
 
 
148
{COMMENT}       {
 
149
                        /* We don't care */
 
150
                }
 
151
 
 
152
{VOID}          {
 
153
                        /* We don't care */
 
154
                }
 
155
 
 
156
{NEWLINE}       {
 
157
                        /* We don't care (; is the separator)
 
158
                         * so we just inscrease the line number
 
159
                         */
 
160
                        yyset_lineno(yyget_lineno()+1);
 
161
                }
 
162
 
 
163
<incl>[ \t]*      /* eat the whitespace */
 
164
<incl>[^ \t\n]+ { /* got the include file name */
 
165
                  if (includes_index >= MAX_INCLUDE_DEPTH) {
 
166
                          yyerror("Includes nested too deeply");
 
167
                          exit(1);
 
168
                  }
 
169
 
 
170
                filename = escape_str(strndup(yytext, yyleng));
 
171
 
 
172
 
 
173
                filenames[includes_index] = filename;
 
174
                includes[includes_index++] = YY_CURRENT_BUFFER;
 
175
 
 
176
                realfile = strdup(filename);
 
177
 
 
178
                yyin = fopen(realfile, "r");
 
179
                if (!yyin) {
 
180
                        printf("Can not open %s\n", realfile);
 
181
                        exit(1);
 
182
                }
 
183
                free(realfile);
 
184
                free(filename);
 
185
 
 
186
                yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
 
187
 
 
188
                  BEGIN(INITIAL);
 
189
                }
 
190
<<EOF>>         {
 
191
                  if (--includes_index < 0)
 
192
                          yyterminate();
 
193
                  else {
 
194
                          free(filenames[includes_index + 1]);
 
195
                          filename = filenames[includes_index];
 
196
                          yy_delete_buffer(YY_CURRENT_BUFFER);
 
197
                          yy_switch_to_buffer(includes[includes_index]);
 
198
                  }
 
199
                }
 
200
 
 
201
%%
 
202
 
 
203
/* remove unused functions */
 
204
typedef void (*dummy_function) ();
 
205
dummy_function picvizparser_unused[] =
 
206
{
 
207
  (dummy_function) input, (dummy_function) yyunput
 
208
};
 
209
 
 
210