All files / src/core/nfa getMatch.js

0% Statements 0/160
0% Branches 0/121
0% Functions 0/16
0% Lines 0/159
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
  var oAssign = require( '../../utils/objectAssign' );
 
/*eslint func-names: 0*/
 
  function Value( val ) {
    this.value = val;
  }
 
  function Piece( frag ) {
    this.fragment = frag;
  }
  function GroupedPiece( frag ) {
    this.fragment = frag;
  }
  function NamedPiece( name, frag ) {
    this.fragment = frag;
    this.name = name;
  }
  function GroupedAltPiece( frag ) {
    this.fragment = frag;
  }
  function NamedAltPiece( name, frag ) {
    this.fragment = frag;
    this.name = name;
  }
  function Nothing() {}
  function Empty() {}
  function MaybeEnter() {}
  function GroupName( name ) {
    this.name = name;
  }
 
  function getMatch( chain, walkFn, walkOpts ) {
    var { conform } = walkOpts;
    var { inputType } = chain;
 
    const valStack = [];
    var r;
 
    chain.forEach( function( curr ) {
      if ( curr.move ) {
        switch ( curr.move.dir ) {
        case 'maybe_enter' : {
          valStack.push( new MaybeEnter() );
        } break;
        case 'maybe_exit': {
          let c,
            acc = new Empty( );
          while ( !( ( c = valStack.pop() ) instanceof MaybeEnter ) ) {
            acc = _foldLeft( inputType, conform, acc, c );
          }
          valStack.push( acc );
        } break;
        case 'maybe_single_enter' : {
          valStack.push( new MaybeEnter() );
        } break;
        case 'maybe_single_exit': {
          let top = valStack.pop(),
            acc;
          if ( top instanceof MaybeEnter ) {
            acc = new Nothing();
          } else {
            acc = top;
          //get rid of MaybeEnter
            valStack.pop();
          }
          valStack.push( acc );
        } break;
        case 'enter' : {
        } break;
        case 'exit': {
          let c = valStack.pop();
          let acc;
          if ( conform && c instanceof NamedAltPiece ) {
            let interim;
            if ( c.fragment instanceof Empty ) {
              interim = {};
            } else {
              interim = { [ c.name ]: c.fragment };
            }
            acc = new GroupedAltPiece( interim );
          } else {
            acc = c;
          }
          valStack.push( acc );
        } break;
        case 'maybe_in': {
          if ( conform && curr.move.name ) {
            valStack.push( new GroupName( curr.move.name ) );
          }
        } break;
        case 'loop': {
        } break;
        case 'maybe_out': {
          if ( conform && curr.move.name ) {
            let c = valStack.pop();
            let gn = valStack.pop();
            valStack.push( _giveName( gn, c ) );
          }
        } break;
        case 'in': {
          if ( conform && curr.move.name ) {
            valStack.push( new GroupName( curr.move.name ) );
          }
        } break;
        case 'out': {
          if ( conform && curr.move.name ) {
            let c = valStack.pop();
            let gn = valStack.pop();
            valStack.push( _giveAltName( gn, c ) );
          }
        } break;
        case 'clause': {
          let conformed = walkFn( curr.clause, curr.guide, walkOpts );
          valStack.push( new Value( conformed ) );
        } break;
        default: console.error( curr ); throw 'FUUU';
        }
      }
    } );
    if ( valStack.length !== 1 ) {
      console.error( 'valStack', valStack );
      throw '!';
    }
    r = valStack.pop();
 
    let retVal;
 
    if ( r instanceof Piece ) {
      retVal = r.fragment;
    } else if ( r instanceof GroupedPiece ) {
      retVal = r.fragment;
    } else if ( r instanceof GroupedAltPiece ) {
      retVal = r.fragment;
    } else if ( r instanceof Value ) {
      retVal = r.value;
    } else if ( r instanceof Empty ) {
      retVal = _coerceToProperType( inputType, [] );
    } else if ( r instanceof Nothing ) {
      retVal = null;
    } else {
      retVal = r;
    }
 
    return retVal;
  }
 
  function _giveName( groupName, c ) {
    if ( c instanceof Nothing ) {
      return new NamedPiece( groupName.name, new Empty() );
    } else if ( c instanceof GroupedPiece ) {
      return new NamedPiece( groupName.name, c.fragment );
    } else if ( c instanceof Piece || c instanceof GroupedAltPiece ) {
      return new NamedPiece( groupName.name, c.fragment );
    } else if ( c instanceof Value ) {
      return new NamedPiece( groupName.name, c.value );
    } else if ( c instanceof Empty ) {
      return new NamedPiece( groupName.name, new Empty() );
    } else {
      console.error( c );
      throw 'c!!!';
    }
  }
 
  function _giveAltName( groupName, c ) {
    if ( c instanceof Nothing ) {
      return new NamedAltPiece( groupName.name, new Empty() );
    } else if ( c instanceof GroupedAltPiece ) {
      return new NamedAltPiece( groupName.name, c.fragment );
    } else if ( c instanceof Piece ) {
      return new NamedAltPiece( groupName.name, c.fragment );
    } else if ( c instanceof GroupedPiece ) {
      return new NamedAltPiece( groupName.name, c.fragment );
    } else if ( c instanceof Value ) {
      return new NamedAltPiece( groupName.name, c.value );
    } else if ( c instanceof Empty ) {
      return new NamedAltPiece( groupName.name, new Empty() );
    } else {
      console.error( c );
      throw 'c!!!alt';
    }
  }
 
  function _foldLeft( inputType, conform, acc, c ) {
 
    if ( conform && c instanceof NamedPiece ) {
      let rightPart;
 
      if ( acc instanceof Nothing ) {
        rightPart = {};
      } else if ( !acc ) {
        rightPart = {};
      } else if ( acc instanceof Empty ) {
        rightPart = {};
      } else if ( acc instanceof GroupedPiece ) {
        rightPart = acc.fragment;
      } else if ( acc instanceof Piece ) {
        rightPart = acc.fragment;
      } else if ( acc instanceof GroupedAltPiece ) {
        rightPart = acc.fragment;
      } else {
        console.error( acc, c );
        throw '!!acc_fl_np';
      }
      let interim;
      if ( c.fragment instanceof Empty ) {
        interim = {};
      } else {
        interim = {
          [ c.name ]: c.fragment
        };
      }
      var gp = new GroupedPiece(
      oAssign( {}, interim, rightPart )
    );
      return gp;
    } else {
      let leftArr,
        rightArr1;
      if ( acc instanceof Nothing ) {
        rightArr1 = [];
      } else if ( !acc ) {
        rightArr1 = [];
      } else if ( acc instanceof Piece ) {
        rightArr1 = acc.fragment;
      } else if ( acc instanceof GroupedPiece ) {
        rightArr1 = [ acc.fragment ];
      } else if ( acc instanceof Empty ) {
        rightArr1 = [];
      } else {
        console.error( acc );
        throw '!!acc_fl';
      }
 
      if ( c instanceof Value ) {
        leftArr = [ c.value ];
      } else if ( c instanceof Piece ) {
        leftArr = c.fragment;
      } else if ( c instanceof GroupedPiece ) {
        leftArr = [ c.fragment ];
      } else if ( c instanceof GroupedAltPiece ) {
        leftArr = [ c.fragment ];
      } else if ( c instanceof Nothing ) {
        leftArr = [];
      } else if ( c instanceof Empty ) {
        leftArr = [];
      } else {
        console.error( c );
        throw 'cc!!';
      }
      let p = new Piece( _coerceToProperType( inputType, leftArr.concat( rightArr1 ) ) );
      return p;
    }
  }
 
  function _coerceToProperType( t, arr ) {
    if ( t === 'string' && Array.isArray( arr ) ) {
      // var r = arr.reduce( ( acc, curr ) => {
      //   if ( typeof curr === 'string' && acc.length > 0 || typeof acc[ acc.length - 1 ] === 'string' ) {
      //     return acc.slice( 0, acc.length - 1 ).concat( acc[ acc.length - 1 ].concat( curr ) );
      //   } else {
      //     return acc.concat( [ curr ] );
      //   }
      // }, [] );
      // if ( !r ) {
      //   debugger;
      // }
      // if ( r.length === 1 && typeof r[ 0 ] === 'string' ) {
      // // pure string
      //   return r[ 0 ];
      // } else {
      //   return r;
      // }
      return arr.join( '' )
    } else {
      return arr;
    }
  }
 
  module.exports = getMatch;