summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mkshparser.go
blob: 583bf1d22d41af8cce9e55dbee2b236a3c02acbb (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main

import (
	"fmt"
	"strconv"
)

func parseShellProgram(line *Line, program string) (list *MkShList, err error) {
	if G.opts.Debug {
		defer tracecall(program)()
	}

	tokens, rest := splitIntoShellTokens(line, program)
	lexer := NewShellLexer(tokens, rest)
	parser := &shyyParserImpl{}

	succeeded := parser.Parse(lexer)

	if succeeded == 0 && lexer.error == "" {
		return lexer.result, nil
	}
	return nil, &ParseError{append([]string{lexer.current}, lexer.remaining...)}
}

type ParseError struct {
	RemainingTokens []string
}

func (e *ParseError) Error() string {
	return fmt.Sprintf("parse error at %v", e.RemainingTokens)
}

type ShellLexer struct {
	current        string
	ioredirect     string
	remaining      []string
	atCommandStart bool
	sinceFor       int
	sinceCase      int
	error          string
	result         *MkShList
}

func NewShellLexer(tokens []string, rest string) *ShellLexer {
	return &ShellLexer{
		current:        "",
		ioredirect:     "",
		remaining:      tokens,
		atCommandStart: true,
		error:          rest}
}
func (lex *ShellLexer) Lex(lval *shyySymType) (ttype int) {
	if len(lex.remaining) == 0 {
		return 0
	}

	if G.opts.Debug {
		defer func() {
			tname := shyyTokname(shyyTok2[ttype-shyyPrivate])
			switch ttype {
			case tkWORD, tkASSIGNMENT_WORD:
				traceStep("lex %v %q", tname, lval.Word.MkText)
			case tkIO_NUMBER:
				traceStep("lex %v %v", tname, lval.IONum)
			default:
				traceStep("lex %v", tname)
			}
		}()
	}

	token := lex.ioredirect
	lex.ioredirect = ""
	if token == "" {
		token = lex.remaining[0]
		lex.current = token
		lex.remaining = lex.remaining[1:]
	}

	switch token {
	case ";":
		lex.atCommandStart = true
		return tkSEMI
	case ";;":
		lex.atCommandStart = true
		return tkSEMISEMI
	case "\n":
		lex.atCommandStart = true
		return tkNEWLINE
	case "&":
		lex.atCommandStart = true
		return tkBACKGROUND
	case "|":
		lex.atCommandStart = true
		return tkPIPE
	case "(":
		lex.atCommandStart = true
		return tkLPAREN
	case ")":
		lex.atCommandStart = true
		return tkRPAREN
	case "&&":
		lex.atCommandStart = true
		return tkAND
	case "||":
		lex.atCommandStart = true
		return tkOR
	case ">":
		lex.atCommandStart = false
		return tkGT
	case ">&":
		lex.atCommandStart = false
		return tkGTAND
	case "<":
		lex.atCommandStart = false
		return tkLT
	case "<&":
		lex.atCommandStart = false
		return tkLTAND
	case "<>":
		lex.atCommandStart = false
		return tkLTGT
	case ">>":
		lex.atCommandStart = false
		return tkGTGT
	case "<<":
		lex.atCommandStart = false
		return tkLTLT
	case "<<-":
		lex.atCommandStart = false
		return tkLTLTDASH
	case ">|":
		lex.atCommandStart = false
		return tkGTPIPE
	}

	if m, fdstr, op := match2(token, `^(\d+)(<<-|<<|<>|<&|>>|>&|>\||<|>)$`); m {
		fd, _ := strconv.Atoi(fdstr)
		lval.IONum = fd
		lex.ioredirect = op
		return tkIO_NUMBER
	}

	if lex.atCommandStart {
		lex.sinceCase = -1
		lex.sinceFor = -1
		switch token {
		case "if":
			return tkIF
		case "then":
			return tkTHEN
		case "elif":
			return tkELIF
		case "else":
			return tkELSE
		case "fi":
			return tkFI
		case "for":
			lex.atCommandStart = false
			lex.sinceFor = 0
			return tkFOR
		case "while":
			return tkWHILE
		case "until":
			return tkUNTIL
		case "do":
			return tkDO
		case "done":
			return tkDONE
		case "in":
			lex.atCommandStart = false
			return tkIN
		case "case":
			lex.atCommandStart = false
			lex.sinceCase = 0
			return tkCASE
		case "{":
			return tkLBRACE
		case "}":
			return tkRBRACE
		case "!":
			return tkEXCLAM
		}
	}

	if lex.sinceFor >= 0 {
		lex.sinceFor++
	}
	if lex.sinceCase >= 0 {
		lex.sinceCase++
	}

	switch {
	case lex.sinceFor == 2 && token == "in":
		ttype = tkIN
		lex.atCommandStart = false
	case lex.sinceFor == 2 && token == "do":
		ttype = tkDO
		lex.atCommandStart = true
	case lex.sinceCase == 2 && token == "in":
		ttype = tkIN
		lex.atCommandStart = false
	case (lex.atCommandStart || lex.sinceCase == 3) && token == "esac":
		ttype = tkESAC
		lex.atCommandStart = false
	case lex.atCommandStart && matches(token, `^[A-Za-z_]\w*=`):
		ttype = tkASSIGNMENT_WORD
		p := NewShTokenizer(dummyLine, token, false)
		lval.Word = p.ShToken()
	default:
		ttype = tkWORD
		p := NewShTokenizer(dummyLine, token, false)
		lval.Word = p.ShToken()
		lex.atCommandStart = false
	}

	return ttype
}

func (lex *ShellLexer) Error(s string) {
	lex.error = s
}