/** * HTMLize JavaScript source for colourizing * @example JavaScript/j_token_html.php * @category PLUG * @param string * @param bool whether to keep whitespace and comments, default is true * @param bool whether to fully support Unicode * @param string optionally specify HTML tag instead of "OL", e.g. "DIV" * @return string */ function j_token_html($src, $ws = true, $unicode = true, $ol = 'ol', $LexClass = 'JLex') { // instantiate Lex instance of appropriate type $Lex = Lex::get($LexClass); // convert non-unix line breaks // @todo replace Unicode breaks too? $src = str_replace(array("\r\n", "\r"), array("\n", "\n"), $src); $tokens = j_token_get_all($src, $ws, $unicode); switch (strtolower($ol)) { case 'ol': case 'ul': $li = 'li'; break; default: $li = 'div'; } $lines = array(''); $line =& $lines[0]; while (list(, $token) = each($tokens)) { list($t, $s, $l, $c) = $token; if ($s === 'true' || $s === 'false' || $s === 'null') { $class = 'J_LITERAL'; } else { if ($Lex->is_word($s)) { $class = 'J_KEYWORD'; } else { if (!is_int($t) && $s === $t) { $class = 'J_PUNCTUATOR'; } else { $class = $Lex->name($t); } } } while (isset($s[0])) { if (!preg_match('/^(.*)\\n/', $s, $r)) { $lines[0] .= '<span class="' . $class . '">' . _j_htmlentities($s) . '</span>'; break; } $lines[0] .= '<span class="' . $class . '">' . _j_htmlentities($r[1]) . '</span>'; array_unshift($lines, ''); $s = substr($s, strlen($r[0])); } } $src = "</{$ol}>"; foreach ($lines as $i => $s) { $class = $i & 1 ? 'odd' : 'even'; $src = "<{$li} class=\"{$class}\">{$s}</{$li}>\n{$src}"; } return "<{$ol} class=\"javascript\">\n{$src}"; }
/** * Debugging function * @param Lex * @param Grammar * @return void */ function dump(Lex $Lex, Grammar $Grammar) { list($lhs, $rhs) = $this->rule; echo $Lex->name($lhs), "->"; foreach ($rhs as $i => $s) { if ($i === $this->i) { echo '•'; } else { if ($i !== 0) { echo ','; } } $t = $Lex->name($s); echo $t === $s ? var_export($s, 1) : $t; } if (is_null($this->current_symbol())) { // accepting state echo '•'; } if (isset($this->la)) { echo ' [', $Lex->name($this->la), ']'; } }
/** * Debugging function to print javascript token stream * @param array * @return void */ function j_token_dump(array $tokens, $LexClass = 'JLex') { // instantiate Lex instance of appropriate type $Lex = Lex::get($LexClass); $line = 0; foreach ($tokens as $token) { list($key, $value, $l) = $token; if ($key === J_WHITESPACE || $key === J_LINE_TERMINATOR) { continue; } // print line number if ($l !== $line) { $line = $l; echo "#{$line}.\n"; } // show token if ($key === $value || is_null($key)) { echo " \"", $value, "\" \n"; } else { echo " ", $Lex->name($key), " : \"", $value, "\" \n"; } } }
function __construct($whitespace, $unicode) { parent::__construct($whitespace, $unicode); $this->Lex = Lex::get('JLex'); }
/** * @ignore */ function dump(Lex $Lex) { $t = max(2, strlen((string) $this->i)); foreach ($this->rules as $i => $rule) { $lhs = $Lex->name($rule[0]); $rhs = $Lex->implode(' ', $rule[1]); $i = str_pad($i, $t, ' ', STR_PAD_LEFT); echo "{$i}: {$lhs} -> {$rhs} \n"; } if ($this->excludela) { echo "Special rules:\n"; foreach ($this->excludela as $nt => $a) { echo ' ', $Lex->name($nt), ' ~{ ', $Lex->implode(' ', $a), " }\n"; } } }
/** * Debugging function, makes table legible * @param Lex * @param Grammar * @param int optionally dump specific row (state) * @return void */ function dump(Lex $Lex, Grammar $Grammar, $state = null) { $table = array(); $heads = array('' => 0); // translate cell data and get other meta data foreach ($this->table as $i => $row) { if (!is_null($state) && $i !== $state) { continue; } $table[$i] = array(); // create row header $table[$i][''] = "#{$i}"; $heads[''] = max($heads[''], strlen($table[$i][''])); // iterate over cols in this row foreach ($row as $sym => $entry) { if (is_null($sym)) { $sym = 'null'; } else { $sym = $Lex->name($sym); } // rules are event, states are odd if ($entry & 1) { $str = " #{$entry} "; } else { list($nt, $rhs) = $Grammar->get_rule($entry); $str = ' ' . $Lex->name($nt) . ' -> '; foreach ($rhs as $t) { $str .= $Lex->name($t) . ' '; } } // insert cell $table[$i][$sym] = $str; // collect known column header with max cell width in column if (!isset($heads[$sym])) { $heads[$sym] = strlen($sym); } $heads[$sym] = max($heads[$sym], strlen($str)); } } // print all headers $a = array(); $b = array(); foreach ($heads as $sym => $len) { $b[] = str_repeat('-', $len); $a[] = str_pad($sym, $len, ' ', STR_PAD_BOTH); } echo '+', implode('+', $b), "+\n"; echo '|', implode('|', $a), "|\n"; foreach ($table as $i => $row) { $c = array(); foreach ($heads as $sym => $len) { if (isset($table[$i][$sym])) { $c[] = str_pad($row[$sym], $len, ' ', STR_PAD_BOTH); } else { $c[] = str_repeat(' ', $len); } } echo '+', implode('+', $b), "+\n"; echo '|', implode('|', $c), "|\n"; } echo '+', implode('+', $b), "+\n"; }
/** * Get name of Javascript terminal symbol * @param int scalar symbol * @return string */ function j_token_name($t) { $Lex = Lex::get('JLex'); return $Lex->name($t); }
/** * Short cut to Lex::get('JLex') useful for code-hinting * @return JLex */ static function singleton() { return Lex::get(__CLASS__); }
/** * @internal * @return void */ function dump(Lex $Lex, Grammar $Grammar, $tab = '') { echo "{$tab}LRStation #{$this->id}\n"; echo "{$tab}[", $Lex->name($this->nt); if (isset($this->la)) { echo ' ' . $Lex->name($this->la); } echo "]\n"; if (isset($this->__dumped)) { echo "{$tab}--recursion--\n"; return; } $this->__dumped = true; foreach ($this->etransitions as $State) { echo "{$tab}--->\n"; $State->dump($Lex, $Grammar, "{$tab} . "); } }
/** * Debugging function to inspect the node tree * @return void */ function dump(Lex $Lex, $tab = '') { $tag = $Lex->name($this->t); if ($this->is_terminal()) { if ($this->value && $this->value !== $this->t) { echo $tab, '<', $tag, ">\n ", $tab, htmlspecialchars($this->value), "\n", $tab, '</', $tag, ">\n"; } else { echo $tab, htmlspecialchars($this->value), "\n"; } } else { if (!$this->length) { echo $tab, '<', $tag, " />\n"; } else { echo $tab, '<', $tag, ">\n"; foreach ($this->children as $Child) { $Child->dump($Lex, " " . $tab); } echo $tab, '</', $tag, ">\n"; } } }