Ejemplo n.º 1
0
void Filter::load(const char* filePath, int startPos) {

	std::ifstream keywordsFile(filePath, std::ios::in | std::ios::binary | std::ios::ate);

	if (keywordsFile.is_open()) {
		std::ifstream::pos_type ___totalSize = keywordsFile.tellg();
		keywordsFile.seekg(0, std::ios::beg);
		char header[startPos];// header
		keywordsFile.read(header, startPos); // header

		char buffer[256];
		int ___count = 0;
		int ___offset = 0;

		while (___count < ___totalSize) {
			keywordsFile.read(buffer + ___offset, 2);
			___count += 2;
			___offset += 2;

			if (buffer[___offset - 4] == '\x0D' && buffer[___offset - 3] == '\x00'
					&& buffer[___offset - 2] == '\x0A' && buffer[___offset - 1] == '\x00') {
				___offset -= 4;
				std::string ___utf16Word;
				___utf16Word.assign(buffer, ___offset);
				std::cout << "count = " << ___count << std::endl;
				std::cout << Logger::toHex(___utf16Word, true) << std::endl;
				__trie.insert(___utf16Word);
				___offset = 0;
			}
		}
	}

	keywordsFile.close();
}
Ejemplo n.º 2
0
void LexerArduino::loadKeywordsFile(const QString &fileName)
{
    QFile keywordsFile(fileName);
    keywordsFile.open(QFile::ReadOnly);
    QString line;
    QStringList pieces;
    QStringList k[3];
    while (! keywordsFile.atEnd())
    {
        line = QString::fromLocal8Bit(keywordsFile.readLine()).trimmed();
        if (line.isEmpty() || line[0] == '#')
            continue;
        pieces = line.split(QRegExp("\\s"), QString::SkipEmptyParts);
        if (pieces.size() < 2)
            continue;
        if (pieces[1] == "LITERAL1" || pieces[1] == "KEYWORD1")
            k[0] << pieces[0];
        else if (pieces[1] == "KEYWORD2")
            k[1] << pieces[0];
        else if (pieces[1] == "KEYWORD3")
            k[2] << pieces[0];
    }

    for (int i = 0; i < 3; i++)
    {
        if (! mKeywords[i].empty())
            mKeywords[i] += ' ';
        mKeywords[i] += k[i].join(" ").toStdString();
    }
}
Ejemplo n.º 3
0
void EBNFParser::registerTargetScannerKeywordsFromFile(const QString &filename, QStringList &collection)
{
    QFile keywordsFile(QString("/home/rovshan/Projects/Qt/OraExp/misc/%1").arg(filename));
    bool opened=keywordsFile.open(QIODevice::ReadOnly);
    Q_ASSERT(opened);
    QTextStream in(&keywordsFile);
    while (!in.atEnd()) {
        QString line = in.readLine().trimmed();
        if(!line.isEmpty()){
            if(!collection.contains(line)){
                collection.append(line);
            }
        }
    }
}
Ejemplo n.º 4
0
SqlHighlighter::SqlHighlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    HighlightningRule rule;

    // Set the formats
    keywordFormat.          setForeground(Qt::blue);
    functionFormat.         setForeground(Qt::darkCyan);
    numberFormat.           setForeground(Qt::red);
    singleLineCommentFormat.setForeground(Qt::darkRed);
    multiLineCommentFormat. setForeground(Qt::darkMagenta);
    quotationFormat.        setForeground(Qt::darkYellow);
    stringFormat.           setForeground(Qt::darkGreen);

    // Keywords pattern. Load keywords from resource file.
    QFile keywordsFile(":/main/highlighter/sql_keywords.txt");
    rule.format  = keywordFormat;

    if (!keywordsFile.open(QIODevice::ReadOnly))
    {
        std::cerr << "Could not open keywords file!" << std::endl;
        exit(1);
    }

    while (!keywordsFile.atEnd())
    {
        QString line = keywordsFile.readLine().replace("\n", "");
        // We should totally ignore empty lines, dude!
        if (line.length() > 0)
        {
            QString keyword = QString("\\b%1\\b").arg(line);
            rule.pattern = QRegExp(keyword, Qt::CaseInsensitive, QRegExp::RegExp2);
            highlightningRules.append(rule);
        }
    }
    keywordsFile.close();

    // Functions pattern. Load functions from resource file.
    QFile functionsFile(":/main/highlighter/sql_functions.txt");
    rule.format  = functionFormat;

    if (!functionsFile.open(QIODevice::ReadOnly))
    {
        std::cerr << "Could not open functions file!" << std::endl;
        exit(1);
    }

    while (!functionsFile.atEnd())
    {
        QString line = functionsFile.readLine().replace("\n", "");
        // We should totally ignore empty lines, dude!
        if (line.length() > 0)
        {
            QString function = QString("\\b%1\\b").arg(line);
            rule.pattern = QRegExp(function, Qt::CaseInsensitive, QRegExp::RegExp2);
            highlightningRules.append(rule);
        }
    }
    functionsFile.close();

    // Quotation pattern
    QString quotationPattern("\\b`[a-z]*`\\b");
    rule.pattern = QRegExp(quotationPattern, Qt::CaseInsensitive, QRegExp::RegExp2);
    rule.format  = quotationFormat;
    highlightningRules.append(rule);

    // Number pattern
    QString numberPattern("\\b((-)?\\d+(\\.\\d+)?)\\b");
    rule.pattern = QRegExp(numberPattern, Qt::CaseInsensitive, QRegExp::RegExp2);
    rule.format  = numberFormat;
    highlightningRules.append(rule);

    // Line comments pattern
    rule.pattern = QRegExp("^--.*", Qt::CaseInsensitive, QRegExp::RegExp2);
    rule.format  = singleLineCommentFormat;
    highlightningRules.append(rule);

    /*
    Here is a complicated regexp, which is used for detecting quoted strings.
    SQLite uses Pascal-quoting, which is double-char instead of backslash-char like this:
      C-Style:      "A \"Quoted\" String"
      Pascal-style: "A ""Quoted"" String"
    The regexp works by first looking for 0-* chars that is not a quote, and then looking for
    a double-quote and anything else after that, repeated. Here's a bit more visual:
          "  A    ""   Quoted  ""   String  "
          ^-----^ ^----------^ ^-----------^
             1st       2nd         3rd
    The first part is just like a normal string. If the string starts with a double-qoute, this
    will be empty. 2nd and 3rd uses the same part of the regexp, and starts with a double-quote
    and continues on to the first quote or to end-of-string.

    SQLite also supports so called BLOB literals, which is just like a string but with a "x" in
    front.

    Finished regexp:
       x?"([^"]*(""[^"]*)*)("|$)

       x?"(               # Opening quote. Support BLOB literal
           [^"]*          # 1st part
           (""[^"]*)*     # 2nd and 3rd part. Double-quote and then a normal repeat.
       )("|$)             # Closing quote or end of string

    Now, SQLite uses this for both single and double quoted strings, so we need to reuse it a
    bit. We therefore replace the quote with a placeholder (%1). We also need to quote some chars
    for the C++ string literal.
    */
    QString quotedString("%1([^%1]*(%1%1[^%1]*)*)(%1|$)");

    // String pattern. Double quotes and single quotes
    rule.format  = stringFormat;
    rule.pattern = QRegExp(quotedString.arg('"'), Qt::CaseInsensitive, QRegExp::RegExp2);
    highlightningRules.append(rule);
    rule.pattern = QRegExp(quotedString.arg('\''), Qt::CaseInsensitive, QRegExp::RegExp2);
    highlightningRules.append(rule);

    // Multi-Line comments pattern
    commentStartExpression = QRegExp("/\\*", Qt::CaseInsensitive, QRegExp::RegExp2);
    commentEndExpression   = QRegExp("\\*/", Qt::CaseInsensitive, QRegExp::RegExp2);
}