File: MacroProcessor.cpp

package info (click to toggle)
blahtexml 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,092 kB
  • sloc: cpp: 10,929; xml: 402; python: 302; ansic: 282; makefile: 99
file content (376 lines) | stat: -rw-r--r-- 12,534 bytes parent folder | download
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
blahtex: a TeX to MathML converter designed with MediaWiki in mind
blahtexml: an extension of blahtex with XML processing in mind
http://gva.noekeon.org/blahtexml

Copyright (c) 2006, David Harvey
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the names of the authors nor the names of their affiliation may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <iterator>
#include "MacroProcessor.h"

using namespace std;

namespace blahtex
{

// Implemented in Parser.cpp:
extern bool IsInTokenTables(const wstring& token);

// If the input string ends with "Reserved", this function strips it off.
// All other input is returned unharmed.
//
// The purpose is to convert internal commands like "\textReserved" into
// plain old "\text" for error reporting purposes.
wstring StripReservedSuffix(const wstring& input)
{
    if (input.size() >= 8 &&
        input.substr(input.size() - 8, 8) == L"Reserved"
    )
        return input.substr(0, input.size() - 8);
    else
        return input;
}

MacroProcessor::MacroProcessor(const vector<Token>& input)
{
	for (vector<Token>::const_reverse_iterator iter = input.rbegin(); iter != input.rend(); ++iter)
	{
		mTokens.push_back(*iter);
	}
	
	mBackIndex = mTokens.size() - 1;
	
    mCostIncurred = input.size();
    mIsTokenReady = false;
}

void MacroProcessor::Advance()
{
    if (mBackIndex >= 0)
    {
		mBackIndex--;
        mCostIncurred++;
        mIsTokenReady = false;
    }
}

void MacroProcessor::SkipWhitespace()
{
    while (Peek().getValue() == L" ")
        Advance();
}

void MacroProcessor::SkipWhitespaceRaw()
{
    while (mBackIndex >= 0 && mTokens[mBackIndex].getValue() == L" ")
        Advance();
}

bool MacroProcessor::ReadArgument(vector<wstring>& output)
{
    SkipWhitespaceRaw();
    if (mBackIndex == -1)
        // Missing argument
        return false;

    wstring token = mTokens[mBackIndex].getValue();
    mBackIndex--;
    mCostIncurred++;
    if (token == L"}")
        // Argument can't start with "}"
        return false;

    if (token == L"{")
    {
        // Keep track of brace nesting depth so we know which is the
        // matching closing brace
        int braceDepth = 1;
        while (mBackIndex >= 0)
        {
            mCostIncurred++;
            wstring token = mTokens[mBackIndex].getValue();
            mBackIndex--;
            if (token == L"{")
                braceDepth++;
            else if (token == L"}" && --braceDepth == 0)
                break;
            output.push_back(token);
        }
        if (braceDepth > 0)
            throw Exception(L"UnmatchedOpenBrace");
    }
    else
        output.push_back(token);

    mIsTokenReady = false;
    return true;
}

wstring MacroProcessor::Get()
{
    wstring token = Peek().getValue();
    Advance();
    return token;
}

Token & MacroProcessor::GetToken()
{
	Token &token = Peek();
    Advance();
    return token;
}

void MacroProcessor::HandleNewcommand()
{
    // pop the "\newcommand" command:
    mBackIndex--;
    mCostIncurred++;

    // gobble opening brace
    SkipWhitespaceRaw();
    if (mBackIndex == -1 || mTokens[mBackIndex].getValue() != L"{")
        throw Exception(L"MissingOpenBraceAfter", L"\\newcommand");
    mBackIndex--;

    // grab new command being defined
    SkipWhitespaceRaw();
    if (mBackIndex == -1 ||
        mTokens[mBackIndex].getValue().empty() ||
        mTokens[mBackIndex].getValue()[0] != L'\\'
    )
        throw Exception(L"MissingCommandAfterNewcommand");
    wstring newCommand = mTokens[mBackIndex].getValue();
    if (mMacros.count(newCommand) || IsInTokenTables(newCommand))
        throw Exception(
            L"IllegalRedefinition",
            StripReservedSuffix(newCommand)
        );
    mBackIndex--;

    // gobble close brace
    SkipWhitespaceRaw();
    if (mBackIndex == -1)
        throw Exception(L"UnmatchedOpenBrace");
    if (mTokens[mBackIndex].getValue() != L"}")
        throw Exception(L"MissingCommandAfterNewcommand");
    mBackIndex--;

    Macro& macro = mMacros[newCommand];

    SkipWhitespaceRaw();
    // Determine the number of arguments, if specified.
    if (mBackIndex >= 0 && mTokens[mBackIndex].getValue() == L"[")
    {
        mBackIndex--;

        SkipWhitespaceRaw();
        if (mBackIndex == -1 || mTokens[mBackIndex].getValue().size() != 1)
            throw Exception(L"MissingOrIllegalParameterCount", newCommand);
        macro.mParameterCount = static_cast<int>(mTokens[mBackIndex].getValue()[0] - L'0');
        if (macro.mParameterCount <= 0 || macro.mParameterCount > 9)
            throw Exception(L"MissingOrIllegalParameterCount", newCommand);
        mBackIndex--;

        SkipWhitespaceRaw();
        if (mBackIndex == -1 || mTokens[mBackIndex].getValue() != L"]")
            throw Exception(L"UnmatchedOpenBracket");
        mBackIndex--;
    }

    // Read and store the tokens which make up the macro replacement.
    if (!ReadArgument(macro.mReplacement))
        throw Exception(L"NotEnoughArguments", L"\\newcommand");
}

Token & MacroProcessor::Peek()
{
    while (mBackIndex >= 0)
    {
        // This is the only place that we check that the user hasn't
        // exceeded the token limit.
        if ((mBackIndex + 1) + (++mCostIncurred) >= cMaxParseCost)
            throw Exception(L"TooManyTokens");

        if (mIsTokenReady)
            return mTokens[mBackIndex];

        // "\sqrt" needs special handling due to its optional argument.
        // Something like "\sqrtReserved{x}" gets converted to "\sqrt{x}".
        // Something like "\sqrtReserved[y]{x}" gets converted to
        // "\rootReserved{y}{x}".
        //
        // (Blahtex doesn't handle grouping of [...] the same way as texvc;
        // it does it the TeX way. For example, "\sqrt[\sqrt[2]{3}]{4}"
        // generates an error, whereas it is valid in texvc.)
        //
        // We need to take into account grouping braces,
        // e.g. "\sqrt[{]}]{2}" should be valid.
        if (mTokens[mBackIndex].getValue() == L"\\sqrtReserved")
        {
            mBackIndex--;

            SkipWhitespaceRaw();
            if (mBackIndex >= 0 && mTokens[mBackIndex].getValue() == L"[")
            {
                mTokens[mBackIndex].setValue(L"{");

#ifndef _MSC_VER
#warning TODO: test this
#endif
				vector<Token>::iterator ptr = mTokens.begin() + mBackIndex + 1;

                int braceDepth = 0;
                while (ptr != mTokens.begin() &&
                    (braceDepth > 0 || (*ptr).getValue() != L"]")
                )
                {
                    mCostIncurred++;
                    if ((*ptr).getValue() == L"{")
                        braceDepth++;
                    else if ((*ptr).getValue() == L"}")
                    {
                        if (--braceDepth < 0)
                            throw Exception(L"UnmatchedCloseBrace");
                    }
                    ptr--;
                }
                if (ptr == mTokens.begin())
                    throw Exception(L"UnmatchedOpenBracket");
                if ((*ptr).getValue() != L"]")
                    throw Exception(L"NotEnoughArguments", L"\\sqrt");
                (*ptr).setValue(L"}");
				
#ifndef _MSC_VER
#warning TODO: actually store the token range and test this
#endif
				mTokens.insert(mTokens.begin() + (mBackIndex + 1), Token(L"\\rootReserved", 0, 0));
				mBackIndex++;
                mIsTokenReady = true;
				
				return mTokens[mBackIndex];
            }
            else
            {
#ifndef _MSC_VER
#warning TODO: actually store the token range and test this
#endif
				mTokens.insert(mTokens.begin() + (mBackIndex + 1), Token(L"\\sqrt", 0, 0));
				mBackIndex++;
                mIsTokenReady = true;
				
				return mTokens[mBackIndex];
            }
        }
        else
        {
            wstring token = mTokens[mBackIndex].getValue();
            wishful_hash_map<wstring, Macro>::const_iterator
                macroPtr = mMacros.find(token);
            if (macroPtr == mMacros.end())
            {
                // In this case it's not "\sqrt" and not a macro, so
                // we're finished here.
                mIsTokenReady = true;
                return mTokens[mBackIndex];
            }

            const Macro& macro = macroPtr->second;
            mBackIndex--;

            // It's a macro. Determines the arguments to substitute in....
            vector<vector<wstring> > arguments(macro.mParameterCount);
            for (int argumentIndex = 0;
                argumentIndex < macro.mParameterCount;
                argumentIndex++
            )
                if (!ReadArgument(arguments[argumentIndex]))
                    throw Exception(
                        L"NotEnoughArguments",
                        StripReservedSuffix(token)
                    );

            // ... and now write the replacement, substituting
            // arguments as we go.
            const vector<wstring>& replacement = macro.mReplacement;
            vector<wstring> output;
            for (vector<wstring>::const_iterator
                source = replacement.begin();
                source != replacement.end();
                source++
            )
            {
                mCostIncurred++;
                if (*source == L"#")
                {
                    if (++source == replacement.end() ||
                        source->size() != 1
                    )
                        throw Exception(
                            L"MissingOrIllegalParameterIndex",
                            token
                        );

                    int parameterIndex
                        = static_cast<int>((*source)[0] - '1');

                    // FIX: perhaps this next error should be flagged when
                    // reading the definition of the macro rather than
                    // during macro expansion
                    if (parameterIndex < 0 ||
                        parameterIndex >= macro.mParameterCount
                    )
                        throw Exception(
                            L"MissingOrIllegalParameterIndex",
                            token
                        );
                    copy(
                        arguments[parameterIndex].begin(),
                        arguments[parameterIndex].end(),
                        back_inserter(output)
                    );
                    mCostIncurred += arguments[parameterIndex].size();
                }
                else
                    output.push_back(*source);
            }
			
#ifndef _MSC_VER
#warning TODO: include token ranges
#endif
			for(vector<wstring>::reverse_iterator iter = output.rbegin(); iter != output.rend(); ++iter)
			{
				mTokens.insert(mTokens.begin() + mBackIndex + 1, Token(*iter, 0, 0));
				mBackIndex++;
			}
			
            mCostIncurred += output.size();
        }
    }
	
	return EmptyToken;
}

Token * MacroProcessor::FindLastInstanceOfToken(const std::wstring & tokenString)
{
	for (long n = mBackIndex+1;  n < mTokens.size(); n++)
	{
		if (mTokens[n].getValue() == tokenString)
			return &mTokens[n];
	}
	
	return NULL;
}

}

// end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@