import Foundation /// Shell scripts, and `.env` files. enum ShellGrammar { static let keywords: Set = [ "if", "then", "else", "elif", "fi", "for", "while", "until", "do", "done", "case", "esac", "in", "function", "select", "time", "return", "local", "export", "readonly", "declare", "unset", "shift", "break", "continue", "exit", "source", "alias", "typeset", "let", "eval", ] /// Painted in the number colour. static let builtins: Set = [ "echo", "cd", "pwd", "printf", "read", "test", "set", "trap", "wait", "kill", "exec", "true", "false", "type", "hash", "getopts", "umask", "ulimit", "command", "builtin", "jobs", "fg", "bg", "pushd", "popd", "dirs", "history", "shopt", "mapfile", ] static func scan(_ lx: inout Lexer, _ st: inout GrammarState) { let lineStart = lx.i while lx.i < lx.end { switch st.mode { case let .quoted(quote): if string(&lx, from: lx.i, quote: quote) { st.mode = .code } continue case let .heredoc(id, interpolates): heredocLine(&lx, &st, id: id, interpolates: interpolates) continue default: break } let start = lx.i let c = lx.u[start] if Unit.isSpace(c) { lx.i += 1 continue } switch c { case Unit.hash where start == lineStart || isWordBreak(lx.unit(at: start - 1)): lx.lineComment() case Unit.apostrophe, Unit.quote: lx.i += 1 if !string(&lx, from: start, quote: c) { st.mode = .quoted(c) } case Unit.dollar: variable(&lx) case Unit.less where lx.peek(1) == Unit.less && lx.peek(2) != Unit.less: heredocStart(&lx, &st) case _ where isOperator(c): lx.operatorRun(isOperator) case _ where isWordBreak(c): lx.i += 1 default: word(&lx, atCommandStart: start == lineStart || isWordBreak(lx.unit(at: start - 1))) } } } private static func isOperator(_ c: UInt16) -> Bool { c == Unit.less || c == Unit.greater || c == Unit.equals || c == Unit.bang || c == Unit.amp || c == Unit.pipe } private static func isWordBreak(_ c: UInt16) -> Bool { switch c { case Unit.space, Unit.tab, Unit.pipe, Unit.amp, Unit.semicolon, Unit.lparen, Unit.rparen, Unit.less, Unit.greater, Unit.lbrace, Unit.rbrace, Unit.backtick, 13: true default: false } } /// One shell word: `NAME=` assigns, a bare keyword or builtin is coloured, digits are a number. private static func word(_ lx: inout Lexer, atCommandStart: Bool) { let start = lx.i while lx.i < lx.end && Unit.isIdentPart(lx.u[lx.i]) { lx.i += 1 } let identEnd = lx.i if identEnd > start && atCommandStart && lx.peek() == Unit.equals && !Unit.isDigit(lx.u[start]) { lx.emit(start, identEnd, .variable) return } while lx.i < lx.end && !isWordBreak(lx.u[lx.i]) && !isQuoteOrDollar(lx.u[lx.i]) { lx.i += 1 } guard lx.i == identEnd, identEnd > start else { if lx.i == start { lx.i += 1 } return } let text = lx.text(start, identEnd) if keywords.contains(text) { lx.emit(start, identEnd, .keyword) } else if builtins.contains(text) || text.utf16.allSatisfy(Unit.isDigit) { lx.emit(start, identEnd, .number) } } private static func isQuoteOrDollar(_ c: UInt16) -> Bool { c == Unit.quote || c == Unit.apostrophe || c == Unit.dollar } /// `$name`, `$1`, `$@`, `${…}`; `$(` is left plain for the command inside it. private static func variable(_ lx: inout Lexer) { let start = lx.i lx.i += 1 let next = lx.peek() if next == Unit.lbrace { while lx.i < lx.end && lx.u[lx.i] != Unit.rbrace { lx.i += 1 } lx.i = min(lx.i + 1, lx.end) } else if Unit.isIdentStart(next) { lx.identifier() } else if Unit.isDigit(next) || "@*#?$!-".utf16.contains(next) { lx.i += 1 } else { return } lx.emit(start, lx.i, .variable) } /// A quoted body; double quotes interpolate `$…`. False when the line ends first. private static func string(_ lx: inout Lexer, from start: Int, quote: UInt16) -> Bool { var runStart = start while lx.i < lx.end { let c = lx.u[lx.i] if quote == Unit.quote && c == Unit.backslash { lx.i = min(lx.i + 2, lx.end) continue } if c == quote { lx.i += 1 lx.emit(runStart, lx.i, .string) return true } if quote == Unit.quote && c == Unit.dollar && (Unit.isIdentStart(lx.peek(1)) || lx.peek(1) == Unit.lbrace) { lx.emit(runStart, lx.i, .string) variable(&lx) runStart = lx.i continue } lx.i += 1 } lx.emit(runStart, lx.i, .string) return false } /// `< idStart else { lx.i = start + 2 lx.emit(start, lx.i, .keyword) return } let id = lx.text(idStart, k) if quoted && lx.unit(at: k) == quote { k += 1 } lx.emit(start, idStart - (quoted ? 1 : 0), .keyword) lx.emit(idStart - (quoted ? 1 : 0), k, .string) lx.i = k st.mode = .heredoc(id: id, interpolates: !quoted) } private static func heredocLine(_ lx: inout Lexer, _ st: inout GrammarState, id: String, interpolates: Bool) { let start = lx.i var k = start while k < lx.end && Unit.isSpace(lx.u[k]) { k += 1 } let marker = Array(id.utf16) if lx.starts(with: marker, at: k) && k + marker.count == lx.end { lx.i = lx.end lx.emit(start, lx.i, .string) st.mode = .code return } var runStart = start while lx.i < lx.end { if interpolates && lx.u[lx.i] == Unit.dollar { lx.emit(runStart, lx.i, .string) let before = lx.i variable(&lx) if lx.i == before { lx.i += 1 } runStart = lx.i continue } lx.i += 1 } lx.emit(runStart, lx.i, .string) } }