Last active
February 10, 2022 11:58
-
-
Save sergioatanacio/5e64144c24fe3343c09e67c1236fedf1 to your computer and use it in GitHub Desktop.
Creando un lexer en php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $tokens = [ | |
| '(' => 'open_parentesis', | |
| ')' => 'close_parentesis', | |
| '=' => 'igual', | |
| '[' => 'open_corchete_cuadrado', | |
| ']' => 'close_corchete_cuadrado', | |
| ]; | |
| $string = 'let hola = (uno) => [ uno ** 2, 5];'; | |
| $recurtion = function($stringFn, $iterator = 0, $previous_character = null, array $resulting_array = []) | |
| use (&$recurtion, $tokens) | |
| { | |
| $stringFn_iterator = $stringFn[$iterator] ?? null; | |
| $temporary_previous_character = $previous_character ?? null; | |
| $tokens_string_iterator = $tokens[$stringFn_iterator] ?? null; | |
| $temporary_array = (isset($tokens_string_iterator)) | |
| ? array_merge($resulting_array, [$tokens_string_iterator => $stringFn_iterator]) | |
| : $resulting_array; | |
| return (isset($stringFn_iterator) && !isset($temporary_previous_character)) | |
| ? $recurtion($stringFn, $iterator + 1, null, $temporary_array) | |
| : ((isset($previous_character)) | |
| ? $recurtion($stringFn, $iterator + 1, $temporary_previous_character, $temporary_array) | |
| :$resulting_array); | |
| }; | |
| var_dump($recurtion($string)); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $fullString = "ignore everything except this (text hola mundo (esto es otro dentro de archivo) siguiente)"; | |
| $function = function($string) | |
| { | |
| $beginning = strpos($string, '('); #30 | |
| $final = strpos($string, ')'); # 35 | |
| $string_length = $final - $beginning; | |
| return substr($string, $beginning + 1, $string_length - 1); | |
| }; | |
| echo($function($fullString)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $uno = '(uno dos tres (cuatro cinco seis))(siete (catorce quince 16) ocho nueve diez)(once doce trece)'; | |
| $dos = function($tres, $cuatro = 0, $cinco = []) | |
| { | |
| $seis = ($tres[$cuatro] == '(') ? $cinco + [$cuatro] : ; | |
| }; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $str = 'let hola = (uno) => [ uno ** 2, 5];'; | |
| $array = [ | |
| 'palabra_reservada' => 'let', | |
| 'variable' => 'hola', | |
| 'igual' => '=', | |
| 'parentesis_open' => '(', | |
| 'variable' => 'uno', | |
| 'parentesis_close' => ')', | |
| 'flecha' => '=>', | |
| 'cuadrado_open' => '[', | |
| 'variable' => 'uno', | |
| 'simbolo' => '**', | |
| 'numero' => '2', | |
| 'coma' => ',', | |
| 'numero' => '5', | |
| 'cuadrado_close' => ']', | |
| 'punto_y_coma' => ';', | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $fullString = "ignore everything except this (text)"; | |
| $start = strpos($fullString, '('); #30 | |
| $spos = strpos($fullString, ')'); # 35 | |
| #$end = strlen($fullString) - strpos(')', $fullString); | |
| $len = strlen($fullString); # 36 | |
| #$end = $spos - $start; | |
| $end = $spos - $start; | |
| #$shortString = substr($fullString, $start, $end); | |
| $shortString = substr($fullString, $start + 1, $end - 1); | |
| echo($shortString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $string_primero = '(uno dos tres (cuatro cinco seis))(siete (catorce quince 16) ocho nueve diez)(once doce trece)'; | |
| $funcion_recusiva = function($string, $indice = 0, $colector = []) use (&$funcion_recusiva) | |
| { | |
| $seis = (isset($string[$indice]) && $string[$indice] == '(') ? array_merge($colector, [$indice]) : $colector ; | |
| return (isset($string[$indice + 1])) ? $funcion_recusiva($string, $indice + 1, $seis) : $seis; | |
| }; | |
| var_dump($funcion_recusiva($string_primero)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $tokens = [ | |
| '(' => 'open_parentesis', | |
| ')' => 'close_parentesis', | |
| '=' => 'igual', | |
| '[' => 'open_corchete_cuadrado', | |
| ']' => 'close_corchete_cuadrado', | |
| ]; | |
| $string = 'let hola = (uno) => [ uno ** 2, 5];'; | |
| $recurtion = function($stringFn, $iterator = 0, $previous_character = null, array $resulting_array = []) | |
| use (&$recurtion, $tokens) | |
| { | |
| $stringFn_iterator = $stringFn[$iterator] ?? null; | |
| $tokens_string_iterator = $tokens[$stringFn_iterator] ?? null; | |
| $temporary_array = (isset($tokens_string_iterator)) | |
| ? array_merge($resulting_array, [$tokens_string_iterator => $stringFn_iterator]) | |
| : $resulting_array; | |
| return (!isset($stringFn_iterator)) | |
| ? $resulting_array | |
| : $recurtion($stringFn, $iterator + 1, null, $temporary_array); | |
| }; | |
| var_dump($recurtion($string)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment