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
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
//
// Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
// Copyright (C) 2013 LunarG, Inc.
// Copyright (C) 2017 ARM Limited.
// Copyright (C) 2015-2018 Google, Inc.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//    Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
//    Redistributions in binary form must reproduce the above
//    copyright notice, this list of conditions and the following
//    disclaimer in the documentation and/or other materials provided
//    with the distribution.
//
//    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.

NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms.  If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.

THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.

IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <cstdlib>
#include <cstring>

#include "PpContext.h"
#include "PpTokens.h"
#include "../Scan.h"

namespace glslang {

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Floating point constants: /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

// 
// Scan a single- or double-precision floating point constant.
// Assumes that the scanner has seen at least one digit,
// followed by either a decimal '.' or the letter 'e', or a
// precision ending (e.g., F or LF).
//
// This is technically not correct, as the preprocessor should just
// accept the numeric literal along with whatever suffix it has, but
// currently, it stops on seeing a bad suffix, treating that as the
// next token. This effects things like token pasting, where it is
// relevant how many tokens something was broken into.
//
// See peekContinuedPasting().
int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
{
    const auto saveName = [&](int ch) {
        if (len <= MaxTokenLength)
            ppToken->name[len++] = static_cast<char>(ch);
    };

    // find the range of non-zero digits before the decimal point
    int startNonZero = 0;
    while (startNonZero < len && ppToken->name[startNonZero] == '0')
        ++startNonZero;
    int endNonZero = len;
    while (endNonZero > startNonZero && ppToken->name[endNonZero-1] == '0')
        --endNonZero;
    int numWholeNumberDigits = endNonZero - startNonZero;

    // accumulate the range's value
    bool fastPath = numWholeNumberDigits <= 15;  // when the number gets too complex, set to false
    unsigned long long wholeNumber = 0;
    if (fastPath) {
        for (int i = startNonZero; i < endNonZero; ++i)
            wholeNumber = wholeNumber * 10 + (ppToken->name[i] - '0');
    }
    int decimalShift = len - endNonZero;

    // Decimal point:
    bool hasDecimalOrExponent = false;
    if (ch == '.') {
        hasDecimalOrExponent = true;
        saveName(ch);
        ch = getChar();
        int firstDecimal = len;

#ifdef ENABLE_HLSL
        // 1.#INF or -1.#INF
        if (ch == '#' && (ifdepth > 0 || parseContext.intermediate.getSource() == EShSourceHlsl)) {
            if ((len <  2) ||
                (len == 2 && ppToken->name[0] != '1') ||
                (len == 3 && ppToken->name[1] != '1' && !(ppToken->name[0] == '-' || ppToken->name[0] == '+')) ||
                (len >  3))
                parseContext.ppError(ppToken->loc, "unexpected use of", "#", "");
            else {
                // we have 1.# or -1.# or +1.#, check for 'INF'
                if ((ch = getChar()) != 'I' ||
                    (ch = getChar()) != 'N' ||
                    (ch = getChar()) != 'F')
                    parseContext.ppError(ppToken->loc, "expected 'INF'", "#", "");
                else {
                    // we have [+-].#INF, and we are targeting IEEE 754, so wrap it up:
                    saveName('I');
                    saveName('N');
                    saveName('F');
                    ppToken->name[len] = '\0';
                    if (ppToken->name[0] == '-')
                        ppToken->i64val = 0xfff0000000000000; // -Infinity
                    else
                        ppToken->i64val = 0x7ff0000000000000; // +Infinity
                    return PpAtomConstFloat;
                }
            }
        }
#endif

        // Consume leading-zero digits after the decimal point
        while (ch == '0') {
            saveName(ch);
            ch = getChar();
        }
        int startNonZeroDecimal = len;
        int endNonZeroDecimal = len;

        // Consume remaining digits, up to the exponent
        while (ch >= '0' && ch <= '9') {
            saveName(ch);
            if (ch != '0')
                endNonZeroDecimal = len;
            ch = getChar();
        }

        // Compute accumulation up to the last non-zero digit
        if (endNonZeroDecimal > startNonZeroDecimal) {
            numWholeNumberDigits += endNonZeroDecimal - endNonZero - 1; // don't include the "."
            if (numWholeNumberDigits > 15)
                fastPath = false;
            if (fastPath) {
                for (int i = endNonZero; i < endNonZeroDecimal; ++i) {
                    if (ppToken->name[i] != '.')
                        wholeNumber = wholeNumber * 10 + (ppToken->name[i] - '0');
                }
            }
            decimalShift = firstDecimal - endNonZeroDecimal;
        }
    }

    // Exponent:
    bool negativeExponent = false;
    double exponentValue = 0.0;
    int exponent = 0;
    {
        if (ch == 'e' || ch == 'E') {
            hasDecimalOrExponent = true;
            saveName(ch);
            ch = getChar();
            if (ch == '+' || ch == '-') {
                negativeExponent = ch == '-';
                saveName(ch);
                ch = getChar();
            }
            if (ch >= '0' && ch <= '9') {
                while (ch >= '0' && ch <= '9') {
                    exponent = exponent * 10 + (ch - '0');
                    saveName(ch);
                    ch = getChar();
                }
            } else {
                parseContext.ppError(ppToken->loc, "bad character in float exponent", "", "");
            }
        }

        // Compensate for location of decimal
        if (negativeExponent)
            exponent -= decimalShift;
        else {
            exponent += decimalShift;
            if (exponent < 0) {
                negativeExponent = true;
                exponent = -exponent;
            }
        }
        if (exponent > 22)
            fastPath = false;

        if (fastPath) {
            // Compute the floating-point value of the exponent
            exponentValue = 1.0;
            if (exponent > 0) {
                double expFactor = 10;
                while (exponent > 0) {
                    if (exponent & 0x1)
                        exponentValue *= expFactor;
                    expFactor *= expFactor;
                    exponent >>= 1;
                }
            }
        }
    }

    // Suffix:
    bool isDouble = false;<--- Assignment 'isDouble=false', assigned value is 0<--- Assignment 'isDouble=false', assigned value is 0
    bool isFloat16 = false;<--- Assignment 'isFloat16=false', assigned value is 0<--- Assignment 'isFloat16=false', assigned value is 0
#ifndef GLSLANG_WEB
    if (ch == 'l' || ch == 'L') {
        if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl)
            parseContext.doubleCheck(ppToken->loc, "double floating-point suffix");
        if (ifdepth == 0 && !hasDecimalOrExponent)
            parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
        if (parseContext.intermediate.getSource() == EShSourceGlsl) {
            int ch2 = getChar();
            if (ch2 != 'f' && ch2 != 'F') {
                ungetChar();
                ungetChar();
            } else {
                saveName(ch);
                saveName(ch2);
                isDouble = true;
            }
        } else if (parseContext.intermediate.getSource() == EShSourceHlsl) {
            saveName(ch);
            isDouble = true;
        }
    } else if (ch == 'h' || ch == 'H') {
        if (ifdepth == 0 && parseContext.intermediate.getSource() == EShSourceGlsl)
            parseContext.float16Check(ppToken->loc, "half floating-point suffix");
        if (ifdepth == 0 && !hasDecimalOrExponent)
            parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
        if (parseContext.intermediate.getSource() == EShSourceGlsl) {
            int ch2 = getChar();
            if (ch2 != 'f' && ch2 != 'F') {
                ungetChar();
                ungetChar();
            } else {
                saveName(ch);
                saveName(ch2);
                isFloat16 = true;
            }
        } else if (parseContext.intermediate.getSource() == EShSourceHlsl) {
            saveName(ch);
            isFloat16 = true;
        }
    } else
#endif
    if (ch == 'f' || ch == 'F') {
#ifndef GLSLANG_WEB
        if (ifdepth == 0)
            parseContext.profileRequires(ppToken->loc,  EEsProfile, 300, nullptr, "floating-point suffix");
        if (ifdepth == 0 && !parseContext.relaxedErrors())
            parseContext.profileRequires(ppToken->loc, ~EEsProfile, 120, nullptr, "floating-point suffix");
#endif
        if (ifdepth == 0 && !hasDecimalOrExponent)
            parseContext.ppError(ppToken->loc, "float literal needs a decimal point or exponent", "", "");
        saveName(ch);
    } else
        ungetChar();

    // Patch up the name and length for overflow

    if (len > MaxTokenLength) {<--- Assuming condition is false<--- Assuming condition is false
        len = MaxTokenLength;
        parseContext.ppError(ppToken->loc, "float literal too long", "", "");
    }
    ppToken->name[len] = '\0';

    // Compute the numerical value
    if (fastPath) {
        // compute the floating-point value of the exponent
        if (exponentValue == 0.0)
            ppToken->dval = (double)wholeNumber;
        else if (negativeExponent)
            ppToken->dval = (double)wholeNumber / exponentValue;
        else
            ppToken->dval = (double)wholeNumber * exponentValue;
    } else {
        // slow path
        ppToken->dval = 0.0;

        // remove suffix
        TString numstr(ppToken->name);
        if (numstr.back() == 'f' || numstr.back() == 'F')
            numstr.pop_back();
        if (numstr.back() == 'h' || numstr.back() == 'H')
            numstr.pop_back();
        if (numstr.back() == 'l' || numstr.back() == 'L')
            numstr.pop_back();

        // use platform library
        strtodStream.clear();
        strtodStream.str(numstr.c_str());
        strtodStream >> ppToken->dval;
        if (strtodStream.fail()) {
            // Assume failure combined with a large exponent was overflow, in
            // an attempt to set INF.
            if (!negativeExponent && exponent + numWholeNumberDigits > 300)
                ppToken->i64val = 0x7ff0000000000000; // +Infinity
            // Assume failure combined with a small exponent was overflow.
            if (negativeExponent && exponent + numWholeNumberDigits > 300)
                ppToken->dval = 0.0;
            // Unknown reason for failure. Theory is that either
            //  - the 0.0 is still there, or
            //  - something reasonable was written that is better than 0.0
        }
    }

    // Return the right token type
    if (isDouble)<--- Condition 'isDouble' is always false<--- Condition 'isDouble' is always false
        return PpAtomConstDouble;
    else if (isFloat16)<--- Condition 'isFloat16' is always false<--- Condition 'isFloat16' is always false
        return PpAtomConstFloat16;
    else
        return PpAtomConstFloat;
}

// Recognize a character literal.
//
// The first ' has already been accepted, read the rest, through the closing '.
//
// Always returns PpAtomConstInt.
//
int TPpContext::characterLiteral(TPpToken* ppToken)
{
    ppToken->name[0] = 0;
    ppToken->ival = 0;

    if (parseContext.intermediate.getSource() != EShSourceHlsl) {
        // illegal, except in macro definition, for which case we report the character
        return '\'';
    }

    int ch = getChar();
    switch (ch) {
    case '\'':
        // As empty sequence:  ''
        parseContext.ppError(ppToken->loc, "unexpected", "\'", "");
        return PpAtomConstInt;
    case '\\':
        // As escape sequence:  '\XXX'
        switch (ch = getChar()) {
        case 'a':
            ppToken->ival = 7;
            break;
        case 'b':
            ppToken->ival = 8;
            break;
        case 't':
            ppToken->ival = 9;
            break;
        case 'n':
            ppToken->ival = 10;
            break;
        case 'v':
            ppToken->ival = 11;
            break;
        case 'f':
            ppToken->ival = 12;
            break;
        case 'r':
            ppToken->ival = 13;
            break;
        case 'x':
        case '0':
            parseContext.ppError(ppToken->loc, "octal and hex sequences not supported", "\\", "");
            break;
        default:
            // This catches '\'', '\"', '\?', etc.
            // Also, things like '\C' mean the same thing as 'C'
            // (after the above cases are filtered out).
            ppToken->ival = ch;
            break;
        }
        break;
    default:
        ppToken->ival = ch;
        break;
    }
    ppToken->name[0] = (char)ppToken->ival;
    ppToken->name[1] = '\0';
    ch = getChar();
    if (ch != '\'') {
        parseContext.ppError(ppToken->loc, "expected", "\'", "");
        // Look ahead for a closing '
        do {
            ch = getChar();
        } while (ch != '\'' && ch != EndOfInput && ch != '\n');
    }

    return PpAtomConstInt;
}

//
// Scanner used to tokenize source stream.
//
// N.B. Invalid numeric suffixes are not consumed.//
// This is technically not correct, as the preprocessor should just
// accept the numeric literal along with whatever suffix it has, but
// currently, it stops on seeing a bad suffix, treating that as the
// next token. This effects things like token pasting, where it is
// relevant how many tokens something was broken into.
// See peekContinuedPasting().
//
int TPpContext::tStringInput::scan(TPpToken* ppToken)
{
    int AlreadyComplained = 0;
    int len = 0;
    int ch = 0;
    int ii = 0;
    unsigned long long ival = 0;
    const auto floatingPointChar = [&](int ch) { return ch == '.' || ch == 'e' || ch == 'E' ||
                                                                     ch == 'f' || ch == 'F' ||
                                                                     ch == 'h' || ch == 'H'; };

    static const char* const Int64_Extensions[] = {
        E_GL_ARB_gpu_shader_int64,
        E_GL_EXT_shader_explicit_arithmetic_types,
        E_GL_EXT_shader_explicit_arithmetic_types_int64 };
    static const int Num_Int64_Extensions = sizeof(Int64_Extensions) / sizeof(Int64_Extensions[0]);

    static const char* const Int16_Extensions[] = {
        E_GL_AMD_gpu_shader_int16,
        E_GL_EXT_shader_explicit_arithmetic_types,
        E_GL_EXT_shader_explicit_arithmetic_types_int16 };
    static const int Num_Int16_Extensions = sizeof(Int16_Extensions) / sizeof(Int16_Extensions[0]);

    ppToken->ival = 0;
    ppToken->i64val = 0;
    ppToken->space = false;
    ch = getch();
    for (;;) {
        while (ch == ' ' || ch == '\t') {
            ppToken->space = true;
            ch = getch();
        }

        ppToken->loc = pp->parseContext.getCurrentLoc();
        len = 0;
        switch (ch) {
        default:
            // Single character token, including EndOfInput, '#' and '\' (escaped newlines are handled at a lower level, so this is just a '\' token)
            if (ch > PpAtomMaxSingle)
                ch = PpAtomBadToken;
            return ch;

        case 'A': case 'B': case 'C': case 'D': case 'E':
        case 'F': case 'G': case 'H': case 'I': case 'J':
        case 'K': case 'L': case 'M': case 'N': case 'O':
        case 'P': case 'Q': case 'R': case 'S': case 'T':
        case 'U': case 'V': case 'W': case 'X': case 'Y':
        case 'Z': case '_':
        case 'a': case 'b': case 'c': case 'd': case 'e':
        case 'f': case 'g': case 'h': case 'i': case 'j':
        case 'k': case 'l': case 'm': case 'n': case 'o':
        case 'p': case 'q': case 'r': case 's': case 't':
        case 'u': case 'v': case 'w': case 'x': case 'y':
        case 'z':
            do {
                if (len < MaxTokenLength) {
                    ppToken->name[len++] = (char)ch;
                    ch = getch();
                } else {
                    if (! AlreadyComplained) {
                        pp->parseContext.ppError(ppToken->loc, "name too long", "", "");
                        AlreadyComplained = 1;
                    }
                    ch = getch();
                }
            } while ((ch >= 'a' && ch <= 'z') ||
                     (ch >= 'A' && ch <= 'Z') ||
                     (ch >= '0' && ch <= '9') ||
                     ch == '_');

            // line continuation with no token before or after makes len == 0, and need to start over skipping white space, etc.
            if (len == 0)
                continue;

            ppToken->name[len] = '\0';
            ungetch();
            return PpAtomIdentifier;
        case '0':
            ppToken->name[len++] = (char)ch;
            ch = getch();
            if (ch == 'x' || ch == 'X') {
                // must be hexadecimal

                bool isUnsigned = false;
                bool isInt64 = false;<--- Assignment 'isInt64=false', assigned value is 0
                bool isInt16 = false;<--- Assignment 'isInt16=false', assigned value is 0
                ppToken->name[len++] = (char)ch;
                ch = getch();
                if ((ch >= '0' && ch <= '9') ||
                    (ch >= 'A' && ch <= 'F') ||
                    (ch >= 'a' && ch <= 'f')) {

                    ival = 0;
                    do {
                        if (len < MaxTokenLength && ival <= 0x0fffffffffffffffull) {
                            ppToken->name[len++] = (char)ch;
                            if (ch >= '0' && ch <= '9') {
                                ii = ch - '0';
                            } else if (ch >= 'A' && ch <= 'F') {
                                ii = ch - 'A' + 10;
                            } else if (ch >= 'a' && ch <= 'f') {
                                ii = ch - 'a' + 10;
                            } else
                                pp->parseContext.ppError(ppToken->loc, "bad digit in hexadecimal literal", "", "");
                            ival = (ival << 4) | ii;
                        } else {
                            if (! AlreadyComplained) {
                                if(len < MaxTokenLength)
                                    pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too big", "", "");
                                else
                                    pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too long", "", "");
                                AlreadyComplained = 1;
                            }
                            ival = 0xffffffffffffffffull;
                        }
                        ch = getch();
                    } while ((ch >= '0' && ch <= '9') ||
                             (ch >= 'A' && ch <= 'F') ||
                             (ch >= 'a' && ch <= 'f'));
                } else {
                    pp->parseContext.ppError(ppToken->loc, "bad digit in hexadecimal literal", "", "");
                }
                if (ch == 'u' || ch == 'U') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isUnsigned = true;

#ifndef GLSLANG_WEB
                    int nextCh = getch();
                    if (nextCh == 'l' || nextCh == 'L') {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt64 = true;
                    } else
                        ungetch();

                    nextCh = getch();
                    if ((nextCh == 's' || nextCh == 'S') &&
                            pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt16 = true;
                    } else
                        ungetch();
                } else if (ch == 'l' || ch == 'L') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt64 = true;
                } else if ((ch == 's' || ch == 'S') &&
                           pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt16 = true;
#endif
                } else
                    ungetch();
                ppToken->name[len] = '\0';

                if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {<--- Condition 'isInt64' is always false
                    if (pp->ifdepth == 0) {
                        pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                        "64-bit hexadecimal literal");
                        pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                            Num_Int64_Extensions, Int64_Extensions, "64-bit hexadecimal literal");
                    }
                    ppToken->i64val = ival;
                    return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
                } else if (isInt16) {<--- Condition 'isInt16' is always false
                    if (pp->ifdepth == 0) {
                        if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                            pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                             "16-bit hexadecimal literal");
                            pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                                Num_Int16_Extensions, Int16_Extensions, "16-bit hexadecimal literal");
                        }
                    }
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
                } else {
                    if (ival > 0xffffffffu && !AlreadyComplained)
                        pp->parseContext.ppError(ppToken->loc, "hexadecimal literal too big", "", "");
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
                }
            } else {
                // could be octal integer or floating point, speculative pursue octal until it must be floating point

                bool isUnsigned = false;
                bool isInt64 = false;
                bool isInt16 = false;
                bool octalOverflow = false;
                bool nonOctal = false;
                ival = 0;

                // see how much octal-like stuff we can read
                while (ch >= '0' && ch <= '7') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    else if (! AlreadyComplained) {
                        pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
                        AlreadyComplained = 1;
                    }
                    if (ival <= 0x1fffffffffffffffull) {
                        ii = ch - '0';
                        ival = (ival << 3) | ii;
                    } else
                        octalOverflow = true;
                    ch = getch();
                }

                // could be part of a float...
                if (ch == '8' || ch == '9') {
                    nonOctal = true;
                    do {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)ch;
                        else if (! AlreadyComplained) {
                            pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
                            AlreadyComplained = 1;
                        }
                        ch = getch();
                    } while (ch >= '0' && ch <= '9');
                }
                if (floatingPointChar(ch))
                    return pp->lFloatConst(len, ch, ppToken);

                // wasn't a float, so must be octal...
                if (nonOctal)
                    pp->parseContext.ppError(ppToken->loc, "octal literal digit too large", "", "");

                if (ch == 'u' || ch == 'U') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isUnsigned = true;

#ifndef GLSLANG_WEB
                    int nextCh = getch();
                    if (nextCh == 'l' || nextCh == 'L') {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt64 = true;
                    } else
                        ungetch();

                    nextCh = getch();
                    if ((nextCh == 's' || nextCh == 'S') && 
                                pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt16 = true;
                    } else
                        ungetch();
                } else if (ch == 'l' || ch == 'L') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt64 = true;
                } else if ((ch == 's' || ch == 'S') && 
                                pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt16 = true;
#endif
                } else
                    ungetch();
                ppToken->name[len] = '\0';

                if (!isInt64 && ival > 0xffffffffu)
                    octalOverflow = true;

                if (octalOverflow)
                    pp->parseContext.ppError(ppToken->loc, "octal literal too big", "", "");

                if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                    if (pp->ifdepth == 0) {
                        pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                        "64-bit octal literal");
                        pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                            Num_Int64_Extensions, Int64_Extensions, "64-bit octal literal");
                    }
                    ppToken->i64val = ival;
                    return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
                } else if (isInt16) {
                    if (pp->ifdepth == 0) {
                        if (pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                            pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                            "16-bit octal literal");
                            pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                                Num_Int16_Extensions, Int16_Extensions, "16-bit octal literal");
                        }
                    }
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
                } else {
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
                }
            }
            break;
        case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            // can't be hexadecimal or octal, is either decimal or floating point

            do {
                if (len < MaxTokenLength)
                    ppToken->name[len++] = (char)ch;
                else if (! AlreadyComplained) {
                    pp->parseContext.ppError(ppToken->loc, "numeric literal too long", "", "");
                    AlreadyComplained = 1;
                }
                ch = getch();
            } while (ch >= '0' && ch <= '9');
            if (floatingPointChar(ch))
                return pp->lFloatConst(len, ch, ppToken);
            else {
                // Finish handling signed and unsigned integers
                int numericLen = len;
                bool isUnsigned = false;
                bool isInt64 = false;<--- Assignment 'isInt64=false', assigned value is 0<--- Assignment 'isInt64=false', assigned value is 0
                bool isInt16 = false;<--- Assignment 'isInt16=false', assigned value is 0<--- Assignment 'isInt16=false', assigned value is 0
                if (ch == 'u' || ch == 'U') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isUnsigned = true;

#ifndef GLSLANG_WEB
                    int nextCh = getch();
                    if (nextCh == 'l' || nextCh == 'L') {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt64 = true;
                    } else
                        ungetch();

                    nextCh = getch();
                    if ((nextCh == 's' || nextCh == 'S') &&
                                pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                        if (len < MaxTokenLength)
                            ppToken->name[len++] = (char)nextCh;
                        isInt16 = true;
                    } else
                        ungetch();
                } else if (ch == 'l' || ch == 'L') {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt64 = true;
                } else if ((ch == 's' || ch == 'S') &&
                                pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                    if (len < MaxTokenLength)
                        ppToken->name[len++] = (char)ch;
                    isInt16 = true;
#endif
                } else
                    ungetch();

                ppToken->name[len] = '\0';
                ival = 0;
                const unsigned oneTenthMaxInt  = 0xFFFFFFFFu / 10;
                const unsigned remainderMaxInt = 0xFFFFFFFFu - 10 * oneTenthMaxInt;
                const unsigned long long oneTenthMaxInt64  = 0xFFFFFFFFFFFFFFFFull / 10;
                const unsigned long long remainderMaxInt64 = 0xFFFFFFFFFFFFFFFFull - 10 * oneTenthMaxInt64;
                const unsigned short oneTenthMaxInt16  = 0xFFFFu / 10;
                const unsigned short remainderMaxInt16 = 0xFFFFu - 10 * oneTenthMaxInt16;
                for (int i = 0; i < numericLen; i++) {
                    ch = ppToken->name[i] - '0';
                    bool overflow = false;
                    if (isInt64)<--- Condition 'isInt64' is always false
                        overflow = (ival > oneTenthMaxInt64 || (ival == oneTenthMaxInt64 && (unsigned long long)ch > remainderMaxInt64));
                    else if (isInt16)<--- Condition 'isInt16' is always false
                        overflow = (ival > oneTenthMaxInt16 || (ival == oneTenthMaxInt16 && (unsigned short)ch > remainderMaxInt16));
                    else
                        overflow = (ival > oneTenthMaxInt || (ival == oneTenthMaxInt && (unsigned)ch > remainderMaxInt));
                    if (overflow) {
                        pp->parseContext.ppError(ppToken->loc, "numeric literal too big", "", "");
                        ival = 0xFFFFFFFFFFFFFFFFull;
                        break;
                    } else
                        ival = ival * 10 + ch;
                }

                if (isInt64 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {<--- Condition 'isInt64' is always false
                    if (pp->ifdepth == 0) {
                        pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                        "64-bit literal");
                        pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                            Num_Int64_Extensions, Int64_Extensions, "64-bit literal");
                    }
                    ppToken->i64val = ival;
                    return isUnsigned ? PpAtomConstUint64 : PpAtomConstInt64;
                } else if (isInt16) {<--- Condition 'isInt16' is always false
                    if (pp->ifdepth == 0 && pp->parseContext.intermediate.getSource() == EShSourceGlsl) {
                        pp->parseContext.requireProfile(ppToken->loc, ~EEsProfile,
                                                        "16-bit  literal");
                        pp->parseContext.profileRequires(ppToken->loc, ~EEsProfile, 0,
                            Num_Int16_Extensions, Int16_Extensions, "16-bit literal");
                    }
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint16 : PpAtomConstInt16;
                } else {
                    ppToken->ival = (int)ival;
                    return isUnsigned ? PpAtomConstUint : PpAtomConstInt;
                }
            }
            break;
        case '-':
            ch = getch();
            if (ch == '-') {
                return PpAtomDecrement;
            } else if (ch == '=') {
                return PPAtomSubAssign;
            } else {
                ungetch();
                return '-';
            }
        case '+':
            ch = getch();
            if (ch == '+') {
                return PpAtomIncrement;
            } else if (ch == '=') {
                return PPAtomAddAssign;
            } else {
                ungetch();
                return '+';
            }
        case '*':
            ch = getch();
            if (ch == '=') {
                return PPAtomMulAssign;
            } else {
                ungetch();
                return '*';
            }
        case '%':
            ch = getch();
            if (ch == '=') {
                return PPAtomModAssign;
            } else {
                ungetch();
                return '%';
            }
        case '^':
            ch = getch();
            if (ch == '^') {
                return PpAtomXor;
            } else {
                if (ch == '=')
                    return PpAtomXorAssign;
                else{
                    ungetch();
                    return '^';
                }
            }

        case '=':
            ch = getch();
            if (ch == '=') {
                return PpAtomEQ;
            } else {
                ungetch();
                return '=';
            }
        case '!':
            ch = getch();
            if (ch == '=') {
                return PpAtomNE;
            } else {
                ungetch();
                return '!';
            }
        case '|':
            ch = getch();
            if (ch == '|') {
                return PpAtomOr;
            } else if (ch == '=') {
                return PpAtomOrAssign;
            } else {
                ungetch();
                return '|';
            }
        case '&':
            ch = getch();
            if (ch == '&') {
                return PpAtomAnd;
            } else if (ch == '=') {
                return PpAtomAndAssign;
            } else {
                ungetch();
                return '&';
            }
        case '<':
            ch = getch();
            if (ch == '<') {
                ch = getch();
                if (ch == '=')
                    return PpAtomLeftAssign;
                else {
                    ungetch();
                    return PpAtomLeft;
                }
            } else if (ch == '=') {
                return PpAtomLE;
            } else {
                ungetch();
                return '<';
            }
        case '>':
            ch = getch();
            if (ch == '>') {
                ch = getch();
                if (ch == '=')
                    return PpAtomRightAssign;
                else {
                    ungetch();
                    return PpAtomRight;
                }
            } else if (ch == '=') {
                return PpAtomGE;
            } else {
                ungetch();
                return '>';
            }
        case '.':
            ch = getch();
            if (ch >= '0' && ch <= '9') {
                ungetch();
                return pp->lFloatConst(0, '.', ppToken);
            } else {
                ungetch();
                return '.';
            }
        case '/':
            ch = getch();
            if (ch == '/') {
                pp->inComment = true;
                do {
                    ch = getch();
                } while (ch != '\n' && ch != EndOfInput);
                ppToken->space = true;
                pp->inComment = false;

                return ch;
            } else if (ch == '*') {
                ch = getch();
                do {
                    while (ch != '*') {
                        if (ch == EndOfInput) {
                            pp->parseContext.ppError(ppToken->loc, "End of input in comment", "comment", "");
                            return ch;
                        }
                        ch = getch();
                    }
                    ch = getch();
                    if (ch == EndOfInput) {
                        pp->parseContext.ppError(ppToken->loc, "End of input in comment", "comment", "");
                        return ch;
                    }
                } while (ch != '/');
                ppToken->space = true;
                // loop again to get the next token...
                break;
            } else if (ch == '=') {
                return PPAtomDivAssign;
            } else {
                ungetch();
                return '/';
            }
            break;
        case '\'':
            return pp->characterLiteral(ppToken);
        case '"':
            // #include uses scanHeaderName() to ignore these escape sequences.
            ch = getch();
            while (ch != '"' && ch != '\n' && ch != EndOfInput) {
                if (len < MaxTokenLength) {
                    if (ch == '\\' && !pp->disableEscapeSequences) {
                        int nextCh = getch();
                        switch (nextCh) {
                        case '\'': ch = 0x27; break;
                        case '"':  ch = 0x22; break;
                        case '?':  ch = 0x3f; break;
                        case '\\': ch = 0x5c; break;
                        case 'a':  ch = 0x07; break;
                        case 'b':  ch = 0x08; break;
                        case 'f':  ch = 0x0c; break;
                        case 'n':  ch = 0x0a; break;
                        case 'r':  ch = 0x0d; break;
                        case 't':  ch = 0x09; break;
                        case 'v':  ch = 0x0b; break;
                        case 'x': 
                            // Hex value, arbitrary number of characters. Terminated by the first
                            // non-hex digit
                            {
                                int numDigits = 0;
                                ch = 0;
                                while (true) {
                                    nextCh = getch();
                                    if (nextCh >= '0' && nextCh <= '9')
                                        nextCh -= '0';
                                    else if (nextCh >= 'A' && nextCh <= 'F')
                                        nextCh -= 'A' - 10;
                                    else if (nextCh >= 'a' && nextCh <= 'f')
                                        nextCh -= 'a' - 10;
                                    else {
                                        ungetch();
                                        break;
                                    }
                                    numDigits++;
                                    ch = ch * 0x10 + nextCh;
                                }
                                if (numDigits == 0) {
                                    pp->parseContext.ppError(ppToken->loc, "Expected hex value in escape sequence", "string", "");
                                }
                                break;
                            }
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                            // Octal value, up to three octal digits
                            {
                                int numDigits = 1;
                                ch = nextCh - '0';
                                while (numDigits < 3) {
                                    nextCh = getch();
                                    if (nextCh >= '0' && nextCh <= '7')
                                        nextCh -= '0';
                                    else {
                                        ungetch();
                                        break;
                                    }
                                    numDigits++;
                                    ch = ch * 8 + nextCh;
                                }
                                break;
                            }
                        default:
                            pp->parseContext.ppError(ppToken->loc, "Invalid escape sequence", "string", "");
                            break;
                        }
                    }
                    ppToken->name[len] = (char)ch;
                    len++;
                    ch = getch();
                } else
                    break;
            };
            ppToken->name[len] = '\0';
            if (ch != '"') {
                ungetch();
                pp->parseContext.ppError(ppToken->loc, "End of line in string", "string", "");
            }
            return PpAtomConstString;
        case ':':
            ch = getch();
            if (ch == ':')
                return PpAtomColonColon;
            ungetch();
            return ':';
        }

        ch = getch();
    }
}

//
// The main functional entry point into the preprocessor, which will
// scan the source strings to figure out and return the next processing token.
//
// Return the token, or EndOfInput when no more tokens.
//
int TPpContext::tokenize(TPpToken& ppToken)
{
    for(;;) {
        int token = scanToken(&ppToken);

        // Handle token-pasting logic
        token = tokenPaste(token, ppToken);

        if (token == EndOfInput) {
            missingEndifCheck();
            return EndOfInput;
        }
        if (token == '#') {
            if (previous_token == '\n') {
                token = readCPPline(&ppToken);
                if (token == EndOfInput) {
                    missingEndifCheck();
                    return EndOfInput;
                }
                continue;
            } else {
                parseContext.ppError(ppToken.loc, "preprocessor directive cannot be preceded by another token", "#", "");
                return EndOfInput;
            }
        }
        previous_token = token;

        if (token == '\n')
            continue;

        // expand macros
        if (token == PpAtomIdentifier) {
            switch (MacroExpand(&ppToken, false, true)) {
            case MacroExpandNotStarted:
                break;
            case MacroExpandError:
                return EndOfInput;
            case MacroExpandStarted:
            case MacroExpandUndef:
                continue;
            }
        }

        switch (token) {
        case PpAtomIdentifier:
        case PpAtomConstInt:
        case PpAtomConstUint:
        case PpAtomConstFloat:
        case PpAtomConstInt64:
        case PpAtomConstUint64:
        case PpAtomConstInt16:
        case PpAtomConstUint16:
        case PpAtomConstDouble:
        case PpAtomConstFloat16:
            if (ppToken.name[0] == '\0')
                continue;
            break;
        case PpAtomConstString:
            // HLSL allows string literals.
            // GLSL allows string literals with GL_EXT_debug_printf.
            if (ifdepth == 0 && parseContext.intermediate.getSource() != EShSourceHlsl) {
                parseContext.requireExtensions(ppToken.loc, 1, &E_GL_EXT_debug_printf, "string literal");
                if (!parseContext.extensionTurnedOn(E_GL_EXT_debug_printf))
                    continue;
            }
            break;
        case '\'':
            parseContext.ppError(ppToken.loc, "character literals not supported", "\'", "");
            continue;
        default:
            snprintf(ppToken.name, sizeof(ppToken.name), "%s", atomStrings.getString(token));
            break;
        }

        return token;
    }
}

//
// Do all token-pasting related combining of two pasted tokens when getting a
// stream of tokens from a replacement list. Degenerates to no processing if a
// replacement list is not the source of the token stream.
//
int TPpContext::tokenPaste(int token, TPpToken& ppToken)
{
    // starting with ## is illegal, skip to next token
    if (token == PpAtomPaste) {
        parseContext.ppError(ppToken.loc, "unexpected location", "##", "");
        return scanToken(&ppToken);
    }

    int resultToken = token; // "foo" pasted with "35" is an identifier, not a number

    // ## can be chained, process all in the chain at once
    while (peekPasting()) {
        TPpToken pastedPpToken;

        // next token has to be ##
        token = scanToken(&pastedPpToken);
        assert(token == PpAtomPaste);

        // This covers end of macro expansion
        if (endOfReplacementList()) {
            parseContext.ppError(ppToken.loc, "unexpected location; end of replacement list", "##", "");
            break;
        }

        // Get the token(s) after the ##.
        // Because of "space" semantics, and prior tokenization, what
        // appeared a single token, e.g. "3A", might have been tokenized
        // into two tokens "3" and "A", but the "A" will have 'space' set to
        // false.  Accumulate all of these to recreate the original lexical
        // appearing token.
        do {
            token = scanToken(&pastedPpToken);

            // This covers end of argument expansion
            if (token == tMarkerInput::marker) {
                parseContext.ppError(ppToken.loc, "unexpected location; end of argument", "##", "");
                return resultToken;
            }

            // get the token text
            switch (resultToken) {
            case PpAtomIdentifier:
                // already have the correct text in token.names
                break;
            case '=':
            case '!':
            case '-':
            case '~':
            case '+':
            case '*':
            case '/':
            case '%':
            case '<':
            case '>':
            case '|':
            case '^':
            case '&':
            case PpAtomRight:
            case PpAtomLeft:
            case PpAtomAnd:
            case PpAtomOr:
            case PpAtomXor:
                snprintf(ppToken.name, sizeof(ppToken.name), "%s", atomStrings.getString(resultToken));
                snprintf(pastedPpToken.name, sizeof(pastedPpToken.name), "%s", atomStrings.getString(token));
                break;
            default:
                parseContext.ppError(ppToken.loc, "not supported for these tokens", "##", "");
                return resultToken;
            }

            // combine the tokens
            if (strlen(ppToken.name) + strlen(pastedPpToken.name) > MaxTokenLength) {
                parseContext.ppError(ppToken.loc, "combined tokens are too long", "##", "");
                return resultToken;
            }
            snprintf(&ppToken.name[0] + strlen(ppToken.name), sizeof(ppToken.name) - strlen(ppToken.name),
                "%s", pastedPpToken.name);

            // correct the kind of token we are making, if needed (identifiers stay identifiers)
            if (resultToken != PpAtomIdentifier) {
                int newToken = atomStrings.getAtom(ppToken.name);
                if (newToken > 0)
                    resultToken = newToken;
                else
                    parseContext.ppError(ppToken.loc, "combined token is invalid", "##", "");
            }
        } while (peekContinuedPasting(resultToken));
    }

    return resultToken;
}

// Checks if we've seen balanced #if...#endif
void TPpContext::missingEndifCheck()
{
    if (ifdepth > 0)
        parseContext.ppError(parseContext.getCurrentLoc(), "missing #endif", "", "");
}

} // end namespace glslang