Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 18x 18x 112x 112x 112x 18x 4x 112x 112x 112x 112x 112x 107x 107x 98x 88x 88x 79x 79x 69x 69x 63x 63x 45x 45x 37x 37x 20x 20x 4x 4x 112x 112x 95x 95x 95x 84x 4x 112x 112x 95x 95x 95x 84x 4x 14x 14x 14x 14x 4x 4x 4x 4x 10x 10x 4x 9x 9x 9x 116x 116x 116x 113x 9x 6x 20x 18x 4x 112x 74x 74x 74x 74x 74x 4x 17x 17x 17x 17x 17x 17x 17x 17x 15x 246x 246x 246x 246x 246x 238x 238x 234x 234x 233x 15x 15x 15x 7x 15x 4x 20x 20x 20x 160x 160x 160x 19x 141x 20x 4x 4x 129x 129x 129x 217x 217x 217x 199x 199x 91x 91x 108x 129x 4x 37x 38x 38x 38x 17x 21x 21x 11x 9x 4x 88x | import { QuoteType } from "lib/env/parser"
import { FileCoordinates, LexicalError } from "./error"
export enum TokenType {
identifier = 'identifier',
operator = 'operator',
literal = 'literal',
quote = 'quote',
newline = 'newline',
whitespace = 'whitespace',
comment = 'comment',
commentBody = 'commentBody'
}
export interface Token {
type: TokenType
position: number
line: number
column: number
length: number
value: string
}
const QUOTE_EXPRESSION = /^["']$/
const OPERATOR_EXPRESSION = /^=$/
const COMMENT_EXPRESSION = /^#$/
const IDENTIFIER_START_EXPRESSION = /^[a-zA-Z]$/
const IDENTIFIER_END_EXPRESSION = /^[^a-zA-Z0-9_-]$/
export type AnalyzeEnvSourceCode = typeof analyzeEnvSourceCode
export const analyzeEnvSourceCode = (path: string, src: string): Token[] => {
const tokens: Token[] = []
for (let i = 0; i < src.length;) {
const token = getTokenAtPosition(path, src, i, tokens)
tokens.push(token)
i += token.length
}
return tokens
}
const getTokenAtPosition = (path: string, src: string, position: number, tokens: Token[]): Token => {
const firstChar = src[position]
const [previousToken, secondPreviousToken] = getPreviousTwoNonWhitespaceTokens(tokens)
const isQuotedLiteral = isInsideQuotes(previousToken, secondPreviousToken)
const isDoubleQuotedLiteral = isQuotedLiteral && previousToken.value === QuoteType.double
if (!isDoubleQuotedLiteral) {
const isNewline = firstChar === '\n' || firstChar === '\r'
if (isNewline) return makeNewlineToken(position, src, tokens)
}
if (!isQuotedLiteral) {
const isCommentBody = isLastTokenComment(tokens)
if (isCommentBody) return makeCommentBodyToken(position, src, tokens)
const isComment = COMMENT_EXPRESSION.test(firstChar)
if (isComment) return makeCommentToken(position, src, tokens)
const isWhiteSpace = /^\s$/.test(firstChar)
if (isWhiteSpace) return makeWhiteSpaceToken(position, src, tokens)
const isQuote = QUOTE_EXPRESSION.test(firstChar)
if (isQuote) return makeQuoteToken(position, src, tokens)
const isOperator = OPERATOR_EXPRESSION.test(firstChar)
if (isOperator) return makeOperatorToken(position, src, tokens)
}
const isLiteral = hasAssignmentOperatorOnCurrentLine(tokens)
if (isLiteral) return makeLiteralToken(position, src, tokens)
const isIdentifier = IDENTIFIER_START_EXPRESSION.test(firstChar)
Eif (isIdentifier) return makeIdentifierToken(position, src, tokens)
{
const previousToken = getPreviousToken(tokens)
const coordinates = getCoordinates(previousToken)
throw new LexicalError().setChar(firstChar).setCoordinates(coordinates).setFilePath(path)
}
}
const getCoordinates = (previousToken?: Token): FileCoordinates => {
if (!previousToken) {
return {
line: 1,
column: 1,
position: 1
}
}
const position = previousToken.position + previousToken.length
const isAfterNewline = previousToken.type === TokenType.newline
if (isAfterNewline) {
return {
line: previousToken.line + 1,
column: 1,
position
}
}
return {
line: previousToken.line,
column: previousToken.column + previousToken.length,
position
}
}
export const getLine = (tokens: Token[]): number => {
const isFirstToken = tokens.length === 0
if (isFirstToken) return 1
const { type, line } = tokens[tokens.length - 1]
const isNewLine = type === TokenType.newline
if (isNewLine) return line + 1
else return line
}
export const getColumn = (tokens: Token[]): number => {
const isFirstToken = tokens.length === 0
if (isFirstToken) return 1
const { type, column, length } = tokens[tokens.length - 1]
const isNewLine = type === TokenType.newline
if (isNewLine) return 1
else return column + length
}
const makeNewlineToken = (position: number, src: string, tokens: Token[]): Token => {
const baseToken: Omit<Token, 'length' | 'value'> = {
type: TokenType.newline,
position,
line: getLine(tokens),
column: getColumn(tokens)
}
const char = src[position]
const isCr = char === '\r'
if (isCr) {
const nextChar = src[position + 1]
const isCrLf = nextChar === '\n'
const value = isCrLf ? '\r\n' : '\r'
return {
...baseToken,
length: value.length,
value
}
}
return {
...baseToken,
length: 1,
value: '\n'
}
}
const makeCommentToken = (position: number, src: string, tokens: Token[]): Token => ({
type: TokenType.comment,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: 1,
value: src[position]
})
const makeCommentBodyToken = (position: number, src: string, tokens: Token[]): Token => {
let i = position
let value = src[i++]
for (; i < src.length; i++) {
const char = src[i]
const isNewline = char === '\n' || char === '\r'
if (isNewline) break
value = `${value}${char}`
}
return {
type: TokenType.commentBody,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: value.length,
value
}
}
const makeWhiteSpaceToken = (position: number, src: string, tokens: Token[]): Token => ({
type: TokenType.whitespace,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: 1,
value: src[position]
})
const makeQuoteToken = (position: number, src: string, tokens: Token[]): Token => ({
type: TokenType.quote,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: 1,
value: src[position]
})
const makeOperatorToken = (position: number, src: string, tokens: Token[]): Token => ({
type: TokenType.operator,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: 1,
value: src[position]
})
const isInsideQuotes = (previousToken?: Token, secondPreviousToken?: Token): boolean => {
if (!previousToken || !secondPreviousToken) return false
const hasOpeningQuote = previousToken.type === TokenType.quote
const isSecondPreviousTokenQuote = secondPreviousToken.type === TokenType.quote
const isSecondPreviousTokenLiteral = secondPreviousToken.type === TokenType.literal
const hasTerminatingQuote = isSecondPreviousTokenQuote || isSecondPreviousTokenLiteral
return hasOpeningQuote && !hasTerminatingQuote
}
const makeLiteralToken = (position: number, src: string, tokens: Token[]): Token => {
let i = position
let value = src[i++]
const [previousToken] = getPreviousTwoNonWhitespaceTokens(tokens)
const isQuotedValue = previousToken.type === TokenType.quote
const firstChar = value
const isClosingQuote = firstChar === previousToken?.value
const isEmptyQuotedValue = isQuotedValue && isClosingQuote
if (isEmptyQuotedValue) return makeQuoteToken(position, src, tokens)
for (; i < src.length; i++) {
const char = src[i]
const previousChar = src[i - 1]
const isClosingQuote = char === previousToken.value
const isEscaped = previousChar === '\\'
if (isQuotedValue && isClosingQuote && !isEscaped) break
const isNewline = char === '\n' || char === '\r'
if (isNewline && !isQuotedValue) break
const isComment = char === '#'
if (isComment && !isQuotedValue) break
value = `${value}${char}`
}
const [ trailingWhitespace ] = /(\s*)$/.exec(value)
const length = isQuotedValue ? value.length : value.length - trailingWhitespace.length
if (!isQuotedValue) {
value = value.substr(0, length)
}
return {
type: TokenType.literal,
position,
line: getLine(tokens),
column: getColumn(tokens),
length,
value
}
}
const makeIdentifierToken = (position: number, src: string, tokens: Token[]): Token => {
let i = position
let value = src[i++]
for (; i < src.length; i++) {
const char = src[i]
const isEndOfIdentifier = IDENTIFIER_END_EXPRESSION.test(char)
if (isEndOfIdentifier) {
break
}
value = `${value}${char}`
}
return {
type: TokenType.identifier,
position,
line: getLine(tokens),
column: getColumn(tokens),
length: value.length,
value
}
}
const getPreviousToken = (tokens: Token[]): Token => {
const hasTokens = tokens.length > 0
if (!hasTokens) return null
return tokens[tokens.length - 1]
}
const getPreviousTwoNonWhitespaceTokens = (tokens: Token[]): [Token?, Token?] => {
let previousToken: Token = null
let secondPreviousToken: Token = null
for (let i = tokens.length - 1; i >= 0; i--) {
const token = tokens[i]
const isWhiteSpace = token.type === TokenType.whitespace
if (!isWhiteSpace) {
const hasPreviousToken = !!previousToken
if (hasPreviousToken) {
secondPreviousToken = token
break
}
previousToken = token
}
}
return [previousToken, secondPreviousToken]
}
const hasAssignmentOperatorOnCurrentLine = (previousTokens: Token[]): boolean => {
for (let i = previousTokens.length - 1; i >= 0; i--) {
const { type, value } = previousTokens[i]
const isAssignmentOperator = type === TokenType.operator && value === '='
if (isAssignmentOperator) {
return true
}
const isNewline = type === TokenType.newline
if (isNewline) {
return false
}
}
return false
}
const isLastTokenComment = (previousTokens: Token[]): boolean =>
previousTokens.length > 0 && previousTokens[previousTokens.length - 1].type === TokenType.comment
|