Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mwlib/_expander.re
blob: 7abb2ac0f09a8a101cc29bb0f3d18edd6ced596c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// -*- mode: c++ -*-
// Copyright (c) 2007-2008 PediaPress GmbH
// See README.txt for additional licensing information.

#include <Python.h>

#include <iostream>
#include <assert.h>
#include <vector>

using namespace std;

#define RET(x) {found(x); return x;}

struct Token
{
	int type;
	int start;
	int len;
};


class MacroScanner
{
public:

	MacroScanner(Py_UNICODE *_start, Py_UNICODE *_end) {
		source = start = _start;
		end = _end;
		cursor = start;
	}

	int found(int val) {
		if (val==5 && tokens.size()) {
			Token &previous_token (tokens[tokens.size()-1]);
			if (previous_token.type==val) {
				previous_token.len += cursor-start;
				return tokens.size()-1;
			}
		}
		Token t;
		t.type = val;
		t.start = (start-source);
		t.len = cursor-start;			
		tokens.push_back(t);
		return tokens.size()-1;
	}

	inline int scan();

	Py_UNICODE *source;

	Py_UNICODE *start;
	Py_UNICODE *cursor;
	Py_UNICODE *end;
	vector<Token> tokens;
};


int MacroScanner::scan()
{

std:

	start=cursor;
	
	Py_UNICODE *marker=cursor;

	Py_UNICODE *save_cursor = cursor;


#define YYCTYPE         Py_UNICODE
#define YYCURSOR        cursor
#define YYMARKER	marker
#define YYLIMIT   (end)
// #define YYFILL(n) return 0;

/*!re2c
re2c:yyfill:enable = 0 ;
*/



/*!re2c
  "{"{2,}          {RET(1);}
  "}"{2,}          {RET(2);}
  "[[" | "]]"      {RET(3);}
  "|"              {RET(6);}

  '<noinclude>'    {goto noinclude;}
  '<nowiki>'       {goto nowiki;}
  '<imagemap' [^<>\000]* '>' {goto imagemap;}
  '<math' [^<>\000]* '>' {goto math;}
  '<gallery' [^<>\000]* '>' {goto gallery;}
 
  "<!--[^\000<>]*-->" {RET(5);}

  "\000" {RET(0);}
  [^\000] {RET(5);}

 */



noinclude:
/*!re2c
	'</noinclude>' {goto std;}
	[^\000] {goto noinclude;}
	"\000" {cursor=start+11; RET(5);}
 */

nowiki:
/*!re2c
	'</nowiki>' {RET(5);}
	[^\000] {goto nowiki;}
	"\000" {RET(0);}
 */

math:
/*!re2c
	'</math>' {RET(5);}
	[^\000] {goto math;}
	"\000" {RET(0);}
 */

gallery:
/*!re2c
	'</gallery>' {RET(5);}
	[^\000] {goto gallery;}
	"\000" {RET(0);}
 */

imagemap:
/*!re2c
	'</imagemap>' {RET(5);}
	[^\000] {goto imagemap;}
	"\000" {RET(0);}
 */

pre:
/*!re2c
	'</pre>' {RET(5);}
	[^\000] {goto pre;}
	"\000" {RET(0);}
 */
	
}


PyObject *py_scan(PyObject *self, PyObject *args) 
{
	PyObject *arg1;
	if (!PyArg_ParseTuple(args, "O:_expander.scan", &arg1)) {
		return 0;
	}
	PyUnicodeObject *unistr = (PyUnicodeObject*)PyUnicode_FromObject(arg1);
	if (unistr == NULL) {
		PyErr_SetString(PyExc_TypeError,
				"parameter cannot be converted to unicode in _expander.scan");
		return 0;
	}

	Py_UNICODE *start = unistr->str;
	Py_UNICODE *end = start+unistr->length;
	

	MacroScanner scanner (start, end);
	Py_BEGIN_ALLOW_THREADS
	while (scanner.scan()) {
	}
	Py_END_ALLOW_THREADS
	Py_XDECREF(unistr);
	
	// return PyList_New(0); // uncomment to see timings for scanning

	int size = scanner.tokens.size();
	PyObject *result = PyList_New(size);
	if (!result) {
		return 0;
	}
	
	for (int i=0; i<size; i++) {
		Token t = scanner.tokens[i];
		PyList_SET_ITEM(result, i, Py_BuildValue("iii", t.type, t.start, t.len));
	}
	
	return result;
}



static PyMethodDef module_functions[] = {
	{"scan", (PyCFunction)py_scan, METH_VARARGS, "scan(text)"},
	{0, 0},
};



extern "C" {
	DL_EXPORT(void) init_expander();
}

DL_EXPORT(void) init_expander()
{
	/*PyObject *m =*/ Py_InitModule("_expander", module_functions);
}