tags:

views:

252

answers:

8

I have a programming text editor (written in PHP) and want to remove the $ signs available in PHP scripts upon loading of the document for editing ... to "quietly" insert them back once the user saves the file. Basically, I want to save the user the work of always typing the $ icon in front of variables (similar to how you don't have to in, say, Python).

My question: With which PHP code could I insert the $ signs back into the script?

Even if the functionality would make some compromises and assumptions in terms of coding style -- i.e. not work in edge cases -- I think it would be very useful.

Edit: As I originally mentioned, I'm OK with compromises in terms of style. Here's an example syntax that might be easin' the parsin' and still be nicer to write IMO:

for (var i = 1; i <= 10; i++) { /* look ma, no $s! */ }
+25  A: 

It's not possible, because it would make the syntax ambiguous. You couldn't tell the difference between a variable and a defined constant, for example.

Lukáš Lalinský
I wouldn't be too sure about the impossible part. Hard yes - but there is plenty of contextual information in a PHP program (not to mention that it is a program and thus the syntax is defined and structured) from which this could be derived. I'm not saying such a system would be easy to make - or even obvious, but saying it's not possible seems to be over reaching.
Streklin
Take this example: define('foo', 1); foo = 2; echo foo;Does it print 1 or 2? Or even simpler: foo = 'bar'; echo foo;Does it print 'foo' or 'bar'?
Lukáš Lalinský
Your second example allows you to work out that foo is a variable since it has an assignment, and isn't a reserved keyword. From context then we we can work out that foo should become $foo . If this sort of thing was actually an issue, languages like "qbasic" would have been rather difficult to use. (In fact foo = 1 print foo is legal qbasic syntax if I recall and yet works just fine. Again - I don't claim it to be easy, or worth the effort, I just don't think it's impossible. First example would require his system to recognize the define keyword and parse accordingly. Hard!=impossible
Streklin
No, there is no right or wrong in those examples. Both cases are completely valid. "define('foo', 1); $foo = 2; echo foo;" and "define('foo', 1); $foo = 2; echo $foo;" simply define two different code paths, yet they would be reduced to the same source code (ie, it would be a one-way transformation).
Lukáš Lalinský
What about in strings: `echo "My name is $name";` becomes `echo "My name is name";`
rojoca
Languages like QBasic, Python and others also don't need you to precede variables, yet they also don't force you to define them in the beginning, hence it's not impossible... but as mentioned, probably hard! Thanks to those who tried to help.
Philipp Lenssen
PS: I was also pondering a compromise, where one would always need to define the first variable use using a $, but not subsequent ones.
Philipp Lenssen
PPS: Additionally, as I mentioned, I'm OK with a solution that requires certain compromises and does not work in edge cases. When detecting an edge case like above's "My name is $name" it could always let the $ sign in. That is actually an example of where the PHP $ comes in handy and may *save* typing -- in most other cases, it doesn't save time.
Philipp Lenssen
Python is a different language with different semantics. Removing the $ would mean designing a new language.
Lukáš Lalinský
If you want to make it work in all edge cases, then perhaps so, but that was not my goal, as I stated. A simple compromise like the following, using var instead of $s everywhere, could go a very long way in easing the parsing: for (var i = 1; i <= 10; i++) { /* look ma, no $s! */ }
Philipp Lenssen
A: 

This would be down to your IDE wouldn't it? You could have a script to run it through which will put the $'s back once you are done editing. Just depends on what your IDE is capable of.

I can't really see it being much of a problem, is it that hard to add one character?

Ben Shelock
"This would be down to your IDE wouldn't it?" Yes, and I am programming this IDE, hence the question. "is it that hard to add one character" No, but it would be quicker without it -- and remember you need to type it not once but many, many types in a given script (the "1"s add up).
Philipp Lenssen
+4  A: 

I suggest you not to add this "feature". People who are acquainted with PHP will habitually type in the '$' sign, voiding the need for this. Additionally, as Lukas said, the $ sign has a reason to be there...

Aviral Dasgupta
> People who are acquainted with PHP> will habitually type in the '$' sign, voiding the need for this.Perhaps many will, but I *am* aquainted with PHP and do not always want to type the $ -- and this programming editor is mostly written for me (though I will release it publicly, too).
Philipp Lenssen
+4  A: 

It doesn't sound like a good idea.

As has been mentioned, the syntax will become ambiguous.

You don't say so, but your text editor is presumably NOT doing syntax highlighting, even though you are putting code in it.

Removing $ signs from variable names would make syntax highlighting impossible to implement - a very valuable addition to a programming text editor.

pavium
+3  A: 

It could probably be done - it is entirely possible to implement programming languages without a "$" symbol - and I not convinced the "$" symbol was implemented to remove ambiguity in the first place. (Having the special symbol in an interpreted language could be used to speed up the parsing when differentiating between what's a variable, a constant, or a function of some sort for example).

However, it would be tricky. You'd pretty much have to have a pretty deep knowledge of PHP and lookup table of some form in order to work out what is what i.e. if it's in the lookup table it's a reserved word, etc. You'd need to track anything the user does which could be mistaken for a variable - which you'd likely need to do by checking the context. IT would be a huge and complex project.

So while I think you could do it (and keep Syntax highlighting - since the information required to do that would be basically required to implement this feature) the effort required seems to far out weigh the benefit of not having to type a simple $ symbol in front of your variables.

Streklin
A: 

I think too it is a bad idea, it might be simpler to code in Python or Lua or some other language without these $ (inherited from Perl, I think).

Beside, you would need a full PHP lexer/parser to get it nearly right, and I guess you will get lot of corner case hard to fix, like interpolation in strings.

PhiLho
+4  A: 

There is no way you can fix:

$blah = "hello";
"blah blah blah $blah blah";

Without using an escape character. So you have to rewrite as:

$blah = "hello";
"blah blah blah " . $blah . " blah";

But that goes against the feeling of php. And that reminds me of the pascal programmer who has problem using C and found the following solution:

#define BEGIN {
#define END }

The morale of the story is to accept a language as it is. There are enough to chose from.

Gamecat
No, in the case of $ being used in a string, it could simply remain as is -- there is no need to handle such border cases perfectly, they would just need to be detected to be ignored. (Though I nearly always write 'blah blah blah ' . $blah . ' blah' in the first place :)).
Philipp Lenssen
+3  A: 

This is a crazy idea but this can be done. If you will have to tell the editor that these are the variables. To do this you will have to use var i,variable_name to let the editor know that these are variables. Or something in comments.

Pasta
Thanks for the reply, I think too this var approach might be a feasible route to go.
Philipp Lenssen
Hmmm...and all this time I thought that with $, I'm explicitly telling the editor (and the interpreter) that this is a $variable . In other words, you've just invented an ad-hoc alternative syntax for $, which is - quite incidentally - incompatible with everything else ever written in PHP.
Piskvor