All files / key-parser key-ast.util.js

96.97% Statements 289/298
91.66% Branches 33/36
100% Functions 5/5
96.97% Lines 289/298

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 2991x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 113x 206x 206x 206x 113x 113x 113x 113x 113x 113x 103x 103x 103x                   103x 103x 103x 103x 103x 103x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 60x 60x 60x 60x 113x 113x 113x 113x 2201x 2201x 2201x 2201x 207x 2201x 46x 46x 46x 161x 2201x 57x 57x 57x 57x 57x 104x 2201x 44x 44x 1x 1x 1x 1x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 2201x 60x 60x 60x 60x 60x 60x 60x 60x 60x 103x 103x 113x 113x 113x 104x 104x 113x 113x 113x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 113x 113x 113x 113x 113x 113x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Enum for token types.
 * @constant
 */
export const states = Object.freeze({
  normal: 0,
  capture: 1,
  capture_expr: 2,
  capture_expr_sep: 3,
  regex: 4,
  sq_string: 5,
  dq_string: 6,
  bt_string: 7,
  escape: 8,
 
  previous: 9,
  previous_ignore: 10,
})
 
/**
 * @param {string} char - target string char
 * @returns {number} the Unicode value of the character
 */
const ch = (char) => char.codePointAt(0) ?? 0
 
const defaultNextState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[states.normal] = states.normal
  state[states.capture] = states.capture_expr
  state[states.capture_expr] = states.capture_expr
  state[states.capture_expr_sep] = states.previous_ignore
  state[states.regex] = states.regex
  state[states.sq_string] = states.sq_string
  state[states.dq_string] = states.dq_string
  state[states.bt_string] = states.bt_string
  state[states.escape] = states.previous
  return state
})()
 
const normalState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('{')] = states.capture
  return state
})()
 
const captureState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('}')] = states.previous
  state[ch('/')] = states.regex
  state[ch('\'')] = states.sq_string
  state[ch('"')] = states.dq_string
  state[ch('`')] = states.bt_string
  state[ch('\\')] = states.escape
  state[ch('\t')] = states.capture
  state[ch(' ')] = states.capture
  state[ch('\n')] = states.capture
  state[ch('|')] = states.capture_expr_sep
  return state
})()
 
const captureExprState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('\\')] = states.previous_ignore
  state[ch('\t')] = states.previous_ignore
  state[ch(' ')] = states.previous_ignore
  state[ch('|')] = states.previous_ignore
  state[ch('\n')] = states.previous_ignore
  state[ch('}')] = states.previous_ignore
  return state
})()
 
const regexState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('/')] = states.previous
  state[ch('\\')] = states.escape
  return state
})()
 
const singleQuoteStringState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('\'')] = states.previous
  state[ch('\\')] = states.escape
  return state
})()
 
const doubleQuoteStringState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('"')] = states.previous
  state[ch('\\')] = states.escape
  return state
})()
 
const backtickStringState = (() => {
  const state = /** @type {StateValues[]} */ ([])
  state[ch('`')] = states.previous
  state[ch('\\')] = states.escape
  return state
})()
 
const stateMachine = (() => {
  const state = []
  state[states.normal] = normalState
  state[states.capture] = captureState
  state[states.capture_expr] = captureExprState
  state[states.capture_expr_sep] = []
  state[states.regex] = regexState
  state[states.sq_string] = singleQuoteStringState
  state[states.dq_string] = doubleQuoteStringState
  state[states.bt_string] = backtickStringState
  state[states.escape] = []
  return state
})()
 
/**
 * Parses the string into an Abstract Syntax Tree (AST)
 * @param {string} key - target sting
 * @returns {AST} the parsed AST of the target key
 */
// eslint-disable-next-line max-lines-per-function -- this function is optimized for speed as its critical for startup speed, it is meant to be complex
export function getAST (key) {
  let currentState = /** @type {StateValues} */(states.normal)
  let currentMachineState = stateMachine[currentState]
 
  /** @type {AST_In_Progress} */
  const rootNode = {
    tokens: [],
  }
 
  /** @type {TmpToken} */
  let currentToken = {
    parentNode: rootNode,
    start: 0,
    end: 0,
    type: states.normal,
    childTokens: [],
  }
 
  /**
   * Sets the current state
   * @param {StateValues} newState - target state
   */
  const setCurrentState = (newState) => {
    currentState = newState
    currentMachineState = stateMachine[currentState]
  }
 
  /**
   * sets the current token the parent node or the next token if the parent in root
   * @param {number} index - index where the current toke ends
   */
  const upOneLevel = (index) => {
    setCurrentState(/** @type {TmpToken} */(currentToken.parentNode).type ?? states.normal)
 
    if (currentToken.type === states.escape) {
      const { parentNode } = currentToken

      if (parentNode === rootNode) {
        currentToken = /** @type {TmpToken} */(rootNode.tokens.pop())
        return
      }
      currentToken = /** @type {TmpToken} */(parentNode)
      return
    }
 
    currentToken.end = index + 1
 
    const { parentNode } = currentToken
 
    if (parentNode === rootNode) {
      parentNode.tokens.push(currentToken)
      currentToken = {
        parentNode,
        start: index + 1,
        end: index + 1,
        type: states.normal,
        childTokens: [],
      }
      return
    }
    const token = /** @type {TmpToken} */(parentNode)
    token.childTokens.push(currentToken)
    currentToken = token
  }
 
  const length = key.length
  const { previous: PREVIOUS, previous_ignore: PREVIOUS_IGNORE, normal: NORMAL } = states
  for (let i = 0; i < length; i++) {
    const ch = key.codePointAt(i) ?? -1
 
    const nextState = currentMachineState[ch] ?? defaultNextState[currentState]
    if (nextState == null || nextState === currentState) continue
 
    if (nextState === PREVIOUS) {
      upOneLevel(i)
      continue
    }
 
    if (nextState === PREVIOUS_IGNORE) {
      upOneLevel(i - 1)
      /* updated-loop-counter -- the idea is to handle the current letter as if it is in another state */
      i--
      continue
    }
 
    if (currentState === NORMAL) {
      // at this point the next state is always `states.capture`
      if (key.codePointAt(i + 1) === ch) {
        /* updated-loop-counter -- this loop update is what escapes the "{" */
        i++
        continue
      }
 
      currentToken.end = i
      if (currentToken.end > currentToken.start) {
        rootNode.tokens.push(currentToken)
      }
      currentToken = {
        parentNode: rootNode,
        start: i,
        end: i,
        type: nextState,
        childTokens: [],
      }
    } else {
      const newToken = {
        parentNode: currentToken,
        start: i,
        end: i,
        type: nextState,
        childTokens: [],
      }
      currentToken = newToken
    }
    setCurrentState(nextState)
  }
 
  currentToken.end = key.length
  if (currentToken.end > currentToken.start) {
    rootNode.tokens.push(currentToken)
  }
 
  /** @type {(a: TmpToken) => Token} */
  const toToken = ({ start, end, type, childTokens }) => {
    const substring = key.slice(start, end)
    const text = type === states.normal ? substring.replaceAll('{{', '{') : substring
 
    return {
      start,
      end,
      type,
      text,
      childTokens: childTokens.map(toToken),
    }
  }
 
  return {
    key,
    tokens: rootNode.tokens.map(toToken),
  }
}
 
/**
 * @typedef {object} AST
 * Abstract syntax tree (AST), or just syntax tree, A parse tree is a visual representation of the
 * syntactic structure of a piece of source code, as produced by a parser.
 * It shows the hierarchy of the elements in the code and the relationships between them.
 * @property {string} key - the target key used to create the key
 * @property {Token[]} tokens - the direct descendant of the root tree
 */
 
/**
 * @typedef {object} AST_In_Progress
 * A "work in progress" AST, it is just a object used to build the {@see AST}
 * @property {TmpToken[]} tokens - the direct descendant of the root tree
 */
 
/**
 * @typedef {object} Token
 * Represents a Node in the {@link AST}
 * @property {number} start - text occurrence starting position (start index), number is inclusive
 * @property {number} end - text occurrence ending position (end index), number is exclusive
 * @property {StateValues} type - the node type
 * @property {string} text - substring of the {@link AST.key} with `start` and `end`
 * @property {Token[]} childTokens - direct child tokens of the node
 */
 
/**
 * @typedef {object} TmpToken
 * Temporary {@link Token}, used to create the final {@link Token}
 * @property {AST_In_Progress | TmpToken} parentNode - parent node
 * @property {number} start - text occurrence starting position (start index), number is inclusive
 * @property {number} end - text occurrence ending position (end index), number is exclusive
 * @property {StateValues} type - the node type
 * @property {TmpToken[]} childTokens - direct child temporary tokens of the node
 */
 
/** @typedef {keyof typeof states} StateNames */
/** @typedef {typeof states[StateNames]} StateValues */