Matches a comma separated list of tokens. Can take a callback to validate the input on each iteration. Note: this always returns a list. The calling function should determine if an empty list is a syntax error or not.
163953
function commaSeparated (fn, inp, cb) {
    var all = [], res;
    while (1) {
      res = fn(inp);
      if ((res && !cb) || (res && cb(res, inp))) {
        all.push(res);
        if (inp.skipWs().takeAPeek(",")) {
          inp.skipWs();
        } else break;
      } else break;
    }
    return all;
  }
1226774
def _group_matching(tlist, cls):
    """"""
    opens = []
    tidx_offset = 0
    for idx, token in enumerate(list(tlist)):
        tidx = idx - tidx_offset

        if token.is_whitespace:
            # ~50% of tokens will be whitespace. Will checking early
            # for them avoid 3 comparisons, but then add 1 more comparison
            # for the other ~50% of tokens...
            continue

        if token.is_group and not isinstance(token, cls):
            # Check inside previously grouped (i.e. parenthesis) if group
            # of different type is inside (i.e., case). though ideally  should
            # should check for all open/close tokens at once to avoid recursion
            _group_matching(token, cls)
            continue

        if token.match(*cls.M_OPEN):
            opens.append(tidx)

        elif token.match(*cls.M_CLOSE):
            try:
                open_idx = opens.pop()
            except IndexError:
                # this indicates invalid sql and unbalanced tokens.
                # instead of break, continue in case other "valid" groups exist
                continue
            close_idx = tidx
            tlist.group_tokens(cls, open_idx, close_idx)
            tidx_offset += close_idx - open_idx
1010252
def _parse_option(cls, token):
        
        description = ""
        validator = None

        if " : " in token:
            token, description = tuple(token.split(" : ", 2))

            token = token.strip()

            description = description.strip()

        # Checking validator:
        matches = re.match(r"(.*)\((.*?)\)", token)
        if matches:
            token = matches.group(1).strip()
            validator = matches.group(2).strip()

        shortcut = None

        matches = re.split(r"\s*\|\s*", token, 2)

        if len(matches) > 1:
            shortcut = matches[0].lstrip("-")
            token = matches[1]
        else:
            token = token.lstrip("-")

        default = None
        mode = Option.NO_VALUE

        if token.endswith("=*"):
            mode = Option.MULTI_VALUED
            token = token.rstrip("=*")
        elif token.endswith("=?*"):
            mode = Option.MULTI_VALUED
            token = token.rstrip("=?*")
        elif token.endswith("=?"):
            mode = Option.OPTIONAL_VALUE
            token = token.rstrip("=?")
        elif token.endswith("="):
            mode = Option.REQUIRED_VALUE
            token = token.rstrip("=")

        matches = re.match(r"(.+)(=[?*]*)(.+)", token)
        if matches:
            token = matches.group(1)
            operator = matches.group(2)
            default = matches.group(3)

            if operator == "=*":
                mode = Option.REQUIRED_VALUE | Option.MULTI_VALUED
            elif operator == "=?*":
                mode = Option.MULTI_VALUED
            elif operator == "=?":
                mode = Option.OPTIONAL_VALUE
            elif operator == "=":
                mode = Option.REQUIRED_VALUE

        return _option(token, shortcut, mode, description, default)
1184001
def underscore_search(input, list, first_match=false)
      meth = first_match ? :find : :select
      return (first_match ? input : [input]) if list.include?(input)
      input = input.to_s
      if input.include?("_")
        underscore_regex = input.split('_').map {|e|
          Regexp.escape(e) }.join("([^_]+)?_")
        list.send(meth) {|e| e.to_s =~ /^#{underscore_regex}/ }
      else
        escaped_input = Regexp.escape(input)
        list.send(meth) {|e| e.to_s =~ /^#{escaped_input}/ }
      end
    end
1213609
function find(list, match) {
	  for (var i = 0; i < list.length; i++) {
	    if (match(list[i])) {
	      return list[i];
	    }
	  }
	  return undefined;
	}
1119377
private boolean match(JsDocToken token1, JsDocToken token2) {
    unreadToken = next();
    return unreadToken == token1 || unreadToken == token2;
  }
579169
def get_tokens(tokens):
    
    tokens = tokens[1:-1]
    ret = []
    start = 0
    stack = 0
    for i in range(len(tokens)):
        if tokens[i] == '(':
            if stack == 0:
                start = i
            stack += 1
        elif tokens[i] == ')':
            stack -= 1
            if stack < 0:
                raise Exception('Bracket mismatch: ' + str(tokens))
            if stack == 0:
                ret.append(get_tokens(tokens[start:i + 1]))
        else:
            if stack == 0:
                ret.append(tokens[i])
    if stack != 0:
        raise Exception('Bracket mismatch: ' + str(tokens))
    return ret
1361890
def token_clean(self, length, numbers=True):

        

        def clean1(tokens):
            return [t for t in tokens if t.isalpha() == 1 and len(t) > length]

        def clean2(tokens):
            return [t for t in tokens if t.isalnum() == 1 and len(t) > length]

        if numbers:
            self.tokens = list(map(clean1, self.tokens))
        else:
            self.tokens = list(map(clean2, self.tokens))
892344
def match(pattern_element)
    @num_match_attempts += 1
    return :no_pattern_element unless pattern_element
    return :skipped if pattern_element.delimiter &&
      (
      if last_match
        last_match.delimiter       # don't match two delimiters in a row
      else
        @num_match_attempts > 1   # don't match a delimiter as the first element unless this is the first match attempt
      end
      )

    if result = pattern_element.parse(self)
      add_match result, pattern_element.name # success, but don't keep EmptyNodes
    end
  end
734043
def contains(self, token: str) -> bool:
        """"""
        self._validate_token(token)
        return token in self
860586
def comma_delimited_to_list(list_param):
    
    if isinstance(list_param, list):
        return list_param
    if isinstance(list_param, str):
        return list_param.split(',')
    else:
        return []
316683
function matches(str, match) {
    if (Array.isArray(match)) return match.indexOf(str) > -1;
    return match.test(str);
}
210393
def create_token_generator(input_list):
    """"""
    ###Generator trick from http://betteratoracle.com/posts/20-how-do-i-bind-a-variable-in-list
    ###The maximum length of the comma separated list is 4000 characters, therefore we need to split the list
    ###ORA-01460: unimplemented or unreasonable conversion requested will thrown if list is larger
    oracle_limit = 4000
    grp_list = []
    if type(input_list[0]) == int :
        input_str = ','.join(map(str, input_list))
    else:
        input_str = ','.join(input_list) 

    if len(input_str) >= oracle_limit:
        index = 0
        while True:
            begin, end = index, index+oracle_limit
	    if end > len(input_str):
                end = len(input_str)
                grp_list.append(input_str[begin:end])
                break
	    else: 	
		index = input_str.rfind(',', begin, end)
		if index == -1:
			break
		grp_list.append(input_str[begin:index])
		index += 1 #to remove the leading comma
    else:
        grp_list.append(input_str)

    token_generator = """
    WITH TOKEN_GENERATOR AS (
    """
    binds = {}
    for index, chunk in enumerate(grp_list):
        if index:
            token_generator += """
            UNION ALL
            """
        bind = "token_%s" % index
        token_generator += """SELECT REGEXP_SUBSTR(:{bind}, '[^,]+', 1, LEVEL) token
        FROM DUAL
        CONNECT BY LEVEL <= LENGTH(:{bind}) - LENGTH(REPLACE(:{bind}, ',', '')) + 1
        """.format(bind=bind)
        binds.update({bind: chunk})
    token_generator += ")"

    return token_generator, binds
792216
function stringMatch(stringValues) {
	return function(testValue) {
		for (var i = stringValues.length - 1; i >= 0; i--) {
			if (stringValues[i] === testValue) {
				return true;
			}
		}
		return false;
	};
}
36388
def match(self, seq, **kwargs):
        '''
        '''
        strict = kwargs.get('strict', False)
        top_level = kwargs.get('top_level', True)
        match = kwargs.get('match', list())

        if top_level:
            kwargs['top_level'] = False
            kwargs['match'] = match

            try:
                seq = IterableList(seq)
                self.match(seq, **kwargs)

                if strict:
                    if not seq.empty():
                        raise MatchFailed('Sequence is too long', seq)

            except MatchFailed as e:
                return e.failure()

            return Match(*match)

        for elem in self.elems:
            elem.match(seq, **kwargs)
481427
function isValidRegExp(context: ParserContext): boolean {
  const previousNonWsToken = context.token(context.count() - 1);

  let previousToken;
  if (context.defaultData.text !== "") {
    previousToken = context.createToken("default", context.defaultData.text);
  } else {
    previousToken = previousNonWsToken;
    // Okay if there is no previous token.
    if (!previousToken) return true;
  }

  // Since JavaScript doesn't require statement terminators, if the previous
  // token was whitespace and contained a newline, then we're good.
  if (
    previousToken.name === "default" &&
    previousToken.value.indexOf("\n") >= 0
  )
    return true;

  // In these case the / stands for division.
  if (util.contains(["keyword", "ident", "number"], previousNonWsToken.name))
    return false;

  if (
    previousNonWsToken.name === "punctuation" &&
    !util.contains(["(", "{", "[", ",", ";"], previousNonWsToken.value)
  )
    return false;

  return true;
}
1019843
function matchSignature(list, arg) {
		var types = arg.map(function(variable) {
				return new RegExp('^(' + type(variable) + ')');
			});

		return list.filter(function(config) {
			var variadic = false,
				result;

			//  result is true if no more arguments are provided than the signature allows OR the last
			//  argument in the signature is variadic
			result = arg.length <= config.arguments.length || (config.arguments[config.arguments.length - 1] && config.arguments[config.arguments.length - 1].type === '...');

			//  test each given argument agains the configured signatures
			if (result) {
				arg.forEach(function(value, index) {
					var expect = config.arguments[index] ? config.arguments[index].type : null;

					//  look at ourself and ahead - if there is a following item, and it is variadic, it may be
					//  left out entirely (zero or more)
					if (isTypeAtIndex('...', config.arguments, index)) {
						variadic = true;
					}

					//  the result remains valid as long as the values match the given signature
					//  (type matches or it is variadic)
					result = result && (variadic || types[index].test(expect) || (expect[expect.length - 1] !== '!' && isExtendOf(expect, value)));
				});
			}

			return result;
		});
	}