<commit_before>#!./parrot-nqp
# Copyright (C) 2010, Parrot Foundation.
# $Id$

INIT {
    pir::load_bytecode('Tree/Transformer.pbc');
}

class Tree::Pattern::Transformer is Tree::Transformer {
    has $pattern;
    has $transform;

    has $depth;

    method new ($pattern, $transform, *%adverbs) {
        my $class := pir::getattribute__PPS(self.HOW(),
                                           'parrotclass');
        my $self := pir::new__PP($class);
        $self.depth(0);
        $self.pattern($pattern);
        $self.transform($transform);
        $self;
    }

    method depth ($depth?) {
        my $result;
        if pir::defined__IP($depth) {
            pir::setattribute(self, '$depth', $depth);
        }
        else {
            $result := pir::getattribute__PPS(self, '$depth');
        }
        $result;
    }

    method pattern ($pattern?) {
        my $result;
        if pir::defined__iP($pattern) {
            pir::setattribute(self, '$pattern', $pattern);
        }
        else {
            $result := pir::getattribute__PPs(self, '$pattern');
        }
        $result;
    }

    method transform ($transform?) {
        my $result;
        if pir::defined__iP($transform) {
            pir::setattribute(self, '$transform', $transform);
        }
        else {
            $result := pir::getattribute__PPs(self, '$transform');
        }
        $result;
    }

}

module Tree::Walker {
    our multi sub walk (Tree::Pattern::Transformer $walker,
                        Capture $node) {
        my $pattern := $walker.pattern();
        my $shouldTransform;
        if ($pattern ~~ Tree::Pattern) {
            $shouldTransform := $pattern.ACCEPTS($node, :pos($node));
        }
        else {
            $shouldTransform := $pattern.ACCEPTS($node);
        }
        my $result;
        if ($shouldTransform) {
            $result := $walker.transform()($shouldTransform);
        }
        else {
            $result := $node;
        }
        $walker.depth($walker.depth + 1);
        my $newChildren := walkChildren($walker, $result);
        replaceChildren($result, $newChildren);
        $walker.depth($walker.depth - 1);
        $result;
    }
<commit_msg>Add Tree::Pattern::Transformer.min_depth and thus Tree::Pattern.transform(..., min_depth(...)).<commit_after>#!./parrot-nqp
# Copyright (C) 2010, Parrot Foundation.
# $Id$

INIT {
    pir::load_bytecode('Tree/Transformer.pbc');
}

class Tree::Pattern::Transformer is Tree::Transformer {
    has $min_depth;
    has $pattern;
    has $transform;

    has $depth;

    method new ($pattern, $transform, *%adverbs) {
        my $class := pir::getattribute__PPS(self.HOW(),
                                           'parrotclass');
        my $self := pir::new__PP($class);
        $self.depth(0);
        $self.min_depth(%adverbs<min_depth> || 0);
        $self.pattern($pattern);
        $self.transform($transform);
        $self;
    }

    method depth ($depth?) {
        my $result;
        if pir::defined__IP($depth) {
            pir::setattribute(self, '$depth', $depth);
        }
        else {
            $result := pir::getattribute__PPS(self, '$depth');
        }
        $result;
    }

    method min_depth ($min_depth?) {
        my $result;
        if pir::defined__IP($min_depth) {
            pir::setattribute(self, '$min_depth', $min_depth);
        }
        else {
            $result := pir::getattribute__PPS(self, '$min_depth');
        }
        $result;
    }

    method pattern ($pattern?) {
        my $result;
        if pir::defined__iP($pattern) {
            pir::setattribute(self, '$pattern', $pattern);
        }
        else {
            $result := pir::getattribute__PPs(self, '$pattern');
        }
        $result;
    }

    method transform ($transform?) {
        my $result;
        if pir::defined__iP($transform) {
            pir::setattribute(self, '$transform', $transform);
        }
        else {
            $result := pir::getattribute__PPs(self, '$transform');
        }
        $result;
    }

}

module Tree::Walker {
    our multi sub walk (Tree::Pattern::Transformer $walker,
                        Capture $node) {
        my $pattern := $walker.pattern();
        my $shouldTransform;
        if ($walker.depth < $walker.min_depth) {
            $shouldTransform := 0;
        }
        elsif ($pattern ~~ Tree::Pattern) {
            $shouldTransform := $pattern.ACCEPTS($node, :pos($node));
        }
        else {
            $shouldTransform := $pattern.ACCEPTS($node);
        }
        my $result;
        if ($shouldTransform) {
            $result := $walker.transform()($shouldTransform);
        }
        else {
            $result := $node;
        }
        $walker.depth($walker.depth + 1);
        my $newChildren := walkChildren($walker, $result);
        replaceChildren($result, $newChildren);
        $walker.depth($walker.depth - 1);
        $result;
    }

{'fraction_non_alphanumeric': 0.15292929292929294, 'fraction_numerical': 0.0032323232323232323, 'mean_word_length': 1.9004100761570006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5006661', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1424, 'n_words': 277}