1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtScript module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL-ONLY$
|
---|
10 | ** GNU Lesser General Public License Usage
|
---|
11 | ** This file may be used under the terms of the GNU Lesser
|
---|
12 | ** General Public License version 2.1 as published by the Free Software
|
---|
13 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
14 | ** packaging of this file. Please review the following information to
|
---|
15 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
17 | **
|
---|
18 | ** If you have questions regarding the use of this file, please contact
|
---|
19 | ** Nokia at [email protected].
|
---|
20 | ** $QT_END_LICENSE$
|
---|
21 | **
|
---|
22 | ****************************************************************************/
|
---|
23 |
|
---|
24 | #include "qscriptlexer_p.h"
|
---|
25 |
|
---|
26 | #include "qscriptgrammar_p.h"
|
---|
27 | #include <ctype.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <string.h>
|
---|
31 |
|
---|
32 | QT_BEGIN_NAMESPACE
|
---|
33 |
|
---|
34 | Q_CORE_EXPORT double qstrtod(const char *s00, char const **se, bool *ok);
|
---|
35 |
|
---|
36 | #define shiftWindowsLineBreak() \
|
---|
37 | do { \
|
---|
38 | if (((current == '\r') && (next1 == '\n')) \
|
---|
39 | || ((current == '\n') && (next1 == '\r'))) { \
|
---|
40 | shift(1); \
|
---|
41 | } \
|
---|
42 | } \
|
---|
43 | while (0)
|
---|
44 |
|
---|
45 | typedef double qsreal; // ###
|
---|
46 |
|
---|
47 | namespace QScript {
|
---|
48 | extern qsreal integerFromString(const char *buf, int size, int radix);
|
---|
49 | }
|
---|
50 |
|
---|
51 | QScript::Lexer::Lexer(QScriptEnginePrivate *eng)
|
---|
52 | : driver(eng),
|
---|
53 | yylineno(0),
|
---|
54 | size8(128), size16(128), restrKeyword(false),
|
---|
55 | stackToken(-1), pos(0),
|
---|
56 | code(0), length(0),
|
---|
57 | bol(true),
|
---|
58 | current(0), next1(0), next2(0), next3(0),
|
---|
59 | err(NoError),
|
---|
60 | check_reserved(true),
|
---|
61 | parenthesesState(IgnoreParentheses),
|
---|
62 | prohibitAutomaticSemicolon(false)
|
---|
63 | {
|
---|
64 | // allocate space for read buffers
|
---|
65 | buffer8 = new char[size8];
|
---|
66 | buffer16 = new QChar[size16];
|
---|
67 | pattern = 0;
|
---|
68 | flags = 0;
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | QScript::Lexer::~Lexer()
|
---|
73 | {
|
---|
74 | delete [] buffer8;
|
---|
75 | delete [] buffer16;
|
---|
76 | }
|
---|
77 |
|
---|
78 | void QScript::Lexer::setCode(const QString &c, int lineno)
|
---|
79 | {
|
---|
80 | errmsg = QString();
|
---|
81 | yylineno = lineno;
|
---|
82 | yycolumn = 1;
|
---|
83 | restrKeyword = false;
|
---|
84 | delimited = false;
|
---|
85 | stackToken = -1;
|
---|
86 | pos = 0;
|
---|
87 | code = c.unicode();
|
---|
88 | length = c.length();
|
---|
89 | bol = true;
|
---|
90 |
|
---|
91 | // read first characters
|
---|
92 | current = (length > 0) ? code[0].unicode() : 0;
|
---|
93 | next1 = (length > 1) ? code[1].unicode() : 0;
|
---|
94 | next2 = (length > 2) ? code[2].unicode() : 0;
|
---|
95 | next3 = (length > 3) ? code[3].unicode() : 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void QScript::Lexer::shift(uint p)
|
---|
99 | {
|
---|
100 | while (p--) {
|
---|
101 | ++pos;
|
---|
102 | ++yycolumn;
|
---|
103 | current = next1;
|
---|
104 | next1 = next2;
|
---|
105 | next2 = next3;
|
---|
106 | next3 = (pos + 3 < length) ? code[pos+3].unicode() : 0;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | void QScript::Lexer::setDone(State s)
|
---|
111 | {
|
---|
112 | state = s;
|
---|
113 | done = true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | int QScript::Lexer::findReservedWord(const QChar *c, int size) const
|
---|
117 | {
|
---|
118 | switch (size) {
|
---|
119 | case 2: {
|
---|
|
---|