Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98622 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17999 invoked from network); 24 Mar 2017 18:43:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Mar 2017 18:43:52 -0000 Authentication-Results: pb1.pair.com header.from=guilhermeblanco@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=guilhermeblanco@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.50 as permitted sender) X-PHP-List-Original-Sender: guilhermeblanco@gmail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:36986] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EA/98-40046-5E865D85 for ; Fri, 24 Mar 2017 13:43:50 -0500 Received: by mail-wm0-f50.google.com with SMTP id n11so9243264wma.0 for ; Fri, 24 Mar 2017 11:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Fvc8lSA/pe8q1rldvUYOu7Hm02EjqPd0pkQ9NmQ0/mU=; b=Si2ZCQddDvHUS+5NLCyUjV2iJOWV3280E4G6niyV/juB+8RWOl6snHPMg6YKkrHe1r 9jISRtafUxVO4IWGR21kqNskUiKtS4HNZ4eRP1vlXX3Dl0gUgIis72o0BZsHhVxmjOmI 3rE5jCKD0abmLGhBguAhstUngdvsyMYVsediB/UnjJoBPxtNbG8LIxXpOPKiCV1w7T8X OKZ+SO4+Hpp9jZFYF+fYn9SzUJe2ojkJCgmafB/gYv+rlLE1VPvL0fFoH80YiIuq9Gtu Ygyx8GZi0QpV4IulPVoAbyree0d/K/+6RAXolYOjAt0i3TvT1UaopgWKwfTYCO+8oW1N SkFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Fvc8lSA/pe8q1rldvUYOu7Hm02EjqPd0pkQ9NmQ0/mU=; b=uHaou7SZNxQgnaYCp69joLJ/jzozzcocDB1bhcP5MqqBst4ibJUJKdW7fck4R15YTS mo6OQfB3O2ke5dq0Aa3eMR/nqEqnkcERhY195sZt9nxkmxFEaqAq/IPiL+aISc+aYhls 1NT39bD1ep5ImJ/tkxYWs5j46I6yVi7liNV0h+iAxL1HFJqecN7KEHb+8rt+6heA09S8 /OHcPrKdOYxllFl1CiMWSw9oG1wQlz5PH5XP5if+DC0OPK7fLCD2k0MtYtYq73khN54h tRtzOik00xqn4ZvkHiLlCXy3yD4H6wpq8i9aD025upwll2JOftvzK153K0u9xHRI06is 2ezQ== X-Gm-Message-State: AFeK/H2yKETuv7XxB2jfdcqckDxHuUTMdh8NqovN6/SluakM0WLDDK1yU+h3NabWEnuCOqKj81+9rWlpRMpejA== X-Received: by 10.28.203.204 with SMTP id b195mr4597535wmg.51.1490381026400; Fri, 24 Mar 2017 11:43:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.160.218 with HTTP; Fri, 24 Mar 2017 11:43:25 -0700 (PDT) In-Reply-To: <153a6a0f-1c22-6560-8bca-584e92915840@fleshgrinder.com> References: <16.06.40046.20A35D85@pb1.pair.com> <153a6a0f-1c22-6560-8bca-584e92915840@fleshgrinder.com> Date: Fri, 24 Mar 2017 14:43:25 -0400 Message-ID: To: PHP internals Cc: Andrea Faulds , Nikita Popov Content-Type: multipart/alternative; boundary=94eb2c1309d2a8a003054b7e5f78 Subject: Re: [PHP-DEV] Re: TOKEN_AS_OBJECT for token_get_all() From: guilhermeblanco@gmail.com ("guilhermeblanco@gmail.com") --94eb2c1309d2a8a003054b7e5f78 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks Fleshgrinder! You expressed exactly what I thought, but was too busy... cough... lazy to put in an email. Cheers, On Fri, Mar 24, 2017 at 2:35 PM, Fleshgrinder wrote: > On 3/24/2017 4:23 PM, Andrea Faulds wrote: > > Hi Nikita, > > > > Nikita Popov wrote: > > > >> I'd like to add a new TOKEN_AS_OBJECT flag to token_get_all(), which > >> returns an array of PhpToken objects, rather than the mix of plain > >> strings > >> and arrays we currently have. The PhpToken class is defined as: > >> > >> class PhpToken { > >> public $type; > >> public $text; > >> public $line; > >> } > > > > Rather than adding a flag to token_get_all() to return objects, you > > could potentially instead make an equivalent static method on PhpToken > > (PhpToken::getAll() perhaps). That would avoid mixing =E2=80=9Cobject-o= riented=E2=80=9D > > and =E2=80=9Cprocedural=E2=80=9D styles, though I don't know if it matt= ers. It seems > > cleaner to me. > > > > Thanks! > > > > I am also against adding another flag because it violates the single > responsibility principle. However, adding a `getAll` method to the > `PhpToken` data class also seems very wrong, because it violates the > single responsibility once again. > > The real and proper solution is to have an actual PHP Lexer that is > capable of creating a token stream. > > class Lexer { > public function __construct(string $source); > public static function fromFile(string $path): self; > public function tokenize(): TokenStream; > } > > final class TokenStream implements IteratorAggregate { > public function getIterator(): Generator > public function toArray(): Token[] > } > > final class Token { > // Ideally this would be an enum, but... > public const OPEN_TAG; > public const CLOSE_TAG; > // ... > > // I hope these are not mutable! > public int $category; // type > public string $lexeme; // text > public int $line; > public int $column; // we don't have that :( > > /** @see token_name */ > public function getCategoryName(): string; > > // We could add `is*` methods for the various categories here. > public function isOpenTag(): bool; > public function isCloseTag(): bool; > // ... > } > > With this in place, we're ready for the future. The `TokenStream` can > easily use a generator over the internal array. Or we offer the > `toArray` method only at the beginning. Users will have to call it, but > that overhead is tiny, considering that we can extend upon it in the > future without introducing any kind of breaking change. > > Obviously this could go into the namespace `PHP\Parser`, or we prefix > everything with `PHP` (I'd be for the former). > > On a side note, going for `Php` instead of `PHP` is inconsistent with > the naming that is currently dominating the PHP core: > > https://secure.php.net/manual/en/indexes.functions.php > > There are already some things that violate it, e.g. `Phar` instead of > `PHAR`, but most things keep acronyms intact (`DOM`, `XML`, `PDO`, ...). > Introducing even more inconsistency to PHP seems like a very bad idea to > me. I know that this is considered bikeshedding by many people, but > consistency is very important and we are already lacking it on too many > levels as it is. > > -- > Richard "Fleshgrinder" Fussenegger > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 Guilherme Blanco Senior Technical Architect at Huge Inc. --94eb2c1309d2a8a003054b7e5f78--