# All files / src/parsers/collections collectionCore.js

100% Statements 15/15
100% Branches 4/4
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    19x

    19x
    6x
    6x

    6x

    6x
    495x

    19x
    6x
    6x
    6x

    35x

    35x

    35x

    35x

     // @flow

    import P from 'parsimmon'
    import crap from '../crap'

    import type {
      CollectionCoreNodeType,
      CollectionCoreValueType,
      TupleNodeType,
      NodeType
    } from '../../types'

    const SeparatorParser = P.string(',').trim(crap)

    const CollectionCoreLiteralParser = P.lazy((): mixed => {
      const TupleParser = require('./tuple').default
      const SpreadParser = P.string('...')
        .then(crap)
        .then(TupleParser)
        .node('spread')
      const SimpleListParser = P.sepBy(TupleParser, SeparatorParser).node(
        'simpleList'
      )
      return P.sepBy(P.alt(SpreadParser, SimpleListParser), SeparatorParser).map(
        (value: CollectionCoreValueType): CollectionCoreNodeType => ({
          name: 'collectionCore',
          value
        })
      )
    })

    const ComprehensionParser = P.lazy((): mixed => {
      const TupleParser = require('./tuple').default
      const ProgramParser = require('../program').default
      return P.seq(
        P.string('each')
          .then(crap)
          .then(TupleParser),
        crap.then(P.string('in')).then(crap.then(TupleParser)),
        crap
          .then(P.string('if'))
          .then(crap.then(TupleParser))
          .atMost(1),
        crap.then(ProgramParser).atMost(1)
      ).map(
        ([left, middle, condition, right]: [
          TupleNodeType,
          TupleNodeType,
          [NodeType],
          [NodeType]
        ]): CollectionCoreNodeType => {
          const extractionPart =
            right.length === 1
              ? {
                name: 'pipe',
                value: {
                  left: middle,
                  right: right[0]
                }
              }
              : middle

          const extractionPartWithOptionalCondition =
            condition.length === 1
              ? {
                name: 'pipe',
                value: {
                  left: extractionPart,
                  right: {
                    name: 'functionCall',
                    value: {
                      left: {
                        name: 'identifier',
                        value: 'filter'
                      },
                      right: {
                        name: 'lambda',
                        value: {
                          variable: 'input',
                          definition: condition[0]
                        }
                      }
                    }
                  }
                }
              }
              : extractionPart

          const mapPart = {
            name: 'functionCall',
            value: {
              left: {
                name: 'identifier',
                value: 'map'
              },
              right: {
                name: 'lambda',
                value: {
                  variable: 'input',
                  definition: left
                }
              }
            }
          }

          return {
            name: 'collectionCore',
            value: [
              {
                name: 'spread',
                value: {
                  name: 'parentheses',
                  value: {
                    name: 'pipe',
                    value: {
                      left: extractionPartWithOptionalCondition,
                      right: mapPart
                    }
                  }
                }
              }
            ]
          }
        }
      )
    })

    export default P.alt(ComprehensionParser, CollectionCoreLiteralParser)