|
| 1 | +package crossplane |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// Token is a lexical token of the NGINX configuration syntax. |
| 12 | +type Token struct { |
| 13 | + // Text is the string corresponding to the token. It could be a directive or symbol. The value is the actual token |
| 14 | + // sequence in order to support defining directives in modules other than the core NGINX module set. |
| 15 | + Text string |
| 16 | + // Line is the source starting line number the token within a file. |
| 17 | + Line int |
| 18 | + // IsQuoted signifies if the token is wrapped by quotes (", '). Quotes are not usually necessary in an NGINX |
| 19 | + // configuration and mostly serve to help make the config less ambiguous. |
| 20 | + IsQuoted bool |
| 21 | +} |
| 22 | + |
| 23 | +type scannerError struct { |
| 24 | + msg string |
| 25 | + line int |
| 26 | +} |
| 27 | + |
| 28 | +func (e *scannerError) Error() string { return e.msg } |
| 29 | +func (e *scannerError) Line() int { return e.line } |
| 30 | + |
| 31 | +func newScannerErrf(line int, format string, a ...any) *scannerError { |
| 32 | + return &scannerError{line: line, msg: fmt.Sprintf(format, a...)} |
| 33 | +} |
| 34 | + |
| 35 | +// LineNumber reports the line on which the error occurred by finding the first error in |
| 36 | +// the errs chain that returns a line number. Otherwise, it returns 0, false. |
| 37 | +// |
| 38 | +// An error type should provide a Line() int method to return a line number. |
| 39 | +func LineNumber(err error) (int, bool) { |
| 40 | + var e interface{ Line() int } |
| 41 | + if !errors.As(err, &e) { |
| 42 | + return 0, false |
| 43 | + } |
| 44 | + |
| 45 | + return e.Line(), true |
| 46 | +} |
| 47 | + |
| 48 | +// Scanner provides an interface for tokenizing an NGINX configuration. Successive calls to the Scane method will step |
| 49 | +// through the 'tokens; of an NGINX configuration. |
| 50 | +// |
| 51 | +// Scanning stops unrecoverably at EOF, the first I/O error, or an unexpected token. |
| 52 | +// |
| 53 | +// Use NewScanner to construct a Scanner. |
| 54 | +type Scanner struct { |
| 55 | + scanner *bufio.Scanner |
| 56 | + lineno int |
| 57 | + tokenStartLine int |
| 58 | + tokenDepth int |
| 59 | + repeateSpecialChar bool // only '}' can be repeated |
| 60 | + prev string |
| 61 | +} |
| 62 | + |
| 63 | +// NewScanner returns a new Scanner to read from r. |
| 64 | +func NewScanner(r io.Reader) *Scanner { |
| 65 | + s := &Scanner{ |
| 66 | + scanner: bufio.NewScanner(r), |
| 67 | + lineno: 1, |
| 68 | + tokenStartLine: 1, |
| 69 | + tokenDepth: 0, |
| 70 | + repeateSpecialChar: false, |
| 71 | + } |
| 72 | + |
| 73 | + s.scanner.Split(bufio.ScanRunes) |
| 74 | + |
| 75 | + return s |
| 76 | +} |
| 77 | + |
| 78 | +// Scan reads the next token from source and returns it.. It returns io.EOF at the end of the source. Scanner errors are |
| 79 | +// returned when encountered. |
| 80 | +func (s *Scanner) Scan() (Token, error) { //nolint: funlen, gocognit, gocyclo |
| 81 | + var tok strings.Builder |
| 82 | + |
| 83 | + lexState := skipSpace |
| 84 | + newToken := false |
| 85 | + readNext := true |
| 86 | + esc := false |
| 87 | + |
| 88 | + var r, quote string |
| 89 | + |
| 90 | + for { |
| 91 | + switch { |
| 92 | + case s.prev != "": |
| 93 | + r = s.prev |
| 94 | + s.prev = "" |
| 95 | + case readNext: |
| 96 | + if !s.scanner.Scan() { |
| 97 | + if tok.Len() > 0 { |
| 98 | + return Token{Text: tok.String(), Line: s.tokenStartLine, IsQuoted: lexState == inQuote}, nil |
| 99 | + } |
| 100 | + |
| 101 | + if s.tokenDepth > 0 { |
| 102 | + return Token{}, &scannerError{line: s.tokenStartLine, msg: "unexpected end of file, expecting }"} |
| 103 | + } |
| 104 | + |
| 105 | + return Token{}, io.EOF |
| 106 | + } |
| 107 | + |
| 108 | + nextRune := s.scanner.Text() |
| 109 | + r = nextRune |
| 110 | + if isEOL(r) { |
| 111 | + s.lineno++ |
| 112 | + } |
| 113 | + default: |
| 114 | + readNext = true |
| 115 | + } |
| 116 | + |
| 117 | + // skip CRs |
| 118 | + if r == "\r" || r == "\\\r" { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if r == "\\" && !esc { |
| 123 | + esc = true |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + if esc { |
| 128 | + esc = false |
| 129 | + r = "\\" + r |
| 130 | + } |
| 131 | + |
| 132 | + switch lexState { |
| 133 | + case skipSpace: |
| 134 | + if !isSpace(r) { |
| 135 | + lexState = inWord |
| 136 | + newToken = true |
| 137 | + readNext = false // re-eval |
| 138 | + s.tokenStartLine = s.lineno |
| 139 | + } |
| 140 | + continue |
| 141 | + |
| 142 | + case inWord: |
| 143 | + if newToken { |
| 144 | + newToken = false |
| 145 | + if r == "#" { |
| 146 | + tok.WriteString(r) |
| 147 | + lexState = inComment |
| 148 | + s.tokenStartLine = s.lineno |
| 149 | + continue |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if isSpace(r) { |
| 154 | + return Token{Text: tok.String(), Line: s.tokenStartLine}, nil |
| 155 | + } |
| 156 | + |
| 157 | + // parameter expansion syntax (ex: "${var[@]}") |
| 158 | + if tok.Len() > 0 && strings.HasSuffix(tok.String(), "$") && r == "{" { |
| 159 | + tok.WriteString(r) |
| 160 | + lexState = inVar |
| 161 | + s.repeateSpecialChar = false |
| 162 | + continue |
| 163 | + } |
| 164 | + |
| 165 | + // add entire quoted string to the token buffer |
| 166 | + if r == `"` || r == "'" { |
| 167 | + if tok.Len() > 0 { |
| 168 | + // if a quote is inside a token, treat it like any other char |
| 169 | + tok.WriteString(r) |
| 170 | + } else { |
| 171 | + quote = r |
| 172 | + lexState = inQuote |
| 173 | + s.tokenStartLine = s.lineno |
| 174 | + } |
| 175 | + s.repeateSpecialChar = false |
| 176 | + continue |
| 177 | + } |
| 178 | + |
| 179 | + // special characters treated as full tokens |
| 180 | + if isSpecialChar(r) { |
| 181 | + if tok.Len() > 0 { |
| 182 | + s.prev = r |
| 183 | + return Token{Text: tok.String(), Line: s.tokenStartLine}, nil |
| 184 | + } |
| 185 | + |
| 186 | + // only } can be repeated |
| 187 | + if s.repeateSpecialChar && r != "}" { |
| 188 | + return Token{}, newScannerErrf(s.tokenStartLine, "unxpected %q", r) |
| 189 | + } |
| 190 | + |
| 191 | + s.repeateSpecialChar = true |
| 192 | + if r == "{" { |
| 193 | + s.tokenDepth++ |
| 194 | + } |
| 195 | + |
| 196 | + if r == "}" { |
| 197 | + s.tokenDepth-- |
| 198 | + if s.tokenDepth < 0 { |
| 199 | + return Token{}, &scannerError{line: s.tokenStartLine, msg: `unexpected "}"`} |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + tok.WriteString(r) |
| 204 | + return Token{Text: tok.String(), Line: s.tokenStartLine}, nil |
| 205 | + } |
| 206 | + |
| 207 | + s.repeateSpecialChar = false |
| 208 | + tok.WriteString(r) |
| 209 | + case inComment: |
| 210 | + if isEOL(r) { |
| 211 | + return Token{Text: tok.String(), Line: s.tokenStartLine}, nil |
| 212 | + } |
| 213 | + tok.WriteString(r) |
| 214 | + case inVar: |
| 215 | + tok.WriteString(r) |
| 216 | + if r != "}" && !isSpace(r) { |
| 217 | + continue |
| 218 | + } |
| 219 | + lexState = inWord |
| 220 | + case inQuote: |
| 221 | + if r == quote { |
| 222 | + return Token{Text: tok.String(), Line: s.tokenStartLine}, nil |
| 223 | + } |
| 224 | + if r == "\\"+quote { |
| 225 | + r = quote |
| 226 | + } |
| 227 | + tok.WriteString(r) |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments