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 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 17x 17x 17x 17x 25x 25x 25x 24x 24x 6x 6x 6x 18x 18x 5x 5x 5x 5x 5x 2x 2x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 2x 1x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 13x 13x 11x 11x 15x 15x 15x 14x 14x 4x 4x 4x 2x 2x 2x 10x 10x 4x 4x 4x 2x 4x 6x 6x 6x 6x 6x 11x 11x 11x 10x 10x 10x 13x | import { Token, TokenType } from "lib/env/lexer"
import { Options } from "lib/options"
import { DuplicateVariableError, ExpectedAssignmentAfterIdentifierError, InvalidTokenAfterCommentError, InvalidTokenAfterIdentifierError, UnexpectedTokenError } from "./error"
export enum NodeType {
literal = 'literal',
quotedLiteral = 'quotedLiteral',
identifier = 'identifier',
variableDeclaration = 'variableDeclaration',
comment = 'comment',
newline = 'newline',
document = 'document'
}
export enum QuoteType {
single = '\'',
double = '"'
}
export interface Node {
type: NodeType
}
export interface RawLiteralNode extends Node {
value: string
}
export interface QuotedLiteralNode extends Node {
quoteType: QuoteType
content: RawLiteralNode|null
}
export type LiteralNode = RawLiteralNode | QuotedLiteralNode
export interface IdentifierNode extends Node {
name: string
}
export interface VariableDeclarationNode extends Node {
identifier: IdentifierNode
value?: LiteralNode
}
export interface CommentNode extends Node {
body: string|null
}
export interface NewlineNode extends Node {}
export type StatementNode = NewlineNode | CommentNode | VariableDeclarationNode
export interface DocumentNode extends Node {
statements: StatementNode[]
}
type VariablesByName = Record<IdentifierNode['name'], VariableDeclarationNode>
export interface ParsedEnvDocument {
variablesByName: VariablesByName
abstractSyntaxTree: DocumentNode
}
export type ParseEnvTokens = typeof parseEnvTokens
export const parseEnvTokens = (path: string, tokens: Token[], { allowDuplicates }: Options): ParsedEnvDocument => {
const document: DocumentNode = {
type: NodeType.document,
statements: []
}
const variablesByName: VariablesByName = {}
for (let i = 0; i < tokens.length;) {
const firstToken = tokens[i++]
const isWhitespace = firstToken.type === TokenType.whitespace
if (isWhitespace) continue
const isNewline = firstToken.type === TokenType.newline
if (isNewline) {
const newline: NewlineNode = {
type: NodeType.newline
}
document.statements.push(newline)
continue
}
const isComment = firstToken.type === TokenType.comment
if (isComment) {
const secondToken = tokens[i++]
const isLastToken = !secondToken
const isNewline = secondToken && secondToken.type === TokenType.newline
const isCommentWithoutBody = isLastToken || isNewline
if (isCommentWithoutBody) {
if (isNewline) i--
const comment: CommentNode = {
type: NodeType.comment,
body: null
}
document.statements.push(comment)
continue
}
const isCommentWithBody = secondToken.type === TokenType.commentBody
if (isCommentWithBody) {
const comment: CommentNode = {
type: NodeType.comment,
body: secondToken.value
}
document.statements.push(comment)
const thirdToken = tokens[i]
const isLastToken = !thirdToken
const isNewline = thirdToken && thirdToken.type === TokenType.newline
const isCorrectlyTerminated = isLastToken || isNewline
Eif (isCorrectlyTerminated) continue
}
throw new InvalidTokenAfterCommentError().setToken(firstToken).setFilePath(path)
}
const isVariableDeclaration = firstToken.type === TokenType.identifier
Eif (isVariableDeclaration) {
const variableName = firstToken.value
let nextNonWhitespaceToken
for (; i < tokens.length;) {
const token = tokens[i++]
const isWhitespace = token.type === TokenType.whitespace
if (!isWhitespace) {
nextNonWhitespaceToken = token
break
}
}
const hasAssignmentOperator = nextNonWhitespaceToken && nextNonWhitespaceToken.type === TokenType.operator
if (!hasAssignmentOperator) throw new ExpectedAssignmentAfterIdentifierError().setToken(firstToken).setFilePath(path)
let value: LiteralNode | QuotedLiteralNode
for (; i < tokens.length;) {
const token = tokens[i++]
const isWhitespace = token.type === TokenType.whitespace
if (isWhitespace) continue
const isQuote = token.type === TokenType.quote
if (isQuote) {
const isOpeningQuote = !value || value.type !== NodeType.quotedLiteral
const isClosingQuote = value && value.type === NodeType.quotedLiteral
if (isOpeningQuote) {
value = {
type: NodeType.quotedLiteral,
quoteType: token.value === '"' ? QuoteType.double : QuoteType.single,
content: null
}
continue
}
Eif (isClosingQuote) continue
// TODO is token right here?
throw new UnexpectedTokenError().setToken(token).setFilePath(path)
}
const isLiteral = token.type === TokenType.literal
if (isLiteral) {
const literal: LiteralNode = {
type: NodeType.literal,
value: token.value
}
const isQuotedLiteral = value && value.type === NodeType.quotedLiteral
if (isQuotedLiteral) (value as QuotedLiteralNode).content = literal
else value = literal
continue
}
const isNewline = token.type === TokenType.newline
const isComment = token.type === TokenType.comment
Eif (isNewline || isComment) {
i--
break
}
throw new InvalidTokenAfterIdentifierError().setToken(firstToken).setFilePath(path)
}
const variableDeclaration: VariableDeclarationNode = {
type: NodeType.variableDeclaration,
identifier: {
type: NodeType.identifier,
name: variableName
},
value
}
const isAlreadyDefined = variableDeclaration.identifier.name in variablesByName
if (!allowDuplicates && isAlreadyDefined) throw new DuplicateVariableError().setToken(firstToken).setFilePath(path)
variablesByName[variableDeclaration.identifier.name] = variableDeclaration
document.statements.push(variableDeclaration)
continue
}
}
return {
variablesByName,
abstractSyntaxTree: document
}
}
|