forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
211 lines (176 loc) · 4.69 KB
/
parser.cpp
File metadata and controls
211 lines (176 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// parser.cpp
//
// Copyright (C) 2001-2019, the Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "astro.h"
#include "parser.h"
#include "tokenizer.h"
#include "value.h"
using namespace std;
using namespace Eigen;
using namespace celmath;
/****** Parser method implementation ******/
Parser::Parser(Tokenizer* _tokenizer) :
tokenizer(_tokenizer)
{
}
ValueArray* Parser::readArray()
{
Tokenizer::TokenType tok = tokenizer->nextToken();
if (tok != Tokenizer::TokenBeginArray)
{
tokenizer->pushBack();
return nullptr;
}
ValueArray* array = new ValueArray();
Value* v = readValue();
while (v != nullptr)
{
array->push_back(v);
v = readValue();
}
tok = tokenizer->nextToken();
if (tok != Tokenizer::TokenEndArray)
{
tokenizer->pushBack();
delete array;
return nullptr;
}
return array;
}
Hash* Parser::readHash()
{
Tokenizer::TokenType tok = tokenizer->nextToken();
if (tok != Tokenizer::TokenBeginGroup)
{
tokenizer->pushBack();
return nullptr;
}
auto* hash = new Hash();
tok = tokenizer->nextToken();
while (tok != Tokenizer::TokenEndGroup)
{
if (tok != Tokenizer::TokenName)
{
tokenizer->pushBack();
delete hash;
return nullptr;
}
string name = tokenizer->getNameValue();
#ifndef USE_POSTFIX_UNITS
readUnits(name, hash);
#endif
Value* value = readValue();
if (value == nullptr)
{
delete hash;
return nullptr;
}
hash->addValue(name, *value);
#ifdef USE_POSTFIX_UNITS
readUnits(name, hash);
#endif
tok = tokenizer->nextToken();
}
return hash;
}
/**
* Reads a units section into the hash.
* @param[in] propertyName Name of the current property.
* @param[in] hash Hash to add units quantities into.
* @return True if a units section was successfully read, false otherwise.
*/
bool Parser::readUnits(const string& propertyName, Hash* hash)
{
Tokenizer::TokenType tok = tokenizer->nextToken();
if (tok != Tokenizer::TokenBeginUnits)
{
tokenizer->pushBack();
return false;
}
tok = tokenizer->nextToken();
while (tok != Tokenizer::TokenEndUnits)
{
if (tok != Tokenizer::TokenName)
{
tokenizer->pushBack();
return false;
}
string unit = tokenizer->getNameValue();
Value* value = new Value(unit);
if (astro::isLengthUnit(unit))
{
string keyName(propertyName + "%Length");
hash->addValue(keyName, *value);
}
else if (astro::isTimeUnit(unit))
{
string keyName(propertyName + "%Time");
hash->addValue(keyName, *value);
}
else if (astro::isAngleUnit(unit))
{
string keyName(propertyName + "%Angle");
hash->addValue(keyName, *value);
}
else if (astro::isMassUnit(unit))
{
string keyName(propertyName + "%Mass");
hash->addValue(keyName, *value);
}
else
{
delete value;
return false;
}
tok = tokenizer->nextToken();
}
return true;
}
Value* Parser::readValue()
{
Tokenizer::TokenType tok = tokenizer->nextToken();
switch (tok)
{
case Tokenizer::TokenNumber:
return new Value(tokenizer->getNumberValue());
case Tokenizer::TokenString:
return new Value(tokenizer->getStringValue());
case Tokenizer::TokenName:
if (tokenizer->getNameValue() == "false")
return new Value(false);
else if (tokenizer->getNameValue() == "true")
return new Value(true);
else
{
tokenizer->pushBack();
return nullptr;
}
case Tokenizer::TokenBeginArray:
tokenizer->pushBack();
{
auto* array = readArray();
if (array == nullptr)
return nullptr;
else
return new Value(array);
}
case Tokenizer::TokenBeginGroup:
tokenizer->pushBack();
{
Hash* hash = readHash();
if (hash == nullptr)
return nullptr;
else
return new Value(hash);
}
default:
tokenizer->pushBack();
return nullptr;
}
}