1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qqmljsengine_p.h"
41 #include "qqmljsglobal_p.h"
42
43 #include <QtCore/private/qnumeric_p.h>
44 #include <QtCore/qhash.h>
45 #include <QtCore/qdebug.h>
46
47 QT_BEGIN_NAMESPACE
48
49 namespace QQmlJS {
50
toDigit(char c)51 static inline int toDigit(char c)
52 {
53 if ((c >= '0') && (c <= '9'))
54 return c - '0';
55 else if ((c >= 'a') && (c <= 'z'))
56 return 10 + c - 'a';
57 else if ((c >= 'A') && (c <= 'Z'))
58 return 10 + c - 'A';
59 return -1;
60 }
61
integerFromString(const char * buf,int size,int radix)62 double integerFromString(const char *buf, int size, int radix)
63 {
64 if (size == 0)
65 return qt_qnan();
66
67 double sign = 1.0;
68 int i = 0;
69 if (buf[0] == '+') {
70 ++i;
71 } else if (buf[0] == '-') {
72 sign = -1.0;
73 ++i;
74 }
75
76 if (((size-i) >= 2) && (buf[i] == '0')) {
77 if (((buf[i+1] == 'x') || (buf[i+1] == 'X'))
78 && (radix < 34)) {
79 if ((radix != 0) && (radix != 16))
80 return 0;
81 radix = 16;
82 i += 2;
83 } else {
84 if (radix == 0) {
85 radix = 8;
86 ++i;
87 }
88 }
89 } else if (radix == 0) {
90 radix = 10;
91 }
92
93 int j = i;
94 for ( ; i < size; ++i) {
95 int d = toDigit(buf[i]);
96 if ((d == -1) || (d >= radix))
97 break;
98 }
99 double result;
100 if (j == i) {
101 if (!qstrcmp(buf, "Infinity"))
102 result = qInf();
103 else
104 result = qt_qnan();
105 } else {
106 result = 0;
107 double multiplier = 1;
108 for (--i ; i >= j; --i, multiplier *= radix)
109 result += toDigit(buf[i]) * multiplier;
110 }
111 result *= sign;
112 return result;
113 }
114
Engine()115 Engine::Engine()
116 : _lexer(nullptr), _directives(nullptr)
117 { }
118
~Engine()119 Engine::~Engine()
120 { }
121
setCode(const QString & code)122 void Engine::setCode(const QString &code)
123 { _code = code; }
124
addComment(int pos,int len,int line,int col)125 void Engine::addComment(int pos, int len, int line, int col)
126 { if (len > 0) _comments.append(QQmlJS::SourceLocation(pos, len, line, col)); }
127
comments() const128 QList<QQmlJS::SourceLocation> Engine::comments() const
129 { return _comments; }
130
lexer() const131 Lexer *Engine::lexer() const
132 { return _lexer; }
133
setLexer(Lexer * lexer)134 void Engine::setLexer(Lexer *lexer)
135 { _lexer = lexer; }
136
directives() const137 Directives *Engine::directives() const
138 { return _directives; }
139
setDirectives(Directives * directives)140 void Engine::setDirectives(Directives *directives)
141 { _directives = directives; }
142
pool()143 MemoryPool *Engine::pool()
144 { return &_pool; }
145
newStringRef(const QString & text)146 QStringRef Engine::newStringRef(const QString &text)
147 {
148 const int pos = _extraCode.length();
149 _extraCode += text;
150 return _extraCode.midRef(pos, text.length());
151 }
152
newStringRef(const QChar * chars,int size)153 QStringRef Engine::newStringRef(const QChar *chars, int size)
154 { return newStringRef(QString(chars, size)); }
155
156 } // end of namespace QQmlJS
157
158 QT_END_NAMESPACE
159