~ubuntu-branches/ubuntu/lucid/structure-synth/lucid

« back to all changes in this revision

Viewing changes to StructureSynth/Parser/Preprocessor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2009-04-13 13:28:45 UTC
  • Revision ID: james.westby@ubuntu.com-20090413132845-d7d42t4llxjxq0ez
Tags: upstream-0.9
ImportĀ upstreamĀ versionĀ 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Preprocessor.h"
 
2
 
 
3
#include <QStringList>
 
4
#include <QRegExp>
 
5
#include <QMap>
 
6
 
 
7
#include "../../SyntopiaCore/Exceptions/Exception.h"
 
8
#include "../../SyntopiaCore/Logging/Logging.h"
 
9
 
 
10
using namespace SyntopiaCore::Exceptions;
 
11
using namespace SyntopiaCore::Logging;
 
12
 
 
13
 
 
14
namespace StructureSynth {
 
15
        namespace Parser {      
 
16
 
 
17
                QString Preprocessor::Process(QString input) {
 
18
                        QStringList in = input.split(QRegExp("\r\n|\r|\n"));
 
19
 
 
20
                        QMap<QString, QString> substitutions;
 
21
                        QRegExp ppCommand("^#"); // Look for #define varname value
 
22
                        QRegExp defineCommand("^#define\\s([^\\s]+)\\s(.*)*$"); // Look for #define varname value
 
23
 
 
24
 
 
25
                        for (QStringList::iterator it = in.begin(); it != in.end(); ++it) {
 
26
 
 
27
                                if (ppCommand.indexIn(*it) != -1) {
 
28
                                        // Preprocessor command
 
29
                                        if (defineCommand.indexIn(*it) != -1) {
 
30
                                                //INFO(QString("Found ppC (%1)->(%2): ").arg(defineCommand.cap(1)).arg(defineCommand.cap(2)) + *it);
 
31
                                                substitutions[defineCommand.cap(1)] = defineCommand.cap(2);
 
32
                                        } else {
 
33
                                                WARNING("Could not understand preprocessor command: " + *it);
 
34
                                        }
 
35
                                } else {
 
36
                                        // Non-preprocessor command
 
37
                                        // Check for substitutions.
 
38
                                        QMap<QString, QString>::const_iterator it2 = substitutions.constBegin();
 
39
                                        int subst = 0;
 
40
                                        while (it2 != substitutions.constEnd()) {
 
41
                                                if (subst>100) {
 
42
                                                        WARNING("More than 100 recursive preprocessor substitutions... breaking.");
 
43
                                                        break;
 
44
                                                }
 
45
                                                if ((*it).contains(it2.key())) {
 
46
                                                        (*it).replace(it2.key(), it2.value());
 
47
                                                        it2 = substitutions.constBegin();
 
48
                                                        subst++;
 
49
                                                } else {
 
50
                                                        it2++;
 
51
                                                }
 
52
                                    }
 
53
                                }
 
54
                        }
 
55
 
 
56
                        QStringList out = in;
 
57
                        return out.join("\r\n");
 
58
                }
 
59
        }
 
60
}
 
61