[#82] add support of shebang

This commit is contained in:
z7zmey 2019-12-30 00:20:20 +02:00
parent 4a518d9e5d
commit 31052588b1
5 changed files with 8968 additions and 8770 deletions

View File

@ -1077,6 +1077,18 @@ func TestParseAndPrintPhp5InlineHtml(t *testing.T) {
}
}
func TestParseAndPrintPhp5Shebang(t *testing.T) {
src := `#!/usr/bin/env php
<?php
$a;?>test<? `
actual := print(parse(src))
if src != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
}
}
func TestParseAndPrintPhp5Interface(t *testing.T) {
src := `<?php
interface Foo extends Bar , Baz {

View File

@ -1200,6 +1200,18 @@ func TestParseAndPrintInlineHtml(t *testing.T) {
}
}
func TestParseAndPrintShebang(t *testing.T) {
src := `#!/usr/bin/env php
<?php
$a;?>test<? `
actual := print(parse(src))
if src != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", src, actual)
}
}
func TestParseAndPrintInterface(t *testing.T) {
src := `<?php
interface Foo extends Bar , Baz {

File diff suppressed because it is too large Load Diff

View File

@ -134,6 +134,16 @@ func (lex *Lexer) Lex(lval Lval) int {
);
main := |*
"#!" any* :>> newline => {
lex.addFreeFloating(freefloating.CommentType, lex.ts, lex.te)
};
any => {
fnext html;
lex.ungetCnt(1)
};
*|;
html := |*
any_line+ -- '<?' => {
lex.ungetStr("<")
lex.setTokenPosition(token)
@ -159,8 +169,8 @@ func (lex *Lexer) Lex(lval Lval) int {
php := |*
whitespace_line* => {lex.addFreeFloating(freefloating.WhiteSpaceType, lex.ts, lex.te)};
'?>' newline? => {lex.setTokenPosition(token); tok = TokenID(int(';')); fnext main; fbreak;};
';' whitespace_line* '?>' newline? => {lex.setTokenPosition(token); tok = TokenID(int(';')); fnext main; fbreak;};
'?>' newline? => {lex.setTokenPosition(token); tok = TokenID(int(';')); fnext html; fbreak;};
';' whitespace_line* '?>' newline? => {lex.setTokenPosition(token); tok = TokenID(int(';')); fnext html; fbreak;};
(dnum | exponent_dnum) => {lex.setTokenPosition(token); tok = T_DNUMBER; fbreak;};
bnum => {

View File

@ -377,6 +377,51 @@ func TestTokens(t *testing.T) {
assert.DeepEqual(t, expected, actual)
}
func TestShebang(t *testing.T) {
src := `#!/usr/bin/env php
<?php
0.1
`
expected := []string{
"#!/usr/bin/env php\n",
"<?php",
"\n",
}
lexer := NewLexer([]byte(src))
lexer.WithFreeFloating = true
lv := &lval{}
actual := []string{}
token := lexer.Lex(lv)
assert.Equal(t, token, int(T_DNUMBER))
for _, tt := range lv.Tkn.FreeFloating {
actual = append(actual, tt.Value)
}
assert.DeepEqual(t, expected, actual)
}
func TestShebangHtml(t *testing.T) {
src := `#!/usr/bin/env php
<br/><?php
0.1
`
lexer := NewLexer([]byte(src))
lexer.WithFreeFloating = true
lv := &lval{}
token := lexer.Lex(lv)
assert.Equal(t, token, int(T_INLINE_HTML))
assert.Equal(t, lv.Tkn.FreeFloating[0].Value, "#!/usr/bin/env php\n")
token = lexer.Lex(lv)
assert.Equal(t, token, int(T_DNUMBER))
}
func TestNumberTokens(t *testing.T) {
src := `<?php
0.1