3
* Copyright 2004 Apache Software Foundation
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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
* $Id: psp_parser.l,v 1.20 2004/02/16 19:47:27 grisha Exp $
19
* This file originally written by Sterling Hughes.
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!
29
#include "psp_parser.h"
31
#define OUTPUT_WHITESPACE(__wsstring) \
32
psp_string_0((__wsstring)); \
33
psp_string_append(&PSP_PG(pycode), (__wsstring)->blob)
35
#define CLEAR_WHITESPACE(__wsstring) psp_string_clear((__wsstring));
39
%option noyywrap nounistd
50
psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\""));
57
psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\""));
63
<TEXT>"<%=" { /* expression */
64
psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\"\"\",0); req.write(str("));
65
PSP_PG(is_psp_echo) = 1;
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;
77
<TEXT>"<%@" { /* directive */
81
<TEXT>"<%--" { /* comment */
86
psp_string_appendc(&PSP_PG(pycode), '\n');
90
if (yytext[0] == '"') {
91
psp_string_appendl(&PSP_PG(pycode), STATIC_STR("\\\""));
93
psp_string_appendc(&PSP_PG(pycode), yytext[0]);
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"));
105
/* we are inside include, continue scanning */
111
psp_string_appendc(&PSP_PG(pycode), '\n');
113
PSP_PG(seen_newline) = 1;
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;
124
if (!PSP_PG(seen_newline)) {
125
/* this will happen is you have <%%> */
126
psp_string_appendc(&PSP_PG(pycode), ';');
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;
135
OUTPUT_WHITESPACE(&PSP_PG(whitespace));
136
psp_string_appendl(&PSP_PG(pycode), STATIC_STR("req.write(\"\"\""));
143
psp_string_appendc(&PSP_PG(pycode), yytext[0]);
144
PSP_PG(after_colon) = 1;
148
psp_string_appendc(&PSP_PG(pycode), yytext[0]);
149
PSP_PG(after_colon) = 0;
154
CLEAR_WHITESPACE(&PSP_PG(whitespace));
155
psp_string_appendl(&PSP_PG(whitespace), yytext, yyleng);
156
psp_string_appendl(&PSP_PG(pycode), yytext, yyleng);
167
CLEAR_WHITESPACE(&PSP_PG(whitespace));
173
CLEAR_WHITESPACE(&PSP_PG(whitespace));
178
<DIR>"include"[ ]+"file"[ ]?=[ ]?"\""[^ ]+"\"" {
185
filename = strchr(yytext, '"') + 1;
186
filename[strchr(filename, '"')-filename] = '\0';
188
/* XXX The absolute path check won't work on Windows,
189
* needs to be corrected
192
if (PSP_PG(dir) && filename[0] != '/') {
193
path = malloc(strlen(filename)+strlen(PSP_PG(dir))+1);
198
strcpy(path, PSP_PG(dir));
199
strcat(path, filename);
205
Py_BEGIN_ALLOW_THREADS
206
f = fopen(path, "rb");
209
PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
212
yypush_buffer_state(yy_create_buffer(f, YY_BUF_SIZE, yyscanner),
217
if (PSP_PG(dir)) free(path);