3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ILexer { /** * return associative array containing tokens for parser * @param string $input code to tokenize * @return array */ public function tokenize($input); } interface INameValidator { /** * Checks if name is valid. In a case of failure trigger syntax error. * @param string $name * @return void */ public function validate($name); } interface IParser { /** * Sets array of keywords. * @param array $keywords associative array where key is keyword and value respective php instruction. * @return void */ public function setKeywords(array $keywords); /** * Parses code. Checks for syntax errors. * If no error occurs returns data for compiler to assemble php class * otherwise trigger syntax error. * @param string $input code to parse. * @return array IParserResult */ public function parse($input); } interface IParseResult { public function getDefinition(); public function getName(); public function getConstants(); } //I know this lexer sucks. class Lexer extends CompilerElement implements ILexer { // Potential hidden dependency private $specialChars = array( '{' => 'start_body', '}' => 'end_body' ); public function tokenize($input) { if (!is_string($input)) { $this->getErrorManager()->ariseFatal(get_class($this).'::parse expects parameter 1 to be string. '.gettype($input).' given.'); } $tokens = array(); $token = $this->getEmptyToken(); $tokenMetadata = $this->getDefaultTokenMetadata(); $inputLength = strlen($input); for ($i = 0; $i < $inputLength; $i++) { $current = $input[$i]; //check if special character if (isset($this->specialChars[$current])) { $token[$this->specialChars[$current]] = $current; if ($i !== $inputLength - 1) continue; } if (isset($token['end_body'])) { $tokens[] = $token; $token = $this->getEmptyToken(); $tokenMetadata = $this->getDefaultTokenMetadata(); if ($i === $inputLength - 1) continue; } $isWhitespace = ctype_space($current); if (isset($token['start_body'])) { //skip if whitespace if ($isWhitespace) { continue; } if ($current === ',') { $tokenMetadata['newItem'] = true; $tokenMetadata['newItemNameResolved'] = false; continue; } if ($current === '=') { $tokenMetadata['newItemNameResolved'] = true; continue; } if ($tokenMetadata['newItem']) { $token['e_'.$current] = null; $tokenMetadata['newItem'] = false; } else { end($token); $lastKey = key($token); if (!$tokenMetadata['newItemNameResolved']) { unset($token[$lastKey]); $token[$lastKey.$current] = null; } else { $token[$lastKey] .= $current; } } continue; } if (!$tokenMetadata['typeResolved']) { if ($isWhitespace) { if (strlen($token['type']) === 0) { continue; } else { $tokenMetadata['typeResolved'] = true; } } else { $token['type'] .= $current; } } else if (!$tokenMetadata['nameResolved']) { if ($isWhitespace) { if (strlen($token['name']) === 0) { continue; } else { $tokenMetadata['nameResolved'] = true; } } else { $token['name'] .= $current; } } } return $tokens; } private function getEmptyToken() { return array( 'type' => '', 'name' => '' ); } private function getDefaultTokenMetadata() { return array( 'typeResolved' => false, 'nameResolved' => false, 'newItem' => true, 'newItemNameResolved' => false ); } public function __construct(IErrorManager $errorManager) { parent::__construct($errorManager); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  (null)
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   DECLARE_CLASS                                            'lexer', 'compilerelement'
  163     1      > RETURN                                                   1

Class ILexer:
Function tokenize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  tokenize
number of ops:  2
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function tokenize

End of class ILexer.

Class INameValidator:
Function validate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  validate
number of ops:  2
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function validate

End of class INameValidator.

Class IParser:
Function setkeywords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  setKeywords
number of ops:  2
compiled vars:  !0 = $keywords
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function setkeywords

Function parse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  parse
number of ops:  2
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function parse

End of class IParser.

Class IParseResult:
Function getdefinition:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  getDefinition
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E > > RETURN                                                   null

End of function getdefinition

Function getname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  getName
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E > > RETURN                                                   null

End of function getname

Function getconstants:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  getConstants
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E > > RETURN                                                   null

End of function getconstants

End of class IParseResult.

Class Lexer:
Function tokenize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 131
Branch analysis from position: 131
2 jumps found. (Code = 44) Position 1 = 133, Position 2 = 26
Branch analysis from position: 133
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 39
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 39
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
2 jumps found. (Code = 44) Position 1 = 133, Position 2 = 26
Branch analysis from position: 133
Branch analysis from position: 26
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 53
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 53
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 99
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 61
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 68
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 73
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 81
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 81
2 jumps found. (Code = 43) Position 1 = 91, Position 2 = 96
Branch analysis from position: 91
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 99
2 jumps found. (Code = 43) Position 1 = 102, Position 2 = 115
Branch analysis from position: 102
2 jumps found. (Code = 43) Position 1 = 103, Position 2 = 112
Branch analysis from position: 103
2 jumps found. (Code = 43) Position 1 = 107, Position 2 = 109
Branch analysis from position: 107
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 109
1 jumps found. (Code = 42) Position 1 = 114
Branch analysis from position: 114
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 112
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 115
2 jumps found. (Code = 43) Position 1 = 118, Position 2 = 130
Branch analysis from position: 118
2 jumps found. (Code = 43) Position 1 = 119, Position 2 = 128
Branch analysis from position: 119
2 jumps found. (Code = 43) Position 1 = 123, Position 2 = 125
Branch analysis from position: 123
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 125
1 jumps found. (Code = 42) Position 1 = 130
Branch analysis from position: 130
Branch analysis from position: 128
2 jumps found. (Code = 44) Position 1 = 133, Position 2 = 26
Branch analysis from position: 133
Branch analysis from position: 26
Branch analysis from position: 130
Branch analysis from position: 53
Branch analysis from position: 39
Branch analysis from position: 15
filename:       /in/0cEBP
function name:  tokenize
number of ops:  135
compiled vars:  !0 = $input, !1 = $tokens, !2 = $token, !3 = $tokenMetadata, !4 = $inputLength, !5 = $i, !6 = $current, !7 = $isWhitespace, !8 = $lastKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        TYPE_CHECK                                   64  ~9      !0
          2        BOOL_NOT                                         ~10     ~9
          3      > JMPZ                                                     ~10, ->15
   54     4    >   INIT_METHOD_CALL                                         'getErrorManager'
          5        DO_FCALL                                      0  $11     
          6        INIT_METHOD_CALL                                         $11, 'ariseFatal'
          7        FETCH_THIS                                       ~12     
          8        GET_CLASS                                        ~13     ~12
          9        CONCAT                                           ~14     ~13, '%3A%3Aparse+expects+parameter+1+to+be+string.+'
         10        GET_TYPE                                         ~15     !0
         11        CONCAT                                           ~16     ~14, ~15
         12        CONCAT                                           ~17     ~16, '+given.'
         13        SEND_VAL_EX                                              ~17
         14        DO_FCALL                                      0          
   57    15    >   ASSIGN                                                   !1, <array>
   58    16        INIT_METHOD_CALL                                         'getEmptyToken'
         17        DO_FCALL                                      0  $20     
         18        ASSIGN                                                   !2, $20
   59    19        INIT_METHOD_CALL                                         'getDefaultTokenMetadata'
         20        DO_FCALL                                      0  $22     
         21        ASSIGN                                                   !3, $22
   60    22        STRLEN                                           ~24     !0
         23        ASSIGN                                                   !4, ~24
   62    24        ASSIGN                                                   !5, 0
         25      > JMP                                                      ->131
   63    26    >   FETCH_DIM_R                                      ~27     !0, !5
         27        ASSIGN                                                   !6, ~27
   66    28        FETCH_OBJ_IS                                     ~29     'specialChars'
         29        ISSET_ISEMPTY_DIM_OBJ                         0          ~29, !6
         30      > JMPZ                                                     ~30, ->39
   67    31    >   FETCH_OBJ_R                                      ~31     'specialChars'
         32        FETCH_DIM_R                                      ~32     ~31, !6
         33        ASSIGN_DIM                                               !2, ~32
         34        OP_DATA                                                  !6
   69    35        SUB                                              ~34     !4, 1
         36        IS_NOT_IDENTICAL                                         !5, ~34
         37      > JMPZ                                                     ~35, ->39
   70    38    > > JMP                                                      ->130
   73    39    >   ISSET_ISEMPTY_DIM_OBJ                         0          !2, 'end_body'
         40      > JMPZ                                                     ~36, ->53
   74    41    >   ASSIGN_DIM                                               !1
         42        OP_DATA                                                  !2
   75    43        INIT_METHOD_CALL                                         'getEmptyToken'
         44        DO_FCALL                                      0  $38     
         45        ASSIGN                                                   !2, $38
   76    46        INIT_METHOD_CALL                                         'getDefaultTokenMetadata'
         47        DO_FCALL                                      0  $40     
         48        ASSIGN                                                   !3, $40
   78    49        SUB                                              ~42     !4, 1
         50        IS_IDENTICAL                                             !5, ~42
         51      > JMPZ                                                     ~43, ->53
   79    52    > > JMP                                                      ->130
   82    53    >   INIT_FCALL                                               'ctype_space'
         54        SEND_VAR                                                 !6
         55        DO_ICALL                                         $44     
         56        ASSIGN                                                   !7, $44
   84    57        ISSET_ISEMPTY_DIM_OBJ                         0          !2, 'start_body'
         58      > JMPZ                                                     ~46, ->99
   86    59    > > JMPZ                                                     !7, ->61
   87    60    > > JMP                                                      ->130
   90    61    >   IS_IDENTICAL                                             !6, '%2C'
         62      > JMPZ                                                     ~47, ->68
   91    63    >   ASSIGN_DIM                                               !3, 'newItem'
         64        OP_DATA                                                  <true>
   92    65        ASSIGN_DIM                                               !3, 'newItemNameResolved'
         66        OP_DATA                                                  <false>
   93    67      > JMP                                                      ->130
   95    68    >   IS_IDENTICAL                                             !6, '%3D'
         69      > JMPZ                                                     ~50, ->73
   96    70    >   ASSIGN_DIM                                               !3, 'newItemNameResolved'
         71        OP_DATA                                                  <true>
   97    72      > JMP                                                      ->130
  100    73    >   FETCH_DIM_R                                      ~52     !3, 'newItem'
         74      > JMPZ                                                     ~52, ->81
  101    75    >   CONCAT                                           ~53     'e_', !6
         76        ASSIGN_DIM                                               !2, ~53
         77        OP_DATA                                                  null
  102    78        ASSIGN_DIM                                               !3, 'newItem'
         79        OP_DATA                                                  <false>
         80      > JMP                                                      ->98
  104    81    >   INIT_FCALL                                               'end'
         82        SEND_REF                                                 !2
         83        DO_ICALL                                                 
  105    84        INIT_FCALL                                               'key'
         85        SEND_VAR                                                 !2
         86        DO_ICALL                                         $57     
         87        ASSIGN                                                   !8, $57
  107    88        FETCH_DIM_R                                      ~59     !3, 'newItemNameResolved'
         89        BOOL_NOT                                         ~60     ~59
         90      > JMPZ                                                     ~60, ->96
  108    91    >   UNSET_DIM                                                !2, !8
  109    92        CONCAT                                           ~61     !8, !6
         93        ASSIGN_DIM                                               !2, ~61
         94        OP_DATA                                                  null
         95      > JMP                                                      ->98
  111    96    >   ASSIGN_DIM_OP                .=               8          !2, !8
         97        OP_DATA                                                  !6
  115    98    > > JMP                                                      ->130
  118    99    >   FETCH_DIM_R                                      ~64     !3, 'typeResolved'
        100        BOOL_NOT                                         ~65     ~64
        101      > JMPZ                                                     ~65, ->115
  119   102    > > JMPZ                                                     !7, ->112
  120   103    >   FETCH_DIM_R                                      ~66     !2, 'type'
        104        STRLEN                                           ~67     ~66
        105        IS_IDENTICAL                                             ~67, 0
        106      > JMPZ                                                     ~68, ->109
  121   107    > > JMP                                                      ->130
        108*       JMP                                                      ->111
  123   109    >   ASSIGN_DIM                                               !3, 'typeResolved'
        110        OP_DATA                                                  <true>
        111      > JMP                                                      ->114
  126   112    >   ASSIGN_DIM_OP                .=               8          !2, 'type'
        113        OP_DATA                                                  !6
        114    > > JMP                                                      ->130
  128   115    >   FETCH_DIM_R                                      ~71     !3, 'nameResolved'
        116        BOOL_NOT                                         ~72     ~71
        117      > JMPZ                                                     ~72, ->130
  129   118    > > JMPZ                                                     !7, ->128
  130   119    >   FETCH_DIM_R                                      ~73     !2, 'name'
        120        STRLEN                                           ~74     ~73
        121        IS_IDENTICAL                                             ~74, 0
        122      > JMPZ                                                     ~75, ->125
  131   123    > > JMP                                                      ->130
        124*       JMP                                                      ->127
  133   125    >   ASSIGN_DIM                                               !3, 'nameResolved'
        126        OP_DATA                                                  <true>
        127      > JMP                                                      ->130
  136   128    >   ASSIGN_DIM_OP                .=               8          !2, 'name'
        129        OP_DATA                                                  !6
   62   130    >   PRE_INC                                                  !5
        131    >   IS_SMALLER                                               !5, !4
        132      > JMPNZ                                                    ~79, ->26
  141   133    > > RETURN                                                   !1
  142   134*     > RETURN                                                   null

End of function tokenize

Function getemptytoken:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  getEmptyToken
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  146     0  E > > RETURN                                                   <array>
  149     1*     > RETURN                                                   null

End of function getemptytoken

Function getdefaulttokenmetadata:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  getDefaultTokenMetadata
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  153     0  E > > RETURN                                                   <array>
  158     1*     > RETURN                                                   null

End of function getdefaulttokenmetadata

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0cEBP
function name:  __construct
number of ops:  5
compiled vars:  !0 = $errorManager
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  160     0  E >   RECV                                             !0      
  161     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
  162     4      > RETURN                                                   null

End of function __construct

End of class Lexer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.32 ms | 1416 KiB | 19 Q