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