Beispiel #1
0
 public function build(OutputInterface &$output)
 {
     $style = new OutputFormatterStyle('red', null, array('bold', 'blink'));
     $output->getFormatter()->setStyle('fire', $style);
     // Disable output.
     ini_set('implicit_flush', false);
     // Lemon takes arguments on the command line.
     $_SERVER['argv'] = $argv = array('-s', ROOT . $this->source);
     $output->writeln("<fg=green>Attempting to build \"{$this->target}\" from \"{$this->source}\"</fg=green>");
     $output->writeln("This could take a few minutes...");
     // The -q flag doesn't seem to work but we can catch the output in a
     // buffer (only want to display the errors).
     ob_start();
     $lemon = new \PHP_ParserGenerator();
     $lemon->main();
     $reply = explode("\n", ob_get_contents());
     ob_end_clean();
     $errors = array();
     $conflicts = 0;
     foreach ($reply as $i => $line) {
         // Errors are prefixed with the grammar file path.
         if (strpos($line, $argv[1]) === 0) {
             $errors[] = str_replace($argv[1], basename($argv[1]), $line);
         }
         if ($i === count($reply) - 2) {
             if (preg_match('/^(\\d+).+/', $line, $m)) {
                 $conflicts = intval($m[1]);
             }
         }
     }
     if ($errors) {
         $output->writeln("<fire>" . implode("\n", $errors) . "</fire>");
     }
     // Build was a success!
     if (!file_exists(ROOT . $this->target) || @unlink(ROOT . $this->target)) {
         $content = file_get_contents(ROOT . 'parser.php');
         // Add namespace declaration.
         $content = strtr($content, array('<?php' => "<?php\n\n" . "namespace Rodchyn\\ElephantLang\\Parser;\n\n" . "use \\ArrayAccess as ArrayAccess;\n\n" . "use Rodchyn\\ElephantLang\\Init as Init;\n\n" . "Init::init();\n\n" . ""));
         // Write.
         file_put_contents(ROOT . $this->target, $content);
         $output->writeln("Parser builded!");
         // Clean up.
         unlink(ROOT . 'parser.php');
         if ($conflicts) {
             $output->writeln("but <fire>{$conflicts} parsing conflicts occurred (see {$this->source}.out).</fire>");
         } else {
             unlink(ROOT . 'parser.out');
         }
         return 0;
     } else {
         // Bad permissions.
         $output->writeln("<fire>Failed!</fire>");
         $output->writeln("<fire>Couldn't remove {$this->target}. Check your permissions.</fire>");
         return 1;
     }
 }
Beispiel #2
0
function make()
{
    require 'vendor/ParserGenerator/ParserGenerator.php';
    $source = 'grammar';
    $target = 'src/CoffeeScript/Parser.php';
    // Lemon takes arguments on the command line.
    $_SERVER['argv'] = $argv = array('-s', ROOT . $source . '.y');
    echo "Attempting to build \"{$target}\" from \"{$source}.y\".\n";
    echo "This could take a few minutes...\n";
    // The -q flag doesn't seem to work but we can catch the output in a
    // buffer (only want to display the errors).
    ob_start();
    $lemon = new PHP_ParserGenerator();
    $lemon->main();
    $reply = explode("\n", ob_get_contents());
    ob_end_clean();
    $errors = array();
    $conflicts = 0;
    foreach ($reply as $i => $line) {
        // Errors are prefixed with the grammar file path.
        if (strpos($line, $argv[1]) === 0) {
            $errors[] = str_replace($argv[1], basename($argv[1]), $line);
        }
        if ($i === count($reply) - 2) {
            if (preg_match('/^(\\d+).+/', $line, $m)) {
                $conflicts = intval($m[1]);
            }
        }
    }
    if ($errors) {
        exit(implode("\n", $errors));
    }
    // Build was a success!
    if (!file_exists(ROOT . $target) || @unlink(ROOT . $target)) {
        $content = file_get_contents(ROOT . $source . '.php');
        // Add namespace declaration.
        $content = strtr($content, array('<?php' => "<?php\n" . "namespace CoffeeScript;\n" . "use \\ArrayAccess as ArrayAccess;\n" . "Init::init();\n"));
        // Write.
        file_put_contents(ROOT . $target, $content);
        echo "Success!\n";
        // Clean up.
        unlink(ROOT . $source . '.php');
        if ($conflicts) {
            echo "{$conflicts} parsing conflicts occurred (see {$source}.out).\n";
        } else {
            unlink(ROOT . $source . '.out');
        }
        exit(0);
    } else {
        // Bad permissions.
        echo "Failed!\n";
        echo "Couldn't remove {$target}. Check your permissions.\n";
        exit(1);
    }
}
Beispiel #3
0
 /**
  * zCode is a string that is the action associated with a rule.  Expand
  * the symbols in this string so that the refer to elements of the parser
  * stack.
  */
 function translate_code(PHP_ParserGenerator_Rule $rp)
 {
     $lhsused = 0;
     /* True if the LHS element has been used */
     $used = array();
     /* True for each RHS element which is used */
     for ($i = 0; $i < $rp->nrhs; $i++) {
         $used[$i] = 0;
     }
     $this->append_str('', 0);
     for ($i = 0; $i < strlen($rp->code); $i++) {
         $cp = $rp->code[$i];
         if (preg_match('/[A-Za-z]/', $cp) && ($i === 0 || !preg_match('/[A-Za-z0-9_]/', $rp->code[$i - 1]))) {
             //*xp = 0;
             // previous line is in essence a temporary substr, so
             // we will simulate it
             $test = substr($rp->code, $i);
             preg_match('/[A-Za-z0-9_]+/', $test, $matches);
             $tempcp = $matches[0];
             $j = strlen($tempcp) + $i;
             if ($rp->lhsalias && $tempcp == $rp->lhsalias) {
                 $this->append_str("\$this->_retvalue", 0);
                 $cp = $rp->code[$j];
                 $i = $j;
                 $lhsused = 1;
             } else {
                 for ($ii = 0; $ii < $rp->nrhs; $ii++) {
                     if ($rp->rhsalias[$ii] && $tempcp == $rp->rhsalias[$ii]) {
                         if ($ii !== 0 && $rp->code[$ii - 1] == '@') {
                             /* If the argument is of the form @X then substitute
                              ** the token number of X, not the value of X */
                             $this->append_str("\$this->yystack[\$this->yyidx + " . ($ii - $rp->nrhs + 1) . "]->major", -1);
                         } else {
                             $sp = $rp->rhs[$ii];
                             if ($sp->type == PHP_ParserGenerator_Symbol::MULTITERMINAL) {
                                 $dtnum = $sp->subsym[0]->dtnum;
                             } else {
                                 $dtnum = $sp->dtnum;
                             }
                             $this->append_str("\$this->yystack[\$this->yyidx + " . ($ii - $rp->nrhs + 1) . "]->minor", 0);
                         }
                         $cp = $rp->code[$j];
                         $i = $j;
                         $used[$ii] = 1;
                         break;
                     }
                 }
             }
         }
         $this->append_str($cp, 1);
     }
     /* End loop */
     /* Check to make sure the LHS has been used */
     if ($rp->lhsalias && !$lhsused) {
         PHP_ParserGenerator::ErrorMsg($this->filename, $rp->ruleline, "Label \"%s\" for \"%s(%s)\" is never used.", $rp->lhsalias, $rp->lhs->name, $rp->lhsalias);
         $this->errorcnt++;
     }
     /* Generate destructor code for RHS symbols which are not used in the
      ** reduce code */
     for ($i = 0; $i < $rp->nrhs; $i++) {
         if ($rp->rhsalias[$i] && !isset($used[$i])) {
             PHP_ParserGenerator::ErrorMsg($this->filename, $rp->ruleline, "Label %s for \"%s(%s)\" is never used.", $rp->rhsalias[$i], $rp->rhs[$i]->name, $rp->rhsalias[$i]);
             $this->errorcnt++;
         } elseif ($rp->rhsalias[$i] == 0) {
             if ($rp->rhs[$i]->type == PHP_ParserGenerator_Symbol::TERMINAL) {
                 $hasdestructor = $this->tokendest != 0;
             } else {
                 $hasdestructor = $this->vardest !== 0 || $rp->rhs[$i]->destructor !== 0;
             }
             if ($hasdestructor) {
                 $this->append_str("  \$this->yy_destructor(" . $rp->rhs[$i]->index . ", \$this->yystack[\$this->yyidx + " . ($i - $rp->nrhs + 1) . "]->minor);\n", 0);
             } else {
                 /* No destructor defined for this term */
             }
         }
     }
     $cp = $this->append_str('', 0);
     $rp->code = $cp;
 }
Beispiel #4
0
<?php

require_once 'PHP/ParserGenerator.php';
$me = new PHP_ParserGenerator();
$me->main();
Beispiel #5
0
 /**
  * Sort the configuration list
  * @uses Configcmp
  */
 public static function Configlist_sortbasis()
 {
     $a = 0;
     self::$basis = PHP_ParserGenerator::msort(self::$current, 'bp', array('PHP_ParserGenerator_Config', 'Configcmp'));
     self::$basisend =& $a;
     self::$basisend = 0;
 }
Beispiel #6
0
 /**
  * Sort parser actions
  * @see PHP_ParserGenerator_Data::FindActions()
  */
 static function Action_sort(PHP_ParserGenerator_Action $ap)
 {
     $ap = PHP_ParserGenerator::msort($ap, 'next', array('PHP_ParserGenerator_Action', 'actioncmp'));
     return $ap;
 }
Beispiel #7
0
 /**
  * Parse a single token
  * @param string token
  */
 function parseonetoken($token)
 {
     $x = $token;
     $this->a = 0;
     // for referencing in WAITING_FOR_DECL_KEYWORD
     if (PHP_ParserGenerator::DEBUG) {
         printf("%s:%d: Token=[%s] state=%d\n", $this->filename, $this->tokenlineno, $token, $this->state);
     }
     switch ($this->state) {
         case self::INITIALIZE:
             $this->prevrule = 0;
             $this->preccounter = 0;
             $this->firstrule = $this->lastrule = 0;
             $this->gp->nrule = 0;
             /* Fall thru to next case */
         /* Fall thru to next case */
         case self::WAITING_FOR_DECL_OR_RULE:
             if ($x[0] == '%') {
                 $this->state = self::WAITING_FOR_DECL_KEYWORD;
             } elseif (preg_match('/[a-z]/', $x[0])) {
                 $this->lhs = PHP_ParserGenerator_Symbol::Symbol_new($x);
                 $this->nrhs = 0;
                 $this->lhsalias = 0;
                 $this->state = self::WAITING_FOR_ARROW;
             } elseif ($x[0] == '{') {
                 if ($this->prevrule === 0) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "There is no prior rule opon which to attach the code\n                             fragment which begins on this line.");
                     $this->errorcnt++;
                 } elseif ($this->prevrule->code != 0) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Code fragment beginning on this line is not the first \\\n                             to follow the previous rule.");
                     $this->errorcnt++;
                 } else {
                     $this->prevrule->line = $this->tokenlineno;
                     $this->prevrule->code = substr($x, 1);
                 }
             } elseif ($x[0] == '[') {
                 $this->state = self::PRECEDENCE_MARK_1;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Token \"%s\" should be either \"%%\" or a nonterminal name.", $x);
                 $this->errorcnt++;
             }
             break;
         case self::PRECEDENCE_MARK_1:
             if (!preg_match('/[A-Z]/', $x[0])) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "The precedence symbol must be a terminal.");
                 $this->errorcnt++;
             } elseif ($this->prevrule === 0) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "There is no prior rule to assign precedence \"[%s]\".", $x);
                 $this->errorcnt++;
             } elseif ($this->prevrule->precsym != 0) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Precedence mark on this line is not the first to follow the previous rule.");
                 $this->errorcnt++;
             } else {
                 $this->prevrule->precsym = PHP_ParserGenerator_Symbol::Symbol_new($x);
             }
             $this->state = self::PRECEDENCE_MARK_2;
             break;
         case self::PRECEDENCE_MARK_2:
             if ($x[0] != ']') {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Missing \"]\" on precedence mark.");
                 $this->errorcnt++;
             }
             $this->state = self::WAITING_FOR_DECL_OR_RULE;
             break;
         case self::WAITING_FOR_ARROW:
             if ($x[0] == ':' && $x[1] == ':' && $x[2] == '=') {
                 $this->state = self::IN_RHS;
             } elseif ($x[0] == '(') {
                 $this->state = self::LHS_ALIAS_1;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Expected to see a \":\" following the LHS symbol \"%s\".", $this->lhs->name);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::LHS_ALIAS_1:
             if (preg_match('/[A-Za-z]/', $x[0])) {
                 $this->lhsalias = $x;
                 $this->state = self::LHS_ALIAS_2;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "\"%s\" is not a valid alias for the LHS \"%s\"\n", $x, $this->lhs->name);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::LHS_ALIAS_2:
             if ($x[0] == ')') {
                 $this->state = self::LHS_ALIAS_3;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Missing \")\" following LHS alias name \"%s\".", $this->lhsalias);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::LHS_ALIAS_3:
             if ($x == '::=') {
                 $this->state = self::IN_RHS;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Missing \"->\" following: \"%s(%s)\".", $this->lhs->name, $this->lhsalias);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::IN_RHS:
             if ($x[0] == '.') {
                 $rp = new PHP_ParserGenerator_Rule();
                 $rp->ruleline = $this->tokenlineno;
                 for ($i = 0; $i < $this->nrhs; $i++) {
                     $rp->rhs[$i] = $this->rhs[$i];
                     $rp->rhsalias[$i] = $this->alias[$i];
                 }
                 $rp->lhs = $this->lhs;
                 $rp->lhsalias = $this->lhsalias;
                 $rp->nrhs = $this->nrhs;
                 $rp->code = 0;
                 $rp->precsym = 0;
                 $rp->index = $this->gp->nrule++;
                 $rp->nextlhs = $rp->lhs->rule;
                 $rp->lhs->rule = $rp;
                 $rp->next = 0;
                 if ($this->firstrule === 0) {
                     $this->firstrule = $this->lastrule = $rp;
                 } else {
                     $this->lastrule->next = $rp;
                     $this->lastrule = $rp;
                 }
                 $this->prevrule = $rp;
                 $this->state = self::WAITING_FOR_DECL_OR_RULE;
             } elseif (preg_match('/[a-zA-Z]/', $x[0])) {
                 if ($this->nrhs >= PHP_ParserGenerator::MAXRHS) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Too many symbols on RHS or rule beginning at \"%s\".", $x);
                     $this->errorcnt++;
                     $this->state = self::RESYNC_AFTER_RULE_ERROR;
                 } else {
                     if (isset($this->rhs[$this->nrhs - 1])) {
                         $msp = $this->rhs[$this->nrhs - 1];
                         if ($msp->type == PHP_ParserGenerator_Symbol::MULTITERMINAL) {
                             $inf = array_reduce($msp->subsym, array($this, '_printmulti'), '');
                             PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, 'WARNING: symbol ' . $x . ' will not' . ' be part of previous multiterminal %s', substr($inf, 0, strlen($inf) - 1));
                         }
                     }
                     $this->rhs[$this->nrhs] = PHP_ParserGenerator_Symbol::Symbol_new($x);
                     $this->alias[$this->nrhs] = 0;
                     $this->nrhs++;
                 }
             } elseif (($x[0] == '|' || $x[0] == '/') && $this->nrhs > 0) {
                 $msp = $this->rhs[$this->nrhs - 1];
                 if ($msp->type != PHP_ParserGenerator_Symbol::MULTITERMINAL) {
                     $origsp = $msp;
                     $msp = new PHP_ParserGenerator_Symbol();
                     $msp->type = PHP_ParserGenerator_Symbol::MULTITERMINAL;
                     $msp->nsubsym = 1;
                     $msp->subsym = array($origsp);
                     $msp->name = $origsp->name;
                     $this->rhs[$this->nrhs - 1] = $msp;
                 }
                 $msp->nsubsym++;
                 $msp->subsym[$msp->nsubsym - 1] = PHP_ParserGenerator_Symbol::Symbol_new(substr($x, 1));
                 if (preg_match('/[a-z]/', $x[1]) || preg_match('/[a-z]/', $msp->subsym[0]->name[0])) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Cannot form a compound containing a non-terminal");
                     $this->errorcnt++;
                 }
             } elseif ($x[0] == '(' && $this->nrhs > 0) {
                 $this->state = self::RHS_ALIAS_1;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Illegal character on RHS of rule: \"%s\".", $x);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::RHS_ALIAS_1:
             if (preg_match('/[A-Za-z]/', $x[0])) {
                 $this->alias[$this->nrhs - 1] = $x;
                 $this->state = self::RHS_ALIAS_2;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n", $x, $this->rhs[$this->nrhs - 1]->name);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::RHS_ALIAS_2:
             if ($x[0] == ')') {
                 $this->state = self::IN_RHS;
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Missing \")\" following LHS alias name \"%s\".", $this->lhsalias);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_RULE_ERROR;
             }
             break;
         case self::WAITING_FOR_DECL_KEYWORD:
             if (preg_match('/[A-Za-z]/', $x[0])) {
                 $this->declkeyword = $x;
                 $this->declargslot =& $this->a;
                 $this->decllnslot =& $this->a;
                 $this->state = self::WAITING_FOR_DECL_ARG;
                 if ('name' == $x) {
                     $this->declargslot =& $this->gp->name;
                 } elseif ('include' == $x) {
                     $this->declargslot =& $this->gp->include_code;
                     $this->decllnslot =& $this->gp->includeln;
                 } elseif ('include_class' == $x) {
                     $this->declargslot =& $this->gp->include_classcode;
                     $this->decllnslot =& $this->gp->include_classln;
                 } elseif ('declare_class' == $x) {
                     $this->declargslot =& $this->gp->declare_classcode;
                     $this->decllnslot =& $this->gp->declare_classln;
                 } elseif ('code' == $x) {
                     $this->declargslot =& $this->gp->extracode;
                     $this->decllnslot =& $this->gp->extracodeln;
                 } elseif ('token_destructor' == $x) {
                     $this->declargslot =& $this->gp->tokendest;
                     $this->decllnslot =& $this->gp->tokendestln;
                 } elseif ('default_destructor' == $x) {
                     $this->declargslot =& $this->gp->vardest;
                     $this->decllnslot =& $this->gp->vardestln;
                 } elseif ('token_prefix' == $x) {
                     $this->declargslot =& $this->gp->tokenprefix;
                 } elseif ('syntax_error' == $x) {
                     $this->declargslot =& $this->gp->error;
                     $this->decllnslot =& $this->gp->errorln;
                 } elseif ('parse_accept' == $x) {
                     $this->declargslot =& $this->gp->accept;
                     $this->decllnslot =& $this->gp->acceptln;
                 } elseif ('parse_failure' == $x) {
                     $this->declargslot =& $this->gp->failure;
                     $this->decllnslot =& $this->gp->failureln;
                 } elseif ('stack_overflow' == $x) {
                     $this->declargslot =& $this->gp->overflow;
                     $this->decllnslot =& $this->gp->overflowln;
                 } elseif ('token_type' == $x) {
                     $this->declargslot =& $this->gp->tokentype;
                 } elseif ('default_type' == $x) {
                     $this->declargslot =& $this->gp->vartype;
                 } elseif ('stack_size' == $x) {
                     $this->declargslot =& $this->gp->stacksize;
                 } elseif ('start_symbol' == $x) {
                     $this->declargslot =& $this->gp->start;
                 } elseif ('left' == $x) {
                     $this->preccounter++;
                     $this->declassoc = PHP_ParserGenerator_Symbol::LEFT;
                     $this->state = self::WAITING_FOR_PRECEDENCE_SYMBOL;
                 } elseif ('right' == $x) {
                     $this->preccounter++;
                     $this->declassoc = PHP_ParserGenerator_Symbol::RIGHT;
                     $this->state = self::WAITING_FOR_PRECEDENCE_SYMBOL;
                 } elseif ('nonassoc' == $x) {
                     $this->preccounter++;
                     $this->declassoc = PHP_ParserGenerator_Symbol::NONE;
                     $this->state = self::WAITING_FOR_PRECEDENCE_SYMBOL;
                 } elseif ('destructor' == $x) {
                     $this->state = self::WAITING_FOR_DESTRUCTOR_SYMBOL;
                 } elseif ('type' == $x) {
                     $this->state = self::WAITING_FOR_DATATYPE_SYMBOL;
                 } elseif ('fallback' == $x) {
                     $this->fallback = 0;
                     $this->state = self::WAITING_FOR_FALLBACK_ID;
                 } else {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Unknown declaration keyword: \"%%%s\".", $x);
                     $this->errorcnt++;
                     $this->state = self::RESYNC_AFTER_DECL_ERROR;
                 }
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Illegal declaration keyword: \"%s\".", $x);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_DECL_ERROR;
             }
             break;
         case self::WAITING_FOR_DESTRUCTOR_SYMBOL:
             if (!preg_match('/[A-Za-z]/', $x[0])) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Symbol name missing after %destructor keyword");
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_DECL_ERROR;
             } else {
                 $sp = PHP_ParserGenerator_Symbol::Symbol_new($x);
                 $this->declargslot =& $sp->destructor;
                 $this->decllnslot =& $sp->destructorln;
                 $this->state = self::WAITING_FOR_DECL_ARG;
             }
             break;
         case self::WAITING_FOR_DATATYPE_SYMBOL:
             if (!preg_match('/[A-Za-z]/', $x[0])) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Symbol name missing after %destructor keyword");
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_DECL_ERROR;
             } else {
                 $sp = PHP_ParserGenerator_Symbol::Symbol_new($x);
                 $this->declargslot =& $sp->datatype;
                 $this->state = self::WAITING_FOR_DECL_ARG;
             }
             break;
         case self::WAITING_FOR_PRECEDENCE_SYMBOL:
             if ($x[0] == '.') {
                 $this->state = self::WAITING_FOR_DECL_OR_RULE;
             } elseif (preg_match('/[A-Z]/', $x[0])) {
                 $sp = PHP_ParserGenerator_Symbol::Symbol_new($x);
                 if ($sp->prec >= 0) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Symbol \"%s\" has already been given a precedence.", $x);
                     $this->errorcnt++;
                 } else {
                     $sp->prec = $this->preccounter;
                     $sp->assoc = $this->declassoc;
                 }
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Can't assign a precedence to \"%s\".", $x);
                 $this->errorcnt++;
             }
             break;
         case self::WAITING_FOR_DECL_ARG:
             if (preg_match('/[A-Za-z0-9{"]/', $x[0])) {
                 if ($this->declargslot != 0) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "The argument \"%s\" to declaration \"%%%s\" is not the first.", $x[0] == '"' ? substr($x, 1) : $x, $this->declkeyword);
                     $this->errorcnt++;
                     $this->state = self::RESYNC_AFTER_DECL_ERROR;
                 } else {
                     $this->declargslot = $x[0] == '"' || $x[0] == '{' ? substr($x, 1) : $x;
                     $this->a = 1;
                     if (!$this->decllnslot) {
                         $this->decllnslot = $this->tokenlineno;
                     }
                     $this->state = self::WAITING_FOR_DECL_OR_RULE;
                 }
             } else {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "Illegal argument to %%%s: %s", $this->declkeyword, $x);
                 $this->errorcnt++;
                 $this->state = self::RESYNC_AFTER_DECL_ERROR;
             }
             break;
         case self::WAITING_FOR_FALLBACK_ID:
             if ($x[0] == '.') {
                 $this->state = self::WAITING_FOR_DECL_OR_RULE;
             } elseif (!preg_match('/[A-Z]/', $x[0])) {
                 PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "%%fallback argument \"%s\" should be a token", $x);
                 $this->errorcnt++;
             } else {
                 $sp = PHP_ParserGenerator_Symbol::Symbol_new($x);
                 if ($this->fallback === 0) {
                     $this->fallback = $sp;
                 } elseif (is_object($sp->fallback)) {
                     PHP_ParserGenerator::ErrorMsg($this->filename, $this->tokenlineno, "More than one fallback assigned to token %s", $x);
                     $this->errorcnt++;
                 } else {
                     $sp->fallback = $this->fallback;
                     $this->gp->has_fallback = 1;
                 }
             }
             break;
         case self::RESYNC_AFTER_RULE_ERROR:
             /*      if ($x[0] == '.') $this->state = self::WAITING_FOR_DECL_OR_RULE;
              **      break; */
         /*      if ($x[0] == '.') $this->state = self::WAITING_FOR_DECL_OR_RULE;
          **      break; */
         case self::RESYNC_AFTER_DECL_ERROR:
             if ($x[0] == '.') {
                 $this->state = self::WAITING_FOR_DECL_OR_RULE;
             }
             if ($x[0] == '%') {
                 $this->state = self::WAITING_FOR_DECL_KEYWORD;
             }
             break;
     }
 }
<?php

ini_set('max_execution_time', 300);
ini_set('xdebug.max_nesting_level', 300);
// Create Lexer
require_once './LexerGenerator.php';
$lex = new PHP_LexerGenerator('Lexer.plex');
$contents = file_get_contents('Lexer.php');
$contents = str_replace(array('SMARTYldel', 'SMARTYrdel'), array('".$this->ldel."', '".$this->rdel."'), $contents);
file_put_contents('Lexer.php', $contents);
// Create Parser
require_once './ParserGenerator.php';
$me = new PHP_ParserGenerator();
$me->main('Parser.y');
$contents = file_get_contents('Parser.php');
$contents = '<?php
/**
 * Brainy Internal Plugin Templateparser
 *
 * This is the template parser.
 * It is generated from the Parser.y file
 * @package Brainy
 * @subpackage Compiler
 * @author Uwe Tews
 * @author Matt Basta
 */

namespace Box\\Brainy\\Compiler;

' . substr($contents, 6);
file_put_contents('Parser.php', $contents);
Beispiel #9
0
<?php

if ($argc != 2) {
    exit("Usage: php " . $argv[0] . " <file>\n");
}
require_once 'PHP/ParserGenerator.php';
$a = new PHP_ParserGenerator();
$_SERVER['argv'] = array('lemon', '-s', $argv[1]);
$a->main();
<?php

ini_set('max_execution_time', 300);
ini_set('xdebug.max_nesting_level', 300);
$smartyPath = '../smarty/libs/sysplugins/';
$lexerPath = '../smarty/lexer/';
if (!is_dir($lexerPath)) {
    echo '<br><br>Fatal error: Missing lexer / parser definition folder \'lexer\' in distribution <br>';
    exit(1);
}
copy("{$smartyPath}smarty_internal_templatelexer.php", "{$lexerPath}smarty_internal_templatelexer.php.bak");
copy("{$smartyPath}smarty_internal_templateparser.php", "{$lexerPath}smarty_internal_templateparser.php.bak");
// Create Lexer
require_once './LexerGenerator.php';
$lex = new PHP_LexerGenerator("{$lexerPath}smarty_internal_templatelexer.plex");
unset($lex);
$content = file_get_contents("{$lexerPath}smarty_internal_templatelexer.php");
$content = str_replace(array('SMARTYldel', 'SMARTYrdel'), array('".$this->ldel."', '".$this->rdel."'), $content);
file_put_contents("{$lexerPath}smarty_internal_templatelexer.php", $content);
// Create Parser
require_once './ParserGenerator.php';
$parser = new PHP_ParserGenerator();
$parser->main("{$lexerPath}smarty_internal_templateparser.y");
unset($parser);
$content = file_get_contents("{$lexerPath}smarty_internal_templateparser.php");
$content = preg_replace(array('#/\\*\\s*\\d+\\s*\\*/#', "#'lhs'#", "#'rhs'#"), array('', 0, 1), $content);
file_put_contents("{$lexerPath}smarty_internal_templateparser.php", $content);
copy("{$lexerPath}smarty_internal_templatelexer.php", "{$smartyPath}smarty_internal_templatelexer.php");
copy("{$lexerPath}smarty_internal_templateparser.php", "{$smartyPath}smarty_internal_templateparser.php");
<?php

$smartyPath = '../smarty/libs/sysplugins/';
$lexerPath = '../smarty/lexer/';
if (!is_dir($lexerPath)) {
    echo '<br><br>Fatal error: Missing lexer / parser definition folder \'lexer\' in distribution <br>';
    exit(1);
}
copy("{$smartyPath}smarty_internal_configfilelexer.php", "{$lexerPath}smarty_internal_configfilelexer.php.bak");
copy("{$smartyPath}smarty_internal_configfileparser.php", "{$lexerPath}smarty_internal_configfileparser.php.bak");
// Create Lexer
require_once './LexerGenerator.php';
$lex = new PHP_LexerGenerator("{$lexerPath}smarty_internal_configfilelexer.plex");
unset($lex);
// Create Parser
require_once './ParserGenerator.php';
$parser = new PHP_ParserGenerator();
$parser->main("{$lexerPath}smarty_internal_configfileparser.y");
unset($parser);
$content = file_get_contents("{$lexerPath}smarty_internal_configfileparser.php");
$content = preg_replace(array('#/\\*\\s*\\d+\\s*\\*/#', "#'lhs'#", "#'rhs'#"), array('', 0, 1), $content);
file_put_contents("{$lexerPath}smarty_internal_configfileparser.php", $content);
copy("{$lexerPath}smarty_internal_configfilelexer.php", "{$smartyPath}smarty_internal_configfilelexer.php");
copy("{$lexerPath}smarty_internal_configfileparser.php", "{$smartyPath}smarty_internal_configfileparser.php");