src/script/parser.cpp

Tue, 03 Feb 2015 15:30:30 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 03 Feb 2015 15:30:30 +0200
branch
scripting
changeset 925
2f316b57b508
parent 924
d1ac217c9165
child 942
afbd122f3eff
permissions
-rw-r--r--

- added/corrected license headers

923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
1 /*
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
2 * LDForge: LDraw parts authoring CAD
925
2f316b57b508 - added/corrected license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 924
diff changeset
3 * Copyright (C) 2015 Teemu Piippo
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
4 *
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
5 * This program is free software: you can redistribute it and/or modify
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
6 * it under the terms of the GNU General Public License as published by
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
8 * (at your option) any later version.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
9 *
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
10 * This program is distributed in the hope that it will be useful,
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
13 * GNU General Public License for more details.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
14 *
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
15 * You should have received a copy of the GNU General Public License
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
17 */
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
18
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "parser.h"
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
21 static const char* TokenNames[] =
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
22 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
23 "if",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
24 "then",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
25 "else",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
26 "endif",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
27 "endmacro",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
28 "macro",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
29 "for",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
30 "while",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
31 "done",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
32 "do",
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
33 "==",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
34 "<=",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
35 ">=",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
36 "&&",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
37 "||",
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
38 "!=",
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
39 ":",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
40 ";",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
41 ".",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
42 ",",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
43 "=",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
44 "<",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
45 ">",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
46 "?",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
47 "{",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
48 "}",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
49 "[",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
50 "]",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
51 "(",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
52 ")",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
53 "-",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
54 "+",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
55 "*",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
56 "/",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
57 "\\",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
58 "&",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
59 "^",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
60 "|",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
61 "!",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
62 "@",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
63 "#",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
64 "~",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
65 "`",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
66 "%",
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
67 "<variable>",
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
68 "<string>",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
69 "<symbol>",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
70 "<number>",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
71 "<any>",
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
72 };
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
73
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
74 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
75 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
76 //
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 Script::Parser::Parser(QString text) :
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
78 m_script(text) {}
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
80 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
81 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
82 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
83 Script::Parser::~Parser() {}
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
84
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
85 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
86 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
87 //
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 void Script::Parser::parse()
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
90 preprocess();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
91 m_state.reset();
924
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
92 m_astRoot = Ast::spawnRoot();
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
93
924
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
94 while (next())
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
95 {
924
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
96 if (m_state.token.type == TOK_Semicolon)
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
97 continue;
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
98
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
99 tokenMustBe (TOK_Macro);
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
100 mustGetNext (TOK_Symbol);
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
101 Ast::MacroPointer macroAst = Ast::spawn<Ast::MacroNode> (m_astRoot, state().token.text);
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
102 mustGetNext (TOK_Semicolon);
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
103
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
104 do
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
105 {
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
106 mustGetNext();
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
107 } while (m_state.token.type != TOK_EndMacro);
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
108 }
924
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
109
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
110 m_astRoot->dump();
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
111 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
112
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
113 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
114 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
115 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
116 void Script::Parser::preprocess()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
117 {
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
118 bool inString = false;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
119 bool inComment = false;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
120 bool inBackslash = false;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
121 int ln = 1;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
122 int pos = 0;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
123
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
124 // Preprocess
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
125 for (QChar qch : m_script)
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
126 {
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
127 char ch = qch.toAscii();
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
128
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
129 if (not inComment and not inString and ch == '\0')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
130 scriptError ("bad character %1 in script text on line %2", qch, ln);
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
131
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
132 if (not inString)
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
133 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
134 if (ch == '\\')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
135 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
136 inBackslash = true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
137 continue;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
138 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
139
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
140 if (inBackslash and ch != '\n')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
141 scriptError ("misplaced backslash on line %1", ln);
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
142 }
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
143
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
144 if (ch == '"')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
145 inString ^= 1;
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
146
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
147 if (ch == '\n')
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
148 {
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
149 if (inString)
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
150 scriptError ("unterminated string on line %1", ln);
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
151
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
152 if (not inBackslash)
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
153 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
154 m_data.append (';');
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
155 m_data.append ('\n');
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
156 pos += 2;
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
157 inComment = false;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
158 m_lineEndings << pos;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
159 ++ln;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
160 }
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
161 else
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
162 inBackslash = false;
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
163
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
164 continue;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
165 }
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
166
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
167 if (ch == '#' and not inString)
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
168 {
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
169 inComment = true;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
170 continue;
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
171 }
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
172
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
173 if (not inComment)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
174 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
175 m_data.append (ch);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
176 ++pos;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
177 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
178 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
179 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
180
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
181 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
182 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
183 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
184 namespace Script
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
185 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
186 class UnexpectedEOF : public std::exception
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
187 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
188 const char* what() const throw()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
189 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
190 return "unexpected EOF";
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
191 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
192 };
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
193 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
194
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
195 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
196 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
197 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
198 char Script::Parser::read()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
199 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
200 if (m_state.position >= m_data.length())
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
201 throw UnexpectedEOF();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
202
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
203 char ch = m_data[m_state.position];
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
204 m_state.position++;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
205
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
206 if (m_state.position == m_lineEndings[m_state.lineNumber])
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
207 m_state.lineNumber++;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
208
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
209 return ch;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
210 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
211
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
212 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
213 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
214 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
215 void Script::Parser::unread()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
216 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
217 if (m_state.position <= 0)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
218 return;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
219
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
220 if (m_state.lineNumber > 0
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
221 and m_state.position == m_lineEndings[m_state.lineNumber - 1])
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
222 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
223 m_state.lineNumber--;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
224 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
225
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
226 m_state.position--;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
227 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
228
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
229 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
230 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
231 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
232 // Takes a hexadecimal character and returns its numerical value. It is assumed that isxdigit(xd)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
233 // is true (if not, result is undefined).
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
234 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
235 int parseXDigit (char xd)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
236 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
237 if (xd >= 'a')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
238 return xd - 'a';
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
239
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
240 if (xd >= 'A')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
241 return xd - 'A';
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
242
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
243 return xd - '0';
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
244 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
245
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
246 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
247 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
248 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
249 bool Script::Parser::next (TokenType desiredType)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
250 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
251 SavedState oldpos = state();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
252 Token oldtoken = m_state.token;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
253
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
254 if (not getNextToken())
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
255 return false;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
256
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
257 if (desiredType != TOK_Any and m_state.token.type != desiredType)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
258 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
259 // Did not find the token we wanted, revert back
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
260 m_rejectedToken = m_state.token;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
261 m_state.token = oldtoken;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
262 setState (oldpos);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
263 return false;
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
264 }
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
265
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
266 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
267 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
268
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
269 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
270 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
271 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
272 bool Script::Parser::getNextToken()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
273 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
274 try
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
275 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
276 m_state.token.text.clear();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
277 m_state.token.number = 0;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
278 m_state.token.real = 0.0;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
279 skipSpace();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
280
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
281 const char* data = m_data.constData() + m_state.position;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
282
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
283 // Does this character start one of our tokens?
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
284 for (int tt = 0; tt <= LastNamedToken; ++tt)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
285 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
286 if (strncmp (data, TokenNames[tt], strlen (TokenNames[tt])) != 0)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
287 continue;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
288
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
289 m_state.position += strlen (TokenNames[tt]);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
290 m_state.token.text = QString::fromAscii (TokenNames[tt]);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
291 m_state.token.type = TokenType (tt);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
292 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
293 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
294
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
295 // Check for number
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
296 if (parseNumber())
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
297 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
298
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
299 // Check for string
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
300 if (*data == '"')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
301 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
302 read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
303 parseString();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
304 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
305 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
306
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
307 // Check for variable
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
308 if (*data == '$')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
309 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
310 read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
311 m_state.token.text = parseIdentifier();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
312 m_state.token.type = TOK_Variable;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
313 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
314 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
315
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
316 // Must be a symbol of some sort then
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
317 m_state.token.text = parseIdentifier();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
318 m_state.token.type = TOK_Symbol;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
319 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
320 catch (UnexpectedEOF)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
321 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
322 return false;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
323 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
324
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
325 return true;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
326 }
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
327
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
328 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
329 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
330 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
331 bool Script::Parser::parseNumber()
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
333 SavedState pos = state();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
334 char ch = read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
335 unread();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
336 QString numberString;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
337
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
338 if (not isdigit (ch) and ch != '.')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
339 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
340 setState (pos);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
341 return false;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
342 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
343
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
344 int base = 10;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
345 bool gotDot = false;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
346
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
347 if (tryMatch ("0x", false))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
348 base = 16;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
349 elif (tryMatch ("0b", false))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
350 base = 2;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
351
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
352 int (*checkFunc)(int) = base == 16 ? isxdigit : isdigit;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
353
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
354 for (int n = 0; not isspace (ch = read()); ++n)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
355 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
356 if (n == 0 && ch == '0')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
357 base = 8;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
358
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
359 if (ch == '.')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
360 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
361 if (gotDot)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
362 scriptError ("multiple dots in numeric literal");
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
363
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
364 // If reading numbers like 0.1234 where the first number is zero, the parser
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
365 // will initially think the number is octal so we must take that into account here.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
366 // Note that even if you have numbers like 05.612, it will still be decimal.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
367 if (base != 10 and base != 8)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
368 scriptError ("real number constant must be decimal");
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
369
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
370 base = 10;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
371 gotDot = true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
372 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
373 else if (checkFunc (ch))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
374 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
375 if (base <= 10 and (ch - '0') >= base)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
376 scriptError ("bad base-%1 numeric literal", base);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
377
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
378 numberString += ch;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
379 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
380 else if (isalpha (ch))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
381 scriptError ("invalid digit %1 in literal", ch);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
382 else
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
383 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
384 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
385
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
386 unread();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
387 bool ok;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
388
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
389 if (gotDot)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
390 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
391 // Floating point number
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
392 m_state.token.real = numberString.toFloat (&ok);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
393 m_state.token.number = m_state.token.real;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
394 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
395 else
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
396 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
397 // Integral number
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
398 m_state.token.number = numberString.toInt (&ok, base);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
399 m_state.token.real = m_state.token.number;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
400 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
401
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
402 if (ok == false)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
403 scriptError ("invalid numeric literal '%1'", numberString);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
404
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
405 m_state.token.text = numberString;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
406 m_state.token.type = TOK_Number;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
407
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
408 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
409 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
410
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
411 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
412 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
413 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
414 void Script::Parser::scriptError (QString text)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
415 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
416 throw ParseError (text);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
417 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
418
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
419 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
420 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
421 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
422 // Checks whether the parser is at the beginning of the given string in the code. The string is
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
423 // expected not to have newlines. If true, the parser jumps over the text.
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
424 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
425 bool Script::Parser::tryMatch (const char* text, bool caseSensitive)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
426 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
427 assert (strstr (text, "\n") == NULL);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
428 const char* data = m_data.constData() + m_state.position;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
429 int (*func) (const char*, const char*) = caseSensitive ? &strcmp : &strcasecmp;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
430
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
431 if ((*func) (data, text) == 0)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
432 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
433 m_state.position += strlen (text);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
434 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
435 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
436
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
437 return false;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
438 }
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
439
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
440 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
441 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
442 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
443 QString Script::Parser::parseEscapeSequence()
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
444 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
445 char ch = read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
446 QString result;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
447
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
448 switch (ch)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
449 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
450 case '"':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
451 result += "\"";
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
452 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
453
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
454 case 'n':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
455 result += "\n";
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
456 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
457
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
458 case 't':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
459 result += "\t";
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
460 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
461
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
462 case '\\':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
463 result += "\\";
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
464 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
465
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
466 case 'x':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
467 case 'X':
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
468 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
469 char n1 = read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
470 char n2 = read();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
471
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
472 if (not isxdigit(n1) or not isxdigit(n2))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
473 scriptError ("bad hexa-decimal character \\x%1%2", n1, n2);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
474
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
475 unsigned char num = parseXDigit(n1) * 16 + parseXDigit(n2);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
476 result += char (num);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
477 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
478 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
479
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
480 default:
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
481 scriptError ("unknown escape sequence \\%1", ch);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
482 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
483
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
484 return result;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
485 }
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
486
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
487 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
488 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
489 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
490 void Script::Parser::parseString()
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
491 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
492 m_state.token.type = TOK_String;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
493 m_state.token.text.clear();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
494
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
495 try
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
496 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
497 char ch;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
498
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
499 while ((ch = read()) != '"')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
500 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
501 if (ch == '\\')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
502 m_state.token.text += parseEscapeSequence();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
503 else
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
504 m_state.token.text += ch;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
505 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
506 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
507 catch (UnexpectedEOF)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
508 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
509 scriptError ("unterminated string");
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
510 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
511 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
512
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
513 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
514 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
515 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
516 void Script::Parser::skipSpace()
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
517 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
518 while (isspace (read()))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
519 ;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
520
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
521 unread();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
522 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
523
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
524 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
525 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
526 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
527 void Script::Parser::mustGetNext (TokenType desiredType)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
528 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
529 if (not next (desiredType))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
530 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
531 scriptError ("Expected %1, got %2",
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
532 TokenNames[m_rejectedToken.type],
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
533 TokenNames[desiredType]);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
534 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
535 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
536
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
537 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
538 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
539 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
540 bool Script::Parser::peekNext (Token& tok)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
541 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
542 SavedState pos = state();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
543
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
544 if (next (TOK_Any))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
545 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
546 tok = m_state.token;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
547 setState (pos);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
548 return true;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
549 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
550
922
81887a77baa0 - more work on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents: 921
diff changeset
551 return false;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
552 }
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
553
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
554 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
555 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
556 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
557 const Script::SavedState& Script::Parser::state() const
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
558 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
559 return m_state;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
560 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
561
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
562 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
563 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
564 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
565 void Script::Parser::setState (const SavedState& pos)
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
566 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
567 m_state = pos;
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
568 }
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
569
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
570 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
571 // -------------------------------------------------------------------------------------------------
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
572 //
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
573 QString Script::Parser::parseIdentifier()
921
e2e9f594de66 - commit work done on scripting
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
574 {
923
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
575 char ch;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
576 QString identifier;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
577
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
578 while (not isspace (ch = read()))
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
579 {
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
580 if (isalnum (ch) == false and ch != '_')
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
581 break;
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
582
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
583 identifier += QChar::fromAscii (ch);
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
584 }
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
585
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
586 unread();
e15a577a0bfe - now parses to tokens
Teemu Piippo <crimsondusk64@gmail.com>
parents: 922
diff changeset
587 return identifier;
924
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
588 }
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
589
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
590 //
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
591 // -------------------------------------------------------------------------------------------------
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
592 //
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
593 void Script::Parser::tokenMustBe (TokenType desiredType)
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
594 {
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
595 if (m_state.token.type != desiredType)
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
596 {
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
597 scriptError ("expected %1, got %2",
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
598 TokenNames[desiredType],
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
599 TokenNames[m_state.token.type]);
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
600 }
d1ac217c9165 - ast: added root and macro nodes and processing of them
Teemu Piippo <crimsondusk64@gmail.com>
parents: 923
diff changeset
601 }

mercurial