| 1 |
// Copyright 2015 Light Code Labs, LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package conffile |
| 16 |
|
| 17 |
import ( |
| 18 |
"bufio" |
| 19 |
"io" |
| 20 |
"strings" |
| 21 |
"unicode" |
| 22 |
) |
| 23 |
|
| 24 |
type ( |
| 25 |
// lexer is a utility which can get values, token by |
| 26 |
// token, from a Reader. A token is a word, and tokens |
| 27 |
// are separated by whitespace. A word can be enclosed |
| 28 |
// in quotes if it contains whitespace. |
| 29 |
lexer struct { |
| 30 |
reader *bufio.Reader |
| 31 |
token Token |
| 32 |
line int |
| 33 |
} |
| 34 |
|
| 35 |
// Token represents a single parsable unit. |
| 36 |
Token struct { |
| 37 |
File string |
| 38 |
Line int |
| 39 |
Text string |
| 40 |
} |
| 41 |
) |
| 42 |
|
| 43 |
// load prepares the lexer to scan an input for tokens. It discards any leading byte order mark. |
| 44 |
func (l *lexer) load(input io.Reader) error { |
| 45 |
l.reader = bufio.NewReader(input) |
| 46 |
l.line = 1 |
| 47 |
|
| 48 |
// discard byte order mark, if present |
| 49 |
firstCh, _, err := l.reader.ReadRune() |
| 50 |
if err != nil { |
| 51 |
if err == io.EOF { |
| 52 |
return nil |
| 53 |
} |
| 54 |
return err |
| 55 |
} |
| 56 |
if firstCh != 0xFEFF { |
| 57 |
err := l.reader.UnreadRune() |
| 58 |
if err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
// next loads the next token into the lexer. A token is delimited by whitespace, unless |
| 67 |
// the token starts with a quotes character (") in which case the token goes until the closing |
| 68 |
// quotes (the enclosing quotes are not included). Inside quoted strings, quotes may be escaped |
| 69 |
// with a preceding \ character. No other chars may be escaped. The rest of the line is skipped |
| 70 |
// if a "#" character is read in. Returns true if a token was loaded; false otherwise. |
| 71 |
func (l *lexer) next() bool { |
| 72 |
var val []rune |
| 73 |
var comment, quoted, escaped bool |
| 74 |
|
| 75 |
makeToken := func() bool { |
| 76 |
l.token.Text = string(val) |
| 77 |
return true |
| 78 |
} |
| 79 |
|
| 80 |
for { |
| 81 |
ch, _, err := l.reader.ReadRune() |
| 82 |
if err != nil { |
| 83 |
if len(val) > 0 { |
| 84 |
return makeToken() |
| 85 |
} |
| 86 |
if err == io.EOF { |
| 87 |
return false |
| 88 |
} |
| 89 |
panic(err) |
| 90 |
} |
| 91 |
|
| 92 |
if quoted { |
| 93 |
if !escaped { |
| 94 |
if ch == '\\' { |
| 95 |
escaped = true |
| 96 |
continue |
| 97 |
} else if ch == '"' { |
| 98 |
quoted = false // nolint:all |
| 99 |
return makeToken() |
| 100 |
} |
| 101 |
} |
| 102 |
if ch == '\n' { |
| 103 |
l.line++ |
| 104 |
} |
| 105 |
if escaped { |
| 106 |
// only escape quotes |
| 107 |
if ch != '"' { |
| 108 |
val = append(val, '\\') |
| 109 |
} |
| 110 |
} |
| 111 |
val = append(val, ch) |
| 112 |
escaped = false |
| 113 |
continue |
| 114 |
} |
| 115 |
|
| 116 |
if unicode.IsSpace(ch) { |
| 117 |
if ch == '\r' { |
| 118 |
continue |
| 119 |
} |
| 120 |
if ch == '\n' { |
| 121 |
l.line++ |
| 122 |
comment = false |
| 123 |
} |
| 124 |
if len(val) > 0 { |
| 125 |
return makeToken() |
| 126 |
} |
| 127 |
continue |
| 128 |
} |
| 129 |
|
| 130 |
if ch == '#' { |
| 131 |
comment = true |
| 132 |
} |
| 133 |
|
| 134 |
if comment { |
| 135 |
continue |
| 136 |
} |
| 137 |
|
| 138 |
if len(val) == 0 { |
| 139 |
l.token = Token{Line: l.line} |
| 140 |
if ch == '"' { |
| 141 |
quoted = true |
| 142 |
continue |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
val = append(val, ch) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// isNextOnNewLine tests whether t2 is on a different line from t1. |
| 151 |
func isNextOnNewLine(t1, t2 Token) bool { |
| 152 |
// If the second token is from a different file, |
| 153 |
// we can assume it's from a different line |
| 154 |
if t1.File != t2.File { |
| 155 |
return true |
| 156 |
} |
| 157 |
|
| 158 |
// If the first token (incl line breaks) ends |
| 159 |
// on a line earlier than the next token, |
| 160 |
// then the second token is on a new line |
| 161 |
return t1.Line+t1.NumLineBreaks() < t2.Line |
| 162 |
} |
| 163 |
|
| 164 |
func (t Token) NumLineBreaks() int { |
| 165 |
lineBreaks := strings.Count(t.Text, "\n") |
| 166 |
return lineBreaks |
| 167 |
} |