From 1bc9802382a027b04c3bf9ba6313fd31f4b0c481 Mon Sep 17 00:00:00 2001 From: z7zmey Date: Wed, 6 Dec 2017 23:10:16 +0200 Subject: [PATCH] echo node --- node/stmt/echo.go | 34 +++++++++ parser/parser.go | 182 +++++++++++++++++++++++----------------------- parser/parser.y | 12 +-- 3 files changed, 131 insertions(+), 97 deletions(-) create mode 100644 node/stmt/echo.go diff --git a/node/stmt/echo.go b/node/stmt/echo.go new file mode 100644 index 0000000..d7938a5 --- /dev/null +++ b/node/stmt/echo.go @@ -0,0 +1,34 @@ +package stmt + +import ( + "fmt" + "io" + + "github.com/z7zmey/php-parser/node" + "github.com/z7zmey/php-parser/token" +) + +type Echo struct { + node.SimpleNode + token token.Token + stmts []node.Node +} + +func NewEcho(token token.Token, stmts []node.Node) node.Node { + return Echo{ + node.SimpleNode{Name: "Echo", Attributes: make(map[string]string)}, + token, + stmts, + } +} + +func (n Echo) Print(out io.Writer, indent string) { + fmt.Fprintf(out, "\n%v%v [%d %d] %q", indent, n.Name, n.token.StartLine, n.token.EndLine, n.token.Value) + + if n.stmts != nil { + fmt.Fprintf(out, "\n%vstmts:", indent+" ") + for _, nn := range n.stmts { + nn.Print(out, indent+" ") + } + } +} diff --git a/parser/parser.go b/parser/parser.go index 5e3f57e..d1351f7 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -601,12 +601,12 @@ var yyAct = [...]int{ 229, 308, 812, 824, 810, 825, 642, 691, 231, 766, 718, 831, 669, 633, 659, 544, 255, 256, 118, 803, 842, 628, 564, 830, 554, 813, 578, 846, 833, 409, - 855, 1, 841, 687, 688, 689, 686, 685, 684, 800, - 864, 854, 666, 163, 165, 166, 829, 178, 255, 256, - 863, 250, 37, 681, 872, 679, 873, 678, 868, 164, - 877, 221, 223, 835, 861, 848, 879, 42, 321, 912, - 804, 748, 855, 529, 782, 878, 570, 41, 882, 365, - 148, 144, 141, 854, 888, 889, 651, 15, 890, 87, + 855, 1, 841, 687, 688, 689, 686, 685, 684, 148, + 864, 854, 800, 163, 165, 166, 666, 178, 255, 256, + 863, 829, 250, 37, 872, 681, 873, 679, 868, 164, + 877, 678, 221, 223, 861, 835, 879, 848, 42, 321, + 912, 804, 855, 748, 782, 878, 529, 570, 882, 41, + 365, 144, 141, 854, 888, 889, 651, 15, 890, 87, 88, 89, 90, 91, 14, 519, 606, 266, 73, 74, 48, 75, 47, 107, 896, 893, 49, 82, 897, 80, 898, 902, 901, 900, 71, 578, 247, 61, 264, 60, @@ -1365,11 +1365,11 @@ var yyPgo = [...]int{ 899, 898, 111, 98, 197, 897, 896, 894, 8, 889, 81, 43, 887, 55, 47, 886, 883, 882, 880, 877, 97, 876, 875, 103, 541, 1, 874, 867, 31, 866, - 862, 861, 860, 859, 7, 19, 38, 24, 857, 856, - 83, 853, 851, 10, 4, 850, 849, 848, 86, 847, - 845, 18, 843, 842, 841, 44, 42, 32, 41, 2, - 16, 837, 835, 833, 5, 786, 832, 664, 831, 28, - 826, 822, 819, 90, 811, 37, 12, + 862, 861, 860, 7, 19, 38, 24, 859, 857, 83, + 856, 853, 10, 4, 851, 850, 849, 86, 848, 847, + 18, 845, 843, 842, 44, 42, 32, 41, 2, 16, + 841, 837, 835, 5, 786, 833, 664, 832, 28, 831, + 826, 822, 90, 819, 811, 37, 12, } var yyR1 = [...]int{ @@ -1381,27 +1381,27 @@ var yyR1 = [...]int{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 73, 73, - 119, 119, 9, 9, 9, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 115, - 115, 16, 16, 19, 19, 125, 125, 17, 17, 22, - 22, 74, 74, 18, 18, 21, 21, 20, 20, 123, - 123, 75, 75, 24, 24, 24, 24, 24, 24, 10, + 118, 118, 9, 9, 9, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 114, + 114, 16, 16, 19, 19, 125, 125, 17, 17, 22, + 22, 74, 74, 18, 18, 21, 21, 20, 20, 122, + 122, 75, 75, 24, 24, 24, 24, 24, 24, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 121, 121, 120, 120, 32, 32, 83, - 83, 33, 11, 1, 1, 2, 2, 12, 12, 116, - 116, 4, 4, 13, 14, 35, 35, 15, 15, 86, - 86, 29, 29, 29, 29, 28, 28, 30, 30, 31, - 31, 79, 79, 79, 79, 87, 87, 87, 126, 126, - 27, 27, 88, 88, 76, 76, 99, 99, 77, 77, - 84, 84, 89, 89, 36, 36, 37, 37, 110, 110, - 108, 108, 108, 109, 109, 90, 90, 91, 91, 38, - 38, 80, 80, 40, 81, 81, 41, 41, 85, 85, - 42, 42, 42, 42, 94, 94, 95, 95, 95, 100, - 100, 43, 43, 44, 45, 45, 45, 45, 47, 47, - 46, 96, 96, 111, 111, 112, 112, 113, 113, 114, - 114, 114, 114, 114, 114, 92, 92, 48, 48, 122, - 122, 93, 23, 82, 82, 49, 78, 78, 97, 97, + 10, 10, 10, 120, 120, 119, 119, 32, 32, 82, + 82, 33, 11, 1, 1, 2, 2, 12, 12, 115, + 115, 4, 4, 13, 14, 35, 35, 15, 15, 85, + 85, 29, 29, 29, 29, 28, 28, 30, 30, 31, + 31, 79, 79, 79, 79, 86, 86, 86, 126, 126, + 27, 27, 87, 87, 76, 76, 98, 98, 77, 77, + 83, 83, 88, 88, 36, 36, 37, 37, 109, 109, + 107, 107, 107, 108, 108, 89, 89, 90, 90, 38, + 38, 80, 80, 40, 81, 81, 41, 41, 84, 84, + 42, 42, 42, 42, 93, 93, 94, 94, 94, 99, + 99, 43, 43, 44, 45, 45, 45, 45, 47, 47, + 46, 95, 95, 110, 110, 111, 111, 112, 112, 113, + 113, 113, 113, 113, 113, 91, 91, 48, 48, 121, + 121, 92, 23, 123, 123, 49, 78, 78, 96, 96, 51, 50, 50, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, @@ -1410,18 +1410,18 @@ var yyR1 = [...]int{ 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 3, - 3, 101, 101, 102, 102, 58, 58, 59, 59, 59, - 59, 52, 52, 53, 53, 56, 56, 118, 118, 118, - 98, 98, 63, 63, 63, 57, 57, 57, 57, 57, + 3, 100, 100, 101, 101, 58, 58, 59, 59, 59, + 59, 52, 52, 53, 53, 56, 56, 117, 117, 117, + 97, 97, 63, 63, 63, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 64, 64, 64, 25, 25, 26, 26, 62, 65, 65, 65, 66, 66, 66, 67, 67, 67, 67, 67, 67, 34, 34, 34, 54, 54, 54, 68, 68, 69, 69, 69, 69, 69, 69, 60, 60, 60, 61, 61, - 61, 105, 106, 106, 104, 104, 103, 103, 103, 103, - 103, 103, 117, 117, 117, 117, 70, 70, 70, 70, + 61, 104, 105, 105, 103, 103, 102, 102, 102, 102, + 102, 102, 116, 116, 116, 116, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 55, 55, 55, - 55, 55, 55, 55, 72, 72, 107, + 55, 55, 55, 55, 72, 72, 106, } var yyR2 = [...]int{ @@ -1480,18 +1480,18 @@ var yyChk = [...]int{ -1000, -124, -73, -8, -10, -11, -12, -13, -14, 127, 154, 120, 114, 164, -76, -77, 97, 96, 99, 106, 110, 111, 115, 122, 76, 95, 90, -25, 124, 101, - 103, 163, 116, 119, 112, 86, 113, -116, 128, 129, - 130, -88, -99, -34, -39, -4, 83, -67, -68, -65, + 103, 163, 116, 119, 112, 86, 113, -115, 128, 129, + 130, -87, -98, -34, -39, -4, 83, -67, -68, -65, 134, 69, 71, 58, 59, 49, 50, 55, 57, 161, -50, -55, 60, 61, 62, 63, 64, 65, 66, 82, 67, -57, 160, 13, 14, 16, 77, 78, -54, -64, -59, -52, -62, -63, 70, 125, 126, 4, 5, 6, 7, 8, 84, 85, 141, 142, 156, 138, 139, 140, 155, 137, 149, 159, 88, 167, -9, -66, 135, 94, - -119, 157, 161, 157, -119, 164, 86, -19, -115, -74, - -119, 157, 113, 114, -20, -21, -123, -23, 86, -75, + -118, 157, 161, 157, -118, 164, 86, -19, -114, -74, + -118, 157, 113, 114, -20, -21, -122, -23, 86, -75, 161, -10, 113, 154, 161, 161, -26, -25, 76, -26, - -26, -80, -40, -54, -81, 113, -41, 88, -82, -49, + -26, -80, -40, -54, -81, 113, -41, 88, -123, -49, -25, 163, 33, 34, 10, 12, 11, 35, 37, 36, 51, 49, 50, 52, 68, 53, 54, 47, 48, 40, 41, 38, 39, 43, 44, 45, 46, 42, 56, 30, @@ -1499,26 +1499,26 @@ var yyChk = [...]int{ 128, -4, 86, 86, 86, 73, 74, 73, 75, 74, 17, 18, 19, 20, 29, 21, 22, 23, 24, 25, 26, 27, 28, 58, 59, 161, 69, 164, 133, 161, - -105, -104, -106, -103, -25, 37, 134, -25, -34, -64, + -104, -103, -105, -102, -25, 37, 134, -25, -34, -64, 161, -63, 76, 69, -34, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -56, 161, -25, - -118, 93, -117, -70, 88, 151, 152, -25, -25, -25, + -117, 93, -116, -70, 88, 151, 152, -25, -25, -25, 69, 153, 153, -53, -51, -52, -69, 128, -9, -54, - 161, 161, -25, -25, 161, -25, -25, 93, 150, -117, - -117, 93, 164, -54, -90, 161, -90, 161, 157, -119, - 162, -119, 163, 164, -73, 163, -16, -74, -119, 157, - 163, 9, 157, 105, -119, -21, 163, 9, 17, -24, + 161, 161, -25, -25, 161, -25, -25, 93, 150, -116, + -116, 93, 164, -54, -89, 161, -89, 161, 157, -118, + 162, -118, 163, 164, -73, 163, -16, -74, -118, 157, + 163, 9, 157, 105, -118, -21, 163, 9, 17, -24, 165, -10, -11, -12, -13, -14, 127, -25, 97, -3, - -78, -97, -25, -25, 163, 163, 163, 163, 9, 163, + -78, -96, -25, -25, 163, 163, 163, 163, 9, 163, 9, -3, 17, 163, 9, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, - -25, -53, -25, 31, -25, -83, -33, -34, -25, -123, + -25, -53, -25, 31, -25, -82, -33, -34, -25, -122, -75, 163, 163, 86, 161, 86, -35, 131, 164, -15, 131, 161, -10, 161, 163, 31, -25, 37, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, - -25, -26, -25, -61, 86, 164, -54, -105, 166, 9, - 15, -34, 161, -25, -105, 162, -26, 160, -70, -70, + -25, -26, -25, -61, 86, 164, -54, -104, 166, 9, + 15, -34, 161, -25, -104, 162, -26, 160, -70, -70, 93, 69, 133, -25, 87, -34, 15, -26, -60, -7, -54, 164, 86, -6, -5, 76, 77, 78, 79, 80, 81, 4, 5, 6, 7, 8, 10, 11, 12, 56, @@ -1528,47 +1528,47 @@ var yyChk = [...]int{ 112, 113, 114, 115, 13, 14, 134, 106, 107, 108, 109, 110, 135, 136, 131, 132, 154, 129, 130, 128, 137, 138, 140, 139, 141, 142, 156, 155, -60, -7, - -54, -98, -90, 153, 69, 164, 133, 153, -98, -72, - -107, -25, -25, -25, 150, 150, 159, -25, 162, -91, - -38, -25, 158, -105, 86, 163, -73, 165, 163, 163, - 157, -119, -20, 157, -119, 164, 86, 157, -23, -25, + -54, -97, -89, 153, 69, 164, 133, 153, -97, -72, + -106, -25, -25, -25, 150, 150, 159, -25, 162, -90, + -38, -25, 158, -104, 86, 163, -73, 165, 163, 163, + 157, -118, -20, 157, -118, 164, 86, 157, -23, -25, 161, 162, 161, 163, 9, 162, -40, -41, 161, -25, - -49, 31, -25, -125, 9, 105, 162, 165, 161, -84, - -89, -36, -37, -110, -108, 30, 135, 136, -9, -35, - -86, 132, -9, -85, 164, -94, -9, -25, -25, -75, - -25, 162, 166, 165, -90, -25, 162, 17, -106, -25, - 37, 134, -105, 162, 166, 162, -71, 86, 89, 50, - 88, 86, 165, 165, 69, 165, -25, 166, -90, -25, - -90, -54, -26, -25, -61, -54, -35, -125, 9, 162, + -49, 31, -25, -125, 9, 105, 162, 165, 161, -83, + -88, -36, -37, -109, -107, 30, 135, 136, -9, -35, + -85, 132, -9, -84, 164, -93, -9, -25, -25, -75, + -25, 162, 166, 165, -89, -25, 162, 17, -105, -25, + 37, 134, -104, 162, 166, 162, -71, 86, 89, 50, + 88, 86, 165, 165, 69, 165, -25, 166, -89, -25, + -89, -54, -26, -25, -61, -54, -35, -125, 9, 162, 162, 165, -125, 9, -25, 162, 165, 164, 157, -17, - -18, -21, -115, 164, 162, -27, -10, 31, -25, -78, - -25, -79, 164, 31, -84, -25, 162, -33, -29, -34, - 37, 134, 69, -31, -10, 31, -121, -84, 162, 9, - -1, 37, -108, -86, 164, -94, 165, -42, -111, -112, - 120, -113, 123, -114, 81, 80, 79, 76, 77, 78, - -85, 9, 162, 162, 75, -10, 31, 165, 17, -25, - -34, 161, 162, 166, 89, -25, 165, 166, 165, -86, - 162, -107, 162, -38, -22, -21, 164, -125, 9, -21, - -17, 163, -75, 162, 163, -87, 163, -87, 163, 162, - 163, 162, 15, -34, 161, -105, -75, -32, 117, 118, - 162, -101, 120, -36, -2, 158, 164, -85, -92, -48, - 88, 114, 113, -94, -114, 165, -9, -10, 31, 163, - -75, -25, -105, 166, 164, -125, 9, -22, 165, -18, - -125, 98, 163, -78, 165, 108, 109, -87, 107, -87, - -101, -30, -10, 31, -29, -105, 166, 104, 161, 164, - -109, 31, -109, 161, 88, -85, 165, 163, 9, 17, - -122, -93, -7, -3, -95, 163, 164, -75, 162, 165, - -85, 165, -21, -125, 165, 163, 162, -25, -126, 31, - 163, 165, 163, 107, -109, -75, 162, 162, 163, -120, - -9, -75, 164, -110, 164, -102, -58, 88, 37, 17, - 165, -48, -25, 163, 9, 17, -7, 165, -100, -43, + -18, -21, -114, 164, 162, -27, -10, 31, -25, -78, + -25, -79, 164, 31, -83, -25, 162, -33, -29, -34, + 37, 134, 69, -31, -10, 31, -120, -83, 162, 9, + -1, 37, -107, -85, 164, -93, 165, -42, -110, -111, + 120, -112, 123, -113, 81, 80, 79, 76, 77, 78, + -84, 9, 162, 162, 75, -10, 31, 165, 17, -25, + -34, 161, 162, 166, 89, -25, 165, 166, 165, -85, + 162, -106, 162, -38, -22, -21, 164, -125, 9, -21, + -17, 163, -75, 162, 163, -86, 163, -86, 163, 162, + 163, 162, 15, -34, 161, -104, -75, -32, 117, 118, + 162, -100, 120, -36, -2, 158, 164, -84, -91, -48, + 88, 114, 113, -93, -113, 165, -9, -10, 31, 163, + -75, -25, -104, 166, 164, -125, 9, -22, 165, -18, + -125, 98, 163, -78, 165, 108, 109, -86, 107, -86, + -100, -30, -10, 31, -29, -104, 166, 104, 161, 164, + -108, 31, -108, 161, 88, -84, 165, 163, 9, 17, + -121, -92, -7, -3, -94, 163, 164, -75, 162, 165, + -84, 165, -21, -125, 165, 163, 162, -25, -126, 31, + 163, 165, 163, 107, -108, -75, 162, 162, 163, -119, + -9, -75, 164, -109, 164, -101, -58, 88, 37, 17, + 165, -48, -25, 163, 9, 17, -7, 165, -99, -43, -44, -45, -46, -47, -9, -7, 154, 86, 165, 165, -28, -10, 31, -126, -75, 163, 164, 102, -30, 88, - 35, 165, -75, -75, 162, 9, 88, -25, -93, -25, + 35, 165, -75, -75, 162, 9, 88, -25, -92, -25, 161, 165, -43, 163, 163, 121, 105, 153, -75, -75, - -75, 163, 162, -9, 165, 165, -58, -84, -94, 86, - -5, -114, -7, 100, 165, 164, 162, -7, 163, -75, - -109, 165, -96, 163, 164, -75, 165, + -75, 163, 162, -9, 165, 165, -58, -83, -93, 86, + -5, -113, -7, 100, 165, 164, 162, -7, 163, -75, + -108, 165, -95, 163, 164, -75, 165, } var yyDef = [...]int{ @@ -2877,7 +2877,7 @@ yydefault: yyDollar = yyS[yypt-3 : yypt+1] //line parser/parser.y:378 { - yyVAL.node = yyDollar[2].node + yyVAL.node = stmt.NewEcho(yyDollar[1].token, yyDollar[2].list) } case 142: yyDollar = yyS[yypt-1 : yypt+1] @@ -3748,19 +3748,19 @@ yydefault: yyDollar = yyS[yypt-3 : yypt+1] //line parser/parser.y:793 { - yyVAL.node = yyDollar[1].node.Append(yyDollar[3].node) + yyVAL.list = append(yyDollar[1].list, yyDollar[3].node) } case 284: yyDollar = yyS[yypt-1 : yypt+1] //line parser/parser.y:794 { - yyVAL.node = node.NewSimpleNode("EchoList").Append(yyDollar[1].node) + yyVAL.list = []node.Node{yyDollar[1].node} } case 285: yyDollar = yyS[yypt-1 : yypt+1] //line parser/parser.y:798 { - yyVAL.node = node.NewSimpleNode("Echo").Append(yyDollar[1].node) + yyVAL.node = yyDollar[1].node } case 286: yyDollar = yyS[yypt-0 : yypt+1] diff --git a/parser/parser.y b/parser/parser.y index 069f88b..59906ac 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -197,7 +197,7 @@ func Parse(src io.Reader, fName string) node.Node { %type encaps_var encaps_var_offset isset_variables %type top_statement_list use_declarations inner_statement_list if_stmt %type alt_if_stmt for_exprs switch_case_list global_var_list static_var_list -%type echo_expr_list unset_variables parameter_list class_statement_list +%type unset_variables parameter_list class_statement_list %type implements_list case_list if_stmt_without_else %type non_empty_parameter_list argument_list non_empty_argument_list property_list %type class_const_decl name_list trait_adaptations method_body non_empty_for_exprs @@ -212,7 +212,7 @@ func Parse(src io.Reader, fName string) node.Node { %type class_modifiers %type encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list -%type const_list +%type const_list echo_expr_list %% @@ -375,7 +375,7 @@ statement: | T_RETURN optional_expr ';' { $$ = node.NewSimpleNode("Return").Append($2) } | T_GLOBAL global_var_list ';' { $$ = $2; } | T_STATIC static_var_list ';' { $$ = $2; } - | T_ECHO echo_expr_list ';' { $$ = $2; } + | T_ECHO echo_expr_list ';' { $$ = stmt.NewEcho($1, $2) } | T_INLINE_HTML { $$ = node.NewSimpleNode("Echo").Append(node.NewSimpleNode("InlineHtml").Attribute("value", $1.String())) } | expr ';' { $$ = $1; } | T_UNSET '(' unset_variables possible_comma ')' ';' @@ -790,12 +790,12 @@ const_decl: ; echo_expr_list: - echo_expr_list ',' echo_expr { $$ = $1.Append($3) } - | echo_expr { $$ = node.NewSimpleNode("EchoList").Append($1) } + echo_expr_list ',' echo_expr { $$ = append($1, $3) } + | echo_expr { $$ = []node.Node{$1} } ; echo_expr: - expr { $$ = node.NewSimpleNode("Echo").Append($1) } + expr { $$ = $1 } ; for_exprs: