~ubuntu-branches/ubuntu/warty/libapache2-mod-python/warty

« back to all changes in this revision

Viewing changes to src/psp_parser.l

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-09-06 20:27:57 UTC
  • Revision ID: james.westby@ubuntu.com-20040906202757-yzpyu1bcabgpjtiu
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
/*
 
3
 * Copyright 2004 Apache Software Foundation 
 
4
 * 
 
5
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 
6
 * may not use this file except in compliance with the License.  You
 
7
 * may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
14
 * implied.  See the License for the specific language governing
 
15
 * permissions and limitations under the License.
 
16
 *
 
17
 * $Id: psp_parser.l,v 1.20 2004/02/16 19:47:27 grisha Exp $
 
18
 *
 
19
 * This file originally written by Sterling Hughes.
 
20
 * 
 
21
 */
 
22
 
 
23
/* NOTE The seemingly unusual generated Python code (sometime using
 
24
 * ";" to separate statements, newline placement, etc) is such that
 
25
 * for vast majority of cases the line number of the input file will
 
26
 * match the line number of the output!
 
27
 */
 
28
 
 
29
#include "psp_parser.h"
 
30
 
 
31
#define OUTPUT_WHITESPACE(__wsstring) \
 
32
        psp_string_0((__wsstring)); \
 
33
        psp_string_append(&PSP_PG(pycode), (__wsstring)->blob)
 
34
 
 
35
#define CLEAR_WHITESPACE(__wsstring) psp_string_clear((__wsstring));
 
36
 
 
37
%}
 
38
 
 
39
%option noyywrap nounistd
 
40
 
 
41
%x TEXT
 
42
%x PYCODE
 
43
%x INDENT
 
44
%x DIR
 
45
%x COMMENT
 
46
 
 
47
%%
 
48
 
 
49
\r\n|\n {
 
50
    psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\"")); 
 
51
 
 
52
    yyless(0);
 
53
    BEGIN TEXT;
 
54
}
 
55
 
 
56
. {
 
57
    psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\"")); 
 
58
 
 
59
    yyless(0);
 
60
    BEGIN TEXT;
 
61
}
 
62
 
 
63
<TEXT>"<%=" {    /* expression */
 
64
    psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\"\"\",0); req.write(str("));
 
65
    PSP_PG(is_psp_echo) = 1;
 
66
 
 
67
    BEGIN PYCODE;
 
68
}
 
69
 
 
70
<TEXT>"<%" {     /* python code */
 
71
    psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\"\"\",0);")); 
 
72
    CLEAR_WHITESPACE(&PSP_PG(whitespace)); 
 
73
    PSP_PG(seen_newline) = 0;
 
74
    BEGIN PYCODE;
 
75
}
 
76
 
 
77
<TEXT>"<%@" {     /* directive */
 
78
    BEGIN DIR;
 
79
}
 
80
 
 
81
<TEXT>"<%--" {    /* comment */
 
82
    BEGIN COMMENT;
 
83
}
 
84
 
 
85
<TEXT>\r\n|\n {
 
86
    psp_string_appendc(&PSP_PG(pycode), '\n');
 
87
}
 
88
 
 
89
<TEXT>. {
 
90
    if (yytext[0] == '"') {
 
91
        psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\\\""));
 
92
    } else {
 
93
        psp_string_appendc(&PSP_PG(pycode), yytext[0]);
 
94
    }
 
95
}
 
96
 
 
97
<TEXT><<EOF>> {
 
98
    yypop_buffer_state(yyscanner);
 
99
    if (!YY_CURRENT_BUFFER) {
 
100
        /* this is really the end */
 
101
        psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\"\"\",0)\n"));
 
102
        yyterminate();
 
103
    }
 
104
    else {
 
105
        /* we are inside include, continue scanning */
 
106
        BEGIN DIR;
 
107
    }
 
108
}
 
109
 
 
110
<PYCODE>\r\n|\n {
 
111
    psp_string_appendc(&PSP_PG(pycode), '\n');
 
112
        
 
113
    PSP_PG(seen_newline) = 1;
 
114
    BEGIN INDENT;
 
115
}
 
116
 
 
117
<PYCODE>"%>" {
 
118
 
 
119
    if (PSP_PG(is_psp_echo)) {
 
120
        psp_string_appendl(&PSP_PG(pycode), STATIC_STR("),0); req.write(\"\"\""));
 
121
        PSP_PG(is_psp_echo) = 0;
 
122
    } 
 
123
    else {
 
124
        if (!PSP_PG(seen_newline)) {
 
125
            /* this will happen is you have <%%> */
 
126
            psp_string_appendc(&PSP_PG(pycode), ';');
 
127
        }
 
128
 
 
129
        if (PSP_PG(after_colon)) {
 
130
            /* this is dumb mistake-proof measure, if %> 
 
131
               is immediately following where there should be an indent */
 
132
            psp_string_appendc(&PSP_PG(whitespace), '\t');
 
133
            PSP_PG(after_colon) = 0;
 
134
        }
 
135
        OUTPUT_WHITESPACE(&PSP_PG(whitespace));
 
136
        psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\""));
 
137
    }
 
138
    
 
139
    BEGIN TEXT;
 
140
}
 
141
 
 
142
<PYCODE>":" {
 
143
    psp_string_appendc(&PSP_PG(pycode), yytext[0]);
 
144
    PSP_PG(after_colon) = 1;
 
145
}
 
146
 
 
147
<PYCODE>. {
 
148
    psp_string_appendc(&PSP_PG(pycode), yytext[0]);
 
149
    PSP_PG(after_colon) = 0;
 
150
}
 
151
 
 
152
<INDENT>^[\t ]* {
 
153
 
 
154
    CLEAR_WHITESPACE(&PSP_PG(whitespace)); 
 
155
    psp_string_appendl(&PSP_PG(whitespace), yytext, yyleng);
 
156
    psp_string_appendl(&PSP_PG(pycode), yytext, yyleng);
 
157
 
 
158
    BEGIN PYCODE;
 
159
}
 
160
 
 
161
<INDENT>"%>" {
 
162
    yyless(0);
 
163
    BEGIN PYCODE;
 
164
}
 
165
 
 
166
<INDENT>\r\n|\n {
 
167
    CLEAR_WHITESPACE(&PSP_PG(whitespace)); 
 
168
    yyless(0);
 
169
    BEGIN PYCODE;
 
170
}
 
171
 
 
172
<INDENT>. {
 
173
    CLEAR_WHITESPACE(&PSP_PG(whitespace)); 
 
174
    yyless(0);
 
175
    BEGIN PYCODE;
 
176
}
 
177
 
 
178
<DIR>"include"[ ]+"file"[ ]?=[ ]?"\""[^ ]+"\"" {
 
179
 
 
180
    char *filename;
 
181
    char *path;
 
182
    FILE *f;
 
183
 
 
184
    /* find a quote */
 
185
    filename = strchr(yytext, '"') + 1;
 
186
    filename[strchr(filename, '"')-filename] = '\0';
 
187
 
 
188
    /* XXX The absolute path check won't work on Windows,
 
189
     * needs to be corrected
 
190
     */
 
191
 
 
192
    if (PSP_PG(dir) && filename[0] != '/') {
 
193
        path = malloc(strlen(filename)+strlen(PSP_PG(dir))+1);
 
194
        if (path == NULL) {
 
195
            PyErr_NoMemory();
 
196
            yyterminate();
 
197
        }
 
198
        strcpy(path, PSP_PG(dir));
 
199
        strcat(path, filename);
 
200
    }
 
201
    else {
 
202
        path = filename;
 
203
    }
 
204
 
 
205
    Py_BEGIN_ALLOW_THREADS
 
206
    f = fopen(path, "rb");
 
207
    Py_END_ALLOW_THREADS
 
208
    if (f == NULL) {
 
209
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
 
210
    }
 
211
    else {
 
212
        yypush_buffer_state(yy_create_buffer(f, YY_BUF_SIZE, yyscanner), 
 
213
                            yyscanner);
 
214
        BEGIN(TEXT);
 
215
    }
 
216
 
 
217
    if (PSP_PG(dir)) free(path);
 
218
}
 
219
 
 
220
<DIR>"%>" {
 
221
    BEGIN TEXT;
 
222
}
 
223
 
 
224
<COMMENT>"--%>" {
 
225
    BEGIN TEXT;
 
226
}
 
227
 
 
228
%%
 
229
 
 
230
/* this is for emacs
 
231
Local Variables:
 
232
mode:C
 
233
End:
 
234
*/