如何在我的Qt应用程序中简单地解析类似(!)文件的CSS?

Ral*_*kum 8 css qt html-parsing qt5 qt5.4

我有一个*.css(层叠样式表)格式的文档,但它有自己的关键字.实际上它是一个个性化的CSS(我称之为*.pss),具有自己的标签和属性.我在这里摘录:

/* CSS like style sheet file *.pss */

@include "otherStyleSheet.pss";

/* comment */
[propertyID="1230000"] { 
  fillColor : #f3f1ed;
  minSize : 5;
  lineWidth : 3;
}

/* sphere */
[propertyID="124???|123000"] { 
  lineType : dotted;
}

/* square */
[propertyID="125???"] {
  lineType : thinline;    
}

/* ring */
[propertyID="133???"] {
  lineType : thickline; 
  [hasInnerRing=true] {
    innerLineType : thinline;
  }  
}
Run Code Online (Sandbox Code Playgroud)

我想很容易地解析它,Qt已经有了一些可以使用的东西吗?什么是最简单的方法?

由于*.css有自己的关键字,我不会在CSS解析器中使用.

解析*.pss后我的进一步意图是将其属性存储在Model结构中.

Rei*_*ica 10

Qt内部没有任何公开内容.您当然可以自由使用Qt的私有CSS解析器 - 您可以复制它并进行修改以满足您的需求.

qtbase/src/gui/text/qcssparser_p.h,in qtbase/src/gui/text.

好消息是,对于上面显示的示例,修改将非常小.Qt的CSS解析器已经支持@import,所以我们只有一些语法是嵌套的选择器语法.如果没有该语法,您可以QCss::Parser按原样使用.解析器是以灵活的方式编写的,您无需担心正式的CSS关键字:它仍然允许您访问所有声明,无论它们是否从正式的CSS观点来看都是有意义的.

迭代解析树就像它得到的一样简单:

int main() {
   QCss::Parser parser(pss);
   QCss::StyleSheet styleSheet;
   if (!parser.parse(&styleSheet))
      return 1;
   for (auto rule : styleSheet.styleRules) {
      qDebug() << "** Rule **";
      for (auto sel : rule.selectors) {
        for (auto bSel : sel.basicSelectors)
           qDebug() << bSel;
      }
      for (auto decl : rule.declarations)
         qDebug() << decl;
   }
}
Run Code Online (Sandbox Code Playgroud)

输出是我们所期望的:

** Rule **
BasicSelector "propertyID"="1230000"
Declaration "fillColor" = '#f3f1ed' % QColor(ARGB 1, 0.952941, 0.945098, 0.929412)
Declaration "minSize" = '5' % 5
Declaration "lineWidth" = '3'
** Rule **
BasicSelector "propertyID"="124???|123000"
Declaration "lineType" = 'dotted'
** Rule **
BasicSelector "propertyID"="125???"
Declaration "lineType" = 'thinline'
** Rule **
BasicSelector "propertyID"="133???"
Declaration "lineType" = 'thickline'
Run Code Online (Sandbox Code Playgroud)

我们必须QCss自己为类实现调试流操作符:

QDebug operator<<(QDebug dbg, const QCss::AttributeSelector & sel) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "\"" << sel.name << "\"";
   switch (sel.valueMatchCriterium) {
   case QCss::AttributeSelector::MatchEqual:
      dbg << "="; break;
   case QCss::AttributeSelector::MatchContains:
      dbg << "~="; break;
   case QCss::AttributeSelector::MatchBeginsWith:
      dbg << "^="; break;
   case QCss::AttributeSelector::NoMatch:
      break;
   }
   if (sel.valueMatchCriterium != QCss::AttributeSelector::NoMatch && !sel.value.isEmpty())
      dbg << "\"" << sel.value << "\"";
   return dbg;
}

QDebug operator<<(QDebug dbg, const QCss::BasicSelector & sel) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "BasicSelector";
   if (!sel.elementName.isEmpty())
      dbg << " #" << sel.elementName;
   for (auto & id : sel.ids)
      dbg << " id:" << id;
   for (auto & aSel : sel.attributeSelectors)
      dbg << " " << aSel;
   return dbg;
}
Run Code Online (Sandbox Code Playgroud)

遍历声明时,QCss::parser已经为我们解释了一些标准值,例如颜色,整数等.

QDebug operator<<(QDebug dbg, const QCss::Declaration & decl) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "Declaration";
   dbg << " \"" << decl.d->property << "\" = ";
   bool first = true;
   for (auto value : decl.d->values) {
      if (!first) dbg << ", ";
      dbg << "\'" << value.toString() << "\'";
      first = false;
   }
   if (decl.d->property == "fillColor")
      dbg << " % " << decl.colorValue();
   else if (decl.d->property == "minSize") {
      int i;
      if (decl.intValue(&i)) dbg << " % " << i;
   }
   return dbg;
}
Run Code Online (Sandbox Code Playgroud)

最后,样板和要解析的样式表:

// https://github.com/KubaO/stackoverflown/tree/master/questions/css-like-parser-31583622
#include <QtGui>
#include <private/qcssparser_p.h>

const char pss[] =
  "/* @include \"otherStyleSheet.pss\"; */ \
  [propertyID=\"1230000\"] {  \
    fillColor : #f3f1ed; \
    minSize : 5; \
    lineWidth : 3; \
  } \
   \
  /* sphere */ \
  [propertyID=\"124???|123000\"] {  \
    lineType : dotted; \
  } \
   \
  /* square */ \
  [propertyID=\"125???\"] { \
    lineType : thinline; \
  } \
   \
  /* ring */ \
  [propertyID=\"133???\"] { \
    lineType : thickline;  \
    /*[hasInnerRing=true] { \
      innerLineType : thinline; \
    }*/   \
  }";
Run Code Online (Sandbox Code Playgroud)

可以通过修改解析器源来实现对嵌套选择器/规则的支持.进行Parser::parseRuleset递归所需的更改非常小.我会留下这个作为读者的练习:)

总而言之,我认为重用现有的解析器比滚动自己的解析器容易得多,尤其是当您的用户不可避免地希望您支持越来越多的CSS规范时.


Mur*_*rat 1

我知道两种可能性:

  1. boost::spirit这里你可以找到关于 boost::spirit 解析器框架的很好的介绍
  2. 我建议您编写自己的递归下降解析器

由于事实上,您的个性化 *.pss 并不像 CSS 那样复杂(简单的括号等),因此我建议使用 2。