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