Debian Codegraph @

syntax/example_test.go

raw
1 // Copyright (c) 2016, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package syntax_test
5
6 import (
7 "fmt"
8 "os"
9 "strings"
10
11 "mvdan.cc/sh/v3/syntax"
12 )
13
14 func Example() {
15 r := strings.NewReader("{ foo; bar; }")
16 f, err := syntax.NewParser().Parse(r, "")
17 if err != nil {
18 return
19 }
20 syntax.NewPrinter().Print(os.Stdout, f)
21 // Output:
22 // {
23 // foo
24 // bar
25 // }
26 }
27
28 func ExampleWord() {
29 r := strings.NewReader("echo foo${bar}'baz'")
30 f, err := syntax.NewParser().Parse(r, "")
31 if err != nil {
32 return
33 }
34
35 printer := syntax.NewPrinter()
36 args := f.Stmts[0].Cmd.(*syntax.CallExpr).Args
37 for i, word := range args {
38 fmt.Printf("Word number %d:\n", i)
39 for _, part := range word.Parts {
40 fmt.Printf("%-20T - ", part)
41 printer.Print(os.Stdout, part)
42 fmt.Println()
43 }
44 fmt.Println()
45 }
46
47 // Output:
48 // Word number 0:
49 // *syntax.Lit - echo
50 //
51 // Word number 1:
52 // *syntax.Lit - foo
53 // *syntax.ParamExp - ${bar}
54 // *syntax.SglQuoted - 'baz'
55 }
56
57 func ExampleCommand() {
58 r := strings.NewReader("echo foo; if x; then y; fi; foo | bar")
59 f, err := syntax.NewParser().Parse(r, "")
60 if err != nil {
61 return
62 }
63
64 printer := syntax.NewPrinter()
65 for i, stmt := range f.Stmts {
66 fmt.Printf("Cmd %d: %-20T - ", i, stmt.Cmd)
67 printer.Print(os.Stdout, stmt.Cmd)
68 fmt.Println()
69 }
70
71 // Output:
72 // Cmd 0: *syntax.CallExpr - echo foo
73 // Cmd 1: *syntax.IfClause - if x; then y; fi
74 // Cmd 2: *syntax.BinaryCmd - foo | bar
75 }
76
77 func ExampleNewParser_options() {
78 src := "for ((i = 0; i < 5; i++)); do echo $i >f; done"
79
80 // LangBash is the default
81 r := strings.NewReader(src)
82 f, err := syntax.NewParser().Parse(r, "")
83 fmt.Println(err)
84
85 // Parser errors with LangPOSIX
86 r = strings.NewReader(src)
87 _, err = syntax.NewParser(syntax.Variant(syntax.LangPOSIX)).Parse(r, "")
88 fmt.Println(err)
89
90 syntax.NewPrinter().Print(os.Stdout, f)
91 syntax.NewPrinter(syntax.SpaceRedirects(true)).Print(os.Stdout, f)
92
93 // Output:
94 // <nil>
95 // 1:5: c-style fors are a bash/zsh feature; tried parsing as posix
96 // for ((i = 0; i < 5; i++)); do echo $i >f; done
97 // for ((i = 0; i < 5; i++)); do echo $i > f; done
98 }
99
100 // Keep in sync with FuzzQuote.
101
102 func ExampleQuote() {
103 for _, s := range []string{
104 "foo",
105 "bar $baz",
106 `"won't"`,
107 "~/home",
108 "#1304",
109 "name=value",
110 "for",
111 "glob-*",
112 "invalid-\xe2'",
113 "nonprint-\x0b\x1b",
114 } {
115 // We assume Bash syntax here.
116 // For general shell syntax quoting, use syntax.LangPOSIX.
117 quoted, err := syntax.Quote(s, syntax.LangBash)
118 if err != nil {
119 fmt.Printf("%q cannot be quoted: %v\n", s, err)
120 } else {
121 fmt.Printf("Quote(%17q): %s\n", s, quoted)
122 }
123 }
124 // Output:
125 // Quote( "foo"): foo
126 // Quote( "bar $baz"): 'bar $baz'
127 // Quote( "\"won't\""): "\"won't\""
128 // Quote( "~/home"): '~/home'
129 // Quote( "#1304"): '#1304'
130 // Quote( "name=value"): 'name=value'
131 // Quote( "for"): 'for'
132 // Quote( "glob-*"): 'glob-*'
133 // Quote( "invalid-\xe2'"): $'invalid-\xe2\''
134 // Quote("nonprint-\v\x1b"): $'nonprint-\v\x1b'
135 }
136
137 func ExampleWalk() {
138 in := strings.NewReader(`echo $foo "and $bar"`)
139 f, err := syntax.NewParser().Parse(in, "")
140 if err != nil {
141 return
142 }
143 syntax.Walk(f, func(node syntax.Node) bool {
144 switch node := node.(type) {
145 case *syntax.ParamExp:
146 node.Param.Value = strings.ToUpper(node.Param.Value)
147 }
148 return true
149 })
150 syntax.NewPrinter().Print(os.Stdout, f)
151 // Output: echo $FOO "and $BAR"
152 }
153
154 func ExampleDebugPrint() {
155 in := strings.NewReader(`echo 'foo'`)
156 f, err := syntax.NewParser().Parse(in, "")
157 if err != nil {
158 return
159 }
160 syntax.DebugPrint(os.Stdout, f)
161 // Output:
162 // *syntax.File {
163 // . Name: ""
164 // . Stmts: []*syntax.Stmt (len = 1) {
165 // . . 0: *syntax.Stmt {
166 // . . . Comments: []syntax.Comment (len = 0) {}
167 // . . . Cmd: *syntax.CallExpr {
168 // . . . . Assigns: []*syntax.Assign (len = 0) {}
169 // . . . . Args: []*syntax.Word (len = 2) {
170 // . . . . . 0: *syntax.Word {
171 // . . . . . . Parts: []syntax.WordPart (len = 1) {
172 // . . . . . . . 0: *syntax.Lit {
173 // . . . . . . . . ValuePos: 1:1
174 // . . . . . . . . ValueEnd: 1:5
175 // . . . . . . . . Value: "echo"
176 // . . . . . . . }
177 // . . . . . . }
178 // . . . . . }
179 // . . . . . 1: *syntax.Word {
180 // . . . . . . Parts: []syntax.WordPart (len = 1) {
181 // . . . . . . . 0: *syntax.SglQuoted {
182 // . . . . . . . . Left: 1:6
183 // . . . . . . . . Right: 1:10
184 // . . . . . . . . Dollar: false
185 // . . . . . . . . Value: "foo"
186 // . . . . . . . }
187 // . . . . . . }
188 // . . . . . }
189 // . . . . }
190 // . . . }
191 // . . . Position: 1:1
192 // . . . Semicolon: 0:0
193 // . . . Negated: false
194 // . . . Background: false
195 // . . . Coprocess: false
196 // . . . Disown: false
197 // . . . Redirs: []*syntax.Redirect (len = 0) {}
198 // . . }
199 // . }
200 // . Last: []syntax.Comment (len = 0) {}
201 // }
202 }