[#82] add support of shebang
This commit is contained in:
parent
4a518d9e5d
commit
31052588b1
@ -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 {
|
||||
|
@ -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 {
|
||||
|
17655
scanner/scanner.go
17655
scanner/scanner.go
File diff suppressed because it is too large
Load Diff
@ -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 => {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user