Example #1
1
function yay_parse(string $source, Directives $directives = null, BlueContext $blueContext = null) : string
{
    if ($gc = gc_enabled()) {
        gc_disable();
    }
    // important optimization!
    static $globalDirectives = null;
    if (null === $globalDirectives) {
        $globalDirectives = new ArrayObject();
    }
    $directives = $directives ?: new Directives();
    $blueContext = $blueContext ?: new BlueContext();
    $cg = (object) ['ts' => TokenStream::fromSource($source), 'directives' => $directives, 'cycle' => new Cycle($source), 'globalDirectives' => $globalDirectives, 'blueContext' => $blueContext];
    foreach ($cg->globalDirectives as $d) {
        $cg->directives->add($d);
    }
    traverse(midrule(function (TokenStream $ts) use($directives, $blueContext) {
        $token = $ts->current();
        tail_call:
        if (null === $token) {
            return;
        }
        // skip when something looks like a new macro to be parsed
        if ('macro' === (string) $token) {
            return;
        }
        // here we do the 'magic' to match and expand userland macros
        $directives->apply($ts, $token, $blueContext);
        $token = $ts->next();
        goto tail_call;
    }), consume(chain(token(T_STRING, 'macro')->as('declaration'), optional(repeat(rtoken('/^·\\w+$/')))->as('tags'), lookahead(token('{')), commit(chain(braces()->as('pattern'), operator('>>'), braces()->as('expansion')))->as('body'), optional(token(';'))), CONSUME_DO_TRIM)->onCommit(function (Ast $macroAst) use($cg) {
        $scope = Map::fromEmpty();
        $tags = Map::fromValues(array_map('strval', $macroAst->{'tags'}));
        $pattern = new Pattern($macroAst->{'declaration'}->line(), $macroAst->{'body pattern'}, $tags, $scope);
        $expansion = new Expansion($macroAst->{'body expansion'}, $tags, $scope);
        $macro = new Macro($tags, $pattern, $expansion, $cg->cycle);
        $cg->directives->add($macro);
        // allocate the userland macro
        // allocate the userland macro globally if it's declared as global
        if ($macro->tags()->contains('·global')) {
            $cg->globalDirectives[] = $macro;
        }
    }))->parse($cg->ts);
    $expansion = (string) $cg->ts;
    if ($gc) {
        gc_enable();
    }
    return $expansion;
}
Example #2
0
/**
 * 字符串补位.
 * @param val 字符串 .
 * @param type r右补位l左补位 .
 * @param size 补足长度 .
 * @param delim 补位用字符串 .
 * @return String .
 */
function pad($val, $type, $size, $delim)
{
    if (empty($val)) {
        return val;
    }
    if ("r" == $type) {
        if (strlen($val) >= $size) {
            return substr($val, 0, $size);
        } else {
            return $val . repeat('r', $delim, $size - strlen($val));
        }
    } else {
        if (strlen($val) >= $size) {
            return substr($val, strlen($val) - $size);
        } else {
            return repeat('l', $delim, $size - strlen($val)) . $val;
        }
    }
}
Example #3
0
function xset_with_review()
{
    return exercise_set(name('exercise set 1'), number(2), exercise(name('exercise 1'), filename('bolt_sample_exercise.php?n=1')), exercise(name('exercise 2'), filename('bolt_sample_exercise.php?n=2')), exercise(name('exercise 3'), filename('bolt_sample_exercise.php?n=3')), repeat(0.3, basic_review(), REVIEW), repeat(0.7, int_review(), REVIEW | REPEAT), repeat(1, null, REPEAT | NEXT), refresh(array(7, 14, 28)));
}
Example #4
0
/**
 * Repeat the string n times
 *
 * @param string $input
 * @param int    $n
 *
 * @return string
 *
 * @author Lucantis Swann <*****@*****.**>
 */
function times($input, $n)
{
    return repeat($input, $n);
}
Example #5
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Number of repetitions must be non-negative
  */
 public function testRepeatNegativeNumError()
 {
     toArray(repeat(1, -1));
 }
Example #6
0
<pre><?php 
require_once 'MongoPlus.php';
require_once 'optional_helpers.php';
$_start = microtime(1);
$mongo = new MongoPlus();
#var_dump($mongo);
$db = $mongo->testdb;
#var_dump($db);
$collection = $db->testtable;
#var_dump($collection);
// start over
// drop table
$collection->drop();
// insert 5 random records
repeat(5, function () use($collection) {
    $collection->insert(array('name' => array('first' => 'Voor' . rand(0, 999) . 'naamz', 'last' => 'Lastest ' . rand(0, 999)), 'email' => 'user-' . rand(0, 999) . '@onderstebuiten.nl', 'some_number' => rand(0, 999)));
});
// query, alter & query new data
// show pre
$all = $collection->find()->limit(2);
#var_dump($all->count());
var_dump($all);
print_r($pre = iterator_to_array($all));
// do manual 'correct' update
if (0) {
    $id = key($pre);
    var_dump($id);
    var_dump($collection->update(array('_id' => new MongoId($id)), array('a' => 'b'), array('multiple' => false)));
}
// do updates
if (0) {
Example #7
0
 public function testRepeat()
 {
     $this->assertSame([1, 1, 1, 1, 1], toArray(repeat(1, 5)));
     $this->assertSame([], toArray(repeat(1, 0)));
 }
Example #8
0
 function testastNestedAst()
 {
     $ts = TokenStream::fromSource('<?php
         interface Foo
         {
             public abstract function foo();
             public abstract static function bar();
             function baz();
         }
     ');
     $modifier = either(token(T_PUBLIC), token(T_STATIC), token(T_PRIVATE));
     $modifiers = optional(either(chain($modifier, $modifier), $modifier));
     $ast = chain(token(T_OPEN_TAG), chain(token(T_INTERFACE), token(T_STRING)->as('name'), token('{'), optional(repeat(chain(optional(either(repeat(either(token(T_PUBLIC)->as('public'), token(T_STATIC)->as('static'), token(T_ABSTRACT)->as('abstract'))), always(new Token(T_PUBLIC, 'public'))->as('public')))->as('is'), token(T_FUNCTION), token(T_STRING)->as('name'), token('('), token(')'), token(';')), token('}')))->as('methods'), token('}'))->as('interface'))->parse($ts);
     $this->assertEquals('Foo', (string) $ast->{'interface name'});
     $this->assertEquals('foo', (string) $ast->{'interface methods 0 name'});
     $this->assertEquals('public', (string) $ast->{'interface methods 0 is public'});
     $this->assertEquals('abstract', (string) $ast->{'interface methods 0 is abstract'});
     $this->assertEmpty($ast->{'interface methods 0 is static'});
     $this->assertEquals('bar', (string) $ast->{'interface methods 1 name'});
     $this->assertEquals('public', (string) $ast->{'interface methods 1 is public'});
     $this->assertEquals('abstract', (string) $ast->{'interface methods 1 is abstract'});
     $this->assertEquals('static', (string) $ast->{'interface methods 1 is static'});
     $this->assertEquals('baz', (string) $ast->{'interface methods 2 name'});
     $this->assertEquals('public', (string) $ast->{'interface methods 2 is public'});
     $this->assertNull($ast->{'interface methods 2 is abstract'});
     $this->assertNull($ast->{'interface methods 2 is static'});
 }
Example #9
0
function findall($password, $rx)
{
    $matches = array();
    while (TRUE) {
        $count = preg_match_all($rx, $password, $captures);
        if (!$count) {
            break;
        }
        for ($i = 0; $i < $count; $i++) {
            $match = array();
            foreach ($captures as $m) {
                $match[] = $m[$i];
            }
            $match['index'] = strpos($password, $match[0]);
            $match['input'] = $password;
            $match['length'] = strlen($match[0]);
            $match['i'] = $match['index'];
            #$match['j'] = $match['length'];
            $match['j'] = $match['index'] + ($match['length'] - 1);
            $matches[] = $match;
            $password = str_replace($match[0], repeat(' ', $match['length']), $password);
        }
    }
    return $matches;
}
Example #10
0
$cities = City::all();
start("Select each by ID");
foreach ($cities as $city) {
    City::get($city->id);
}
finish();
// ----------------------------------------------
start("Select each by name");
foreach ($cities as $city) {
    City::get($city->id);
}
finish();
// ----------------------------------------------z
start("Update all");
repeat(20, function () {
    City::objects()->update(['population' => 1]);
});
finish();
// ----------------------------------------------z
$cities = City::all();
start("Update each (update)");
foreach ($cities as $city) {
    $city->population += 1;
    $city->update();
}
finish();
// ----------------------------------------------z
$cities = City::all();
start("Update each (save)");
foreach ($cities as $city) {
    $city->population += 1;
Example #11
0
$href = isset($data) ? path($this->application . "/cpanel/{$action}/{$ID}/") : path($this->application . "/cpanel/add/");
print div("add-form", "class");
print formOpen($href, "form-add", "form-add");
print p(__(_(ucfirst(whichApplication()))), "resalt");
print isset($alert) ? $alert : NULL;
print formInput(array("name" => "title", "class" => "input required", "field" => __(_("Question")), "p" => TRUE, "value" => $title));
print div("answers");
print formField(NULL, __(_("Answers")) . " (" . __(_("Empty answers not be added")) . "))");
if (is_array($answers)) {
    foreach ($answers as $key => $answer) {
        print p(TRUE, "field panswer");
        print span("count", $key + 1 . ".-");
        print formInput(array("name" => "answers[]", "class" => "input required", "value" => $answer));
        print p(FALSE);
    }
} else {
    print p(TRUE, "field panswer");
    print span("count", "1.-");
    print formInput(array("name" => "answers[]", "class" => "input required", "value" => $answers));
    print p(FALSE);
}
print div(FALSE);
print span(NULL, repeat("&nbsp;", 4), "add-img");
$options = array(0 => array("value" => "Simple", "option" => __(_("Simple")), "selected" => $type === "Simple" ? TRUE : FALSE), 1 => array("value" => "Multiple", "option" => __(_("Multiple")), "selected" => $type === "Inactive" ? TRUE : FALSE));
print formSelect(array("name" => "type", "class" => "select", "p" => TRUE, "field" => __(_("Type"))), $options);
$options = array(0 => array("value" => "Active", "option" => __(_("Active")), "selected" => $situation === "Active" ? TRUE : FALSE), 1 => array("value" => "Inactive", "option" => __(_("Inactive")), "selected" => $situation === "Inactive" ? TRUE : FALSE));
print formSelect(array("name" => "situation", "class" => "select", "p" => TRUE, "field" => __(_("Situation"))), $options);
print formSave($action);
print formInput(array("name" => "ID", "type" => "hidden", "value" => $ID));
print formClose();
print div(FALSE);