turbo-editors/turbo-corepublic Fork 0
v1.0.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v1.0.0 · k33g · 19h ago
bash.go · 226 lines · 6.9 KBGo Blame HistoryRaw
  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
222
223
224
225
226
package syntax

// bashKeywords are the words that structure a shell script, and the builtins
// worth telling apart from a command on the PATH.
//
// The set is what sh, bash and zsh share: a script is coloured the same
// whichever of them it names in its shebang, because the differences between
// them are not what a highlighter can usefully show.
var bashKeywords = map[string]Class{
	"if": ClassKeyword, "then": ClassKeyword, "elif": ClassKeyword,
	"else": ClassKeyword, "fi": ClassKeyword, "for": ClassKeyword,
	"while": ClassKeyword, "until": ClassKeyword, "do": ClassKeyword,
	"done": ClassKeyword, "case": ClassKeyword, "esac": ClassKeyword,
	"in": ClassKeyword, "function": ClassKeyword, "select": ClassKeyword,
	"time": ClassKeyword, "return": ClassKeyword, "break": ClassKeyword,
	"continue": ClassKeyword, "exit": ClassKeyword,

	"true": ClassConstant, "false": ClassConstant,

	"echo": ClassBuiltin, "printf": ClassBuiltin, "read": ClassBuiltin,
	"cd": ClassBuiltin, "export": ClassBuiltin, "local": ClassBuiltin,
	"declare": ClassBuiltin, "readonly": ClassBuiltin, "unset": ClassBuiltin,
	"shift": ClassBuiltin, "set": ClassBuiltin, "test": ClassBuiltin,
	"source": ClassBuiltin, "eval": ClassBuiltin, "exec": ClassBuiltin,
	"trap": ClassBuiltin, "wait": ClassBuiltin, "command": ClassBuiltin,
}

// highlightBash colours a shell script, one slice of spans per line.
//
// Heredocs are deliberately not recognised. Following `<<EOF` to its closing
// delimiter means carrying an arbitrary word across lines and dealing with the
// `<<-` and quoted-delimiter spellings, which is a good deal of machinery for
// a construct that is usually a few lines of plain text.
//
// Nothing here spans lines, so the scanner carries no state — the empty struct
// is what says so.
func highlightBash(src string) [][]Span {
	return ScanLines(src, scanBashLine)
}

// bashScanner walks one line of shell, remembering whether it has already
// seen a word.
//
// That flag is the whole of the command rule: **the first bare word on a line
// is the command being run; every later one is a plain argument.** Following
// the real rule — a command also starts after ";", "|", "&&" and after "then"
// or "do" — would colour a little more and cost a table of what resets it,
// and getting it wrong colours an argument as a command, which reads as a
// mistake in the script rather than in the highlighter.
type bashScanner struct {
	LineScanner
	seenWord bool
}

// scanBashLine returns the spans of one line. Nothing is ever left open.
func scanBashLine(line []rune, _ struct{}) ([]Span, struct{}) {
	s := &bashScanner{LineScanner: LineScanner{line: line}}
	for !s.AtEnd() {
		s.step()
	}
	return s.spans, struct{}{}
}

// step recognises whatever starts at the current position.
func (s *bashScanner) step() {
	switch {
	case s.line[s.pos] == ' ' || s.line[s.pos] == '\t':
		s.pos++
	case s.line[s.pos] == '#':
		s.TakeRest(ClassComment)
	case s.line[s.pos] == '\'':
		// A single-quoted string has no escapes and no expansion: everything
		// up to the next quote is literal, backslashes included.
		takeLiteralQuoted(&s.LineScanner)
	case s.line[s.pos] == '"':
		takeDoubleQuoted(&s.LineScanner)
	case s.line[s.pos] == '$':
		takeExpansion(&s.LineScanner)
	case IsDigit(s.line[s.pos]):
		s.TakeWhile(ClassNumber, IsDigit)
	case IsWordRune(s.line[s.pos]) || s.startsAnOption():
		s.takeWord()
	case IsPunctuationRune(s.line[s.pos]):
		s.Take(1, ClassPunctuation)
	case IsOperatorRune(s.line[s.pos]):
		s.TakeWhile(ClassOperator, IsOperatorRune)
	default:
		s.pos++
	}
}

// startsAnOption reports whether a hyphen here begins a flag rather than an
// operator.
//
// Without this, "-eu" is a minus followed by a word, and every option in every
// script is coloured as arithmetic. A hyphen followed by a letter, a digit or
// another hyphen is an option; one followed by a space is not.
func (s *bashScanner) startsAnOption() bool {
	return s.line[s.pos] == '-' && (IsWordRune(s.Peek(1)) || s.Peek(1) == '-')
}

// takeLiteralQuoted colours '…', where nothing is escaped or expanded.
func takeLiteralQuoted(s *LineScanner) {
	start := s.pos
	s.pos++

	for !s.AtEnd() {
		if s.line[s.pos] == '\'' {
			s.pos++
			s.Emit(start, s.pos, ClassString)
			return
		}
		s.pos++
	}
	s.Emit(start, len(s.line), ClassString)
}

// takeDoubleQuoted colours "…", with the expansions inside it coloured as
// expansions rather than as part of the string.
//
// That is the point of the double-quoted form: "$HOME/bin" is a path with a
// variable in it, and seeing which part is the variable is most of what a
// reader wants from it.
func takeDoubleQuoted(s *LineScanner) {
	start := s.pos
	s.pos++ // the opening quote

	for !s.AtEnd() {
		switch {
		case s.line[s.pos] == '\\' && s.pos+1 < len(s.line):
			s.pos += 2
		case s.line[s.pos] == '$':
			s.Emit(start, s.pos, ClassString)
			takeExpansion(s)
			start = s.pos
		case s.line[s.pos] == '"':
			s.pos++
			s.Emit(start, s.pos, ClassString)
			return
		default:
			s.pos++
		}
	}
	s.Emit(start, len(s.line), ClassString)
}

// takeExpansion colours $NAME, ${…}, $(…) and the one-character forms such as
// $1, $?, $@ and $#.
func takeExpansion(s *LineScanner) {
	switch s.Peek(1) {
	case '{':
		takeBracketed(s, '{', '}')
	case '(':
		takeBracketed(s, '(', ')')
	default:
		takePlainExpansion(s)
	}
}

// takePlainExpansion colours $NAME and the single-character variables.
func takePlainExpansion(s *LineScanner) {
	start := s.pos
	s.pos++ // the dollar

	if !s.AtEnd() && IsWordRune(s.line[s.pos]) {
		for !s.AtEnd() && IsWordRune(s.line[s.pos]) {
			s.pos++
		}
		s.Emit(start, s.pos, ClassBuiltin)
		return
	}
	// $? $@ $# $* $$ $! and friends are one character each.
	if !s.AtEnd() {
		s.pos++
	}
	s.Emit(start, s.pos, ClassBuiltin)
}

// takeBracketed colours ${…} and $(…) up to the matching close, counting
// nesting so that $(a $(b) c) is one span.
//
// An unclosed one is coloured to the end of the line, which is what it looks
// like while it is being typed.
func takeBracketed(s *LineScanner, open, close rune) {
	start := s.pos
	s.pos += 2 // the dollar and the bracket

	depth := 1
	for !s.AtEnd() && depth > 0 {
		switch s.line[s.pos] {
		case open:
			depth++
		case close:
			depth--
		}
		s.pos++
	}
	s.Emit(start, s.pos, ClassBuiltin)
}

// takeWord colours a bare word: a keyword, a builtin, a variable being
// assigned, the command being run, or one of its arguments.
func (s *bashScanner) takeWord() {
	start := s.pos
	for !s.AtEnd() && (IsWordRune(s.line[s.pos]) || s.line[s.pos] == '-') {
		s.pos++
	}

	class := s.classOf(string(s.line[start:s.pos]))
	s.seenWord = true
	s.Emit(start, s.pos, class)
}

// classOf decides what a bare word is.
func (s *bashScanner) classOf(word string) Class {
	if class, ok := bashKeywords[word]; ok {
		return class
	}
	if !s.AtEnd() && s.line[s.pos] == '=' {
		return ClassIdentifier // NAME=value
	}
	if s.seenWord {
		return ClassIdentifier // an argument
	}
	return ClassFunction // the command being run
}