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
|
package main
import "fmt"
type MkShList struct {
AndOrs []*MkShAndOr
Separators []MkShSeparator
}
func NewMkShList() *MkShList {
return &MkShList{nil, nil}
}
func (list *MkShList) AddAndOr(andor *MkShAndOr) *MkShList {
list.AndOrs = append(list.AndOrs, andor)
return list
}
func (list *MkShList) AddSeparator(separator MkShSeparator) *MkShList {
list.Separators = append(list.Separators, separator)
return list
}
type MkShAndOr struct {
Pipes []*MkShPipeline
Ops []string // Either "&&" or "||"
}
func NewMkShAndOr(pipeline *MkShPipeline) *MkShAndOr {
return &MkShAndOr{[]*MkShPipeline{pipeline}, nil}
}
func (andor *MkShAndOr) Add(op string, pipeline *MkShPipeline) *MkShAndOr {
andor.Pipes = append(andor.Pipes, pipeline)
andor.Ops = append(andor.Ops, op)
return andor
}
type MkShPipeline struct {
Negated bool
Cmds []*MkShCommand
}
func NewMkShPipeline(negated bool, cmds ...*MkShCommand) *MkShPipeline {
return &MkShPipeline{negated, cmds}
}
func (pipe *MkShPipeline) Add(cmd *MkShCommand) *MkShPipeline {
pipe.Cmds = append(pipe.Cmds, cmd)
return pipe
}
type MkShCommand struct {
Simple *MkShSimpleCommand
Compound *MkShCompoundCommand
FuncDef *MkShFunctionDefinition
Redirects []*MkShRedirection // For Compound and FuncDef
}
type MkShCompoundCommand struct {
Brace *MkShList
Subshell *MkShList
For *MkShForClause
Case *MkShCaseClause
If *MkShIfClause
Loop *MkShLoopClause
}
type MkShForClause struct {
Varname string
Values []*ShToken
Body *MkShList
}
type MkShCaseClause struct {
Word *ShToken
Cases []*MkShCaseItem
}
type MkShCaseItem struct {
Patterns []*ShToken
Action *MkShList
Separator MkShSeparator
}
type MkShIfClause struct {
Conds []*MkShList
Actions []*MkShList
Else *MkShList
}
func (cl *MkShIfClause) Prepend(cond *MkShList, action *MkShList) {
cl.Conds = append([]*MkShList{cond}, cl.Conds...)
cl.Actions = append([]*MkShList{action}, cl.Actions...)
}
type MkShLoopClause struct {
Cond *MkShList
Action *MkShList
Until bool
}
type MkShFunctionDefinition struct {
Name string
Body *MkShCompoundCommand
}
type MkShSimpleCommand struct {
Assignments []*ShToken
Name *ShToken
Args []*ShToken
Redirections []*MkShRedirection
}
func NewStrCommand(cmd *MkShSimpleCommand) *StrCommand {
strcmd := &StrCommand{
make([]string, len(cmd.Assignments)),
"",
make([]string, len(cmd.Args))}
for i, assignment := range cmd.Assignments {
strcmd.Assignments[i] = assignment.MkText
}
if cmd.Name != nil {
strcmd.Name = cmd.Name.MkText
}
for i, arg := range cmd.Args {
strcmd.Args[i] = arg.MkText
}
return strcmd
}
type StrCommand struct {
Assignments []string
Name string
Args []string
}
func (c *StrCommand) HasOption(opt string) bool {
for _, arg := range c.Args {
if arg == opt {
return true
}
}
return false
}
func (c *StrCommand) AnyArgMatches(pattern RegexPattern) bool {
for _, arg := range c.Args {
if matches(arg, pattern) {
return true
}
}
return false
}
func (c *StrCommand) String() string {
return fmt.Sprintf("%v %v %v", c.Assignments, c.Name, c.Args)
}
type MkShRedirection struct {
Fd int // Or -1
Op string
Target *ShToken
}
type MkShSeparator uint8
const (
sepNone MkShSeparator = iota
sepSemicolon // ;
sepBackground // &
sepNewline // \n
)
|