From 1a41c9661e837e2463c492693d4526df8cc0538c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Alvarez?= Date: Mon, 11 Apr 2011 18:43:40 -0300 Subject: Add support for default values in variable substitutions. If there is no variable 'foo' set, ${foo} exits with a fatal error. With this patch, you can use ${foo|sometext}, which will substitute to 'sometext' if the variable 'foo' isn't set. The default text may be empty. --- src/ruleparser.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ruleparser.cpp b/src/ruleparser.cpp index f4c7387..eb309bf 100644 --- a/src/ruleparser.cpp +++ b/src/ruleparser.cpp @@ -98,7 +98,7 @@ void Rules::load(const QString &filename) QRegExp matchAnnotateLine("annotated\\s+(\\S+)", Qt::CaseInsensitive); QRegExp matchPrefixLine("prefix\\s+(\\S+)", Qt::CaseInsensitive); QRegExp declareLine("declare\\s+("+varRegex+")\\s*=\\s*(\\S+)", Qt::CaseInsensitive); - QRegExp variableLine("\\$\\{("+varRegex+")\\}", Qt::CaseInsensitive); + QRegExp variableLine("\\$\\{("+varRegex+")(\\|[^}$]*)?\\}", Qt::CaseInsensitive); QRegExp includeLine("include\\s+(.*)", Qt::CaseInsensitive); enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone; @@ -133,9 +133,17 @@ void Rules::load(const QString &filename) load(includeFile); } else { while( variableLine.indexIn(line) != -1 ) { - if (!m_variables.contains(variableLine.cap(1))) - qFatal("Undeclared variable: %s", qPrintable(variableLine.cap(1))); - line = line.replace(variableLine, m_variables[variableLine.cap(1)]); + QString replacement; + if (m_variables.contains(variableLine.cap(1))) { + replacement = m_variables[variableLine.cap(1)]; + } else { + if (variableLine.cap(2).startsWith('|')) { + replacement = variableLine.cap(2).mid(1); + } else { + qFatal("Undeclared variable: %s", qPrintable(variableLine.cap(1))); + } + } + line = line.replace(variableLine, replacement); } if (state == ReadingRepository) { if (matchBranchLine.exactMatch(line)) { -- cgit v1.2.1