swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 1 | #include "Parser.h" |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 2 | #include "PPrint.h" |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 3 | |
| 4 | #include <QDebug> |
| 5 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 6 | ParseResult::ParseResult(bool okay) |
| 7 | : ParseResult((int)okay) |
| 8 | {} |
| 9 | |
| 10 | ParseResult::ParseResult(int status, ParsePos pos) |
| 11 | : ParseResult(status, "", pos) |
| 12 | {} |
| 13 | |
| 14 | ParseResult::ParseResult(int status, QString message, ParsePos pos) |
| 15 | { |
| 16 | _status = status; |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 17 | _message = message; |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 18 | _pos = pos; |
| 19 | } |
| 20 | |
| 21 | ParseResult::operator bool() const |
| 22 | { |
| 23 | return _status == COMPLETE; |
| 24 | } |
| 25 | |
| 26 | ParsePos ParseResult::pos() const |
| 27 | { |
| 28 | return _pos; |
| 29 | } |
| 30 | |
| 31 | QString ParseResult::message() const |
| 32 | { |
| 33 | return _message; |
| 34 | } |
| 35 | |
| 36 | int ParseResult::status() const |
| 37 | { |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 38 | return _status; |
| 39 | } |
| 40 | |
| 41 | ParseResult::operator QString() const |
| 42 | { |
| 43 | return pprint(pos()) + "\n" + message(); |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 44 | } |
| 45 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 46 | Parser::Parser(QString input) |
| 47 | { |
| 48 | _input = input; |
| 49 | } |
| 50 | |
| 51 | QChar Parser::peek() |
| 52 | { |
| 53 | if (atEnd()) |
| 54 | return 0; |
| 55 | |
| 56 | return _input[_pos]; |
| 57 | } |
| 58 | |
| 59 | QChar Parser::get() |
| 60 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 61 | QChar c = _input[_pos++]; |
| 62 | |
| 63 | if (c == '\n') |
| 64 | { |
| 65 | _line++; |
| 66 | _offset = 0; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | _offset++; |
| 71 | } |
| 72 | |
| 73 | return c; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool Parser::atEnd() |
| 77 | { |
| 78 | return _pos >= _input.length(); |
| 79 | } |
| 80 | |
| 81 | void Parser::skip() |
| 82 | { |
| 83 | while (peek().isSpace()) |
| 84 | get(); |
| 85 | } |
| 86 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 87 | QString Parser::line(int n) const |
| 88 | { |
| 89 | return _input.split("\n")[n]; |
| 90 | } |
| 91 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 92 | ParsePos Parser::save() const |
| 93 | { |
| 94 | return ParsePos{_line, _pos, _offset}; |
| 95 | } |
| 96 | |
| 97 | void Parser::reset(ParsePos pos) |
| 98 | { |
| 99 | _line = pos.line; |
| 100 | _pos = pos.pos; |
| 101 | _offset = pos.lineOffset; |
| 102 | } |
| 103 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 104 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 105 | ParseResult Parser::parseSymbol(T *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 106 | { |
| 107 | skip(); |
| 108 | |
| 109 | if (peek().isLetter()) |
| 110 | { |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 111 | *node = T(get()); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 118 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 119 | ParseResult Parser::parseIdentifier(T *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 120 | { |
| 121 | skip(); |
| 122 | |
| 123 | QString buffer; |
| 124 | |
| 125 | if (peek().isUpper()) |
| 126 | { |
| 127 | while (peek().isLetter() || peek() == '-' || peek() == '_' || peek().isNumber()) |
| 128 | { |
| 129 | buffer += get(); |
| 130 | } |
| 131 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 132 | *node = T(buffer); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 139 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 140 | ParseResult Parser::parseNumber(T *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 141 | { |
| 142 | skip(); |
| 143 | |
| 144 | QString buffer; |
| 145 | |
| 146 | if (peek().isDigit()) |
| 147 | { |
| 148 | while (peek().isDigit()) |
| 149 | buffer += get(); |
| 150 | |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame] | 151 | *node = T::fromInteger(buffer.toInt()); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 158 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 159 | ParseResult Parser::parseVariable(T *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 160 | { |
| 161 | skip(); |
| 162 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 163 | ParsePos pos = save(); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 164 | |
| 165 | if (peek() == 's' || peek() == 'e' || peek() == 't') |
| 166 | { |
| 167 | char type = get().toLatin1(); |
| 168 | |
| 169 | if (peek() == '.') |
| 170 | { |
| 171 | get(); |
| 172 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 173 | T nameNode; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 174 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 175 | if (parseIdentifier(&nameNode)) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 176 | { |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 177 | *node = T(type, nameNode.name()); |
| 178 | return true; |
| 179 | } |
| 180 | else if (parseSymbol(&nameNode)) |
| 181 | { |
| 182 | *node = T(type, QString(nameNode.symbol())); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 183 | return true; |
| 184 | } |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 185 | else |
| 186 | { |
| 187 | ParseResult ret(ParseResult::INCOMPLETE, |
| 188 | "Expected identifier or symbol after . in variable", |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 189 | save()); |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 190 | |
| 191 | reset(pos); |
| 192 | |
| 193 | return ret; |
| 194 | } |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 198 | reset(pos); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 202 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 203 | ParseResult Parser::parseMany(QList<T> *list) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 204 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 205 | QList<T> nodes, string; |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 206 | T next; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 207 | ParseResult ret, stringRet; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 208 | |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 209 | while ((ret = parseOne(&next)) || (stringRet = parseString(&string))) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 210 | { |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 211 | if (ret) |
| 212 | nodes.append(next); |
| 213 | else if (stringRet) |
| 214 | nodes.append(string); |
| 215 | |
| 216 | // So that we can check if anything was incomplete recently at the end |
| 217 | ret = stringRet = false; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 218 | } |
| 219 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 220 | *list = nodes; |
| 221 | |
| 222 | if (ret.status() == ParseResult::INCOMPLETE) |
| 223 | return ret; |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 224 | else if (stringRet.status() == ParseResult::INCOMPLETE) |
| 225 | return ret; |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 226 | else |
| 227 | return true; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 228 | } |
| 229 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 230 | template <typename T> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 231 | ParseResult Parser::parseParens(T *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 232 | { |
| 233 | skip(); |
| 234 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 235 | ParsePos pos = save(); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 236 | |
| 237 | if (peek() != '(') |
| 238 | return false; |
| 239 | |
| 240 | get(); |
| 241 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 242 | QList<T> many; |
| 243 | ParseResult ret = parseMany(&many); |
| 244 | |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 245 | *node = T(many); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 246 | |
| 247 | skip(); |
| 248 | if (peek() != ')') |
| 249 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 250 | ret = ParseResult(ParseResult::INCOMPLETE, "Expected ) in parenthesized list", save()); |
| 251 | reset(pos); |
| 252 | |
| 253 | return ret; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | get(); |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 261 | ParseResult Parser::parseFunction(AstNode *node) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 262 | { |
| 263 | skip(); |
| 264 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 265 | ParsePos pos = save(); |
| 266 | ParseResult ret; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 267 | |
| 268 | if (peek() != '<') |
| 269 | return false; |
| 270 | |
| 271 | get(); |
| 272 | |
| 273 | AstNode head; |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 274 | if (!(ret = parseIdentifier(&head))) |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 275 | { |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 276 | ParsePos endPos = save(); |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 277 | reset(pos); |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 278 | return ParseResult(ParseResult::INCOMPLETE, "Expected identifier following < in function call", endPos); |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 279 | } |
| 280 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 281 | QList<AstNode> body; |
| 282 | ret = parseMany(&body); |
| 283 | |
| 284 | if (!ret) |
| 285 | { |
| 286 | reset(pos); |
| 287 | return ret; |
| 288 | } |
| 289 | |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 290 | *node = AstNode(head.name(), body); |
| 291 | |
| 292 | skip(); |
| 293 | if (peek() != '>') |
| 294 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 295 | ret = ParseResult(ParseResult::INCOMPLETE, "Expected >", save()); |
| 296 | reset(pos); |
| 297 | return ret; |
swissChili | c71acc6 | 2021-12-07 08:03:37 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | get(); |
| 301 | |
| 302 | return true; |
| 303 | } |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 304 | |
| 305 | template <> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 306 | ParseResult Parser::parseOne<Token>(Token *node) |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 307 | { |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 308 | ParseResult ret; |
| 309 | |
| 310 | if ((ret = parseVariable(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 311 | return ret; |
| 312 | if ((ret = parseNumber(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 313 | return ret; |
| 314 | if ((ret = parseIdentifier(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 315 | return ret; |
| 316 | if ((ret = parseSymbol(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 317 | return ret; |
| 318 | return parseParens(node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | template <> |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 322 | ParseResult Parser::parseOne<AstNode>(AstNode *node) |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 323 | { |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 324 | ParseResult ret; |
| 325 | |
| 326 | if ((ret = parseFunction(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 327 | return ret; |
| 328 | if ((ret = parseVariable(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 329 | return ret; |
| 330 | if ((ret = parseNumber(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 331 | return ret; |
| 332 | if ((ret = parseIdentifier(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 333 | return ret; |
| 334 | if ((ret = parseSymbol(node)).status() == ParseResult::COMPLETE || ret.status() == ParseResult::INCOMPLETE) |
| 335 | return ret; |
| 336 | return parseParens(node); |
swissChili | 682e7bc | 2021-12-07 09:04:54 -0800 | [diff] [blame] | 337 | } |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 338 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 339 | ParseResult Parser::parseSentence(Sentence *sentence) |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 340 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 341 | ParsePos pos = save(); |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 342 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 343 | if (peek() == '}') |
| 344 | { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | QList<Token> pattern; |
| 349 | ParseResult ret = parseMany(&pattern); |
| 350 | |
| 351 | if (!ret) |
| 352 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 353 | reset(pos); |
| 354 | return ret; |
| 355 | } |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 356 | |
| 357 | skip(); |
| 358 | |
| 359 | if (get() != '=') |
| 360 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 361 | ret = ParseResult(ParseResult::INCOMPLETE, "Expected = in sentence", save()); |
| 362 | reset(pos); |
| 363 | return ret; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 364 | } |
| 365 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 366 | QList<AstNode> result; |
| 367 | ret = parseMany(&result); |
| 368 | |
| 369 | if (!ret) |
| 370 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 371 | reset(pos); |
| 372 | return ret; |
| 373 | } |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 374 | |
| 375 | skip(); |
| 376 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 377 | if (peek() != '}' && get() != ';') |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 378 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 379 | ret = ParseResult(ParseResult::INCOMPLETE, "Expected ; or } after sentence", save()); |
| 380 | reset(pos); |
| 381 | return ret; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | *sentence = Sentence(pattern, result); |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 385 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 389 | ParseResult Parser::parseFunctionDefinition(Function *function) |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 390 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 391 | ParsePos pos = save(); |
| 392 | ParseResult ret; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 393 | |
| 394 | Token identifier; |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 395 | if (!(ret = parseIdentifier(&identifier))) |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 396 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 397 | reset(pos); |
| 398 | return ret; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | QString name = identifier.name(); |
| 402 | Function func(name); |
| 403 | |
| 404 | skip(); |
| 405 | |
| 406 | if (get() != '{') |
| 407 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 408 | reset(pos); |
| 409 | return false; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | Sentence sentence; |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 413 | while ((ret = parseSentence(&sentence))) |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 414 | { |
| 415 | func.addSentence(sentence); |
| 416 | skip(); |
| 417 | } |
| 418 | |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 419 | if (ret.status() == ParseResult::INCOMPLETE) |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 420 | { |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 421 | reset(pos); |
| 422 | return ret; |
| 423 | } |
| 424 | |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 425 | if (get() != '}') |
| 426 | { |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 427 | ret = ParseResult(ParseResult::INCOMPLETE, "Expected } at end of function", save()); |
swissChili | 847a78c | 2021-12-09 17:44:52 -0800 | [diff] [blame] | 428 | reset(pos); |
| 429 | return ret; |
swissChili | 8a581c6 | 2021-12-07 13:29:21 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | *function = func; |
| 433 | return true; |
| 434 | } |
swissChili | 323883d | 2022-02-20 16:35:23 -0800 | [diff] [blame] | 435 | |
| 436 | ParsePos::operator QString() |
| 437 | { |
| 438 | return QString::number(line) + ":" + QString::number(lineOffset); |
| 439 | } |
swissChili | 918557c | 2022-02-20 20:16:34 -0800 | [diff] [blame] | 440 | |
| 441 | template <typename T> |
| 442 | ParseResult Parser::parseString(QList<T> *list) |
| 443 | { |
| 444 | skip(); |
| 445 | |
| 446 | ParsePos pos = save(); |
| 447 | |
| 448 | if (peek() != '\'') |
| 449 | return false; |
| 450 | |
| 451 | get(); |
| 452 | |
| 453 | list->clear(); |
| 454 | |
| 455 | while (peek() != 0 && peek() != '\'') |
| 456 | { |
| 457 | QChar c = get(); |
| 458 | if (c == '\\') |
| 459 | { |
| 460 | QChar next = get(); |
| 461 | QString conversions = "''n\nt\tr\r"; |
| 462 | |
| 463 | for (int i = 0; i < conversions.size(); i += 2) |
| 464 | { |
| 465 | if (next == conversions[i]) |
| 466 | list->append(T(conversions[i + 1])); |
| 467 | } |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | list->append(T(c)); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (get() == 0) |
| 476 | { |
| 477 | ParseResult ret(ParseResult::INCOMPLETE, "Expected ' before end of input", save()); |
| 478 | reset(pos); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | return true; |
| 483 | } |