2017-11-07 06:21:38 +00:00
|
|
|
%{
|
|
|
|
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// blame: jnml, labs.nic.cz
|
|
|
|
|
2017-12-04 10:40:36 +00:00
|
|
|
package parser
|
2017-11-07 06:21:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-16 17:00:34 +00:00
|
|
|
"bytes"
|
2017-12-03 18:49:18 +00:00
|
|
|
"github.com/z7zmey/php-parser/token"
|
2018-01-02 22:12:28 +00:00
|
|
|
"github.com/z7zmey/php-parser/comment"
|
2017-11-07 06:21:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-12-01 14:04:53 +00:00
|
|
|
INITIAL = iota
|
|
|
|
PHP
|
|
|
|
STRING
|
|
|
|
STRING_VAR
|
|
|
|
STRING_VAR_INDEX
|
|
|
|
STRING_VAR_NAME
|
|
|
|
PROPERTY
|
|
|
|
HEREDOC_END
|
|
|
|
NOWDOC
|
|
|
|
HEREDOC
|
|
|
|
BACKQUOTE
|
2017-11-07 06:21:38 +00:00
|
|
|
)
|
|
|
|
|
2017-12-01 14:04:53 +00:00
|
|
|
var heredocLabel []byte
|
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
func (l *lexer) Lex(lval *yySymType) int {
|
2018-01-06 12:04:02 +00:00
|
|
|
l.comments = nil
|
2018-01-05 15:03:59 +00:00
|
|
|
c := l.Enter()
|
2017-11-07 06:21:38 +00:00
|
|
|
|
|
|
|
%}
|
|
|
|
|
2017-11-20 10:33:18 +00:00
|
|
|
%s PHP STRING STRING_VAR STRING_VAR_INDEX STRING_VAR_NAME PROPERTY HEREDOC_END NOWDOC HEREDOC BACKQUOTE
|
2017-11-07 06:21:38 +00:00
|
|
|
|
|
|
|
%yyb last == '\n' || last = '\0'
|
2017-12-01 14:04:53 +00:00
|
|
|
%yyt l.getCurrentState()
|
2017-11-07 06:21:38 +00:00
|
|
|
%yyc c
|
|
|
|
%yyn c = l.Next()
|
|
|
|
%yym l.Mark()
|
2017-12-17 20:42:40 +00:00
|
|
|
%optioncase-insensitive
|
2017-11-07 06:21:38 +00:00
|
|
|
|
2017-11-16 10:53:21 +00:00
|
|
|
LNUM [0-9]+
|
|
|
|
DNUM ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)
|
|
|
|
HNUM 0x[0-9a-fA-F]+
|
|
|
|
BNUM 0b[01]+
|
|
|
|
EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM})
|
|
|
|
VAR_NAME [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
|
|
|
|
OPERATORS [;:,.\[\]()|\/\^&\+-*=%!~$<>?@]
|
2017-11-16 14:50:55 +00:00
|
|
|
NEW_LINE (\r|\n|\r\n)
|
2017-11-07 06:21:38 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
c = l.Rule0()
|
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<INITIAL>[ \t\n\r]+ lval.token = l.newToken();
|
2017-11-07 06:21:38 +00:00
|
|
|
<INITIAL>.
|
2017-11-28 22:35:21 +00:00
|
|
|
tb := []byte{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if c == -1 {
|
2017-11-29 09:37:16 +00:00
|
|
|
tb = l.TokenBytes(nil);
|
2017-11-28 22:35:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if '?' == rune(c) {
|
|
|
|
tb = l.TokenBytes(nil);
|
|
|
|
if (len(tb) < 2 || tb[len(tb)-1] != '<') {
|
|
|
|
c = l.Next()
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tb = l.ungetN(1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = l.Next()
|
|
|
|
}
|
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_INLINE_HTML
|
2017-11-28 22:35:21 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<INITIAL>\<\?php([ \t]|{NEW_LINE}) l.begin(PHP);lval.token = l.newToken();// return T_OPEN_TAG;
|
|
|
|
<INITIAL>\<\? l.begin(PHP);lval.token = l.newToken();// return T_OPEN_TAG;
|
|
|
|
<INITIAL>\<\?= l.begin(PHP);lval.token = l.newToken(); return T_ECHO;
|
2017-11-16 10:53:21 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>[ \t\n\r]+ lval.token = l.newToken();// return T_WHITESPACE
|
|
|
|
<PHP>\?\>{NEW_LINE}? l.begin(INITIAL);lval.token = l.newToken(); return rune2Class(';');
|
2017-11-20 11:50:30 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>{DNUM}|{EXPONENT_DNUM} lval.token = l.newToken(); return T_DNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
<PHP>{BNUM}
|
|
|
|
tb := l.TokenBytes(nil)
|
|
|
|
i:=2
|
|
|
|
BNUMFOR:for {
|
2017-12-06 10:35:05 +00:00
|
|
|
if i > len(tb)-1 {
|
|
|
|
break BNUMFOR;
|
|
|
|
}
|
2017-11-20 11:50:30 +00:00
|
|
|
switch tb[i] {
|
|
|
|
case '0': i++;
|
|
|
|
default: break BNUMFOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(tb) - i < 64 {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_LNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
} else {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_DNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
}
|
|
|
|
<PHP>{LNUM}
|
|
|
|
if len(l.TokenBytes(nil)) < 20 {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_LNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
} else {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_DNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
}
|
|
|
|
<PHP>{HNUM}
|
|
|
|
tb := l.TokenBytes(nil)
|
|
|
|
i:=2
|
|
|
|
HNUMFOR:for {
|
2017-12-06 10:35:05 +00:00
|
|
|
if i > len(tb)-1 {
|
|
|
|
break HNUMFOR;
|
|
|
|
}
|
2017-11-20 11:50:30 +00:00
|
|
|
switch tb[i] {
|
|
|
|
case '0': i++;
|
|
|
|
default: break HNUMFOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length := len(tb) - i
|
|
|
|
if length < 16 || (length == 16 && tb[i] <= '7') {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_LNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
} else {
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_DNUMBER
|
2017-11-20 11:50:30 +00:00
|
|
|
}
|
2017-11-16 10:53:21 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>abstract lval.token = l.newToken(); return T_ABSTRACT
|
|
|
|
<PHP>array lval.token = l.newToken(); return T_ARRAY
|
|
|
|
<PHP>as lval.token = l.newToken(); return T_AS
|
|
|
|
<PHP>break lval.token = l.newToken(); return T_BREAK
|
|
|
|
<PHP>callable lval.token = l.newToken(); return T_CALLABLE
|
|
|
|
<PHP>case lval.token = l.newToken(); return T_CASE
|
|
|
|
<PHP>catch lval.token = l.newToken(); return T_CATCH
|
|
|
|
<PHP>class lval.token = l.newToken(); return T_CLASS
|
|
|
|
<PHP>clone lval.token = l.newToken(); return T_CLONE
|
|
|
|
<PHP>const lval.token = l.newToken(); return T_CONST;
|
|
|
|
<PHP>continue lval.token = l.newToken(); return T_CONTINUE;
|
|
|
|
<PHP>declare lval.token = l.newToken(); return T_DECLARE;
|
|
|
|
<PHP>default lval.token = l.newToken(); return T_DEFAULT;
|
|
|
|
<PHP>do lval.token = l.newToken(); return T_DO;
|
|
|
|
<PHP>echo lval.token = l.newToken(); return T_ECHO;
|
|
|
|
<PHP>else lval.token = l.newToken(); return T_ELSE;
|
|
|
|
<PHP>elseif lval.token = l.newToken(); return T_ELSEIF;
|
|
|
|
<PHP>empty lval.token = l.newToken(); return T_EMPTY;
|
|
|
|
<PHP>enddeclare lval.token = l.newToken(); return T_ENDDECLARE
|
|
|
|
<PHP>endfor lval.token = l.newToken(); return T_ENDFOR
|
|
|
|
<PHP>endforeach lval.token = l.newToken(); return T_ENDFOREACH
|
|
|
|
<PHP>endif lval.token = l.newToken(); return T_ENDIF
|
|
|
|
<PHP>endswitch lval.token = l.newToken(); return T_ENDSWITCH
|
|
|
|
<PHP>endwhile lval.token = l.newToken(); return T_ENDWHILE
|
|
|
|
<PHP>eval lval.token = l.newToken(); return T_EVAL
|
|
|
|
<PHP>exit|die lval.token = l.newToken(); return T_EXIT
|
|
|
|
<PHP>extends lval.token = l.newToken(); return T_EXTENDS
|
|
|
|
<PHP>final lval.token = l.newToken(); return T_FINAL
|
|
|
|
<PHP>finally lval.token = l.newToken(); return T_FINALLY
|
|
|
|
<PHP>for lval.token = l.newToken(); return T_FOR
|
|
|
|
<PHP>foreach lval.token = l.newToken(); return T_FOREACH
|
|
|
|
<PHP>function|cfunction lval.token = l.newToken(); return T_FUNCTION
|
|
|
|
<PHP>global lval.token = l.newToken(); return T_GLOBAL
|
|
|
|
<PHP>goto lval.token = l.newToken(); return T_GOTO
|
|
|
|
<PHP>if lval.token = l.newToken(); return T_IF
|
|
|
|
<PHP>isset lval.token = l.newToken(); return T_ISSET
|
|
|
|
<PHP>implements lval.token = l.newToken(); return T_IMPLEMENTS
|
|
|
|
<PHP>instanceof lval.token = l.newToken(); return T_INSTANCEOF
|
|
|
|
<PHP>insteadof lval.token = l.newToken(); return T_INSTEADOF
|
|
|
|
<PHP>interface lval.token = l.newToken(); return T_INTERFACE
|
|
|
|
<PHP>list lval.token = l.newToken(); return T_LIST
|
|
|
|
<PHP>namespace lval.token = l.newToken(); return T_NAMESPACE
|
|
|
|
<PHP>private lval.token = l.newToken(); return T_PRIVATE
|
|
|
|
<PHP>public lval.token = l.newToken(); return T_PUBLIC
|
|
|
|
<PHP>print lval.token = l.newToken(); return T_PRINT
|
|
|
|
<PHP>protected lval.token = l.newToken(); return T_PROTECTED
|
|
|
|
<PHP>return lval.token = l.newToken(); return T_RETURN
|
|
|
|
<PHP>static lval.token = l.newToken(); return T_STATIC
|
|
|
|
<PHP>switch lval.token = l.newToken(); return T_SWITCH
|
|
|
|
<PHP>throw lval.token = l.newToken(); return T_THROW
|
|
|
|
<PHP>trait lval.token = l.newToken(); return T_TRAIT
|
|
|
|
<PHP>try lval.token = l.newToken(); return T_TRY
|
|
|
|
<PHP>unset lval.token = l.newToken(); return T_UNSET
|
|
|
|
<PHP>use lval.token = l.newToken(); return T_USE
|
|
|
|
<PHP>var lval.token = l.newToken(); return T_VAR
|
|
|
|
<PHP>while lval.token = l.newToken(); return T_WHILE
|
|
|
|
<PHP>yield[ \t\n\r]+from[^a-zA-Z0-9_\x80-\xff] lval.token = l.newToken(); return T_YIELD_FROM
|
|
|
|
<PHP>yield lval.token = l.newToken(); return T_YIELD
|
|
|
|
<PHP>include lval.token = l.newToken(); return T_INCLUDE
|
|
|
|
<PHP>include_once lval.token = l.newToken(); return T_INCLUDE_ONCE
|
|
|
|
<PHP>require lval.token = l.newToken(); return T_REQUIRE
|
|
|
|
<PHP>require_once lval.token = l.newToken(); return T_REQUIRE_ONCE
|
|
|
|
<PHP>__CLASS__ lval.token = l.newToken(); return T_CLASS_C
|
|
|
|
<PHP>__DIR__ lval.token = l.newToken(); return T_DIR
|
|
|
|
<PHP>__FILE__ lval.token = l.newToken(); return T_FILE
|
|
|
|
<PHP>__FUNCTION__ lval.token = l.newToken(); return T_FUNC_C
|
|
|
|
<PHP>__LINE__ lval.token = l.newToken(); return T_LINE
|
|
|
|
<PHP>__NAMESPACE__ lval.token = l.newToken(); return T_NS_C
|
|
|
|
<PHP>__METHOD__ lval.token = l.newToken(); return T_METHOD_C
|
|
|
|
<PHP>__TRAIT__ lval.token = l.newToken(); return T_TRAIT_C
|
|
|
|
<PHP>__halt_compiler lval.token = l.newToken(); return T_HALT_COMPILER
|
|
|
|
<PHP>\([ \t]*array[ \t]*\) lval.token = l.newToken(); return T_ARRAY_CAST
|
|
|
|
<PHP>\([ \t]*(bool|boolean)[ \t]*\) lval.token = l.newToken(); return T_BOOL_CAST
|
|
|
|
<PHP>\([ \t]*(real|double|float)[ \t]*\) lval.token = l.newToken(); return T_DOUBLE_CAST
|
|
|
|
<PHP>\([ \t]*(int|integer)[ \t]*\) lval.token = l.newToken(); return T_INT_CAST
|
|
|
|
<PHP>\([ \t]*object[ \t]*\) lval.token = l.newToken(); return T_OBJECT_CAST
|
|
|
|
<PHP>\([ \t]*string[ \t]*\) lval.token = l.newToken(); return T_STRING_CAST
|
|
|
|
<PHP>\([ \t]*unset[ \t]*\) lval.token = l.newToken(); return T_UNSET_CAST
|
|
|
|
<PHP>new lval.token = l.newToken(); return T_NEW
|
|
|
|
<PHP>and lval.token = l.newToken(); return T_LOGICAL_AND
|
|
|
|
<PHP>or lval.token = l.newToken(); return T_LOGICAL_OR
|
|
|
|
<PHP>xor lval.token = l.newToken(); return T_LOGICAL_XOR
|
|
|
|
<PHP>\\ lval.token = l.newToken(); return T_NS_SEPARATOR
|
|
|
|
<PHP>\.\.\. lval.token = l.newToken(); return T_ELLIPSIS;
|
|
|
|
<PHP>:: lval.token = l.newToken(); return T_PAAMAYIM_NEKUDOTAYIM; // T_DOUBLE_COLON
|
|
|
|
<PHP>&& lval.token = l.newToken(); return T_BOOLEAN_AND
|
|
|
|
<PHP>\|\| lval.token = l.newToken(); return T_BOOLEAN_OR
|
|
|
|
<PHP>&= lval.token = l.newToken(); return T_AND_EQUAL
|
|
|
|
<PHP>\|= lval.token = l.newToken(); return T_OR_EQUAL
|
|
|
|
<PHP>\.= lval.token = l.newToken(); return T_CONCAT_EQUAL;
|
|
|
|
<PHP>\*= lval.token = l.newToken(); return T_MUL_EQUAL
|
|
|
|
<PHP>\*\*= lval.token = l.newToken(); return T_POW_EQUAL
|
|
|
|
<PHP>[/]= lval.token = l.newToken(); return T_DIV_EQUAL;
|
|
|
|
<PHP>\+= lval.token = l.newToken(); return T_PLUS_EQUAL
|
|
|
|
<PHP>-= lval.token = l.newToken(); return T_MINUS_EQUAL
|
|
|
|
<PHP>\^= lval.token = l.newToken(); return T_XOR_EQUAL
|
|
|
|
<PHP>%= lval.token = l.newToken(); return T_MOD_EQUAL
|
|
|
|
<PHP>-- lval.token = l.newToken(); return T_DEC;
|
|
|
|
<PHP>\+\+ lval.token = l.newToken(); return T_INC
|
|
|
|
<PHP>=> lval.token = l.newToken(); return T_DOUBLE_ARROW;
|
|
|
|
<PHP>\<=\> lval.token = l.newToken(); return T_SPACESHIP
|
|
|
|
<PHP>\!=|\<\> lval.token = l.newToken(); return T_IS_NOT_EQUAL
|
|
|
|
<PHP>\!== lval.token = l.newToken(); return T_IS_NOT_IDENTICAL
|
|
|
|
<PHP>== lval.token = l.newToken(); return T_IS_EQUAL
|
|
|
|
<PHP>=== lval.token = l.newToken(); return T_IS_IDENTICAL
|
|
|
|
<PHP>\<\<= lval.token = l.newToken(); return T_SL_EQUAL
|
|
|
|
<PHP>\>\>= lval.token = l.newToken(); return T_SR_EQUAL
|
|
|
|
<PHP>\>= lval.token = l.newToken(); return T_IS_GREATER_OR_EQUAL
|
|
|
|
<PHP>\<= lval.token = l.newToken(); return T_IS_SMALLER_OR_EQUAL
|
|
|
|
<PHP>\*\* lval.token = l.newToken(); return T_POW
|
|
|
|
<PHP>\<\< lval.token = l.newToken(); return T_SL
|
|
|
|
<PHP>\>\> lval.token = l.newToken(); return T_SR
|
|
|
|
<PHP>\?\? lval.token = l.newToken(); return T_COALESCE
|
|
|
|
<PHP>(#|[/][/]).*{NEW_LINE} lval.token = l.newToken();// return T_COMMENT; // TODO: handle ?>
|
2018-01-02 11:53:55 +00:00
|
|
|
<PHP>([/][*])|([/][*][*])
|
|
|
|
tb := l.TokenBytes(nil)
|
|
|
|
is_doc_comment := false
|
|
|
|
if len(tb) > 2 {
|
|
|
|
is_doc_comment = true
|
|
|
|
l.phpDocComment = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if c == -1 {
|
|
|
|
break; // TODO: Unterminated comment starting line %d
|
|
|
|
}
|
|
|
|
|
|
|
|
p := c
|
|
|
|
c = l.Next()
|
|
|
|
|
|
|
|
if rune(p) == '*' && rune(c) == '/' {
|
|
|
|
c = l.Next()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil)))
|
|
|
|
if is_doc_comment {
|
|
|
|
l.phpDocComment = string(l.TokenBytes(nil))
|
2018-01-06 12:04:02 +00:00
|
|
|
l.addComment(comment.NewDocComment(string(l.TokenBytes(nil))))
|
2018-01-02 11:53:55 +00:00
|
|
|
// return T_DOC_COMMENT
|
|
|
|
} else {
|
2018-01-06 12:04:02 +00:00
|
|
|
l.addComment(comment.NewPlainComment(string(l.TokenBytes(nil))))
|
2018-01-02 11:53:55 +00:00
|
|
|
// return T_COMMENT
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>{OPERATORS} lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
2017-12-03 18:49:18 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>\{ l.pushState(PHP); lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
|
|
|
<PHP>\} l.popState(); lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0])); l.phpDocComment = ""
|
|
|
|
<PHP>\${VAR_NAME} lval.token = l.newToken(); return T_VARIABLE
|
|
|
|
<PHP>{VAR_NAME} lval.token = l.newToken(); return T_STRING
|
2017-12-03 18:49:18 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>-> l.begin(PROPERTY);lval.token = l.newToken(); return T_OBJECT_OPERATOR;
|
|
|
|
<PROPERTY>[ \t\n\r]+ lval.token = l.newToken(); return T_WHITESPACE;
|
|
|
|
<PROPERTY>-> lval.token = l.newToken(); return T_OBJECT_OPERATOR;
|
|
|
|
<PROPERTY>{VAR_NAME} l.begin(PHP);lval.token = l.newToken(); return T_STRING;
|
2017-12-01 14:04:53 +00:00
|
|
|
<PROPERTY>. l.ungetN(1);l.begin(PHP)
|
2017-11-16 10:53:21 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>[\']([^\\\']*([\\].)*)*[\'] lval.token = l.newToken(); return T_CONSTANT_ENCAPSED_STRING;
|
2017-11-16 14:50:55 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<PHP>` l.begin(BACKQUOTE); lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
|
|
|
<BACKQUOTE>` l.begin(PHP); lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
2017-11-20 10:33:18 +00:00
|
|
|
|
2017-11-16 14:50:55 +00:00
|
|
|
<PHP>[b]?\<\<\<[ \t]*({VAR_NAME}|([']{VAR_NAME}['])|(["]{VAR_NAME}["])){NEW_LINE}
|
|
|
|
tb := l.TokenBytes(nil)
|
2017-11-16 17:00:34 +00:00
|
|
|
binPrefix := 0
|
|
|
|
if tb[0] == 'b' {
|
|
|
|
binPrefix = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
lblFirst := 3 + binPrefix
|
|
|
|
lblLast := len(tb)-2
|
|
|
|
if tb[lblLast] == '\r' {
|
|
|
|
lblLast--
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if tb[lblFirst] == ' ' || tb[lblFirst] == '\t' {
|
|
|
|
lblFirst++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tb[lblFirst] {
|
|
|
|
case '\'' :
|
|
|
|
lblFirst++
|
|
|
|
lblLast--
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(NOWDOC)
|
2017-11-16 17:00:34 +00:00
|
|
|
case '"' :
|
|
|
|
lblFirst++
|
|
|
|
lblLast--
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(HEREDOC)
|
2017-11-16 17:00:34 +00:00
|
|
|
default:
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(HEREDOC)
|
2017-11-16 17:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 10:22:03 +00:00
|
|
|
heredocLabel = make([]byte, lblLast - lblFirst + 1)
|
|
|
|
copy(heredocLabel, tb[lblFirst:lblLast+1])
|
2017-11-16 17:00:34 +00:00
|
|
|
|
|
|
|
ungetCnt := len(heredocLabel)
|
|
|
|
searchLabelAhead := []byte{}
|
|
|
|
for i := 0; i < len(heredocLabel); i++ {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
searchLabelAhead = append(searchLabelAhead, byte(rune(c)))
|
|
|
|
c = l.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(heredocLabel, searchLabelAhead) && ';' == rune(c) {
|
|
|
|
ungetCnt++
|
|
|
|
c = l.Next()
|
|
|
|
if '\n' == rune(c) || '\r' == rune(c) {
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(HEREDOC_END)
|
2017-11-16 17:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.ungetN(ungetCnt)
|
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_START_HEREDOC
|
2017-11-16 17:00:34 +00:00
|
|
|
|
2017-11-16 17:58:46 +00:00
|
|
|
<NOWDOC>.
|
|
|
|
searchLabel := []byte{}
|
|
|
|
tb := []byte{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if '\n' == rune(c) || '\r' == rune(c) {
|
|
|
|
if bytes.Equal(append(heredocLabel, ';'), searchLabel) {
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(HEREDOC_END)
|
2017-11-16 17:58:46 +00:00
|
|
|
tb = l.ungetN(len(heredocLabel)+1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchLabel = []byte{}
|
|
|
|
} else {
|
|
|
|
searchLabel = append(searchLabel, byte(rune(c)))
|
|
|
|
}
|
|
|
|
|
|
|
|
c = l.Next()
|
|
|
|
}
|
2017-11-20 10:22:03 +00:00
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-16 17:58:46 +00:00
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
<HEREDOC_END>{VAR_NAME}\; l.begin(PHP);lval.token = token.NewToken(l.handleNewLine(l.ungetN(1))); return T_END_HEREDOC
|
2017-11-16 14:50:55 +00:00
|
|
|
|
2017-11-13 21:55:09 +00:00
|
|
|
<PHP>[b]?[\"]
|
|
|
|
binPrefix := l.TokenBytes(nil)[0] == 'b'
|
2017-11-15 17:47:09 +00:00
|
|
|
|
2017-11-23 15:33:47 +00:00
|
|
|
beginString := func() int {
|
2017-11-15 17:47:09 +00:00
|
|
|
cnt := 1; if (binPrefix) {cnt = 2}
|
|
|
|
|
|
|
|
l.ungetN(len(l.TokenBytes(nil))-cnt)
|
|
|
|
tokenBytes := l.TokenBytes(nil)[:cnt]
|
2017-12-01 14:04:53 +00:00
|
|
|
l.pushState(STRING)
|
2017-11-23 15:33:47 +00:00
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tokenBytes)); return rune2Class('"')
|
2017-11-15 17:47:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 21:55:09 +00:00
|
|
|
F:for {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c {
|
|
|
|
case '"' :
|
|
|
|
c = l.Next();
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken(); return T_CONSTANT_ENCAPSED_STRING
|
2017-11-13 21:55:09 +00:00
|
|
|
break F;
|
2017-11-15 17:47:09 +00:00
|
|
|
|
2017-11-13 21:55:09 +00:00
|
|
|
case '$':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '{' || c >= 'A' && c <= 'Z' || c == '_' || c >= 'a' && c <= 'z' || c >= '\u007f' && c <= 'ÿ' {
|
2017-11-23 15:33:47 +00:00
|
|
|
return beginString()
|
2017-11-13 21:55:09 +00:00
|
|
|
break F;
|
2017-11-15 17:47:09 +00:00
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
2017-11-13 21:55:09 +00:00
|
|
|
case '{':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '$' {
|
2017-11-23 15:33:47 +00:00
|
|
|
return beginString()
|
2017-11-13 21:55:09 +00:00
|
|
|
break F;
|
2017-11-15 17:47:09 +00:00
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
2017-11-13 21:55:09 +00:00
|
|
|
case '\\':
|
|
|
|
c = l.Next();
|
|
|
|
}
|
|
|
|
|
2017-11-15 22:05:44 +00:00
|
|
|
c = l.Next()
|
|
|
|
}
|
2017-11-07 06:21:38 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<STRING>\" l.popState(); lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
2017-12-03 18:49:18 +00:00
|
|
|
<STRING,HEREDOC,BACKQUOTE>\{\$ lval.token = token.NewToken(l.handleNewLine(l.ungetN(1))); l.pushState(PHP); return T_CURLY_OPEN
|
2018-01-05 15:03:59 +00:00
|
|
|
<STRING,HEREDOC,BACKQUOTE>\$\{ l.pushState(STRING_VAR_NAME);lval.token = l.newToken(); return T_DOLLAR_OPEN_CURLY_BRACES
|
2017-12-01 14:04:53 +00:00
|
|
|
<STRING,HEREDOC,BACKQUOTE>\$ l.ungetN(1);l.pushState(STRING_VAR)
|
2017-12-03 07:17:05 +00:00
|
|
|
<STRING>.|[ \t\n\r]
|
2017-11-15 22:05:44 +00:00
|
|
|
F1:for {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
2017-11-12 11:13:31 +00:00
|
|
|
|
2017-11-15 22:05:44 +00:00
|
|
|
switch c {
|
|
|
|
case '"' :
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken();
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-15 22:05:44 +00:00
|
|
|
break F1;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '{' || c >= 'A' && c <= 'Z' || c == '_' || c >= 'a' && c <= 'z' || c >= '\u007f' && c <= 'ÿ' {
|
|
|
|
l.ungetN(1)
|
|
|
|
tb := l.TokenBytes(nil)
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb[:len(tb)-1]));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-15 22:05:44 +00:00
|
|
|
break F1;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '$' {
|
|
|
|
l.ungetN(1)
|
|
|
|
tb := l.TokenBytes(nil)
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb[:len(tb)-1]));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-15 22:05:44 +00:00
|
|
|
break F1;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
|
|
|
case '\\':
|
2017-11-20 10:33:18 +00:00
|
|
|
c = l.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
c = l.Next()
|
|
|
|
}
|
|
|
|
|
2017-12-03 21:29:17 +00:00
|
|
|
<BACKQUOTE>.|[ \t\n\r]
|
2017-11-20 10:33:18 +00:00
|
|
|
F2:for {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c {
|
2017-12-03 07:17:05 +00:00
|
|
|
case '`' :
|
2018-01-05 15:03:59 +00:00
|
|
|
lval.token = l.newToken();
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-20 10:33:18 +00:00
|
|
|
break F2;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '{' || c >= 'A' && c <= 'Z' || c == '_' || c >= 'a' && c <= 'z' || c >= '\u007f' && c <= 'ÿ' {
|
|
|
|
l.ungetN(1)
|
|
|
|
tb := l.TokenBytes(nil)
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb[:len(tb)-1]));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-20 10:33:18 +00:00
|
|
|
break F2;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '$' {
|
|
|
|
l.ungetN(1)
|
|
|
|
tb := l.TokenBytes(nil)
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb[:len(tb)-1]));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-20 10:33:18 +00:00
|
|
|
break F2;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
|
|
|
|
case '\\':
|
2017-11-15 22:05:44 +00:00
|
|
|
c = l.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
c = l.Next()
|
|
|
|
}
|
2017-11-07 06:21:38 +00:00
|
|
|
|
2017-11-20 10:22:03 +00:00
|
|
|
<HEREDOC>.|[ \t\n\r]
|
|
|
|
searchLabel := []byte{}
|
|
|
|
tb := []byte{}
|
|
|
|
|
|
|
|
HEREDOCFOR:for {
|
|
|
|
if c == -1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c {
|
|
|
|
case '\n': fallthrough
|
|
|
|
case '\r':
|
2017-11-22 10:03:12 +00:00
|
|
|
if bytes.Equal(append(heredocLabel, ';'), searchLabel) { // TODO handle ';' as optional
|
2017-12-01 14:04:53 +00:00
|
|
|
l.begin(HEREDOC_END)
|
2017-11-20 10:22:03 +00:00
|
|
|
tb = l.ungetN(len(heredocLabel)+1)
|
|
|
|
break HEREDOCFOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchLabel = []byte{}
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '{' || c >= 'A' && c <= 'Z' || c == '_' || c >= 'a' && c <= 'z' || c >= '\u007f' && c <= 'ÿ' {
|
|
|
|
tb = l.ungetN(1)
|
|
|
|
break HEREDOCFOR;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
searchLabel = []byte{}
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
c = l.Next();
|
|
|
|
if rune(c) == '$' {
|
|
|
|
tb = l.ungetN(1)
|
|
|
|
break HEREDOCFOR;
|
|
|
|
}
|
|
|
|
l.ungetN(0)
|
|
|
|
searchLabel = []byte{}
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
c = l.Next();
|
|
|
|
searchLabel = []byte{}
|
|
|
|
|
|
|
|
default:
|
|
|
|
searchLabel = append(searchLabel, byte(rune(c)))
|
|
|
|
}
|
|
|
|
|
|
|
|
c = l.Next()
|
|
|
|
}
|
2017-11-23 15:33:47 +00:00
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
lval.token = token.NewToken(l.handleNewLine(tb));
|
2017-12-03 07:17:05 +00:00
|
|
|
return T_ENCAPSED_AND_WHITESPACE
|
2017-11-20 10:22:03 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<STRING_VAR>\${VAR_NAME} lval.token = l.newToken(); return T_VARIABLE
|
2017-12-03 18:49:18 +00:00
|
|
|
<STRING_VAR>->{VAR_NAME} lval.token = token.NewToken(l.handleNewLine(l.ungetN(len(l.TokenBytes(nil))-2))); return T_OBJECT_OPERATOR
|
2018-01-05 15:03:59 +00:00
|
|
|
<STRING_VAR>{VAR_NAME} l.popState();lval.token = l.newToken(); return T_STRING
|
|
|
|
<STRING_VAR>\[ l.pushState(STRING_VAR_INDEX);lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
2017-12-01 14:04:53 +00:00
|
|
|
<STRING_VAR>.|[ \t\n\r] l.ungetN(1);l.popState()
|
2017-11-13 07:48:57 +00:00
|
|
|
|
2018-01-05 15:03:59 +00:00
|
|
|
<STRING_VAR_INDEX>{LNUM}|{HNUM}|{BNUM} lval.token = l.newToken(); return T_NUM_STRING
|
|
|
|
<STRING_VAR_INDEX>\${VAR_NAME} lval.token = l.newToken(); return T_VARIABLE
|
|
|
|
<STRING_VAR_INDEX>{VAR_NAME} lval.token = l.newToken(); return T_STRING
|
|
|
|
<STRING_VAR_INDEX>\] l.popState(); l.popState();lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
|
|
|
<STRING_VAR_INDEX>[ \n\r\t\\'#] l.popState(); l.popState();lval.token = l.newToken(); return T_ENCAPSED_AND_WHITESPACE
|
|
|
|
<STRING_VAR_INDEX>{OPERATORS} lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
|
|
|
<STRING_VAR_INDEX>. lval.token = l.newToken(); return rune2Class(rune(l.TokenBytes(nil)[0]))
|
2017-11-13 07:48:57 +00:00
|
|
|
|
2017-12-03 18:49:18 +00:00
|
|
|
<STRING_VAR_NAME>{VAR_NAME}[\[\}] l.popState();l.pushState(PHP);lval.token = token.NewToken(l.handleNewLine(l.ungetN(1))); return T_STRING_VARNAME
|
2017-12-01 14:04:53 +00:00
|
|
|
<STRING_VAR_NAME>. l.ungetN(1);l.popState();l.pushState(PHP)
|
2017-11-15 22:05:44 +00:00
|
|
|
|
2017-11-07 06:21:38 +00:00
|
|
|
%%
|
|
|
|
if c, ok := l.Abort(); ok { return int(c) }
|
|
|
|
goto yyAction
|
|
|
|
}
|