示例#1
0
void QsCodeParser::applyReplacementList(QString& source, const Doc& doc)
{
    QStringList args = doc.metaCommandArgs(COMMAND_REPLACE);
    QStringList::ConstIterator a = args.begin();
    while (a != args.end()) {
	if (replaceRegExp.exactMatch(*a)) {
	    QRegExp before(replaceRegExp.cap(1));
	    before.setMinimal(true);
	    QString after = replaceRegExp.cap(2);

	    if (before.isValid()) {
		int oldLen = source.size();
		source.replace(before, after);

		// this condition is sufficient but not necessary
		if (oldLen == source.size() && !source.contains(after))
		    doc.location().warning(
			    tr("Regular expression '%1' did not match anything")
			    .arg(before.pattern()));
	    }
            else {
		doc.location().warning(
			tr("Invalid regular expression '%1'")
			.arg(before.pattern()));
	    }
	}
        else {
	    doc.location().warning(tr("Bad syntax in '\\%1'")
				    .arg(COMMAND_REPLACE));
	}
	++a;
    }

    QRegExp codeRegExp("\\\\" + COMMAND_CODE + "(.*)\\\\" + COMMAND_ENDCODE);
    codeRegExp.setMinimal(true);

    QRegExp quickcodeRegExp(
	    "\\\\" + COMMAND_QUICKCODE + "(.*)\\\\" + COMMAND_ENDQUICKCODE);
    quickcodeRegExp.setMinimal(true);

    int quickcodePos = doc.source().indexOf(quickcodeRegExp);
    if (quickcodePos != -1) {
	int codePos = source.indexOf(codeRegExp);
	if (codePos == -1) {
	    doc.location().warning(
		    tr("Cannot find any '\\%1' snippet corresponding to '\\%2'")
		    .arg(COMMAND_CODE).arg(COMMAND_QUICKCODE));
	}
        else {
	    source.replace(codeRegExp.pos(1), codeRegExp.cap(1).length(),
			    quickcodeRegExp.cap(1));
	    codePos = codeRegExp.pos(1) + quickcodeRegExp.cap(1).length();

	    if (doc.source().indexOf(quickcodeRegExp, quickcodePos + 1) != -1) {
		doc.location().warning(
			tr("Cannot use '\\%1' twice in a row")
			.arg(COMMAND_QUICKCODE));
	    }
            else if (source.indexOf(codeRegExp, codePos + 1) != -1) {
		doc.location().warning(tr("Ambiguous '\\%1'")
					.arg(COMMAND_QUICKCODE));
	    }
	}
    }
}