From 3bf37a6785f30741636dfa96fc725489fc39a6e3 Mon Sep 17 00:00:00 2001 From: z7zmey Date: Thu, 30 Nov 2017 01:25:07 +0200 Subject: [PATCH] parse constant and string --- lexer.go | 2 +- lexer.l | 3 +- parser.go | 2408 +++++++++++++++++++++++++++++++---------------------- parser.y | 91 +- 4 files changed, 1503 insertions(+), 1001 deletions(-) diff --git a/lexer.go b/lexer.go index b0821ab..a90231d 100644 --- a/lexer.go +++ b/lexer.go @@ -8761,7 +8761,6 @@ yyrule145: // [b]?[\"] l.ungetN(len(l.TokenBytes(nil)) - cnt) tokenBytes := l.TokenBytes(nil)[:cnt] - fmt.Println(string(tokenBytes)) // TODO: RETURN TOKEN pushState(STRING) lval.token = string(tokenBytes) return rune2Class('"') @@ -8810,6 +8809,7 @@ yyrule146: // \" yyrule147: // \{\$ { lval.token = string(l.ungetN(1)) + pushState(PHP) return T_CURLY_OPEN goto yystate0 } diff --git a/lexer.l b/lexer.l index 73c661f..5523809 100644 --- a/lexer.l +++ b/lexer.l @@ -453,7 +453,6 @@ NEW_LINE (\r|\n|\r\n) l.ungetN(len(l.TokenBytes(nil))-cnt) tokenBytes := l.TokenBytes(nil)[:cnt] - fmt.Println(string(tokenBytes)) // TODO: RETURN TOKEN pushState(STRING) lval.token = string(tokenBytes); return rune2Class('"') @@ -494,7 +493,7 @@ NEW_LINE (\r|\n|\r\n) } \" popState(); lval.token = "\""; return rune2Class(rune(l.TokenBytes(nil)[0])) -\{\$ lval.token = string(l.ungetN(1)); return T_CURLY_OPEN +\{\$ lval.token = string(l.ungetN(1)); pushState(PHP); return T_CURLY_OPEN \$\{ pushState(STRING_VAR_NAME);lval.token = string(l.TokenBytes(nil)); return T_DOLLAR_OPEN_CURLY_BRACES \$ l.ungetN(1);pushState(STRING_VAR) . diff --git a/parser.go b/parser.go index c900301..fcfcdb2 100644 --- a/parser.go +++ b/parser.go @@ -358,6 +358,7 @@ var yyToknames = [...]string{ "'('", "')'", "']'", + "'\"'", "'$'", } var yyStatenames = [...]string{} @@ -366,12 +367,12 @@ const yyEofCode = 1 const yyErrCode = 2 const yyInitialStackSize = 16 -//line parser.y:903 +//line parser.y:984 const src = ` trait_method_reference %type absolute_trait_method_reference %type method_body +%type scalar +%type encaps_list +%type encaps_var +%type encaps_var_offset +%type constant +%type class_name +%type variable_class_name +%type dereferencable +%type dereferencable_scalar %% @@ -866,7 +875,7 @@ expr_without_variable: | T_BOOL_CAST expr { $$ = Node("CastBool").append($2); } | T_UNSET_CAST expr { $$ = Node("CastUnset").append($2); } | '@' expr { $$ = Node("Silence").append($2); } - + | scalar { $$ = $1; } | T_PRINT expr { $$ = Node("Print").append($2); } | T_YIELD { $$ = Node("Yield"); } | T_YIELD expr { $$ = Node("Yield").append($2); } @@ -874,6 +883,78 @@ expr_without_variable: | T_YIELD_FROM expr { $$ = Node("YieldFrom").append($2); } ; +scalar: + T_LNUMBER { $$ = Node("Scalar").append(Node("Lnumber").attribute("value", $1)) } + | T_DNUMBER { $$ = Node("Scalar").append(Node("Dnumber").attribute("value", $1)) } + | T_LINE { $$ = Node("Scalar").append(Node("__LINE__")) } + | T_FILE { $$ = Node("Scalar").append(Node("__FILE__")) } + | T_DIR { $$ = Node("Scalar").append(Node("__DIR__")) } + | T_TRAIT_C { $$ = Node("Scalar").append(Node("__TRAIT__")) } + | T_METHOD_C { $$ = Node("Scalar").append(Node("__METHOD__")); } + | T_FUNC_C { $$ = Node("Scalar").append(Node("__FUNCTION__")); } + | T_NS_C { $$ = Node("Scalar").append(Node("__NAMESPACE__")); } + | T_CLASS_C { $$ = Node("Scalar").append(Node("__CLASS__")); } + | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC + { $$ = Node("Scalar").append(Node("Heredoc").attribute("value", $2)) } + | T_START_HEREDOC T_END_HEREDOC + { $$ = Node("Scalar").append(Node("Heredoc")) } + | '"' encaps_list '"' { $$ = $2; } + | T_START_HEREDOC encaps_list T_END_HEREDOC { $$ = $2; } + | dereferencable_scalar { $$ = $1; } + | constant { $$ = $1; } +; + +encaps_list: + encaps_list encaps_var { $$ = $1.append($2) } + | encaps_list T_ENCAPSED_AND_WHITESPACE { $$ = $1.append(Node("String").attribute("value", $2)) } + | encaps_var { $$ = Node("EncapsList").append($1) } + | T_ENCAPSED_AND_WHITESPACE encaps_var { $$ = Node("EncapsList").append(Node("String").attribute("value", $1)).append($2) } +; +encaps_var: + T_VARIABLE { $$ = Node("Variable").attribute("value", $1) } + | T_VARIABLE '[' encaps_var_offset ']' { $$ = Node("Variable").attribute("value", $1).append(Node("offset").append($3)) } + | T_VARIABLE T_OBJECT_OPERATOR T_STRING { $$ = Node("Variable").attribute("value", $1).append(Node("property").attribute("value", $3)) } + | T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Node("Variable").append(Node("expr").append($2)) } + | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = Node("Variable").attribute("value", $2) } + | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}' + { $$ = Node("Variable").attribute("value", $2).append(Node("offset").append($4)) } + | T_CURLY_OPEN variable '}' { $$ = $2; } +; +encaps_var_offset: + T_STRING { $$ = Node("OffsetString").attribute("value", $1) } + | T_NUM_STRING { $$ = Node("OffsetNumString").attribute("value", $1) } + | '-' T_NUM_STRING { $$ = Node("OffsetNegateNumString").attribute("value", $2) } + | T_VARIABLE { $$ = Node("OffsetVariable").attribute("value", $1) } +; + +constant: + name { $$ = Node("Const").append($1) } + | class_name T_PAAMAYIM_NEKUDOTAYIM identifier { $$ = Node("Const").append($1).append($3) } + | variable_class_name T_PAAMAYIM_NEKUDOTAYIM identifier + { $$ = Node("Const").append($1).append($3) } +; + +class_name: + T_STATIC { $$ = Node("Static") } + | name { $$ = $1; } +; + +variable_class_name: + dereferencable { $$ = $1; } +; + +dereferencable: + variable { $$ = $1; } + | '(' expr ')' { $$ = $2; } + | dereferencable_scalar { $$ = $1; } +; + +dereferencable_scalar: + T_ARRAY '(' array_pair_list ')' { $$ = $3; } + | '[' array_pair_list ']' { $$ = $2; } + | T_CONSTANT_ENCAPSED_STRING { $$ = Node("String").attribute("value", $1) } +; + expr: variable { $$ = $1; } | expr_without_variable { $$ = $1; } @@ -894,7 +975,7 @@ variable: simple_variable: T_VARIABLE { $$ = Node("Variable").attribute("name", $1); } - | '$' '{' expr '}' { $$ = $3; } + | '$' '{' expr '}' { $$ = Node("Variable").append($3); } | '$' simple_variable { $$ = Node("Variable").append($2); } ; @@ -904,9 +985,9 @@ simple_variable: const src = `