function tokenize($code) { $regexes = [ // ... ]; $statements = split(' ', $code); $tokens = []; foreach($regexes as $regex) { foreach($statements as $statement) { $tokens[] = /* ... */; }); }); return $tokens; } function lexer($tokens) { $ast = []; foreach($tokens as $token) { $ast[] = /* ... */; }); return $ast; } function parseBetterJSAlternative($code) { $tokens = tokenize($code); $ast = lexer($tokens); foreach($ast as $node) { // parse... }); }