Code coverage report for lib/parser/cssParser.js

# all files / lib/parser/ cssParser.js

96.26% Statements 618/642
94.46% Branches 460/487
100% Functions 80/80
96.36% Lines 609/632

    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
    10501×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×
    1×
    242×
    242×
    242×
    242×

    1×
    117800×
    117800×
    100405×

    17395×
    7768×
    7768×

    9627×

    1×
    5847×
    4034×

    1813×

    1×
    4084×

    1×
    9563×
    9563×

    1×
    6019×

    1×
    2663×
    2663×
    2663×

    1×
    5×
    5×
    65×
    4×
    4×

    1×

    1×
    86647×
    86647×
    8119×
    8119×

    78528×

    1×
    122×
    260×
    44×
    44×

    216×
    7×

    209×
    71×

    138×

    1×
    13015×

    1×
    43426×
    43426×
    43426×

    1×

    13482×
    13475×
    191×

    13475×

    13474×
    13474×

    13482×

    1×
    201×
    172×
    172×

    201×
    121×

    1×
    205×
    205×
    1095×

    1095×

    205×

    1×
    897×
    897×
    897×
    897×
    889×
    205×

    684×

    897×

    1×
    339×
    339×
    339×
    339×
    347×
    347×
    780×
    780×
    780×
    430×
    430×
    430×
    430×
    1×

    780×

    16×
    16×

    347×
    335×

    12×
    7×
    1×

    6×

    7×

    12×

    339×

    1×
    779×

    1×
    477×
    477×
    241×

    241×
    61×
    61×

    416×
    416×

    1×
    969×
    969×
    969×
    551×

    418×

    418×

    1×
    749×

    1×
    749×
    744×

    5×
    5×
    5×
    1×

    4×

    1×
    550×

    80×

    170×

    26×

    260×

    14×

    1×
    703×
    703×
    15×

    688×
    688×
    633×
    213×

    420×
    43×

    377×

    377×

    645×
    22×

    623×

    1×
    700×
    15×

    685×

    1×
    1487×
    1487×
    1487×

    519×

    1487×
    782×
    782×

    1487×

    1×
    483×
    483×
    220×

    263×
    9×

    254×
    254×
    13×

    241×
    241×
    137×

    241×

    1×
    744×
    710×

    34×
    34×

    34×

    34×
    34×
    34×

    3×
    3×
    3×
    1×
    1×

    3×

    34×
    34×
    25×
    25×
    18×
    18×
    18×

    16×
    16×
    16×
    16×
    1×

    15×

    1×
    16×
    16×
    16×
    16×
    16×
    16×
    104×

    13×
    1×

    12×

    2×
    1×

    1×

    7×
    7×

    17×
    17×

    10×
    8×

    2×

    7×

    9×
    9×

    9×
    9×
    1×

    8×

    9×
    9×

    9×
    9×
    1×

    8×

    2×
    2×

    2×

    2×

    88×

    10×

    1×
    29×
    29×

    1×
    1×

    28×
    28×

    1×
    849×
    849×
    849×

    3×

    849×
    459×

    390×

    1×
    705×

    1×
    339×
    339×
    336×

    3×
    1×

    2×
    1×

    1×

    1×
    186×
    186×
    177×

    9×
    1×

    8×
    8×

    1×

    512×
    512×
    506×

    6×
    6×
    1×

    5×
    1×

    4×

    1×
    611×
    595×

    16×
    16×
    16×

    1×
    514×

    512×

    2×
    2×
    2×

    1×
    635×
    635×
    635×

    604×

    31×
    31×

    31×
    1×

    30×

    1×
    51×
    51×
    23×

    28×
    2×
    1×

    27×

    1×
    29×

    1×

    648×
    648×
    616×

    32×
    1×

    31×
    3×

    31×

    1×

    35×
    35×
    35×
    35×
    31×

    31×
    2×

    29×
    29×

    33×
    20×
    1×
    1×

    19×
    1×

    18×
    16×
    1×

    17×
    1×

    16×

    29×

    1×
    15×

    1×
    22×
    22×
    5×
    3×

    5×

    17×

    1×
    28×
    28×
    11×

    17×

    1×
    24×

    1×

    544×
    544×
    527×

    17×
    11×
    2×
    1×

    16×

    1×

    24×
    24×
    19×

    5×
    1×

    5×

    1×

    20×
    20×
    7×

    13×
    13×
    12×
    1×

    12×

    1×

    506×
    506×
    505×

    1×
    1×

    1×

    1290×
    1290×

    192×

    1098×

    1×
    2287×
    2287×
    10×

    2277×

    1×
    1305×
    1305×
    11×
    11×

    1294×
    12×
    12×

    1282×
    2×
    2×

    1280×

    1×

    2640×
    2640×
    2640×
    709×

    2640×
    382×

    2640×

    1×
    2360×

    1×
    1934×

    1×
    2277×
    2234×

    43×
    43×
    3×
    1×

    40×

    42×

    1×

    2236×
    2034×

    202×
    202×
    202×
    3×

    199×

    1×

    2644×
    2644×
    713×

    1931×

    1×

    2040×
    2020×

    20×
    20×
    20×

    20×

    20×

    1×

    1746×
    1305×

    441×
    441×
    441×
    441×

    82×
    1×

    81×
    4×

    77×
    14×
    14×

    77×

    359×
    359×

    1×
    558×
    540×

    18×
    18×
    18×

    1×
    951×
    951×
    951×
    92×

    859×
    950×
    84×
    56×

    28×

    894×
    803×

    803×

    1×
    2084×
    2084×
    945×

    1139×
    917×

    222×

    222×
    222×
    222×
    41×

    222×

    1×
    2286×
    2286×
    2286×

    1097×

    1189×

    1×
    656×
    656×
    624×

    32×
    32×
    1×

    31×

    1×
    1825×
    1825×

    508×

    1317×

    1×
    1882×
    1882×
    57×

    1825×

    1×
    1890×
    1890×
    8×

    1882×

    1×
    3784×
    3784×
    32×

    3784×
    3784×
    1374×

    2410×

    1×
    2307×
    2307×
    2307×
    1897×

    410×
    345×
    345×

    65×
    54×
    39×
    1×

    65×
    4×

    61×

    1×
    2304×
    2304×
    2304×

    2×
    2×

    2×

    2302×
    405×

    1897×

    1×
    61×
    61×
    54×

    7×

    1×
    1319×
    55×
    55×
    55×

    1264×

    1×

    1×

     (function (factory) {
        Eif (typeof module === "object" && typeof module.exports === "object") {
            var v = factory(require, exports);
            Iif (v !== undefined) module.exports = v;
        }
        else if (typeof define === "function" && define.amd) {
            define(["require", "exports", "./cssScanner", "./cssNodes", "./cssErrors", "../services/languageFacts"], factory);
        }
    })(function (require, exports) {
        /*---------------------------------------------------------------------------------------------
         *  Copyright (c) Microsoft Corporation. All rights reserved.
         *  Licensed under the MIT License. See License.txt in the project root for license information.
         *--------------------------------------------------------------------------------------------*/
        'use strict';
        Object.defineProperty(exports, "__esModule", { value: true });
        var cssScanner_1 = require("./cssScanner");
        var nodes = require("./cssNodes");
        var cssErrors_1 = require("./cssErrors");
        var languageFacts = require("../services/languageFacts");
        /// <summary>
        /// A parser for the css core specification. See for reference:
        /// http://www.w3.org/TR/CSS21/syndata.html#tokenization
        /// </summary>
        var Parser = (function () {
            function Parser(scnr) {
                if (scnr === void 0) { scnr = new cssScanner_1.Scanner(); }
                this.scanner = scnr;
                this.token = null;
                this.prevToken = null;
            }
            Parser.prototype.peek = function (type, text, ignoreCase) {
                if (ignoreCase === void 0) { ignoreCase = true; }
                if (type !== this.token.type) {
                    return false;
                }
                if (typeof text !== 'undefined') {
                    Eif (ignoreCase) {
                        return text.toLowerCase() === this.token.text.toLowerCase();
                    }
                    else {
                        return text === this.token.text;
                    }
                }
                return true;
            };
            Parser.prototype.peekRegExp = function (type, regEx) {
                if (type !== this.token.type) {
                    return false;
                }
                return regEx.test(this.token.text);
            };
            Parser.prototype.hasWhitespace = function () {
                return this.prevToken && (this.prevToken.offset + this.prevToken.len !== this.token.offset);
            };
            Parser.prototype.consumeToken = function () {
                this.prevToken = this.token;
                this.token = this.scanner.scan();
            };
            Parser.prototype.mark = function () {
                return {
                    prev: this.prevToken,
                    curr: this.token,
                    pos: this.scanner.pos()
                };
            };
            Parser.prototype.restoreAtMark = function (mark) {
                this.prevToken = mark.prev;
                this.token = mark.curr;
                this.scanner.goBackTo(mark.pos);
            };
            Parser.prototype.acceptOne = function (type, text, ignoreCase) {
                Eif (ignoreCase === void 0) { ignoreCase = true; }
                for (var i = 0; i < text.length; i++) {
                    if (this.peek(type, text[i], ignoreCase)) {
                        this.consumeToken();
                        return true;
                    }
                }
                return false;
            };
            Parser.prototype.accept = function (type, text, ignoreCase) {
                if (ignoreCase === void 0) { ignoreCase = true; }
                if (this.peek(type, text, ignoreCase)) {
                    this.consumeToken();
                    return true;
                }
                return false;
            };
            Parser.prototype.resync = function (resyncTokens, resyncStopTokens) {
                while (true) {
                    if (resyncTokens && resyncTokens.indexOf(this.token.type) !== -1) {
                        this.consumeToken();
                        return true;
                    }
                    else if (resyncStopTokens && resyncStopTokens.indexOf(this.token.type) !== -1) {
                        return true;
                    }
                    else {
                        if (this.token.type === cssScanner_1.TokenType.EOF) {
                            return false;
                        }
                        this.token = this.scanner.scan();
                    }
                }
            };
            Parser.prototype.createNode = function (nodeType) {
                return new nodes.Node(this.token.offset, this.token.len, nodeType);
            };
            Parser.prototype.create = function (ctor) {
                var obj = Object.create(ctor.prototype);
                ctor.apply(obj, [this.token.offset, this.token.len]);
                return obj;
            };
            Parser.prototype.finish = function (node, error, resyncTokens, resyncStopTokens) {
                // parseNumeric misuses error for boolean flagging (however the real error mustn't be a false)
                // + nodelist offsets mustn't be modified, because there is a offset hack in rulesets for smartselection
                if (!(node instanceof nodes.Nodelist)) {
                    if (error) {
                        this.markError(node, error, resyncTokens, resyncStopTokens);
                    }
                    // set the node end position
                    if (this.prevToken !== null) {
                        // length with more elements belonging together
                        var prevEnd = this.prevToken.offset + this.prevToken.len;
                        node.length = prevEnd > node.offset ? prevEnd - node.offset : 0; // offset is taken from current token, end from previous: Use 0 for empty nodes
                    }
                }
                return node;
            };
            Parser.prototype.markError = function (node, error, resyncTokens, resyncStopTokens) {
                if (this.token !== this.lastErrorToken) {
                    node.addIssue(new nodes.Marker(node, error, nodes.Level.Error, null, this.token.offset, this.token.len));
                    this.lastErrorToken = this.token;
                }
                if (resyncTokens || resyncStopTokens) {
                    this.resync(resyncTokens, resyncStopTokens);
                }
            };
            Parser.prototype.parseStylesheet = function (textDocument) {
                var versionId = textDocument.version;
                var textProvider = function (offset, length) {
                    Iif (textDocument.version !== versionId) {
                        throw new Error('Underlying model has changed, AST is no longer valid');
                    }
                    return textDocument.getText().substr(offset, length);
                };
                return this.internalParse(textDocument.getText(), this._parseStylesheet, textProvider);
            };
            Parser.prototype.internalParse = function (input, parseFunc, textProvider) {
                this.scanner.setSource(input);
                this.token = this.scanner.scan();
                var node = parseFunc.bind(this)();
                if (node) {
                    if (textProvider) {
                        node.textProvider = textProvider;
                    }
                    else {
                        node.textProvider = function (offset, length) { return input.substr(offset, length); };
                    }
                }
                return node;
            };
            Parser.prototype._parseStylesheet = function () {
                var node = this.create(nodes.Stylesheet);
                node.addChild(this._parseCharset());
                var inRecovery = false;
                do {
                    var hasMatch = false;
                    do {
                        hasMatch = false;
                        var statement = this._parseStylesheetStatement();
                        if (statement) {
                            node.addChild(statement);
                            hasMatch = true;
                            inRecovery = false;
                            if (!this.peek(cssScanner_1.TokenType.EOF) && this._needsSemicolonAfter(statement) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
                                this.markError(node, cssErrors_1.ParseError.SemiColonExpected);
                            }
                        }
                        while (this.accept(cssScanner_1.TokenType.SemiColon) || this.accept(cssScanner_1.TokenType.CDO) || this.accept(cssScanner_1.TokenType.CDC)) {
                            // accept empty statements
                            hasMatch = true;
                            inRecovery = false;
                        }
                    } while (hasMatch);
                    if (this.peek(cssScanner_1.TokenType.EOF)) {
                        break;
                    }
                    if (!inRecovery) {
                        if (this.peek(cssScanner_1.TokenType.AtKeyword)) {
                            this.markError(node, cssErrors_1.ParseError.UnknownAtRule);
                        }
                        else {
                            this.markError(node, cssErrors_1.ParseError.RuleOrSelectorExpected);
                        }
                        inRecovery = true;
                    }
                    this.consumeToken();
                } while (!this.peek(cssScanner_1.TokenType.EOF));
                return this.finish(node);
            };
            Parser.prototype._parseStylesheetStatement = function () {
                return this._parseRuleset(false)
                    || this._parseImport()
                    || this._parseMedia()
                    || this._parsePage()
                    || this._parseFontFace()
                    || this._parseKeyframe()
                    || this._parseViewPort()
                    || this._parseNamespace()
                    || this._parseDocument();
            };
            Parser.prototype._tryParseRuleset = function (isNested) {
                var mark = this.mark();
                if (this._parseSelector(isNested)) {
                    while (this.accept(cssScanner_1.TokenType.Comma) && this._parseSelector(isNested)) {
                        // loop
                    }
                    if (this.accept(cssScanner_1.TokenType.CurlyL)) {
                        this.restoreAtMark(mark);
                        return this._parseRuleset(isNested);
                    }
                }
                this.restoreAtMark(mark);
                return null;
            };
            Parser.prototype._parseRuleset = function (isNested) {
                if (isNested === void 0) { isNested = false; }
                var node = this.create(nodes.RuleSet);
                if (!node.getSelectors().addChild(this._parseSelector(isNested))) {
                    return null;
                }
                while (this.accept(cssScanner_1.TokenType.Comma) && node.getSelectors().addChild(this._parseSelector(isNested))) {
                    // loop
                }
                return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
            };
            Parser.prototype._parseRuleSetDeclaration = function () {
                return this._parseAtApply() || this._tryParseCustomPropertyDeclaration() || this._parseDeclaration();
            };
            /**
             * Parses declarations like:
             *   @apply --my-theme;
             *
             * Follows https://tabatkins.github.io/specs/css-apply-rule/#using
             */
            Parser.prototype._parseAtApply = function () {
                if (!this.peek(cssScanner_1.TokenType.AtKeyword, '@apply')) {
                    return null;
                }
                var node = this.create(nodes.AtApplyRule);
                this.consumeToken();
                if (!node.setIdentifier(this._parseIdent([nodes.ReferenceType.Variable]))) {
                    return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._needsSemicolonAfter = function (node) {
                switch (node.type) {
                    case nodes.NodeType.Keyframe:
                    case nodes.NodeType.ViewPort:
                    case nodes.NodeType.Media:
                    case nodes.NodeType.Ruleset:
                    case nodes.NodeType.Namespace:
                    case nodes.NodeType.If:
                    case nodes.NodeType.For:
                    case nodes.NodeType.Each:
                    case nodes.NodeType.While:
                    case nodes.NodeType.MixinDeclaration:
                    case nodes.NodeType.FunctionDeclaration:
                        return false;
                    case nodes.NodeType.VariableDeclaration:
                    case nodes.NodeType.ExtendsReference:
                    case nodes.NodeType.MixinContent:
                    case nodes.NodeType.ReturnStatement:
                    case nodes.NodeType.MediaQuery:
                    case nodes.NodeType.Debug:
                    case nodes.NodeType.Import:
                    case nodes.NodeType.AtApplyRule:
                    case nodes.NodeType.CustomPropertyDeclaration:
                        return true;
                    case nodes.NodeType.MixinReference:
                        return !node.getContent();
                    case nodes.NodeType.Declaration:
                        return !node.getNestedProperties();
                }
                return false;
            };
            Parser.prototype._parseDeclarations = function (parseDeclaration) {
                var node = this.create(nodes.Declarations);
                if (!this.accept(cssScanner_1.TokenType.CurlyL)) {
                    return null;
                }
                var decl = parseDeclaration();
                while (node.addChild(decl)) {
                    if (this.peek(cssScanner_1.TokenType.CurlyR)) {
                        break;
                    }
                    if (this._needsSemicolonAfter(decl) && !this.accept(cssScanner_1.TokenType.SemiColon)) {
                        return this.finish(node, cssErrors_1.ParseError.SemiColonExpected, [cssScanner_1.TokenType.SemiColon, cssScanner_1.TokenType.CurlyR]);
                    }
                    while (this.accept(cssScanner_1.TokenType.SemiColon)) {
                        // accept empty statements
                    }
                    decl = parseDeclaration();
                }
                if (!this.accept(cssScanner_1.TokenType.CurlyR)) {
                    return this.finish(node, cssErrors_1.ParseError.RightCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
                }
                return this.finish(node);
            };
            Parser.prototype._parseBody = function (node, parseDeclaration) {
                if (!node.setDeclarations(this._parseDeclarations(parseDeclaration))) {
                    return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected, [cssScanner_1.TokenType.CurlyR, cssScanner_1.TokenType.SemiColon]);
                }
                return this.finish(node);
            };
            Parser.prototype._parseSelector = function (isNested) {
                var node = this.create(nodes.Selector);
                var hasContent = false;
                if (isNested) {
                    // nested selectors can start with a combinator
                    hasContent = node.addChild(this._parseCombinator());
                }
                while (node.addChild(this._parseSimpleSelector())) {
                    hasContent = true;
                    node.addChild(this._parseCombinator()); // optional
                }
                return hasContent ? this.finish(node) : null;
            };
            Parser.prototype._parseDeclaration = function (resyncStopTokens) {
                var node = this.create(nodes.Declaration);
                if (!node.setProperty(this._parseProperty())) {
                    return null;
                }
                if (!this.accept(cssScanner_1.TokenType.Colon)) {
                    return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon], resyncStopTokens);
                }
                node.colonPosition = this.prevToken.offset;
                if (!node.setValue(this._parseExpr())) {
                    return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
                }
                node.addChild(this._parsePrio());
                if (this.peek(cssScanner_1.TokenType.SemiColon)) {
                    node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
                }
                return this.finish(node);
            };
            Parser.prototype._tryParseCustomPropertyDeclaration = function () {
                if (!this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/)) {
                    return null;
                }
                var node = this.create(nodes.CustomPropertyDeclaration);
                Iif (!node.setProperty(this._parseProperty())) {
                    return null;
                }
                Iif (!this.accept(cssScanner_1.TokenType.Colon)) {
                    return this.finish(node, cssErrors_1.ParseError.ColonExpected, [cssScanner_1.TokenType.Colon]);
                }
                node.colonPosition = this.prevToken.offset;
                var mark = this.mark();
                if (this.peek(cssScanner_1.TokenType.CurlyL)) {
                    // try to parse it as nested declaration
                    var propertySet = this.create(nodes.CustomPropertySet);
                    var declarations = this._parseDeclarations(this._parseRuleSetDeclaration.bind(this));
                    if (propertySet.setDeclarations(declarations) && !declarations.isErroneous(true)) {
                        propertySet.addChild(this._parsePrio());
                        Iif (this.peek(cssScanner_1.TokenType.SemiColon)) {
                            this.finish(propertySet);
                            node.setPropertySet(propertySet);
                            node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
                            return this.finish(node);
                        }
                    }
                    this.restoreAtMark(mark);
                }
                // try tp parse as expression
                var expression = this._parseExpr();
                if (expression && !expression.isErroneous(true)) {
                    this._parsePrio();
                    if (this.peek(cssScanner_1.TokenType.SemiColon)) {
                        node.setValue(expression);
                        node.semicolonPosition = this.token.offset; // not part of the declaration, but useful information for code assist
                        return this.finish(node);
                    }
                }
                this.restoreAtMark(mark);
                node.addChild(this._parseCustomPropertyValue());
                node.addChild(this._parsePrio());
                if (this.token.offset === node.colonPosition + 1) {
                    return this.finish(node, cssErrors_1.ParseError.PropertyValueExpected);
                }
                return this.finish(node);
            };
            /**
             * Parse custom property values.
             *
             * Based on https://www.w3.org/TR/css-variables/#syntax
             *
             * This code is somewhat unusual, as the allowed syntax is incredibly broad,
             * parsing almost any sequence of tokens, save for a small set of exceptions.
             * Unbalanced delimitors, invalid tokens, and declaration
             * terminators like semicolons and !important directives (when not inside
             * of delimitors).
             */
            Parser.prototype._parseCustomPropertyValue = function () {
                var node = this.create(nodes.Node);
                var isTopLevel = function () { return curlyDepth === 0 && parensDepth === 0 && bracketsDepth === 0; };
                var curlyDepth = 0;
                var parensDepth = 0;
                var bracketsDepth = 0;
                done: while (true) {
                    switch (this.token.type) {
                        case cssScanner_1.TokenType.SemiColon:
                            // A semicolon only ends things if we're not inside a delimitor.
                            if (isTopLevel()) {
                                break done;
                            }
                            break;
                        case cssScanner_1.TokenType.Exclamation:
                            // An exclamation ends the value if we're not inside delims.
                            if (isTopLevel()) {
                                break done;
                            }
                            break;
                        case cssScanner_1.TokenType.CurlyL:
                            curlyDepth++;
                            break;
                        case cssScanner_1.TokenType.CurlyR:
                            curlyDepth--;
                            if (curlyDepth < 0) {
                                // The property value has been terminated without a semicolon, and
                                // this is the last declaration in the ruleset.
                                if (parensDepth === 0 && bracketsDepth === 0) {
                                    break done;
                                }
                                return this.finish(node, cssErrors_1.ParseError.LeftCurlyExpected);
                            }
                            break;
                        case cssScanner_1.TokenType.ParenthesisL:
                            parensDepth++;
                            break;
                        case cssScanner_1.TokenType.ParenthesisR:
                            parensDepth--;
                            if (parensDepth < 0) {
                                return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected);
                            }
                            break;
                        case cssScanner_1.TokenType.BracketL:
                            bracketsDepth++;
                            break;
                        case cssScanner_1.TokenType.BracketR:
                            bracketsDepth--;
                            if (bracketsDepth < 0) {
                                return this.finish(node, cssErrors_1.ParseError.LeftSquareBracketExpected);
                            }
                            break;
                        case cssScanner_1.TokenType.BadString: // fall through
                        case cssScanner_1.TokenType.BadUri:
                            break done;
                        case cssScanner_1.TokenType.EOF:
                            // We shouldn't have reached the end of input, something is
                            // unterminated.
                            var error = cssErrors_1.ParseError.RightCurlyExpected;
                            Iif (bracketsDepth > 0) {
                                error = cssErrors_1.ParseError.RightSquareBracketExpected;
                            }
                            else Iif (parensDepth > 0) {
                                error = cssErrors_1.ParseError.RightParenthesisExpected;
                            }
                            return this.finish(node, error);
                    }
                    this.consumeToken();
                }
                return this.finish(node);
            };
            Parser.prototype._tryToParseDeclaration = function () {
                var mark = this.mark();
                if (this._parseProperty() && this.accept(cssScanner_1.TokenType.Colon)) {
                    // looks like a declaration, go ahead
                    this.restoreAtMark(mark);
                    return this._parseDeclaration();
                }
                this.restoreAtMark(mark);
                return null;
            };
            Parser.prototype._parseProperty = function () {
                var node = this.create(nodes.Property);
                var mark = this.mark();
                if (this.accept(cssScanner_1.TokenType.Delim, '*') || this.accept(cssScanner_1.TokenType.Delim, '_')) {
                    // support for  IE 5.x, 6 and 7 star hack: see http://en.wikipedia.org/wiki/CSS_filter#Star_hack
                    Iif (this.hasWhitespace()) {
                        this.restoreAtMark(mark);
                        return null;
                    }
                }
                if (node.setIdentifier(this._parsePropertyIdentifier())) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parsePropertyIdentifier = function () {
                return this._parseIdent();
            };
            Parser.prototype._parseCharset = function () {
                var node = this.create(nodes.Node);
                if (!this.accept(cssScanner_1.TokenType.Charset)) {
                    return null;
                }
                if (!this.accept(cssScanner_1.TokenType.String)) {
                    return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                }
                if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
                    return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parseImport = function () {
                var node = this.create(nodes.Import);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@import')) {
                    return null;
                }
                if (!this.accept(cssScanner_1.TokenType.URI) && !this.accept(cssScanner_1.TokenType.String)) {
                    return this.finish(node, cssErrors_1.ParseError.URIOrStringExpected);
                }
                node.setMedialist(this._parseMediaList());
                return this.finish(node);
            };
            Parser.prototype._parseNamespace = function () {
                // http://www.w3.org/TR/css3-namespace/
                // namespace  : NAMESPACE_SYM S* [IDENT S*]? [STRING|URI] S* ';' S*
                var node = this.create(nodes.Namespace);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@namespace')) {
                    return null;
                }
                node.addChild(this._parseIdent()); // optional prefix
                if (!this.accept(cssScanner_1.TokenType.URI) && !this.accept(cssScanner_1.TokenType.String)) {
                    return this.finish(node, cssErrors_1.ParseError.URIExpected, [cssScanner_1.TokenType.SemiColon]);
                }
                if (!this.accept(cssScanner_1.TokenType.SemiColon)) {
                    return this.finish(node, cssErrors_1.ParseError.SemiColonExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parseFontFace = function () {
                if (!this.peek(cssScanner_1.TokenType.AtKeyword, '@font-face')) {
                    return null;
                }
                var node = this.create(nodes.FontFace);
                this.consumeToken(); // @font-face
                return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
            };
            Parser.prototype._parseViewPort = function () {
                if (!this.peek(cssScanner_1.TokenType.AtKeyword, '@-ms-viewport') &&
                    !this.peek(cssScanner_1.TokenType.AtKeyword, '@-o-viewport') &&
                    !this.peek(cssScanner_1.TokenType.AtKeyword, '@viewport')) {
                    return null;
                }
                var node = this.create(nodes.ViewPort);
                this.consumeToken(); // @-ms-viewport
                return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
            };
            Parser.prototype._parseKeyframe = function () {
                var node = this.create(nodes.Keyframe);
                var atNode = this.create(nodes.Node);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@keyframes') &&
                    !this.accept(cssScanner_1.TokenType.AtKeyword, '@-webkit-keyframes') &&
                    !this.accept(cssScanner_1.TokenType.AtKeyword, '@-ms-keyframes') &&
                    !this.accept(cssScanner_1.TokenType.AtKeyword, '@-moz-keyframes') &&
                    !this.accept(cssScanner_1.TokenType.AtKeyword, '@-o-keyframes')) {
                    return null;
                }
                node.setKeyword(this.finish(atNode));
                Iif (atNode.getText() === '@-ms-keyframes') {
                    this.markError(atNode, cssErrors_1.ParseError.UnknownKeyword);
                }
                if (!node.setIdentifier(this._parseIdent([nodes.ReferenceType.Keyframe]))) {
                    return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [cssScanner_1.TokenType.CurlyR]);
                }
                return this._parseBody(node, this._parseKeyframeSelector.bind(this));
            };
            Parser.prototype._parseKeyframeSelector = function () {
                var node = this.create(nodes.KeyframeSelector);
                if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
                    return null;
                }
                while (this.accept(cssScanner_1.TokenType.Comma)) {
                    if (!node.addChild(this._parseIdent()) && !this.accept(cssScanner_1.TokenType.Percentage)) {
                        return this.finish(node, cssErrors_1.ParseError.PercentageExpected);
                    }
                }
                return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
            };
            Parser.prototype._parseMediaDeclaration = function () {
                return this._tryParseRuleset(false)
                    || this._tryToParseDeclaration()
                    || this._parseStylesheetStatement();
            };
            Parser.prototype._parseMedia = function () {
                // MEDIA_SYM S* media_query_list '{' S* ruleset* '}' S*
                // media_query_list : S* [media_query [ ',' S* media_query ]* ]?
                var node = this.create(nodes.Media);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@media')) {
                    return null;
                }
                if (!node.addChild(this._parseMediaQuery([cssScanner_1.TokenType.CurlyL]))) {
                    return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                }
                while (this.accept(cssScanner_1.TokenType.Comma)) {
                    Iif (!node.addChild(this._parseMediaQuery([cssScanner_1.TokenType.CurlyL]))) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                    }
                }
                return this._parseBody(node, this._parseMediaDeclaration.bind(this));
            };
            Parser.prototype._parseMediaQuery = function (resyncStopToken) {
                // http://www.w3.org/TR/css3-mediaqueries/
                // media_query : [ONLY | NOT]? S* IDENT S* [ AND S* expression ]* | expression [ AND S* expression ]*
                // expression : '(' S* IDENT S* [ ':' S* expr ]? ')' S*
                var node = this.create(nodes.MediaQuery);
                var parseExpression = true;
                var hasContent = false;
                if (!this.peek(cssScanner_1.TokenType.ParenthesisL)) {
                    if (this.accept(cssScanner_1.TokenType.Ident, 'only', true) || this.accept(cssScanner_1.TokenType.Ident, 'not', true)) {
                        // optional
                    }
                    if (!node.addChild(this._parseIdent())) {
                        return null;
                    }
                    hasContent = true;
                    parseExpression = this.accept(cssScanner_1.TokenType.Ident, 'and', true);
                }
                while (parseExpression) {
                    if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
                        Eif (hasContent) {
                            return this.finish(node, cssErrors_1.ParseError.LeftParenthesisExpected, [], resyncStopToken);
                        }
                        return null;
                    }
                    if (!node.addChild(this._parseMediaFeatureName())) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected, [], resyncStopToken);
                    }
                    if (this.accept(cssScanner_1.TokenType.Colon)) {
                        if (!node.addChild(this._parseExpr())) {
                            return this.finish(node, cssErrors_1.ParseError.TermExpected, [], resyncStopToken);
                        }
                    }
                    if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
                        return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected, [], resyncStopToken);
                    }
                    parseExpression = this.accept(cssScanner_1.TokenType.Ident, 'and', true);
                }
                return node;
            };
            Parser.prototype._parseMediaFeatureName = function () {
                return this._parseIdent();
            };
            Parser.prototype._parseMediaList = function () {
                var node = this.create(nodes.Medialist);
                if (node.getMediums().addChild(this._parseMedium())) {
                    while (this.accept(cssScanner_1.TokenType.Comma)) {
                        Iif (!node.getMediums().addChild(this._parseMedium())) {
                            return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                        }
                    }
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseMedium = function () {
                var node = this.create(nodes.Node);
                if (node.addChild(this._parseIdent())) {
                    return this.finish(node);
                }
                else {
                    return null;
                }
            };
            Parser.prototype._parsePageDeclaration = function () {
                return this._parsePageMarginBox() || this._parseRuleSetDeclaration();
            };
            Parser.prototype._parsePage = function () {
                // http://www.w3.org/TR/css3-page/
                // page_rule : PAGE_SYM S* page_selector_list '{' S* page_body '}' S*
                // page_body :  /* Can be empty */ declaration? [ ';' S* page_body ]? | page_margin_box page_body
                var node = this.create(nodes.Page);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@Page')) {
                    return null;
                }
                if (node.addChild(this._parsePageSelector())) {
                    while (this.accept(cssScanner_1.TokenType.Comma)) {
                        if (!node.addChild(this._parsePageSelector())) {
                            return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                        }
                    }
                }
                return this._parseBody(node, this._parsePageDeclaration.bind(this));
            };
            Parser.prototype._parsePageMarginBox = function () {
                // page_margin_box :  margin_sym S* '{' S* declaration? [ ';' S* declaration? ]* '}' S*
                var node = this.create(nodes.PageBoxMarginBox);
                if (!this.peek(cssScanner_1.TokenType.AtKeyword)) {
                    return null;
                }
                if (!this.acceptOne(cssScanner_1.TokenType.AtKeyword, languageFacts.getPageBoxDirectives())) {
                    this.markError(node, cssErrors_1.ParseError.UnknownAtRule, [], [cssScanner_1.TokenType.CurlyL]);
                }
                return this._parseBody(node, this._parseRuleSetDeclaration.bind(this));
            };
            Parser.prototype._parsePageSelector = function () {
                // page_selector : pseudo_page+ | IDENT pseudo_page*
                // pseudo_page :  ':' [ "left" | "right" | "first" | "blank" ];
                var node = this.create(nodes.Node);
                if (!this.peek(cssScanner_1.TokenType.Ident) && !this.peek(cssScanner_1.TokenType.Colon)) {
                    return null;
                }
                node.addChild(this._parseIdent()); // optional ident
                if (this.accept(cssScanner_1.TokenType.Colon)) {
                    if (!node.addChild(this._parseIdent())) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                    }
                }
                return this.finish(node);
            };
            Parser.prototype._parseDocument = function () {
                // -moz-document is experimental but has been pushed to css4
                var node = this.create(nodes.Document);
                if (!this.accept(cssScanner_1.TokenType.AtKeyword, '@-moz-document')) {
                    return null;
                }
                this.resync([], [cssScanner_1.TokenType.CurlyL]); // ignore all the rules
                return this._parseBody(node, this._parseStylesheetStatement.bind(this));
            };
            Parser.prototype._parseOperator = function () {
                // these are operators for binary expressions
                var node = this.createNode(nodes.NodeType.Operator);
                if (this.accept(cssScanner_1.TokenType.Delim, '/') ||
                    this.accept(cssScanner_1.TokenType.Delim, '*') ||
                    this.accept(cssScanner_1.TokenType.Delim, '+') ||
                    this.accept(cssScanner_1.TokenType.Delim, '-') ||
                    this.accept(cssScanner_1.TokenType.Dashmatch) ||
                    this.accept(cssScanner_1.TokenType.Includes) ||
                    this.accept(cssScanner_1.TokenType.SubstringOperator) ||
                    this.accept(cssScanner_1.TokenType.PrefixOperator) ||
                    this.accept(cssScanner_1.TokenType.SuffixOperator) ||
                    this.accept(cssScanner_1.TokenType.Delim, '=')) {
                    return this.finish(node);
                }
                else {
                    return null;
                }
            };
            Parser.prototype._parseUnaryOperator = function () {
                var node = this.create(nodes.Node);
                if (this.accept(cssScanner_1.TokenType.Delim, '+') || this.accept(cssScanner_1.TokenType.Delim, '-')) {
                    return this.finish(node);
                }
                else {
                    return null;
                }
            };
            Parser.prototype._parseCombinator = function () {
                var node = this.create(nodes.Node);
                if (this.accept(cssScanner_1.TokenType.Delim, '>')) {
                    node.type = nodes.NodeType.SelectorCombinatorParent;
                    return this.finish(node);
                }
                else if (this.accept(cssScanner_1.TokenType.Delim, '+')) {
                    node.type = nodes.NodeType.SelectorCombinatorSibling;
                    return this.finish(node);
                }
                else if (this.accept(cssScanner_1.TokenType.Delim, '~')) {
                    node.type = nodes.NodeType.SelectorCombinatorAllSiblings;
                    return this.finish(node);
                }
                else {
                    return null;
                }
            };
            Parser.prototype._parseSimpleSelector = function () {
                // simple_selector
                //  : element_name [ HASH | class | attrib | pseudo ]* | [ HASH | class | attrib | pseudo ]+ ;
                var node = this.create(nodes.SimpleSelector);
                var c = 0;
                if (node.addChild(this._parseElementName())) {
                    c++;
                }
                while ((c === 0 || !this.hasWhitespace()) && node.addChild(this._parseSimpleSelectorBody())) {
                    c++;
                }
                return c > 0 ? this.finish(node) : null;
            };
            Parser.prototype._parseSimpleSelectorBody = function () {
                return this._parsePseudo() || this._parseHash() || this._parseClass() || this._parseAttrib();
            };
            Parser.prototype._parseSelectorIdent = function () {
                return this._parseIdent();
            };
            Parser.prototype._parseHash = function () {
                if (!this.peek(cssScanner_1.TokenType.Hash) && !this.peek(cssScanner_1.TokenType.Delim, '#')) {
                    return null;
                }
                var node = this.createNode(nodes.NodeType.IdentifierSelector);
                if (this.accept(cssScanner_1.TokenType.Delim, '#')) {
                    if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                    }
                }
                else {
                    this.consumeToken(); // TokenType.Hash
                }
                return this.finish(node);
            };
            Parser.prototype._parseClass = function () {
                // class: '.' IDENT ;
                if (!this.peek(cssScanner_1.TokenType.Delim, '.')) {
                    return null;
                }
                var node = this.createNode(nodes.NodeType.ClassSelector);
                this.consumeToken(); // '.'
                if (this.hasWhitespace() || !node.addChild(this._parseSelectorIdent())) {
                    return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parseElementName = function () {
                // element_name: IDENT | '*';
                var node = this.createNode(nodes.NodeType.ElementNameSelector);
                if (node.addChild(this._parseSelectorIdent()) || this.accept(cssScanner_1.TokenType.Delim, '*')) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseAttrib = function () {
                // attrib : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S*   [ IDENT | STRING ] S* ]? ']'
                if (!this.peek(cssScanner_1.TokenType.BracketL)) {
                    return null;
                }
                var node = this.createNode(nodes.NodeType.AttributeSelector);
                this.consumeToken(); // BracketL
                Iif (!node.addChild(this._parseBinaryExpr())) {
                    // is this bad?
                }
                Iif (!this.accept(cssScanner_1.TokenType.BracketR)) {
                    return this.finish(node, cssErrors_1.ParseError.RightSquareBracketExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parsePseudo = function () {
                // pseudo: ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
                if (!this.peek(cssScanner_1.TokenType.Colon)) {
                    return null;
                }
                var pos = this.mark();
                var node = this.createNode(nodes.NodeType.PseudoSelector);
                this.consumeToken(); // Colon
                if (!this.hasWhitespace()) {
                    // optional, support ::
                    if (this.accept(cssScanner_1.TokenType.Colon) && this.hasWhitespace()) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                    }
                    if (!node.addChild(this._parseIdent())) {
                        return this.finish(node, cssErrors_1.ParseError.IdentifierExpected);
                    }
                    if (!this.hasWhitespace() && this.accept(cssScanner_1.TokenType.ParenthesisL)) {
                        node.addChild(this._parseBinaryExpr() || this._parseSimpleSelector());
                        Iif (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
                            return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
                        }
                    }
                    return this.finish(node);
                }
                this.restoreAtMark(pos);
                return null;
            };
            Parser.prototype._parsePrio = function () {
                if (!this.peek(cssScanner_1.TokenType.Exclamation)) {
                    return null;
                }
                var node = this.createNode(nodes.NodeType.Prio);
                Eif (this.accept(cssScanner_1.TokenType.Exclamation) && this.accept(cssScanner_1.TokenType.Ident, 'important', true)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseExpr = function (stopOnComma) {
                if (stopOnComma === void 0) { stopOnComma = false; }
                var node = this.create(nodes.Expression);
                if (!node.addChild(this._parseBinaryExpr())) {
                    return null;
                }
                while (true) {
                    if (this.peek(cssScanner_1.TokenType.Comma)) {
                        if (stopOnComma) {
                            return this.finish(node);
                        }
                        this.consumeToken();
                    }
                    if (!node.addChild(this._parseBinaryExpr())) {
                        break;
                    }
                }
                return this.finish(node);
            };
            Parser.prototype._parseBinaryExpr = function (preparsedLeft, preparsedOper) {
                var node = this.create(nodes.BinaryExpression);
                if (!node.setLeft((preparsedLeft || this._parseTerm()))) {
                    return null;
                }
                if (!node.setOperator(preparsedOper || this._parseOperator())) {
                    return this.finish(node);
                }
                Iif (!node.setRight(this._parseTerm())) {
                    return this.finish(node, cssErrors_1.ParseError.TermExpected);
                }
                // things needed for multiple binary expressions
                node = this.finish(node);
                var operator = this._parseOperator();
                if (operator) {
                    node = this._parseBinaryExpr(node, operator);
                }
                return this.finish(node);
            };
            Parser.prototype._parseTerm = function () {
                var node = this.create(nodes.Term);
                node.setOperator(this._parseUnaryOperator()); // optional
                if (node.setExpression(this._parseFunction()) ||
                    node.setExpression(this._parseIdent()) ||
                    node.setExpression(this._parseURILiteral()) ||
                    node.setExpression(this._parseStringLiteral()) ||
                    node.setExpression(this._parseNumeric()) ||
                    node.setExpression(this._parseHexColor()) ||
                    node.setExpression(this._parseOperation())) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseOperation = function () {
                var node = this.create(nodes.Node);
                if (!this.accept(cssScanner_1.TokenType.ParenthesisL)) {
                    return null;
                }
                node.addChild(this._parseExpr());
                if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
                    return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parseNumeric = function () {
                var node = this.create(nodes.NumericValue);
                if (this.accept(cssScanner_1.TokenType.Num) ||
                    this.accept(cssScanner_1.TokenType.Percentage) ||
                    this.accept(cssScanner_1.TokenType.Resolution) ||
                    this.accept(cssScanner_1.TokenType.Length) ||
                    this.accept(cssScanner_1.TokenType.EMS) ||
                    this.accept(cssScanner_1.TokenType.EXS) ||
                    this.accept(cssScanner_1.TokenType.Angle) ||
                    this.accept(cssScanner_1.TokenType.Time) ||
                    this.accept(cssScanner_1.TokenType.Dimension) ||
                    this.accept(cssScanner_1.TokenType.Freq)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseStringLiteral = function () {
                var node = this.createNode(nodes.NodeType.StringLiteral);
                if (this.accept(cssScanner_1.TokenType.String) || this.accept(cssScanner_1.TokenType.BadString)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseURILiteral = function () {
                var node = this.createNode(nodes.NodeType.URILiteral);
                if (this.accept(cssScanner_1.TokenType.URI) || this.accept(cssScanner_1.TokenType.BadUri)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseIdent = function (referenceTypes) {
                var node = this.create(nodes.Identifier);
                if (referenceTypes) {
                    node.referenceTypes = referenceTypes;
                }
                node.isCustomProperty = this.peekRegExp(cssScanner_1.TokenType.Ident, /^--/);
                if (this.accept(cssScanner_1.TokenType.Ident)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseFunction = function () {
                var pos = this.mark();
                var node = this.create(nodes.Function);
                if (!node.setIdentifier(this._parseFunctionIdentifier())) {
                    return null;
                }
                if (this.hasWhitespace() || !this.accept(cssScanner_1.TokenType.ParenthesisL)) {
                    this.restoreAtMark(pos);
                    return null;
                }
                if (node.getArguments().addChild(this._parseFunctionArgument())) {
                    while (this.accept(cssScanner_1.TokenType.Comma)) {
                        if (!node.getArguments().addChild(this._parseFunctionArgument())) {
                            this.markError(node, cssErrors_1.ParseError.ExpressionExpected);
                        }
                    }
                }
                if (!this.accept(cssScanner_1.TokenType.ParenthesisR)) {
                    return this.finish(node, cssErrors_1.ParseError.RightParenthesisExpected);
                }
                return this.finish(node);
            };
            Parser.prototype._parseFunctionIdentifier = function () {
                var node = this.create(nodes.Identifier);
                node.referenceTypes = [nodes.ReferenceType.Function];
                if (this.accept(cssScanner_1.TokenType.Ident, 'progid')) {
                    // support for IE7 specific filters: 'progid:DXImageTransform.Microsoft.MotionBlur(strength=13, direction=310)'
                    Eif (this.accept(cssScanner_1.TokenType.Colon)) {
                        while (this.accept(cssScanner_1.TokenType.Ident) && this.accept(cssScanner_1.TokenType.Delim, '.')) {
                            // loop
                        }
                    }
                    return this.finish(node);
                }
                else if (this.accept(cssScanner_1.TokenType.Ident)) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseFunctionArgument = function () {
                var node = this.create(nodes.FunctionArgument);
                if (node.setValue(this._parseExpr(true))) {
                    return this.finish(node);
                }
                return null;
            };
            Parser.prototype._parseHexColor = function () {
                if (this.peekRegExp(cssScanner_1.TokenType.Hash, /^#[0-9A-Fa-f]{3}([0-9A-Fa-f]{3})?$/g)) {
                    var node = this.create(nodes.HexColorValue);
                    this.consumeToken();
                    return this.finish(node);
                }
                else {
                    return null;
                }
            };
            return Parser;
        }());
        exports.Parser = Parser;
    });
    //# sourceMappingURL=cssParser.js.map