Cutelee  6.1.0
scriptableparser.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2.1 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "scriptableparser.h"
22 
23 #include "parser.h"
24 
25 #include <QtQml/QJSEngine>
26 
27 ScriptableParser::ScriptableParser(Cutelee::Parser *p, QJSEngine *engine)
28  : QObject(engine), m_p(p), m_engine(engine)
29 {
30 }
31 
32 void ScriptableParser::removeNextToken() { m_p->removeNextToken(); }
33 
34 bool ScriptableParser::hasNextToken() const { return m_p->hasNextToken(); }
35 
36 void ScriptableParser::loadLib(const QString &name) { m_p->loadLib(name); }
37 
38 QJSValue ScriptableParser::takeNextToken()
39 {
40  Token t = m_p->takeNextToken();
41  auto obj = m_engine->newObject();
42  obj.setProperty(QStringLiteral("tokenType"), t.tokenType);
43  obj.setProperty(QStringLiteral("content"), t.content);
44  return obj;
45 }
46 
47 void ScriptableParser::skipPast(const QString &tag) { m_p->skipPast(tag); }
48 
49 QList<QObject *> ScriptableParser::parse(QObject *parent, const QString &stopAt)
50 {
51  return parse(parent, QStringList() << stopAt);
52 }
53 
54 QList<QObject *> ScriptableParser::parse(QObject *parent,
55  const QStringList &stopAt)
56 {
57  auto node = qobject_cast<Node *>(parent);
58  Q_ASSERT(node);
59 
60  auto nodeList = m_p->parse(node, stopAt);
61  QList<QObject *> objList;
62  for (auto n : nodeList) {
63  objList << n;
64  }
65  return objList;
66 }
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
bool hasNextToken() const
Definition: parser.cpp:285
void skipPast(const QString &tag)
Definition: parser.cpp:145
Token takeNextToken()
Definition: parser.cpp:291
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition: parser.cpp:180
void removeNextToken()
Definition: parser.cpp:297
QString content
The content of this Token.
Definition: token.h:51
int tokenType
The Type of this Token.
Definition: token.h:49