Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | /* |
43 | jscodemarker.cpp |
44 | */ |
45 | |
46 | #include "declarativeparser/qdeclarativejsast_p.h" |
47 | #include "declarativeparser/qdeclarativejsengine_p.h" |
48 | #include "declarativeparser/qdeclarativejslexer_p.h" |
49 | #include "declarativeparser/qdeclarativejsnodepool_p.h" |
50 | #include "declarativeparser/qdeclarativejsparser_p.h" |
51 | |
52 | #include "atom.h" |
53 | #include "node.h" |
54 | #include "jscodemarker.h" |
55 | #include "qmlmarkupvisitor.h" |
56 | #include "text.h" |
57 | #include "tree.h" |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | JsCodeMarker::JsCodeMarker() |
62 | { |
63 | } |
64 | |
65 | JsCodeMarker::~JsCodeMarker() |
66 | { |
67 | } |
68 | |
69 | /*! |
70 | Returns true if the \a code is recognized by the parser. |
71 | */ |
72 | bool JsCodeMarker::recognizeCode(const QString &code) |
73 | { |
74 | QDeclarativeJS::Engine engine; |
75 | QDeclarativeJS::Lexer lexer(&engine); |
76 | QDeclarativeJS::Parser parser(&engine); |
77 | QDeclarativeJS::NodePool m_nodePool("<JsCodeMarker::recognizeCode>", &engine); |
78 | |
79 | QString newCode = code; |
80 | QList<QDeclarativeJS::AST::SourceLocation> pragmas = extractPragmas(newCode); |
81 | lexer.setCode(newCode, 1); |
82 | |
83 | return parser.parseProgram(); |
84 | } |
85 | |
86 | /*! |
87 | Returns true if \a ext is any of a list of file extensions |
88 | for the QML language. |
89 | */ |
90 | bool JsCodeMarker::recognizeExtension(const QString &ext) |
91 | { |
92 | return ext == "js"; |
93 | } |
94 | |
95 | /*! |
96 | Returns true if the \a language is recognized. Only "QML" is |
97 | recognized by this marker. |
98 | */ |
99 | bool JsCodeMarker::recognizeLanguage(const QString &language) |
100 | { |
101 | return language == "JavaScript"|| language == "ECMAScript"; |
102 | } |
103 | |
104 | /*! |
105 | Returns the type of atom used to represent JavaScript code in the documentation. |
106 | */ |
107 | Atom::Type JsCodeMarker::atomType() const |
108 | { |
109 | return Atom::JavaScript; |
110 | } |
111 | |
112 | QString JsCodeMarker::markedUpCode(const QString &code, |
113 | const Node *relative, |
114 | const Location &location) |
115 | { |
116 | return addMarkUp(code, relative, location); |
117 | } |
118 | |
119 | QString JsCodeMarker::addMarkUp(const QString &code, |
120 | const Node * /* relative */, |
121 | const Location &location) |
122 | { |
123 | QDeclarativeJS::Engine engine; |
124 | QDeclarativeJS::Lexer lexer(&engine); |
125 | |
126 | QString newCode = code; |
127 | QList<QDeclarativeJS::AST::SourceLocation> pragmas = extractPragmas(newCode); |
128 | lexer.setCode(newCode, 1); |
129 | |
130 | QDeclarativeJS::Parser parser(&engine); |
131 | QDeclarativeJS::NodePool m_nodePool("<JsCodeMarker::addMarkUp>", &engine); |
132 | QString output; |
133 | |
134 | if (parser.parseProgram()) { |
135 | QDeclarativeJS::AST::Node *ast = parser.rootNode(); |
136 | // Pass the unmodified code to the visitor so that pragmas and other |
137 | // unhandled source text can be output. |
138 | QmlMarkupVisitor visitor(code, pragmas, &engine); |
139 | QDeclarativeJS::AST::Node::accept(ast, &visitor); |
140 | output = visitor.markedUpCode(); |
141 | } else { |
142 | location.warning(tr("Unable to parse JavaScript: \"%1\" at line %2, column %3").arg( |
143 | parser.errorMessage()).arg(parser.errorLineNumber()).arg( |
144 | parser.errorColumnNumber())); |
145 | output = protect(code); |
146 | } |
147 | |
148 | return output; |
149 | } |
150 | |
151 | QT_END_NAMESPACE |
152 |
Warning: That file was not part of the compilation database. It may have many parsing errors.