-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added TypeScript PhpLexerBase for php grammar #3994
base: master
Are you sure you want to change the base?
Conversation
This code doesn't compile, and even after fixing the compilation errors, it does not work. Please make the following changes:
Here is what it should be:
Note the large number of missing tokens in your TypeScript port. NB: There is a bug in the other targets with the token text for the closing parentheses. That should be fixed, but you don't have to fix that.
|
…t default abstract class.
@teverett hi, thanks for reviewing this. I'm not too familiar with this repository. I had to implement this and it was working for what I was doing so I thought of contributing it. I fixed some of the problems but I'm not sure how to fix the HEREDOC problem. Could you point me in the right direction? Thank you! |
// Add semicolon to the end of statement if it is absent. | ||
// For example: <?php echo "Hello world" ?> | ||
if (this._prevTokenType === PhpLexer.SemiColon || this._prevTokenType === PhpLexer.Colon || this._prevTokenType === PhpLexer.OpenCurlyBracket || this._prevTokenType === PhpLexer.CloseCurlyBracket) { | ||
token = super.nextToken() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is wrong. Check out the CSharp port. The code should be token.channel = 1;
. ("1" is the hidden channel.)
token = super.nextToken() | ||
} else { | ||
token = new CommonToken(undefined, PhpLexer.SemiColon, undefined, undefined, undefined); | ||
token.text = ';'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change these two lines to correspond to that in the CSharp port: token.type = PhpLexer.SemiColon;
and delete the "new CommonToken()" call.
else if (this._mode === PhpLexer.HereDoc) { | ||
// Heredoc and Nowdoc syntax support: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc | ||
if (token.type === PhpLexer.StartHereDoc || token.type === PhpLexer.StartNowDoc) { | ||
this._heredocIdentifier = token.text.slice(3).trim().replace(/\'$/, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code does not correspond to the CSharp port. This fails because in C#, trim(char)
removes both the leading and trailing char
. But, in TypeScript, replace(pat, string)
only replaces the first occurrence of the pattern. So, 'NOWDOC
is replaced with NOWDOC'
. The way I did this is to have two replace(), one for the beginning of the string, and another for the end. Or, you could just call replace(/\'/, '')
twice.
if (token.text.trim().endsWith(';')) { | ||
token = new CommonToken((CommonToken as any).EMPTY_SOURCE, PhpParser.SemiColon, | ||
undefined, undefined, undefined) | ||
token.text = `${heredocIdentifier};\n`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this line (token.text = '$........;\n';
. This rewrite of the text makes it very confusing. In addition, just delete the line with token = new CommonToken(...);
because it's not in the CSharp port.
token.text = `${heredocIdentifier};\n`; | ||
} else { | ||
token = super.nextToken() | ||
token.text = `${heredocIdentifier}\n;`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, delete this line token.text = ...;
. Same reason as above--very confusing. If anything, it should just set the text to ';'.
this._heredocIdentifier = token.text.slice(3).trim().replace(/\'$/, ''); | ||
} | ||
|
||
if (token.type === PhpLexer.HereDocText) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really should be else if (token.type === PhpLexer.HereDocText) }
because it does not follow exactly the switch statement in the CSharp port. Notice the "break" following the two cases.
grammars-v4/php/CSharp/PhpLexerBase.cs
Line 107 in c993c98
break; |
Note, the above changes takes care of a number of problems but the code still fails for a couple of tests in the examples/ directory. I'm working out why. |
I added the TypeScript class for using the php grammar from the typescript target. It's very similar to the JavaScript one.