2
* Copyright 2004 Apache Software Foundation
4
* Licensed under the Apache License, Version 2.0 (the "License"); you
5
* may not use this file except in compliance with the License. You
6
* may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
* implied. See the License for the specific language governing
14
* permissions and limitations under the License.
16
* This file originally written by Stering Hughes
18
* $Id: _pspmodule.c,v 1.7 2004/02/16 19:47:27 grisha Exp $
20
* See accompanying documentation and source code comments for
26
#include "psp_parser.h"
27
#include "psp_string.h"
28
#include "_pspmodule.h"
31
/* calm down compile warning from psp_flex.h*/
32
static int yy_init_globals (yyscan_t yyscanner ) {return 0;};
34
static psp_parser_t *psp_parser_init(void)
38
parser = (psp_parser_t *) malloc(sizeof(*parser));
40
memset(&parser->pycode, 0, sizeof(psp_string));
41
memset(&parser->whitespace, 0, sizeof(psp_string));
43
parser->is_psp_echo = 0;
44
parser->after_colon = 0;
45
parser->seen_newline = 0;
50
static void psp_parser_cleanup(psp_parser_t *parser)
52
if (parser->pycode.allocated) {
53
free(parser->pycode.blob);
56
if (parser->whitespace.allocated) {
57
free(parser->whitespace.blob);
63
static PyObject * _psp_module_parse(PyObject *self, PyObject *argv)
73
if (!PyArg_ParseTuple(argv, "s|s", &filename, &dir)) {
78
path = malloc(strlen(filename)+strlen(dir)+1);
80
return PyErr_NoMemory();
82
strcat(path, filename);
88
Py_BEGIN_ALLOW_THREADS
89
f = fopen(path, "rb");
93
PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
99
parser = psp_parser_init();
103
yylex_init(&scanner);
104
yyset_in(f, scanner);
105
yyset_extra(parser, scanner);
107
yylex_destroy(scanner);
110
psp_string_0(&parser->pycode);
112
if (PyErr_Occurred()) {
113
psp_parser_cleanup(parser);
117
if (parser->pycode.blob) {
118
code = PyString_FromString(parser->pycode.blob);
121
code = PyString_FromString("");
124
psp_parser_cleanup(parser);
129
static PyObject * _psp_module_parsestring(PyObject *self, PyObject *argv)
134
psp_parser_t *parser;
137
if (!PyArg_ParseTuple(argv, "S", &str)) {
141
Py_BEGIN_ALLOW_THREADS
142
parser = psp_parser_init();
143
yylex_init(&scanner);
144
yyset_extra(parser, scanner);
146
bs = yy_scan_string(PyString_AsString(str), scanner);
149
yy_delete_buffer(bs, scanner);
150
yylex_destroy(scanner);
152
psp_string_0(&parser->pycode);
155
code = PyString_FromString(parser->pycode.blob);
157
psp_parser_cleanup(parser);
162
struct PyMethodDef _psp_module_methods[] = {
163
{"parse", (PyCFunction) _psp_module_parse, METH_VARARGS},
164
{"parsestring", (PyCFunction) _psp_module_parsestring, METH_VARARGS},
170
Py_InitModule("_psp", _psp_module_methods);