Compiles all the rules from the environment into a list of rules.
194964
def compile_rules(environment):
    """"""
    e = re.escape
    rules = [
        (len(environment.comment_start_string), 'comment',
         e(environment.comment_start_string)),
        (len(environment.block_start_string), 'block',
         e(environment.block_start_string)),
        (len(environment.variable_start_string), 'variable',
         e(environment.variable_start_string))
    ]

    if environment.line_statement_prefix is not None:
        rules.append((len(environment.line_statement_prefix), 'linestatement',
                      r'^\s*' + e(environment.line_statement_prefix)))
    if environment.line_comment_prefix is not None:
        rules.append((len(environment.line_comment_prefix), 'linecomment',
                      r'(?:^|(?<=\S))[^\S\r\n]*' +
                      e(environment.line_comment_prefix)))

    return [x[1:] for x in sorted(rules, reverse=True)]
490939
function Rules (options) {
  this.options = options;
  this._keep = [];
  this._remove = [];

  this.blankRule = {
    replacement: options.blankReplacement
  };

  this.keepReplacement = options.keepReplacement;

  this.defaultRule = {
    replacement: options.defaultReplacement
  };

  this.array = [];
  for (var key in options.rules) this.array.push(options.rules[key]);
}
913
def create_css(self, rules):
        
        style = rules[0].legacy_compiler_options.get(
            'style', self.compiler.output_style)
        debug_info = self.compiler.generate_source_map

        if style == 'legacy':
            sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg = True, ' ', '  ', False, '', '\n', '\n', '\n', debug_info
        elif style == 'compressed':
            sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg = False, '', '', False, '', '', '', '', False
        elif style == 'compact':
            sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg = True, ' ', '', False, '\n', ' ', '\n', ' ', debug_info
        elif style == 'expanded':
            sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg = True, ' ', '  ', False, '\n', '\n', '\n', '\n', debug_info
        else:  # if style == 'nested':
            sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg = True, ' ', '  ', True, '\n', '\n', '\n', ' ', debug_info

        return self._create_css(rules, sc, sp, tb, nst, srnl, nl, rnl, lnl, dbg)
543656
function Ruleset(rules, composite) {
  if (!(this instanceof Ruleset)) {
    return new Ruleset(rules, composite);
  }
  this.composite = composite || 'all';
  this.rules = rules || [];
}
219295
List<Object> getSortedRules() {
        List<Object> result = new ArrayList<Object>();
        for (RuleEntry entry : getSortedEntries()) {
            result.add(entry.rule);
        }
        return result;
    }
731366
function prepareRules(options, rules, defaultDir) {
  rules = u.findRules(options, rules, defaultDir);
  var anyStartRules = [];
  var anyEndRules = [];
  var typeToRules = {'': anyStartRules, 'end': anyEndRules};

  rules.forEach(function (rule) {
    var ruleTypes = rule.type || [];
    ruleTypes = Array.isArray(ruleTypes) ? ruleTypes : [ruleTypes];
    if (ruleTypes.length === 0) {
      ruleTypes.push('');
    }
    ruleTypes.forEach(function (ruleType) {
      if (ruleType === '') {
        anyStartRules.push(rule);
        // add this any rule to the end of any typed start rule lists
        Object.keys(typeToRules).forEach(function (key) {
          if (key !== '' && key !== 'end' && key.substring(0,4) !== 'end-') {
            typeToRules[key].push(rule);
          }
        });
      } else if (ruleType === 'end') {
        anyEndRules.push(rule);
        // add this any rule to the end of any typed end rule lists
        Object.keys(typeToRules).forEach(function (key) {
          if (key !== '' && key.substring(0,4) === 'end-') {
            typeToRules[key].push(rule);
          }
        });
      } else {
        if (!(ruleType in typeToRules)) {
          typeToRules[ruleType] = [];
          if (ruleType.substring(0,4) !== 'end-') {
            // put all preceding any start rules first
            typeToRules[ruleType] = [].concat(anyStartRules);
          } else {
            // put all preceding any end rules first
            typeToRules[ruleType] = [].concat(anyEndRules);
          }
        }
        typeToRules[ruleType].push(rule);
      }
    });
  });

  return typeToRules;
}
66916
def compile(patterns, flags):  # noqa A001
    """"""

    positive = []
    negative = []
    if isinstance(patterns, (str, bytes)):
        patterns = [patterns]

    for pattern in patterns:
        for expanded in expand_braces(pattern, flags):
            (negative if is_negative(expanded, flags) else positive).append(_compile(expanded, flags))

    if patterns and flags & REALPATH and negative and not positive:
        positive.append(_compile(b'**' if isinstance(patterns[0], bytes) else '**', flags))

    return WcRegexp(tuple(positive), tuple(negative), flags & REALPATH, flags & PATHNAME, flags & FOLLOW)
532175
public function setupRules()
    {
        $standard = new Validator(['!string', '!comment']);

        $this->rules->addMany([
            'keyword' => new Rule(new WordMatcher([
                'and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global', 'or', 'with', 'assert', 'else', 'if',
                'pass', 'yield', 'break', 'except', 'import', 'print', 'class', 'exec', 'in', 'raise', 'continue',
                'finally', 'is', 'return', 'def', 'for', 'lambda', 'try',
            ])),

            'operator' => new Rule(
                new RegexMatcher('/([-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~])|\b(or|and|not)\b/'), [
                    'priority' => -1
                ]
            ),

            'expression' => new Rule(new RegexMatcher('/\{(\S+)\}/'), [
                'context' => ['string']
            ]),

            'variable' => [
                new Rule(new RegexMatcher('/[^\w.]([a-z_]\w*)\.\w/i')),
                'property' => new Rule(new RegexMatcher('/(?=(?:\w|\)|\])\s*\.([a-z_]\w*))/i'), [
                    'priority' => -2,
                    'context' => ['*none', '*expression']
                ]),
            ],

            'symbol' => [
                new Rule(new RegexMatcher('/import\s+([a-z_][\w.]*)(?:\s*,\s*([a-z_][\w.]*))*/i', [
                    1 => Token::NAME,
                    2 => Token::NAME,
                ])),
                'library' => new Rule(new RegexMatcher('/from\s+([a-z_][\w.]*)\s+import/i', [
                    1 => Token::NAME,
                ]))
            ],

            'operator.escape' => new Rule(new RegexMatcher('/(\\\(?:\R|.|[0-7]{3}|x\x{2}))/s'), [
                'context' => Validator::everywhere()
            ]),

            'comment' => new Rule(new CommentMatcher(['#'])),
            'constant.special' => [
                new Rule(new WordMatcher(['True', 'False', 'NotImplemented', 'Ellipsis'], [
                    'case-sensitivity' => true
                ])),
                new Rule(new RegexMatcher('/\b(__\w+__)\b/'))
            ],
            'call' => new Rule(new RegexMatcher('/([a-z_]\w*)\s*\(/i'), ['priority' => 2]),

            'meta.newline' => new CloseRule(new RegexMatcher('/()\R/'), [
                'factory' => new TokenFactory(TerminatorToken::class),
                'context' => ['!operator.escape', 'string.single-line'],
                'closes'  => ['string.single-line.double', 'string.single-line.single']
            ]),

            'number' => new Rule(
                new RegexMatcher('/(-?\b(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?)\b/')
            ),

            'string' => [
                'single-line' => [
                    'double' => new Rule(new RegexMatcher('/(?:^|[^"])(")(?=[^"]|$)/'), [
                        'factory' => new TokenFactory(ContextualToken::class),
                        'context' => $standard,
                    ]),
                    'single' => new Rule(new RegexMatcher('/(?:^|[^\'])(\')(?=[^\']|$)/'), [
                        'factory' => new TokenFactory(ContextualToken::class),
                        'context' => $standard,
                    ]),
                ],
                'multi-line' => [
                    'double' => new Rule(new SubStringMatcher('"""'), [
                        'factory'  => new TokenFactory(ContextualToken::class),
                        'context'  => $standard,
                        'priority' => 2,
                    ]),
                    'single' => new Rule(new SubStringMatcher('\'\'\''), [
                        'factory'  => new TokenFactory(ContextualToken::class),
                        'context'  => $standard,
                        'priority' => 2,
                    ]),
                ]
            ],

            'symbol.function' => new Rule(new RegexMatcher('/def\s+([a-z_]\w+)\s*\(/i')),
            'symbol.class'    => new Rule(new RegexMatcher('/class\s+([a-z_]\w+)/i')),
        ]);
    }
578798
public function setupRules()
    {
        parent::setupRules();

        $css = new Css();
        $js  = new JavaScript();
        $this->rules->addMany([
            'language.'.$js->getIdentifier() => [
                new OpenRule(new RegexMatcher('/<script.*?>()/'), [
                    'factory'     => new TokenFactory(LanguageToken::class),
                    'inject'      => $js,
                    'language'    => $this,
                    'postProcess' => true
                ]),
                new CloseRule(new RegexMatcher('/()<\/script>/'), [
                    'factory'  => new TokenFactory(LanguageToken::class),
                    'language' => $js
                ])
            ],
            'language.'.$css->getIdentifier() => [
                new OpenRule(new RegexMatcher('/<style.*?>()/'), [
                    'factory'     => new TokenFactory(LanguageToken::class),
                    'inject'      => $css,
                    'language'    => $this,
                    'postProcess' => true
                ]),
                new CloseRule(new RegexMatcher('/()<\/style>/'), [
                    'factory'  => new TokenFactory(LanguageToken::class),
                    'language' => $css
                ])
            ]
        ]);
    }
319342
static void compileRules(String rules, OutputStream os) throws IOException
    {
        //
        // Read the input rules, generate a parse tree, symbol table,
        // and list of all Unicode Sets referenced by the rules.
        //
        RBBIRuleBuilder builder = new RBBIRuleBuilder(rules);
        builder.fScanner.parse();

        //
        // UnicodeSet processing.
        //    Munge the Unicode Sets to create a set of character categories.
        //    Generate the mapping tables (TRIE) from input 32-bit characters to
        //    the character categories.
        //
        builder.fSetBuilder.build();

        //
        //   Generate the DFA state transition table.
        //
        builder.fForwardTables = new RBBITableBuilder(builder, fForwardTree);
        builder.fReverseTables = new RBBITableBuilder(builder, fReverseTree);
        builder.fSafeFwdTables = new RBBITableBuilder(builder, fSafeFwdTree);
        builder.fSafeRevTables = new RBBITableBuilder(builder, fSafeRevTree);
        builder.fForwardTables.build();
        builder.fReverseTables.build();
        builder.fSafeFwdTables.build();
        builder.fSafeRevTables.build();
        if (builder.fDebugEnv != null
                && builder.fDebugEnv.indexOf("states") >= 0) {
            builder.fForwardTables.printRuleStatusTable();
        }

        //
        //   Package up the compiled data, writing it to an output stream
        //      in the serialization format.  This is the same as the ICU4C runtime format.
        //
        builder.flattenData(os);
    }
748960
def process_ruleset( ruleset, dirname = nil )
      all_file_names = []
      all_parts = []
      all_parts_name = nil
      current_part = nil
      current_part_name = nil
      ruleset.lines do |line|
        if !all_parts_name && ( all_parts_name = get_all( line ) )
          all_parts_name = File.join( dirname, all_parts_name ) if dirname
          all_file_names << all_parts_name
        elsif ( current_part_name = get_start( line ) )
          current_part_name = File.join( dirname, current_part_name ) if dirname
          if current_part
            current_part.close
          end
          current_part = File.open( current_part_name, "w" )
          all_file_names << current_part_name
        elsif get_end( line ) && current_part
          current_part.close
          current_part = nil
        elsif current_part
          current_part.puts line
          all_parts << line
        else
          all_parts << line
        end
      end
      if current_part
        current_part.close
      end
      if all_parts_name
        f = File.open( all_parts_name, "w" )
        all_parts.each do |line|
          f.puts( line )
        end
        f.close
      end
      if all_file_names.length
        xml_fn = File.basename( all_file_names[0],".*" ) + "_xml_entity_refs"
        xml_fn = File.join( File.dirname( all_file_names[0] ), xml_fn )
        xml = File.open( xml_fn, "w" )
        all_file_names.each do |fn|
          bn = File.basename( fn, ".*" )
          xml.puts( "<!ENTITY #{bn} PUBLIC '' '#{fn}'>")
        end
        xml.close
      end
    end
383849
public function setupRules()
    {
        $this->rules->addMany([
            'number.status'   => new Rule(new RegexMatcher('/^HTTP\/.+\s(\d+)/')),
            'constant.status' => new Rule(new RegexMatcher('/^HTTP\/.+\s+\d+\s+(.+?)\R/')),

            'call.method' => new Rule(new RegexMatcher('/^(\w+).*HTTP\//')),
            'string.path' => new Rule(new RegexMatcher('/^\w+\s(.*?)\sHTTP\//')),

            'symbol.version' => new Rule(new RegexMatcher('/^.*(HTTP\/\S+)/')),

            'symbol.header' => new Rule(new RegexMatcher('/^([\w-]+:)/m')),
        ]);
    }
1072899
def load_rule_definitions(self, ruleset_generator = False, rule_dirs = []):
        

        # Load rules from JSON files
        self.rule_definitions = {}
        for rule_filename in self.rules:
            for rule in self.rules[rule_filename]:
                if not rule.enabled and not ruleset_generator:
                    continue
            self.rule_definitions[os.path.basename(rule_filename)] = RuleDefinition(rule_filename, rule_dirs = rule_dirs)
        # In case of the ruleset generator, list all available built-in rules
        if ruleset_generator:
            rule_dirs.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/findings'))
            rule_filenames = []
            for rule_dir in rule_dirs:
                rule_filenames += [f for f in os.listdir(rule_dir) if os.path.isfile(os.path.join(rule_dir, f))]
            for rule_filename in rule_filenames:
                if rule_filename not in self.rule_definitions:
                    self.rule_definitions[os.path.basename(rule_filename)] = RuleDefinition(rule_filename)
1028795
private function generateAllRules(): void
    {
        for ($k = 2; isset($this->large[$k]); ++$k) {
            foreach ($this->large[$k] as $frequent) {
                $this->generateRules($frequent);
            }
        }
    }
549439
def _clean_css(self):
        
        # Init the cleaned CSS rules and contents string
        css_rules = []

        # For every rule in the CSS
        for rule in self.stylesheet.rules:

            try:
                # Clean the CSS rule
                cleaned_rule = self._clean_rule(rule)

                # Append the rule to matched CSS rules
                if cleaned_rule is not None:
                    css_rules.append(cleaned_rule)

            except:
                # On error, assume the rule matched the tree
                css_rules.append(rule)

        return self._build_css(css_rules)
142905
def _rules_from_env(self, val):
        """"""
        val = val.split(':')
        if 'DEFAULT_RULES' in val:
            val = const.DEFAULT_RULES + [rule for rule in val if rule != 'DEFAULT_RULES']
        return val
684696
def _build_rules(specs):
    
    for spec in specs:
        if "context" in spec:
            yield Context(spec["context"], list(_build_rules(spec.get("routes", []))))
        else:
            rulespec = spec.copy()
            match = rulespec.pop("match")
            name = rulespec.pop("name")
            yield Rule(match, endpoint=name, **rulespec)