Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98624 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21598 invoked from network); 24 Mar 2017 19:15:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Mar 2017 19:15:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.216 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.216 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.216] ([81.169.146.216:16544] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A5/49-40046-94075D85 for ; Fri, 24 Mar 2017 14:15:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1490382918; l=9185; s=domk; d=kelunik.com; h=Content-Type:Cc:To:Subject:Date:From:References:In-Reply-To: MIME-Version; bh=YsmcpharqXoUQr2AeMaacOzcDMJajN0RciTV0mn5TUg=; b=K1OVK1KR4bvXDcTNyAINcUA2bA3nAzeGpdy/vEwX0f023U1oH4prpXk/K8mZD+m4zu cBjoR/qfKcp2wIIQApNqqb40nX6Am5kKZgowjEwTp167jQPuY2Q593Q8fJo1Eh16cvxW CxQJ1UxnPugvNQiyBHc+2qoizKaWHO02hqP/Q= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLDup6E67mzuoNJBqD/tMc= X-RZG-CLASS-ID: mo00 Received: from mail-qt0-f174.google.com ([209.85.216.174]) by smtp.strato.de (RZmta 40.1 AUTH) with ESMTPSA id 40bdb8t2OJFI9LE (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp384r1 with 384 ECDH bits, eq. 7680 bits RSA)) (Client did not present a certificate) for ; Fri, 24 Mar 2017 20:15:18 +0100 (CET) Received: by mail-qt0-f174.google.com with SMTP id n21so10297201qta.1 for ; Fri, 24 Mar 2017 12:15:18 -0700 (PDT) X-Gm-Message-State: AFeK/H3xrb+jjnu47F6ACezuVLNICP7R9zDbxt2P1ek8MGF6BjAKhGkvjZ0M2p7iJfGeI1m6s831xCP56UDpAg== X-Received: by 10.237.32.240 with SMTP id 103mr9578476qtb.93.1490382917384; Fri, 24 Mar 2017 12:15:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.12.144.167 with HTTP; Fri, 24 Mar 2017 12:15:16 -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 20:15:16 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP Internals Cc: Andrea Faulds , Nikita Popov Content-Type: multipart/alternative; boundary=94eb2c0c5c725ec960054b7ed077 Subject: Re: [PHP-DEV] Re: TOKEN_AS_OBJECT for token_get_all() From: me@kelunik.com (Niklas Keller) --94eb2c0c5c725ec960054b7ed077 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2017-03-24 19:35 GMT+01:00 Fleshgrinder : > 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; > } > If PhpToken::getAll() violates the SRP, then Lexer::fromFile() is just the same sort of violation. Regards, Niklas > 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 > > --94eb2c0c5c725ec960054b7ed077--