Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111073 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31890 invoked from network); 19 Jul 2020 00:40:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Jul 2020 00:40:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6E45C1804C6 for ; Sat, 18 Jul 2020 16:33:43 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11olkn2107.outbound.protection.outlook.com [40.92.20.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 18 Jul 2020 16:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mRb0rW7x4515rXblfBdIk0CY6O0hCHWIDMAO4MZvC2ROmh2MGSoZ/E3Bd2xdOEhF6DamElNDCy069K+oDQEI1twuhNJJC/pe5WO/Uq09IH0PSSmpQnDvZkgIZ9qT3EfOPelyjbcAyrLVVXcggrOE3SHhNlsKY/J9aRfUM7SbflEPXP8oojuIzzLTKnEExd8xWOrobf5escb1UL5wF0kGOTX//fPxjUHsr6EyvOIEZAdKHjE8WaZEQSpgrfV1ZGcgdRSAQevw/bNTm+oy5Cq0G0QxD1LQmfFfHyYYOUb9Iq9t+Tkit1ejSPKrJ5xoXpWeia/9KUo1gqkSyV7LIXMEJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LFj4YtU5OntZFgiK/drvOmn7/ph7ZCDFZLAxAxn9qWw=; b=n4RTDP19kk7hBXM+FiuFEexZmRmgHKwvZmlTxlPXgks4latORZtXhs52dMfhfjacNisY9AGKMWG9gcdBgACxDD+KjybBOHUCAr9Pb9q/wZTYoXQChbmVPYxVo3q3jFlrElwqoLasoc1jxpPnjotJ/bhyfb2m75NURkbzMViIqds4XcRMQkZ1Qk3mPULHdLf+FMjExOmgSdLDoPUJjsKsQgP5tUh0Tlo+5xNsRxs7J46GfuZrS1h5HWhftI1Xn5b4QUb/dhWbTa4bDvqBArPHRc9XkIKl7LGEmid4A0bqG1swM/z2L5e7SJiTNIzbVGiTJkXO3sa/wjVdj6DHisYXEA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LFj4YtU5OntZFgiK/drvOmn7/ph7ZCDFZLAxAxn9qWw=; b=UwhXmf2Y/of0SWk1pKfHU6DyhgfP848NxR2wqfmLpr9zVZHwV3nsHoNUTtlRcs7IIHW9lTOL/k6RHEWEgyjqmSRc50XYbYQe3bZzU7ZxqhsHkroyIaNJR/oiDPjsvt1Chdoy8tlF3DDRj9zdhELH2ptZQ5i+Elq9X2zKYyJGTKclloD2lBLQXCG9PrDhu47FVJt+Pk4t3ekwz+RIPCxcMgIHbXig+f0V6+nJuF9gNKpXdKTrVES9r/9V+8FDdgkgWD37exVQnsLYorUHXZbHyzvjQvCWr3/QoM6F7bGEptcdzQIPU4jJDXt83dFG1irifmWl3S6V/gh4QVZ0BN9Ttg== Received: from BN8NAM11FT024.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4b::40) by BN8NAM11HT125.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4b::228) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.18; Sat, 18 Jul 2020 23:33:41 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:fc4b::47) by BN8NAM11FT024.mail.protection.outlook.com (2a01:111:e400:fc4b::294) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.18 via Frontend Transport; Sat, 18 Jul 2020 23:33:41 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::f0c8:f413:c7c1:e934]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::f0c8:f413:c7c1:e934%4]) with mapi id 15.20.3195.023; Sat, 18 Jul 2020 23:33:41 +0000 To: "internals@lists.php.net" Thread-Topic: Improving the usability of PHP's interactive shell? (completions, displaying expression results, custom syntax) Thread-Index: AQHWXVBlEJWFSBe2+kWGt77QcnGQFA== Date: Sat, 18 Jul 2020 23:33:41 +0000 Message-ID: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:37AB4B728699FDCBDB9EA8AA2A7AA64360D4704D2335397766B104D8EDFAB999;UpperCasedChecksum:D68595CA933AD7167288DAF12BD2B5B0D9C6489E638EA32C2B8176035AE4FB22;SizeAsReceived:6972;Count:41 x-tmn: [TT/c4gFbwNS/k3lIZve36Lm8bn7fDssXAGz+HWvo52lKW6XAyduUNbw6RXVcTvoL] x-ms-publictraffictype: Email x-incomingheadercount: 41 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 8ec1ae2f-88a5-4231-bf9b-08d82b730153 x-ms-traffictypediagnostic: BN8NAM11HT125: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: cKMCc1tReq6rU/OvuUuN9e/2aw8s5I+6IFuKUWG0amWWn7EaYwHgEJT5+MI0adl0JLIBiVDozdPLL7O0Ag3ciKBflN7uxsF3TcK2jRuNpo76SCbH0W3EbjqR4V7PJeWyWIHNB2q2DjeTq5bmyWEWE4xH9gdSkNuZ7Jr3oEYBGXW5I8XMUj+5oP/BvaLg3lp8se7Zv05YtrFJaC8T7YdJiy4hSUN5j2Isqh/oeIsinUzn0UGvnWsqIvuJiXCfuofw x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:0;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR07MB6618.namprd07.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:;DIR:OUT;SFP:1901; x-ms-exchange-antispam-messagedata: lGQsYcsiL8wQNqzgFczBo+u/gX8ttTsfr+ayx4QYdUSOE6nIyE66PlokXbwixpO9na4zCKEWt7GrLqJUXLDa6TddR1I+l75LZVNiXZZyR2UQojko3Mo3fdifpzroRSrD+LI9GdQnI2SRVwg6nob6EZn8IT8/LBQB9etmg3aqW9wvtACUsXY2icVPj4A5gyDS95bUlwS6WXTyNG7YpnIUKg== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT024.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 8ec1ae2f-88a5-4231-bf9b-08d82b730153 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Jul 2020 23:33:41.5334 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8NAM11HT125 Subject: Improving the usability of PHP's interactive shell? (completions, displaying expression results, custom syntax) From: tysonandre775@hotmail.com (tyson andre) Hi internals,=0A= =0A= `php -a` currently provides an interactive shell with tab completion writte= n in C, in readline/libedit.=0A= This currently has a number of limitations:=0A= =0A= - Adding new features or bug fixes would require a time getting familiar wi= th=0A= C programming, PHP's internals and memory management, and with how readli= ne works internally.=0A= =0A= Making it possible to write replace more of the REPL implementation in PH= P instead of C=0A= may open this up to more contributors.=0A= (at the cost of a completion performance slowdown/memory increase/install= ation size,=0A= which should be unimportant if only some interactive shells are affected = or if there are few candidates for completion)=0A= - Completions for syntax such as constants, member variables/properties, et= c. (`$x->`) have been a TODO for years.=0A= =0A= ```=0A= // ext/readline/readline_cli.c=0A= static char *cli_completion_generator(const char *text, int index) /* {{{ *= /=0A= {=0A= /*=0A= TODO:=0A= - constants=0A= - maybe array keys=0A= - language constructs and other things outside a hashtable (echo, try, func= tion, class, ...)=0A= - object/class members=0A= =0A= - future: respect scope ("php > function foo() { $[tab]" should only expand= to local variables...)=0A= */=0A= ```=0A= =0A= So, my ideas for improving the situation in 8.1 or later=0A= =0A= - Allow overriding readline_completion_function() in auto_prepend_file=0A= (I think that that not working is a bug, and hope it could get fixed in 8= .0)=0A= https://github.com/php/php-src/pull/5872=0A= =0A= https://github.com/phan/phan/commit/228827516df606de23e9ac94873c7b953f4bf= 4c1 (`tool/phan_repl_helpers.php`) includes a prototype=0A= of replacing PHP's C readline completion with a readline callback written= in pure PHP.=0A= (I started working on that file today and it's missing some completion fu= nctionality, but it may be a good starting point)=0A= - Add a hook to `readline` such as `readline_set_php_result_handler_callbac= k(function(mixed $result, string $snippet){...})`=0A= that can be used to create a user-defined function to print/process the r= esult of expressions.=0A= (Create a clone of `zend_eval_stringl` to support that)=0A= (The result value would instead be freed after the callback was called)= =0A= =0A= Many other REPLs (Read-Eval-Print Loops) that I'm familiar with print a r= epresentation of the result of expressions, but PHP doesn't.=0A= =0A= Some values such as $GLOBALS, extremely long binary strings, etc. may nee= d to have the representation truncated in some way.=0A= A tiny example implementation of `readline_set_php_result_handler_callbac= k` would be `if ($result !=3D=3D null) { var_dump($result); }`=0A= - Add a hook such as `readline_before_evaluate_php_snippet(string $snippet)= : bool {}`=0A= that gets called before attempting to evaluate snippets (whether or not t= hey have parse errors).=0A= =0A= Returning true would indicate that the snippet should not be evaluated.= =0A= =0A= This could be useful for adding meta commands that don't use PHP syntax, = such as `help;` `help SomeClass::someClassElement;`, `cd /dirname`, etc.=0A= - Add some way to provide the contents of all previous lines during `php -a= ` to provide better completions for multi-line snippets=0A= (e.g. suggest local variables in multi-line function declarations instead= of global variables)=0A= =0A= Finally, it would be useful to have something to tie those together:=0A= - Provide an `iphp`, `phpi`, `php-interactive`, or some other entry point t= o prepend a bundled phar script=0A= that uses those hooks to provide a better user experience.=0A= (Analogous to how `ipython` and `irb` start a different interactive shell= than `python` or `ruby`)=0A= =0A= =0A= =0A= Miscellaneous thoughts on implementation details:=0A= - Bundling an actual parser (e.g. https://github.com/nikic/PHP-Parser) woul= d help in properly analyzing `Foo::$var->`=0A= by being less reliant on heuristics (e.g. checking if $var was a variable= or a property, making it easier to collect local variables, etc).=0A= =0A= Is packaging a parser practical for a `phpi` binary (e.g. for package mana= gers, maintainers of php, other reasons)?=0A= =0A= - A parser may fail for code using new token types until the parser gets = updated to handle the new token types. This stops being a concern after fea= ture freezes.=0A= Looping over `@token_get_all()` and bailing out on an unknown token typ= e may help with that.=0A= - How would crash/bug fixes of `phpi` or the parser be handled in patch r= eleases of php if this was released with php?=0A= - Automatically rewriting the code to namespace the parser and its depend= encies with `\PHP\Internal\BundledPhpParser`=0A= would let `phpi` be used with projects that depend on a different php-= parser version.=0A= =0A= (clarifications may be necessary to indicate to end users that the bun= dled parser copy won't get updates or support outside of php minor releases= ,=0A= should not be used by libraries/applications and that it won't support= newer php syntax, and possibly other clarifications)=0A= - It may be useful to have an ini setting to disable these new hooks,=0A= in case bugs/crashes in libraries using those hooks interfered with debu= gging.=0A= =0A= P.S. What do developers here use for an interactive shell?=0A= I've seen https://github.com/bobthecow/psysh mentioned as an alternative fo= r `php -a` while investigating options but I haven't gotten around to using= it.=0A= It's useful that `php -a` doesn't die from otherwise unrecoverable errors.= =0A= =0A= Thanks,=0A= - Tyson=