| 1 | //===-- ResourceScriptParser.cpp --------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===---------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This implements the parser defined in ResourceScriptParser.h. |
| 10 | // |
| 11 | //===---------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "ResourceScriptParser.h" |
| 14 | #include "llvm/ADT/StringExtras.h" |
| 15 | #include "llvm/Support/Process.h" |
| 16 | |
| 17 | // Take an expression returning llvm::Error and forward the error if it exists. |
| 18 | #define RETURN_IF_ERROR(Expr) \ |
| 19 | if (auto Err = (Expr)) \ |
| 20 | return std::move(Err); |
| 21 | |
| 22 | // Take an expression returning llvm::Expected<T> and assign it to Var or |
| 23 | // forward the error out of the function. |
| 24 | #define ASSIGN_OR_RETURN(Var, Expr) \ |
| 25 | auto Var = (Expr); \ |
| 26 | if (!Var) \ |
| 27 | return Var.takeError(); |
| 28 | |
| 29 | namespace llvm { |
| 30 | namespace rc { |
| 31 | |
| 32 | RCParser::ParserError::ParserError(const Twine &Expected, const LocIter CurLoc, |
| 33 | const LocIter End) { |
| 34 | CurMessage = "Error parsing file: expected " + Expected.str() + ", got " + |
| 35 | (CurLoc == End ? "<EOF>" : CurLoc->value()).str(); |
| 36 | } |
| 37 | |
| 38 | char RCParser::ParserError::ID = 0; |
| 39 | |
| 40 | RCParser::RCParser(std::vector<RCToken> TokenList) |
| 41 | : Tokens(std::move(TokenList)), CurLoc(Tokens.begin()), End(Tokens.end()) {} |
| 42 | |
| 43 | bool RCParser::isEof() const { return CurLoc == End; } |
| 44 | |
| 45 | RCParser::ParseType RCParser::parseSingleResource() { |
| 46 | // The first thing we read is usually a resource's name. However, in some |
| 47 | // cases (LANGUAGE and STRINGTABLE) the resources don't have their names |
| 48 | // and the first token to be read is the type. |
| 49 | ASSIGN_OR_RETURN(NameToken, readTypeOrName()); |
| 50 | |
| 51 | if (NameToken->equalsLower(Str: "LANGUAGE" )) |
| 52 | return parseLanguageResource(); |
| 53 | else if (NameToken->equalsLower(Str: "STRINGTABLE" )) |
| 54 | return parseStringTableResource(); |
| 55 | |
| 56 | // If it's not an unnamed resource, what we've just read is a name. Now, |
| 57 | // read resource type; |
| 58 | ASSIGN_OR_RETURN(TypeToken, readTypeOrName()); |
| 59 | |
| 60 | ParseType Result = std::unique_ptr<RCResource>(); |
| 61 | (void)!Result; |
| 62 | |
| 63 | if (TypeToken->equalsLower(Str: "ACCELERATORS" )) |
| 64 | Result = parseAcceleratorsResource(); |
| 65 | else if (TypeToken->equalsLower(Str: "BITMAP" )) |
| 66 | Result = parseBitmapResource(); |
| 67 | else if (TypeToken->equalsLower(Str: "CURSOR" )) |
| 68 | Result = parseCursorResource(); |
| 69 | else if (TypeToken->equalsLower(Str: "DIALOG" )) |
| 70 | Result = parseDialogResource(IsExtended: false); |
| 71 | else if (TypeToken->equalsLower(Str: "DIALOGEX" )) |
| 72 | Result = parseDialogResource(IsExtended: true); |
| 73 | else if (TypeToken->equalsLower(Str: "HTML" )) |
| 74 | Result = parseHTMLResource(); |
| 75 | else if (TypeToken->equalsLower(Str: "ICON" )) |
| 76 | Result = parseIconResource(); |
| 77 | else if (TypeToken->equalsLower(Str: "MENU" )) |
| 78 | Result = parseMenuResource(); |
| 79 | else if (TypeToken->equalsLower(Str: "MENUEX" )) |
| 80 | Result = parseMenuExResource(); |
| 81 | else if (TypeToken->equalsLower(Str: "RCDATA" )) |
| 82 | Result = parseUserDefinedResource(Type: RkRcData); |
| 83 | else if (TypeToken->equalsLower(Str: "VERSIONINFO" )) |
| 84 | Result = parseVersionInfoResource(); |
| 85 | else |
| 86 | Result = parseUserDefinedResource(Type: *TypeToken); |
| 87 | |
| 88 | if (Result) |
| 89 | (*Result)->setName(*NameToken); |
| 90 | |
| 91 | return Result; |
| 92 | } |
| 93 | |
| 94 | bool RCParser::isNextTokenKind(Kind TokenKind) const { |
| 95 | return !isEof() && look().kind() == TokenKind; |
| 96 | } |
| 97 | |
| 98 | const RCToken &RCParser::look() const { |
| 99 | assert(!isEof()); |
| 100 | return *CurLoc; |
| 101 | } |
| 102 | |
| 103 | const RCToken &RCParser::read() { |
| 104 | assert(!isEof()); |
| 105 | return *CurLoc++; |
| 106 | } |
| 107 | |
| 108 | void RCParser::consume() { |
| 109 | assert(!isEof()); |
| 110 | CurLoc++; |
| 111 | } |
| 112 | |
| 113 | // An integer description might consist of a single integer or |
| 114 | // an arithmetic expression evaluating to the integer. The expressions |
| 115 | // can contain the following tokens: <int> ( ) + - | & ~ not. Their meaning |
| 116 | // is the same as in C++ except for 'not' expression. |
| 117 | // The operators in the original RC implementation have the following |
| 118 | // precedence: |
| 119 | // 1) Unary operators (- ~ not), |
| 120 | // 2) Binary operators (+ - & |), with no precedence. |
| 121 | // |
| 122 | // 'not' expression is mostly useful for style values. It evaluates to 0, |
| 123 | // but value given to the operator is stored separately from integer value. |
| 124 | // It's mostly useful for control style expressions and causes bits from |
| 125 | // default control style to be excluded from generated style. For binary |
| 126 | // operators the mask from the right operand is applied to the left operand |
| 127 | // and masks from both operands are combined in operator result. |
| 128 | // |
| 129 | // The following grammar is used to parse the expressions Exp1: |
| 130 | // Exp1 ::= Exp2 || Exp1 + Exp2 || Exp1 - Exp2 || Exp1 | Exp2 || Exp1 & Exp2 |
| 131 | // Exp2 ::= Exp3 || Exp3 * Exp3 || Exp3 / Exp3 |
| 132 | // Exp3 ::= -Exp3 || ~Exp3 || not Expr3 || Int || (Exp1) |
| 133 | // (More conveniently, Exp1 and Exp2 are non-empty sequences of Exp3 |
| 134 | // expressions, separated by binary operators.) |
| 135 | // |
| 136 | // Expressions of type Exp1 are read by parseIntExpr1(Inner) method, Exp2 |
| 137 | // is read by parseIntExpr2() and Exp3 is read by parseIntExpr3(). |
| 138 | // |
| 139 | // The original Microsoft tool handles multiple unary operators incorrectly. |
| 140 | // For example, in 16-bit little-endian integers: |
| 141 | // 1 => 01 00, -1 => ff ff, --1 => ff ff, ---1 => 01 00; |
| 142 | // 1 => 01 00, ~1 => fe ff, ~~1 => fd ff, ~~~1 => fc ff. |
| 143 | // Our implementation differs from the original one and handles these |
| 144 | // operators correctly: |
| 145 | // 1 => 01 00, -1 => ff ff, --1 => 01 00, ---1 => ff ff; |
| 146 | // 1 => 01 00, ~1 => fe ff, ~~1 => 01 00, ~~~1 => fe ff. |
| 147 | |
| 148 | Expected<RCInt> RCParser::readInt() { |
| 149 | ASSIGN_OR_RETURN(Value, parseIntExpr1()); |
| 150 | return (*Value).getValue(); |
| 151 | } |
| 152 | |
| 153 | Expected<IntWithNotMask> RCParser::parseIntExpr1() { |
| 154 | // Exp1 ::= Exp2 || Exp1 + Exp2 || Exp1 - Exp2 || Exp1 | Exp2 || Exp1 & Exp2. |
| 155 | ASSIGN_OR_RETURN(FirstResult, parseIntExpr2()); |
| 156 | IntWithNotMask Result = *FirstResult; |
| 157 | |
| 158 | while (!isEof() && look().isLowPrecedenceBinaryOp()) { |
| 159 | auto OpToken = read(); |
| 160 | ASSIGN_OR_RETURN(NextResult, parseIntExpr2()); |
| 161 | |
| 162 | switch (OpToken.kind()) { |
| 163 | case Kind::Plus: |
| 164 | Result += *NextResult; |
| 165 | break; |
| 166 | |
| 167 | case Kind::Minus: |
| 168 | Result -= *NextResult; |
| 169 | break; |
| 170 | |
| 171 | case Kind::Pipe: |
| 172 | Result |= *NextResult; |
| 173 | break; |
| 174 | |
| 175 | case Kind::Amp: |
| 176 | Result &= *NextResult; |
| 177 | break; |
| 178 | |
| 179 | default: |
| 180 | llvm_unreachable("Already processed all low precedence binary ops." ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return Result; |
| 185 | } |
| 186 | |
| 187 | Expected<IntWithNotMask> RCParser::parseIntExpr2() { |
| 188 | // Exp2 ::= Exp3 || Exp3 * Exp3 || Exp3 / Exp3. |
| 189 | ASSIGN_OR_RETURN(FirstResult, parseIntExpr3()); |
| 190 | IntWithNotMask Result = *FirstResult; |
| 191 | |
| 192 | while (!isEof() && look().isHighPrecedenceBinaryOp()) { |
| 193 | auto OpToken = read(); |
| 194 | ASSIGN_OR_RETURN(NextResult, parseIntExpr3()); |
| 195 | |
| 196 | switch (OpToken.kind()) { |
| 197 | case Kind::Asterisk: |
| 198 | Result *= *NextResult; |
| 199 | break; |
| 200 | |
| 201 | case Kind::Slash: |
| 202 | Result /= *NextResult; |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | llvm_unreachable("Already processed all high precedence binary ops." ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return Result; |
| 211 | } |
| 212 | |
| 213 | Expected<IntWithNotMask> RCParser::parseIntExpr3() { |
| 214 | // Exp3 ::= -Exp3 || ~Exp3 || not Expr3 || Int || (Exp1). |
| 215 | static const char ErrorMsg[] = "'-', '~', integer or '('" ; |
| 216 | |
| 217 | if (isEof()) |
| 218 | return getExpectedError(Message: ErrorMsg); |
| 219 | |
| 220 | switch (look().kind()) { |
| 221 | case Kind::Minus: { |
| 222 | consume(); |
| 223 | ASSIGN_OR_RETURN(Result, parseIntExpr3()); |
| 224 | return -(*Result); |
| 225 | } |
| 226 | |
| 227 | case Kind::Tilde: { |
| 228 | consume(); |
| 229 | ASSIGN_OR_RETURN(Result, parseIntExpr3()); |
| 230 | return ~(*Result); |
| 231 | } |
| 232 | |
| 233 | case Kind::Int: |
| 234 | return RCInt(read()); |
| 235 | |
| 236 | case Kind::LeftParen: { |
| 237 | consume(); |
| 238 | ASSIGN_OR_RETURN(Result, parseIntExpr1()); |
| 239 | RETURN_IF_ERROR(consumeType(Kind::RightParen)); |
| 240 | return *Result; |
| 241 | } |
| 242 | |
| 243 | case Kind::Identifier: { |
| 244 | if (!read().value().equals_insensitive(RHS: "not" )) |
| 245 | return getExpectedError(Message: ErrorMsg, IsAlreadyRead: true); |
| 246 | ASSIGN_OR_RETURN(Result, parseIntExpr3()); |
| 247 | return IntWithNotMask(0, (*Result).getValue()); |
| 248 | } |
| 249 | |
| 250 | default: |
| 251 | return getExpectedError(Message: ErrorMsg); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | Expected<StringRef> RCParser::readString() { |
| 256 | if (!isNextTokenKind(TokenKind: Kind::String)) |
| 257 | return getExpectedError(Message: "string" ); |
| 258 | return read().value(); |
| 259 | } |
| 260 | |
| 261 | Expected<StringRef> RCParser::readFilename() { |
| 262 | if (!isNextTokenKind(TokenKind: Kind::String) && !isNextTokenKind(TokenKind: Kind::Identifier)) |
| 263 | return getExpectedError(Message: "string" ); |
| 264 | const RCToken &Token = read(); |
| 265 | StringRef Str = Token.value(); |
| 266 | if (Token.kind() != Kind::String) |
| 267 | return Str; |
| 268 | while (isNextTokenKind(TokenKind: Kind::String)) { |
| 269 | const RCToken &NextToken = read(); |
| 270 | StringRef Next = NextToken.value(); |
| 271 | bool IsWide = Str.consume_front_insensitive(Prefix: "L" ); |
| 272 | Next.consume_front_insensitive(Prefix: "L" ); |
| 273 | bool StrUnquoted = Str.consume_front(Prefix: "\"" ) && Str.consume_back(Suffix: "\"" ); |
| 274 | bool NextUnquoted = Next.consume_front(Prefix: "\"" ) && Next.consume_back(Suffix: "\"" ); |
| 275 | assert(StrUnquoted && NextUnquoted); |
| 276 | (void)StrUnquoted; |
| 277 | (void)NextUnquoted; |
| 278 | |
| 279 | Str = Saver.save(S: Twine(IsWide ? "L" : "" ) + "\"" + Str + Next + "\"" ); |
| 280 | } |
| 281 | return Str; |
| 282 | } |
| 283 | |
| 284 | Expected<StringRef> RCParser::readIdentifier() { |
| 285 | if (!isNextTokenKind(TokenKind: Kind::Identifier)) |
| 286 | return getExpectedError(Message: "identifier" ); |
| 287 | return read().value(); |
| 288 | } |
| 289 | |
| 290 | Expected<IntOrString> RCParser::readIntOrString() { |
| 291 | if (!isNextTokenKind(TokenKind: Kind::Int) && !isNextTokenKind(TokenKind: Kind::String)) |
| 292 | return getExpectedError(Message: "int or string" ); |
| 293 | return IntOrString(read()); |
| 294 | } |
| 295 | |
| 296 | Expected<IntOrString> RCParser::readTypeOrName() { |
| 297 | // We suggest that the correct resource name or type should be either an |
| 298 | // identifier or an integer. The original RC tool is much more liberal. |
| 299 | if (!isNextTokenKind(TokenKind: Kind::Identifier) && !isNextTokenKind(TokenKind: Kind::Int)) |
| 300 | return getExpectedError(Message: "int or identifier" ); |
| 301 | return IntOrString(read()); |
| 302 | } |
| 303 | |
| 304 | Error RCParser::consumeType(Kind TokenKind) { |
| 305 | if (isNextTokenKind(TokenKind)) { |
| 306 | consume(); |
| 307 | return Error::success(); |
| 308 | } |
| 309 | |
| 310 | switch (TokenKind) { |
| 311 | #define TOKEN(TokenName) \ |
| 312 | case Kind::TokenName: \ |
| 313 | return getExpectedError(#TokenName); |
| 314 | #define SHORT_TOKEN(TokenName, TokenCh) \ |
| 315 | case Kind::TokenName: \ |
| 316 | return getExpectedError(#TokenCh); |
| 317 | #include "ResourceScriptTokenList.def" |
| 318 | } |
| 319 | |
| 320 | llvm_unreachable("All case options exhausted." ); |
| 321 | } |
| 322 | |
| 323 | bool RCParser::consumeOptionalType(Kind TokenKind) { |
| 324 | if (isNextTokenKind(TokenKind)) { |
| 325 | consume(); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | Expected<SmallVector<RCInt, 8>> RCParser::readIntsWithCommas(size_t MinCount, |
| 333 | size_t MaxCount) { |
| 334 | assert(MinCount <= MaxCount); |
| 335 | |
| 336 | SmallVector<RCInt, 8> Result; |
| 337 | |
| 338 | auto FailureHandler = |
| 339 | [&](llvm::Error Err) -> Expected<SmallVector<RCInt, 8>> { |
| 340 | if (Result.size() < MinCount) |
| 341 | return std::move(Err); |
| 342 | consumeError(Err: std::move(Err)); |
| 343 | return Result; |
| 344 | }; |
| 345 | |
| 346 | for (size_t i = 0; i < MaxCount; ++i) { |
| 347 | // Try to read a comma unless we read the first token. |
| 348 | // Sometimes RC tool requires them and sometimes not. We decide to |
| 349 | // always require them. |
| 350 | if (i >= 1) { |
| 351 | if (auto CommaError = consumeType(TokenKind: Kind::Comma)) |
| 352 | return FailureHandler(std::move(CommaError)); |
| 353 | } |
| 354 | |
| 355 | if (auto IntResult = readInt()) |
| 356 | Result.push_back(Elt: *IntResult); |
| 357 | else |
| 358 | return FailureHandler(IntResult.takeError()); |
| 359 | } |
| 360 | |
| 361 | return std::move(Result); |
| 362 | } |
| 363 | |
| 364 | Expected<uint32_t> RCParser::parseFlags(ArrayRef<StringRef> FlagDesc, |
| 365 | ArrayRef<uint32_t> FlagValues) { |
| 366 | assert(!FlagDesc.empty()); |
| 367 | assert(FlagDesc.size() == FlagValues.size()); |
| 368 | |
| 369 | uint32_t Result = 0; |
| 370 | while (isNextTokenKind(TokenKind: Kind::Comma)) { |
| 371 | consume(); |
| 372 | ASSIGN_OR_RETURN(FlagResult, readIdentifier()); |
| 373 | bool FoundFlag = false; |
| 374 | |
| 375 | for (size_t FlagId = 0; FlagId < FlagDesc.size(); ++FlagId) { |
| 376 | if (!FlagResult->equals_insensitive(RHS: FlagDesc[FlagId])) |
| 377 | continue; |
| 378 | |
| 379 | Result |= FlagValues[FlagId]; |
| 380 | FoundFlag = true; |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | if (!FoundFlag) |
| 385 | return getExpectedError(Message: join(R&: FlagDesc, Separator: "/" ), IsAlreadyRead: true); |
| 386 | } |
| 387 | |
| 388 | return Result; |
| 389 | } |
| 390 | |
| 391 | uint16_t RCParser::parseMemoryFlags(uint16_t Flags) { |
| 392 | while (!isEof()) { |
| 393 | const RCToken &Token = look(); |
| 394 | if (Token.kind() != Kind::Identifier) |
| 395 | return Flags; |
| 396 | const StringRef Ident = Token.value(); |
| 397 | if (Ident.equals_insensitive(RHS: "PRELOAD" )) |
| 398 | Flags |= MfPreload; |
| 399 | else if (Ident.equals_insensitive(RHS: "LOADONCALL" )) |
| 400 | Flags &= ~MfPreload; |
| 401 | else if (Ident.equals_insensitive(RHS: "FIXED" )) |
| 402 | Flags &= ~(MfMoveable | MfDiscardable); |
| 403 | else if (Ident.equals_insensitive(RHS: "MOVEABLE" )) |
| 404 | Flags |= MfMoveable; |
| 405 | else if (Ident.equals_insensitive(RHS: "DISCARDABLE" )) |
| 406 | Flags |= MfDiscardable | MfMoveable | MfPure; |
| 407 | else if (Ident.equals_insensitive(RHS: "PURE" )) |
| 408 | Flags |= MfPure; |
| 409 | else if (Ident.equals_insensitive(RHS: "IMPURE" )) |
| 410 | Flags &= ~(MfPure | MfDiscardable); |
| 411 | else if (Ident.equals_insensitive(RHS: "SHARED" )) |
| 412 | Flags |= MfPure; |
| 413 | else if (Ident.equals_insensitive(RHS: "NONSHARED" )) |
| 414 | Flags &= ~(MfPure | MfDiscardable); |
| 415 | else |
| 416 | return Flags; |
| 417 | consume(); |
| 418 | } |
| 419 | return Flags; |
| 420 | } |
| 421 | |
| 422 | Expected<OptionalStmtList> |
| 423 | RCParser::parseOptionalStatements(OptStmtType StmtsType) { |
| 424 | OptionalStmtList Result; |
| 425 | |
| 426 | // The last statement is always followed by the start of the block. |
| 427 | while (!isNextTokenKind(TokenKind: Kind::BlockBegin)) { |
| 428 | ASSIGN_OR_RETURN(SingleParse, parseSingleOptionalStatement(StmtsType)); |
| 429 | Result.addStmt(Stmt: std::move(*SingleParse)); |
| 430 | } |
| 431 | |
| 432 | return std::move(Result); |
| 433 | } |
| 434 | |
| 435 | Expected<std::unique_ptr<OptionalStmt>> |
| 436 | RCParser::parseSingleOptionalStatement(OptStmtType StmtsType) { |
| 437 | ASSIGN_OR_RETURN(TypeToken, readIdentifier()); |
| 438 | if (TypeToken->equals_insensitive(RHS: "CHARACTERISTICS" )) |
| 439 | return parseCharacteristicsStmt(); |
| 440 | if (TypeToken->equals_insensitive(RHS: "LANGUAGE" )) |
| 441 | return parseLanguageStmt(); |
| 442 | if (TypeToken->equals_insensitive(RHS: "VERSION" )) |
| 443 | return parseVersionStmt(); |
| 444 | |
| 445 | if (StmtsType != OptStmtType::BasicStmt) { |
| 446 | if (TypeToken->equals_insensitive(RHS: "CAPTION" )) |
| 447 | return parseCaptionStmt(); |
| 448 | if (TypeToken->equals_insensitive(RHS: "CLASS" )) |
| 449 | return parseClassStmt(); |
| 450 | if (TypeToken->equals_insensitive(RHS: "EXSTYLE" )) |
| 451 | return parseExStyleStmt(); |
| 452 | if (TypeToken->equals_insensitive(RHS: "FONT" )) |
| 453 | return parseFontStmt(DialogType: StmtsType); |
| 454 | if (TypeToken->equals_insensitive(RHS: "STYLE" )) |
| 455 | return parseStyleStmt(); |
| 456 | if (TypeToken->equals_insensitive(RHS: "MENU" )) |
| 457 | return parseMenuStmt(); |
| 458 | } |
| 459 | |
| 460 | return getExpectedError(Message: "optional statement type, BEGIN or '{'" , |
| 461 | /* IsAlreadyRead = */ true); |
| 462 | } |
| 463 | |
| 464 | RCParser::ParseType RCParser::parseLanguageResource() { |
| 465 | // Read LANGUAGE as an optional statement. If it's read correctly, we can |
| 466 | // upcast it to RCResource. |
| 467 | return parseLanguageStmt(); |
| 468 | } |
| 469 | |
| 470 | RCParser::ParseType RCParser::parseAcceleratorsResource() { |
| 471 | uint16_t MemoryFlags = |
| 472 | parseMemoryFlags(Flags: AcceleratorsResource::getDefaultMemoryFlags()); |
| 473 | ASSIGN_OR_RETURN(OptStatements, parseOptionalStatements()); |
| 474 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 475 | |
| 476 | auto Accels = std::make_unique<AcceleratorsResource>( |
| 477 | args: std::move(*OptStatements), args&: MemoryFlags); |
| 478 | |
| 479 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 480 | ASSIGN_OR_RETURN(EventResult, readIntOrString()); |
| 481 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 482 | ASSIGN_OR_RETURN(IDResult, readInt()); |
| 483 | ASSIGN_OR_RETURN( |
| 484 | FlagsResult, |
| 485 | parseFlags(AcceleratorsResource::Accelerator::OptionsStr, |
| 486 | AcceleratorsResource::Accelerator::OptionsFlags)); |
| 487 | Accels->addAccelerator(Event: *EventResult, Id: *IDResult, Flags: *FlagsResult); |
| 488 | } |
| 489 | |
| 490 | return std::move(Accels); |
| 491 | } |
| 492 | |
| 493 | RCParser::ParseType RCParser::parseCursorResource() { |
| 494 | uint16_t MemoryFlags = |
| 495 | parseMemoryFlags(Flags: CursorResource::getDefaultMemoryFlags()); |
| 496 | ASSIGN_OR_RETURN(Arg, readFilename()); |
| 497 | return std::make_unique<CursorResource>(args&: *Arg, args&: MemoryFlags); |
| 498 | } |
| 499 | |
| 500 | RCParser::ParseType RCParser::parseDialogResource(bool IsExtended) { |
| 501 | uint16_t MemoryFlags = |
| 502 | parseMemoryFlags(Flags: DialogResource::getDefaultMemoryFlags()); |
| 503 | // Dialog resources have the following format of the arguments: |
| 504 | // DIALOG: x, y, width, height [opt stmts...] {controls...} |
| 505 | // DIALOGEX: x, y, width, height [, helpID] [opt stmts...] {controls...} |
| 506 | // These are very similar, so we parse them together. |
| 507 | ASSIGN_OR_RETURN(LocResult, readIntsWithCommas(4, 4)); |
| 508 | |
| 509 | uint32_t HelpID = 0; // When HelpID is unset, it's assumed to be 0. |
| 510 | if (IsExtended && consumeOptionalType(TokenKind: Kind::Comma)) { |
| 511 | ASSIGN_OR_RETURN(HelpIDResult, readInt()); |
| 512 | HelpID = *HelpIDResult; |
| 513 | } |
| 514 | |
| 515 | ASSIGN_OR_RETURN(OptStatements, parseOptionalStatements( |
| 516 | IsExtended ? OptStmtType::DialogExStmt |
| 517 | : OptStmtType::DialogStmt)); |
| 518 | |
| 519 | assert(isNextTokenKind(Kind::BlockBegin) && |
| 520 | "parseOptionalStatements, when successful, halts on BlockBegin." ); |
| 521 | consume(); |
| 522 | |
| 523 | auto Dialog = std::make_unique<DialogResource>( |
| 524 | args&: (*LocResult)[0], args&: (*LocResult)[1], args&: (*LocResult)[2], args&: (*LocResult)[3], |
| 525 | args&: HelpID, args: std::move(*OptStatements), args&: IsExtended, args&: MemoryFlags); |
| 526 | |
| 527 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 528 | ASSIGN_OR_RETURN(ControlDefResult, parseControl()); |
| 529 | Dialog->addControl(Ctl: std::move(*ControlDefResult)); |
| 530 | } |
| 531 | |
| 532 | return std::move(Dialog); |
| 533 | } |
| 534 | |
| 535 | RCParser::ParseType RCParser::parseUserDefinedResource(IntOrString Type) { |
| 536 | uint16_t MemoryFlags = |
| 537 | parseMemoryFlags(Flags: UserDefinedResource::getDefaultMemoryFlags()); |
| 538 | if (isEof()) |
| 539 | return getExpectedError(Message: "filename, '{' or BEGIN" ); |
| 540 | |
| 541 | // Check if this is a file resource. |
| 542 | switch (look().kind()) { |
| 543 | case Kind::String: |
| 544 | case Kind::Identifier: { |
| 545 | ASSIGN_OR_RETURN(Filename, readFilename()); |
| 546 | return std::make_unique<UserDefinedResource>(args&: Type, args&: *Filename, args&: MemoryFlags); |
| 547 | } |
| 548 | default: |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 553 | std::vector<IntOrString> Data; |
| 554 | |
| 555 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 556 | ASSIGN_OR_RETURN(Item, readIntOrString()); |
| 557 | Data.push_back(x: *Item); |
| 558 | |
| 559 | // There can be zero or more commas after each token (but not before |
| 560 | // the first one). |
| 561 | while (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return std::make_unique<UserDefinedResource>(args&: Type, args: std::move(Data), |
| 566 | args&: MemoryFlags); |
| 567 | } |
| 568 | |
| 569 | RCParser::ParseType RCParser::parseVersionInfoResource() { |
| 570 | uint16_t MemoryFlags = |
| 571 | parseMemoryFlags(Flags: VersionInfoResource::getDefaultMemoryFlags()); |
| 572 | ASSIGN_OR_RETURN(FixedResult, parseVersionInfoFixed()); |
| 573 | ASSIGN_OR_RETURN(BlockResult, parseVersionInfoBlockContents(StringRef())); |
| 574 | return std::make_unique<VersionInfoResource>( |
| 575 | args: std::move(**BlockResult), args: std::move(*FixedResult), args&: MemoryFlags); |
| 576 | } |
| 577 | |
| 578 | Expected<Control> RCParser::parseControl() { |
| 579 | // Each control definition (except CONTROL) follows one of the schemes below |
| 580 | // depending on the control class: |
| 581 | // [class] text, id, x, y, width, height [, style] [, exstyle] [, helpID] |
| 582 | // [class] id, x, y, width, height [, style] [, exstyle] [, helpID] |
| 583 | // Note that control ids must be integers. |
| 584 | // Text might be either a string or an integer pointing to resource ID. |
| 585 | ASSIGN_OR_RETURN(ClassResult, readIdentifier()); |
| 586 | std::string ClassUpper = ClassResult->upper(); |
| 587 | auto CtlInfo = Control::SupportedCtls.find(Key: ClassUpper); |
| 588 | if (CtlInfo == Control::SupportedCtls.end()) |
| 589 | return getExpectedError(Message: "control type, END or '}'" , IsAlreadyRead: true); |
| 590 | |
| 591 | // Read caption if necessary. |
| 592 | IntOrString Caption{StringRef()}; |
| 593 | if (CtlInfo->getValue().HasTitle) { |
| 594 | ASSIGN_OR_RETURN(CaptionResult, readIntOrString()); |
| 595 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 596 | Caption = *CaptionResult; |
| 597 | } |
| 598 | |
| 599 | ASSIGN_OR_RETURN(ID, readInt()); |
| 600 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 601 | |
| 602 | IntOrString Class; |
| 603 | std::optional<IntWithNotMask> Style; |
| 604 | if (ClassUpper == "CONTROL" ) { |
| 605 | // CONTROL text, id, class, style, x, y, width, height [, exstyle] [, |
| 606 | // helpID] |
| 607 | ASSIGN_OR_RETURN(ClassStr, readString()); |
| 608 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 609 | Class = *ClassStr; |
| 610 | ASSIGN_OR_RETURN(StyleVal, parseIntExpr1()); |
| 611 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 612 | Style = *StyleVal; |
| 613 | } else { |
| 614 | Class = CtlInfo->getValue().CtlClass; |
| 615 | } |
| 616 | |
| 617 | // x, y, width, height |
| 618 | ASSIGN_OR_RETURN(Args, readIntsWithCommas(4, 4)); |
| 619 | |
| 620 | if (ClassUpper != "CONTROL" ) { |
| 621 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 622 | ASSIGN_OR_RETURN(Val, parseIntExpr1()); |
| 623 | Style = *Val; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | std::optional<uint32_t> ExStyle; |
| 628 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 629 | ASSIGN_OR_RETURN(Val, readInt()); |
| 630 | ExStyle = *Val; |
| 631 | } |
| 632 | std::optional<uint32_t> HelpID; |
| 633 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 634 | ASSIGN_OR_RETURN(Val, readInt()); |
| 635 | HelpID = *Val; |
| 636 | } |
| 637 | |
| 638 | return Control(*ClassResult, Caption, *ID, (*Args)[0], (*Args)[1], (*Args)[2], |
| 639 | (*Args)[3], Style, ExStyle, HelpID, Class); |
| 640 | } |
| 641 | |
| 642 | RCParser::ParseType RCParser::parseBitmapResource() { |
| 643 | uint16_t MemoryFlags = |
| 644 | parseMemoryFlags(Flags: BitmapResource::getDefaultMemoryFlags()); |
| 645 | ASSIGN_OR_RETURN(Arg, readFilename()); |
| 646 | return std::make_unique<BitmapResource>(args&: *Arg, args&: MemoryFlags); |
| 647 | } |
| 648 | |
| 649 | RCParser::ParseType RCParser::parseIconResource() { |
| 650 | uint16_t MemoryFlags = |
| 651 | parseMemoryFlags(Flags: IconResource::getDefaultMemoryFlags()); |
| 652 | ASSIGN_OR_RETURN(Arg, readFilename()); |
| 653 | return std::make_unique<IconResource>(args&: *Arg, args&: MemoryFlags); |
| 654 | } |
| 655 | |
| 656 | RCParser::ParseType RCParser::parseHTMLResource() { |
| 657 | uint16_t MemoryFlags = |
| 658 | parseMemoryFlags(Flags: HTMLResource::getDefaultMemoryFlags()); |
| 659 | ASSIGN_OR_RETURN(Arg, readFilename()); |
| 660 | return std::make_unique<HTMLResource>(args&: *Arg, args&: MemoryFlags); |
| 661 | } |
| 662 | |
| 663 | RCParser::ParseType RCParser::() { |
| 664 | uint16_t MemoryFlags = |
| 665 | parseMemoryFlags(Flags: MenuResource::getDefaultMemoryFlags()); |
| 666 | ASSIGN_OR_RETURN(OptStatements, parseOptionalStatements()); |
| 667 | ASSIGN_OR_RETURN(Items, parseMenuItemsList()); |
| 668 | return std::make_unique<MenuResource>(args: std::move(*OptStatements), |
| 669 | args: std::move(*Items), args&: MemoryFlags); |
| 670 | } |
| 671 | |
| 672 | RCParser::ParseType RCParser::() { |
| 673 | uint16_t MemoryFlags = |
| 674 | parseMemoryFlags(Flags: MenuExResource::getDefaultMemoryFlags()); |
| 675 | ASSIGN_OR_RETURN(Items, parseMenuExItemsList()); |
| 676 | return std::make_unique<MenuExResource>(args: std::move(*Items), args&: MemoryFlags); |
| 677 | } |
| 678 | |
| 679 | Expected<MenuDefinitionList> RCParser::() { |
| 680 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 681 | |
| 682 | MenuDefinitionList List; |
| 683 | |
| 684 | // Read a set of items. Each item is of one of three kinds: |
| 685 | // MENUITEM SEPARATOR |
| 686 | // MENUITEM caption:String, result:Int [, menu flags]... |
| 687 | // POPUP caption:String [, menu flags]... { items... } |
| 688 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 689 | ASSIGN_OR_RETURN(ItemTypeResult, readIdentifier()); |
| 690 | |
| 691 | bool = ItemTypeResult->equals_insensitive(RHS: "MENUITEM" ); |
| 692 | bool = ItemTypeResult->equals_insensitive(RHS: "POPUP" ); |
| 693 | if (!IsMenuItem && !IsPopup) |
| 694 | return getExpectedError(Message: "MENUITEM, POPUP, END or '}'" , IsAlreadyRead: true); |
| 695 | |
| 696 | if (IsMenuItem && isNextTokenKind(TokenKind: Kind::Identifier)) { |
| 697 | // Now, expecting SEPARATOR. |
| 698 | ASSIGN_OR_RETURN(SeparatorResult, readIdentifier()); |
| 699 | if (SeparatorResult->equals_insensitive(RHS: "SEPARATOR" )) { |
| 700 | List.addDefinition(Def: std::make_unique<MenuSeparator>()); |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | return getExpectedError(Message: "SEPARATOR or string" , IsAlreadyRead: true); |
| 705 | } |
| 706 | |
| 707 | // Not a separator. Read the caption. |
| 708 | ASSIGN_OR_RETURN(CaptionResult, readString()); |
| 709 | |
| 710 | // If MENUITEM, expect also a comma and an integer. |
| 711 | uint32_t = -1; |
| 712 | |
| 713 | if (IsMenuItem) { |
| 714 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 715 | ASSIGN_OR_RETURN(IntResult, readInt()); |
| 716 | MenuResult = *IntResult; |
| 717 | } |
| 718 | |
| 719 | ASSIGN_OR_RETURN(FlagsResult, parseFlags(MenuDefinition::OptionsStr, |
| 720 | MenuDefinition::OptionsFlags)); |
| 721 | |
| 722 | if (IsPopup) { |
| 723 | // If POPUP, read submenu items recursively. |
| 724 | ASSIGN_OR_RETURN(, parseMenuItemsList()); |
| 725 | List.addDefinition(Def: std::make_unique<PopupItem>( |
| 726 | args&: *CaptionResult, args&: *FlagsResult, args: std::move(*SubMenuResult))); |
| 727 | continue; |
| 728 | } |
| 729 | |
| 730 | assert(IsMenuItem); |
| 731 | List.addDefinition( |
| 732 | Def: std::make_unique<MenuItem>(args&: *CaptionResult, args&: MenuResult, args&: *FlagsResult)); |
| 733 | } |
| 734 | |
| 735 | return std::move(List); |
| 736 | } |
| 737 | |
| 738 | Expected<MenuDefinitionList> RCParser::() { |
| 739 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 740 | |
| 741 | MenuDefinitionList List; |
| 742 | |
| 743 | // Read a set of items. Each item is of one of two kinds: |
| 744 | // MENUITEM caption:String [,[id][, [type][, state]]]] |
| 745 | // POPUP caption:String [,[id][, [type][, [state][, helpID]]]] { popupBody } |
| 746 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 747 | ASSIGN_OR_RETURN(ItemTypeResult, readIdentifier()); |
| 748 | |
| 749 | bool = ItemTypeResult->equals_insensitive(RHS: "MENUITEM" ); |
| 750 | bool = ItemTypeResult->equals_insensitive(RHS: "POPUP" ); |
| 751 | if (!IsMenuItem && !IsPopup) |
| 752 | return getExpectedError(Message: "MENUITEM, POPUP, END or '}'" , IsAlreadyRead: true); |
| 753 | |
| 754 | // Not a separator. Read the caption. |
| 755 | ASSIGN_OR_RETURN(CaptionResult, readString()); |
| 756 | |
| 757 | // If MENUITEM, expect [,[id][, [type][, state]]]] |
| 758 | if (IsMenuItem) { |
| 759 | uint32_t = 0; |
| 760 | uint32_t = 0; |
| 761 | uint32_t = 0; |
| 762 | |
| 763 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 764 | auto IntId = readInt(); |
| 765 | if (IntId) { |
| 766 | MenuId = *IntId; |
| 767 | } |
| 768 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 769 | auto IntType = readInt(); |
| 770 | if (IntType) { |
| 771 | MenuType = *IntType; |
| 772 | } |
| 773 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 774 | auto IntState = readInt(); |
| 775 | if (IntState) { |
| 776 | MenuState = *IntState; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | List.addDefinition(Def: std::make_unique<MenuExItem>(args&: *CaptionResult, args&: MenuId, |
| 782 | args&: MenuType, args&: MenuState)); |
| 783 | continue; |
| 784 | } |
| 785 | |
| 786 | assert(IsPopup); |
| 787 | |
| 788 | uint32_t = 0; |
| 789 | uint32_t = 0; |
| 790 | uint32_t = 0; |
| 791 | uint32_t = 0; |
| 792 | |
| 793 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 794 | auto IntId = readInt(); |
| 795 | if (IntId) { |
| 796 | PopupId = *IntId; |
| 797 | } |
| 798 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 799 | auto IntType = readInt(); |
| 800 | if (IntType) { |
| 801 | PopupType = *IntType; |
| 802 | } |
| 803 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 804 | auto IntState = readInt(); |
| 805 | if (IntState) { |
| 806 | PopupState = *IntState; |
| 807 | } |
| 808 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 809 | auto IntHelpID = readInt(); |
| 810 | if (IntHelpID) { |
| 811 | PopupHelpID = *IntHelpID; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | // If POPUP, read submenu items recursively. |
| 818 | ASSIGN_OR_RETURN(, parseMenuExItemsList()); |
| 819 | List.addDefinition(Def: std::make_unique<PopupExItem>( |
| 820 | args&: *CaptionResult, args&: PopupId, args&: PopupType, args&: PopupState, args&: PopupHelpID, |
| 821 | args: std::move(*SubMenuResult))); |
| 822 | } |
| 823 | |
| 824 | return std::move(List); |
| 825 | } |
| 826 | |
| 827 | RCParser::ParseType RCParser::parseStringTableResource() { |
| 828 | uint16_t MemoryFlags = |
| 829 | parseMemoryFlags(Flags: StringTableResource::getDefaultMemoryFlags()); |
| 830 | ASSIGN_OR_RETURN(OptStatements, parseOptionalStatements()); |
| 831 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 832 | |
| 833 | auto Table = std::make_unique<StringTableResource>(args: std::move(*OptStatements), |
| 834 | args&: MemoryFlags); |
| 835 | |
| 836 | // Read strings until we reach the end of the block. |
| 837 | while (!consumeOptionalType(TokenKind: Kind::BlockEnd)) { |
| 838 | // Each definition consists of string's ID (an integer) and a string. |
| 839 | // Some examples in documentation suggest that there might be a comma in |
| 840 | // between, however we strictly adhere to the single statement definition. |
| 841 | ASSIGN_OR_RETURN(IDResult, readInt()); |
| 842 | consumeOptionalType(TokenKind: Kind::Comma); |
| 843 | |
| 844 | std::vector<StringRef> Strings; |
| 845 | ASSIGN_OR_RETURN(StrResult, readString()); |
| 846 | Strings.push_back(x: *StrResult); |
| 847 | while (isNextTokenKind(TokenKind: Kind::String)) |
| 848 | Strings.push_back(x: read().value()); |
| 849 | |
| 850 | Table->addStrings(ID: *IDResult, Strings: std::move(Strings)); |
| 851 | } |
| 852 | |
| 853 | return std::move(Table); |
| 854 | } |
| 855 | |
| 856 | Expected<std::unique_ptr<VersionInfoBlock>> |
| 857 | RCParser::parseVersionInfoBlockContents(StringRef BlockName) { |
| 858 | RETURN_IF_ERROR(consumeType(Kind::BlockBegin)); |
| 859 | |
| 860 | auto Contents = std::make_unique<VersionInfoBlock>(args&: BlockName); |
| 861 | |
| 862 | while (!isNextTokenKind(TokenKind: Kind::BlockEnd)) { |
| 863 | ASSIGN_OR_RETURN(Stmt, parseVersionInfoStmt()); |
| 864 | Contents->addStmt(Stmt: std::move(*Stmt)); |
| 865 | } |
| 866 | |
| 867 | consume(); // Consume BlockEnd. |
| 868 | |
| 869 | return std::move(Contents); |
| 870 | } |
| 871 | |
| 872 | Expected<std::unique_ptr<VersionInfoStmt>> RCParser::parseVersionInfoStmt() { |
| 873 | // Expect either BLOCK or VALUE, then a name or a key (a string). |
| 874 | ASSIGN_OR_RETURN(TypeResult, readIdentifier()); |
| 875 | |
| 876 | if (TypeResult->equals_insensitive(RHS: "BLOCK" )) { |
| 877 | ASSIGN_OR_RETURN(NameResult, readString()); |
| 878 | return parseVersionInfoBlockContents(BlockName: *NameResult); |
| 879 | } |
| 880 | |
| 881 | if (TypeResult->equals_insensitive(RHS: "VALUE" )) { |
| 882 | ASSIGN_OR_RETURN(KeyResult, readString()); |
| 883 | // Read a non-empty list of strings and/or ints, each |
| 884 | // possibly preceded by a comma. Unfortunately, the tool behavior depends |
| 885 | // on them existing or not, so we need to memorize where we found them. |
| 886 | std::vector<IntOrString> Values; |
| 887 | BitVector PrecedingCommas; |
| 888 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 889 | while (!isNextTokenKind(TokenKind: Kind::Identifier) && |
| 890 | !isNextTokenKind(TokenKind: Kind::BlockEnd)) { |
| 891 | // Try to eat a comma if it's not the first statement. |
| 892 | bool HadComma = Values.size() > 0 && consumeOptionalType(TokenKind: Kind::Comma); |
| 893 | ASSIGN_OR_RETURN(ValueResult, readIntOrString()); |
| 894 | Values.push_back(x: *ValueResult); |
| 895 | PrecedingCommas.push_back(Val: HadComma); |
| 896 | } |
| 897 | return std::make_unique<VersionInfoValue>(args&: *KeyResult, args: std::move(Values), |
| 898 | args: std::move(PrecedingCommas)); |
| 899 | } |
| 900 | |
| 901 | return getExpectedError(Message: "BLOCK or VALUE" , IsAlreadyRead: true); |
| 902 | } |
| 903 | |
| 904 | Expected<VersionInfoResource::VersionInfoFixed> |
| 905 | RCParser::parseVersionInfoFixed() { |
| 906 | using RetType = VersionInfoResource::VersionInfoFixed; |
| 907 | RetType Result; |
| 908 | |
| 909 | // Read until the beginning of the block. |
| 910 | while (!isNextTokenKind(TokenKind: Kind::BlockBegin)) { |
| 911 | ASSIGN_OR_RETURN(TypeResult, readIdentifier()); |
| 912 | auto FixedType = RetType::getFixedType(Type: *TypeResult); |
| 913 | |
| 914 | if (!RetType::isTypeSupported(Type: FixedType)) |
| 915 | return getExpectedError(Message: "fixed VERSIONINFO statement type" , IsAlreadyRead: true); |
| 916 | if (Result.IsTypePresent[FixedType]) |
| 917 | return getExpectedError(Message: "yet unread fixed VERSIONINFO statement type" , |
| 918 | IsAlreadyRead: true); |
| 919 | |
| 920 | // VERSION variations take multiple integers. |
| 921 | size_t NumInts = RetType::isVersionType(Type: FixedType) ? 4 : 1; |
| 922 | ASSIGN_OR_RETURN(ArgsResult, readIntsWithCommas(1, NumInts)); |
| 923 | SmallVector<uint32_t, 4> ArgInts(ArgsResult->begin(), ArgsResult->end()); |
| 924 | while (ArgInts.size() < NumInts) |
| 925 | ArgInts.push_back(Elt: 0); |
| 926 | Result.setValue(Type: FixedType, Value: ArgInts); |
| 927 | } |
| 928 | |
| 929 | return Result; |
| 930 | } |
| 931 | |
| 932 | RCParser::ParseOptionType RCParser::parseLanguageStmt() { |
| 933 | ASSIGN_OR_RETURN(Args, readIntsWithCommas(/* min = */ 2, /* max = */ 2)); |
| 934 | return std::make_unique<LanguageResource>(args&: (*Args)[0], args&: (*Args)[1]); |
| 935 | } |
| 936 | |
| 937 | RCParser::ParseOptionType RCParser::parseCharacteristicsStmt() { |
| 938 | ASSIGN_OR_RETURN(Arg, readInt()); |
| 939 | return std::make_unique<CharacteristicsStmt>(args&: *Arg); |
| 940 | } |
| 941 | |
| 942 | RCParser::ParseOptionType RCParser::parseVersionStmt() { |
| 943 | ASSIGN_OR_RETURN(Arg, readInt()); |
| 944 | return std::make_unique<VersionStmt>(args&: *Arg); |
| 945 | } |
| 946 | |
| 947 | RCParser::ParseOptionType RCParser::parseCaptionStmt() { |
| 948 | ASSIGN_OR_RETURN(Arg, readString()); |
| 949 | return std::make_unique<CaptionStmt>(args&: *Arg); |
| 950 | } |
| 951 | |
| 952 | RCParser::ParseOptionType RCParser::parseClassStmt() { |
| 953 | ASSIGN_OR_RETURN(Arg, readIntOrString()); |
| 954 | return std::make_unique<ClassStmt>(args&: *Arg); |
| 955 | } |
| 956 | |
| 957 | RCParser::ParseOptionType RCParser::parseFontStmt(OptStmtType DialogType) { |
| 958 | assert(DialogType != OptStmtType::BasicStmt); |
| 959 | |
| 960 | ASSIGN_OR_RETURN(SizeResult, readInt()); |
| 961 | RETURN_IF_ERROR(consumeType(Kind::Comma)); |
| 962 | ASSIGN_OR_RETURN(NameResult, readString()); |
| 963 | |
| 964 | // Default values for the optional arguments. |
| 965 | uint32_t FontWeight = 0; |
| 966 | bool FontItalic = false; |
| 967 | uint32_t FontCharset = 1; |
| 968 | if (DialogType == OptStmtType::DialogExStmt) { |
| 969 | if (consumeOptionalType(TokenKind: Kind::Comma)) { |
| 970 | ASSIGN_OR_RETURN(Args, readIntsWithCommas(/* min = */ 0, /* max = */ 3)); |
| 971 | if (Args->size() >= 1) |
| 972 | FontWeight = (*Args)[0]; |
| 973 | if (Args->size() >= 2) |
| 974 | FontItalic = (*Args)[1] != 0; |
| 975 | if (Args->size() >= 3) |
| 976 | FontCharset = (*Args)[2]; |
| 977 | } |
| 978 | } |
| 979 | return std::make_unique<FontStmt>(args&: *SizeResult, args&: *NameResult, args&: FontWeight, |
| 980 | args&: FontItalic, args&: FontCharset); |
| 981 | } |
| 982 | |
| 983 | RCParser::ParseOptionType RCParser::parseStyleStmt() { |
| 984 | ASSIGN_OR_RETURN(Arg, readInt()); |
| 985 | return std::make_unique<StyleStmt>(args&: *Arg); |
| 986 | } |
| 987 | |
| 988 | RCParser::ParseOptionType RCParser::parseExStyleStmt() { |
| 989 | ASSIGN_OR_RETURN(Arg, readInt()); |
| 990 | return std::make_unique<ExStyleStmt>(args&: *Arg); |
| 991 | } |
| 992 | |
| 993 | RCParser::ParseOptionType RCParser::() { |
| 994 | ASSIGN_OR_RETURN(Arg, readIntOrString()); |
| 995 | return std::make_unique<MenuStmt>(args&: *Arg); |
| 996 | } |
| 997 | |
| 998 | Error RCParser::getExpectedError(const Twine &Message, bool IsAlreadyRead) { |
| 999 | return make_error<ParserError>( |
| 1000 | Args: Message, Args: IsAlreadyRead ? std::prev(x: CurLoc) : CurLoc, Args: End); |
| 1001 | } |
| 1002 | |
| 1003 | } // namespace rc |
| 1004 | } // namespace llvm |
| 1005 | |