<commit_before>part of rethinkdb;

class RqlQuery{
    p.Term_TermType tt;

    int _err_depth = 0;
    List args = [];
    Map optargs = {};

    RqlQuery([List args, Map optargs]){
        if(args != null)
          
          args.forEach((e){
            if(_check_if_options(e,tt)){
              optargs = optargs == null ? e : optargs;
            }else{
            if(e != null)
              this.args.add(_expr(e));
            }
          });
        if(optargs != null)
          optargs.forEach((k,v){
           this.optargs[k] = _expr(v);
          });
        //args = [new Args(args)];
    }
    
    _expr(val, [nesting_depth=20]){

        if(nesting_depth <= 0)
          throw new RqlDriverError("Nesting depth limit exceeded");

        if(nesting_depth is int == false)
            throw new RqlDriverError("Second argument to `r.expr` must be a number.");

        if(val is RqlQuery)
            return val;
        else if(val is List){
            val.forEach((v){
              v = _expr(v,nesting_depth - 1);
            });
            
            return new MakeArray(val);
        }
        else if(val is Map){
            Map obj = {};
      
            val.forEach((k,v){
              obj[k] = _expr(v,nesting_depth -1);
            });

            return new MakeObj(obj);
        }
        else if(val is Function)
            return new Func(val);
        else if(val is DateTime){
           return new Time.nativeTime(val);
        }
        else
          return new Datum(val);
    }


    Future run(Connection c, [global_optargs]){
      if(c == null)
        throw new RqlDriverError("RqlQuery.run must be given a connection to run on.");
      if(global_optargs == null)
        global_optargs = {};
      return c._start(this, global_optargs);
    }

    //since a term that may take multiple options can now be passed
    //one or two, we can't know if the final argument in a query
    //is actually an option or just another arg.  _check_if_options
    //checks if all of the keys in the object are in options
    _check_if_options(obj,p.Term_TermType tt){
      if(obj is Map == false){
        return false;
      }else{
        List options = new _RqlAllOptions(tt).options;

        bool isOptions = true;
        obj.keys.forEach((k){
          if(!options.contains(k)){
            isOptions = false;
          }
        });
        return isOptions;
      }
    }

    build(){
        List res = [];
        res.add(this.tt.value);
        List argList = [];
        args.forEach((arg){
          if(arg != null)
            argList.add(arg.build());
        });
        res.add(argList);

        if(optargs.length > 0){
          Map optArgsMap = {};
          optargs.forEach((k,v){
            optArgsMap[k] = v.build();
          });
          res.add(optArgsMap);
        }
        return res;
    }

    
    _recursively_convert_pseudotypes(obj, format_opts){
        if(obj is Map){
          obj.forEach((k,v){
            obj[k] = _recursively_convert_pseudotypes(v,format_opts);
          });
          obj = _convert_pseudotype(obj, format_opts);
        }else if(obj is List){
          obj.forEach((e){
            e = _recursively_convert_pseudotypes(e,format_opts);
          });
        }
        return obj;
    }
    
    _listify(args,[parg]){
      if(args is List){
        args.insert(0,parg);
        return args;
      }
      else{
        if(args != null){
          if(parg != null)
            return [parg,args];
          else
            return [args];
        }
        else
          return [];
      }
    }


    _ivar_scan(query){

      if(!(query is RqlQuery)){
         return false;
     }

     if(query is ImplicitVar){
         return true;
     }
     if(query.args.any(_ivar_scan)){
       return true;
     }

     var optArgKeys = query.optargs.keys;
     
     if(optArgKeys.any(_ivar_scan)){
       return true;
     }
       return false;
    }

    // Called on arguments that should be functions
    _func_wrap(val){
     val = _expr(val);
     if(_ivar_scan(val)){
         return new Func((x) => val);
     }
     return val;
    }


    _reql_type_time_to_datetime(Map obj){
        if(obj["epoch_time"] == null){
          throw new RqlDriverError('pseudo-type TIME object $obj does not have expected field "epoch_time".');
        }else{
          String s = obj["epoch_time"].toString();
                   if(s.indexOf(".")>=0){
                   List l = s.split('.');<commit_msg>#32 - bug where implicit vars weren't getting wrapped<commit_after>part of rethinkdb;

class RqlQuery{
    p.Term_TermType tt;

    int _err_depth = 0;
    List args = [];
    Map optargs = {};

    RqlQuery([List args, Map optargs]){
        if(args != null)
          
          args.forEach((e){
            if(_check_if_options(e,tt)){
              optargs = optargs == null ? e : optargs;
            }else{
            if(e != null)
              this.args.add(_expr(e));
            }
          });
        if(optargs != null)
          optargs.forEach((k,v){
           this.optargs[k] = _expr(v);
          });
        //args = [new Args(args)];
    }
    
    _expr(val, [nesting_depth=20]){

        if(nesting_depth <= 0)
          throw new RqlDriverError("Nesting depth limit exceeded");

        if(nesting_depth is int == false)
            throw new RqlDriverError("Second argument to `r.expr` must be a number.");

        if(val is RqlQuery)
            return val;
        else if(val is List){
            val.forEach((v){
              v = _expr(v,nesting_depth - 1);
            });
            
            return new MakeArray(val);
        }
        else if(val is Map){
            Map obj = {};
      
            val.forEach((k,v){
              obj[k] = _expr(v,nesting_depth -1);
            });

            return new MakeObj(obj);
        }
        else if(val is Function)
            return new Func(val);
        else if(val is DateTime){
           return new Time.nativeTime(val);
        }
        else
          return new Datum(val);
    }


    Future run(Connection c, [global_optargs]){
      if(c == null)
        throw new RqlDriverError("RqlQuery.run must be given a connection to run on.");
      if(global_optargs == null)
        global_optargs = {};
      return c._start(this, global_optargs);
    }

    //since a term that may take multiple options can now be passed
    //one or two, we can't know if the final argument in a query
    //is actually an option or just another arg.  _check_if_options
    //checks if all of the keys in the object are in options
    _check_if_options(obj,p.Term_TermType tt){
      if(obj is Map == false){
        return false;
      }else{
        List options = new _RqlAllOptions(tt).options;

        bool isOptions = true;
        obj.keys.forEach((k){
          if(!options.contains(k)){
            isOptions = false;
          }
        });
        return isOptions;
      }
    }

    build(){
        List res = [];
        res.add(this.tt.value);
        List argList = [];
        args.forEach((arg){
          if(arg != null)
            argList.add(arg.build());
        });
        res.add(argList);

        if(optargs.length > 0){
          Map optArgsMap = {};
          optargs.forEach((k,v){
            optArgsMap[k] = v.build();
          });
          res.add(optArgsMap);
        }
        return res;
    }

    
    _recursively_convert_pseudotypes(obj, format_opts){
        if(obj is Map){
          obj.forEach((k,v){
            obj[k] = _recursively_convert_pseudotypes(v,format_opts);
          });
          obj = _convert_pseudotype(obj, format_opts);
        }else if(obj is List){
          obj.forEach((e){
            e = _recursively_convert_pseudotypes(e,format_opts);
          });
        }
        return obj;
    }
    
    _listify(args,[parg]){
      if(args is List){
        args.insert(0,parg);
        return args;
      }
      else{
        if(args != null){
          if(parg != null)
            return [parg,args];
          else
            return [args];
        }
        else
          return [];
      }
    }


    _ivar_scan(query){

      if(!(query is RqlQuery)){
         return false;
     }

     if(query is ImplicitVar){
         return true;
     }
     if(query.args.any(_ivar_scan)){
       return true;
     }

     var optArgKeys = query.optargs.values;
     
     if(optArgKeys.any(_ivar_scan)){
       return true;
     }
       return false;
    }

    // Called on arguments that should be functions
    _func_wrap(val){
     val = _expr(val);
     if(_ivar_scan(val)){
         return new Func((x) => val);
     }
     return val;
    }


    _reql_type_time_to_datetime(Map obj){
        if(obj["epoch_time"] == null){
          throw new RqlDriverError('pseudo-type TIME object $obj does not have expected field "epoch_time".');
        }else{
          String s = obj["epoch_time"].toString();
                   if(s.indexOf(".")>=0){
                   List l = s.split('.');
{'fraction_non_alphanumeric': 0.12094102054340623, 'fraction_numerical': 0.002208968411751712, 'mean_word_length': 1.5608031674208145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '300204', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2730, 'n_words': 654}