All files tokenizer.ts

92.13% Statements 164/178
88.72% Branches 118/133
95% Functions 19/20
92.26% Lines 155/168

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 3263x                                       3x   3x   3x 5926x 5926x   463x 463x     3x 1610x                         3x 1x 1x 1x     3x 552x 552x 1476x 1476x 1472x     552x     3x                   292x 292x         292x     3x 2457x     3x 1882x 1043x 255x     1627x     3x                   3x 1527x 1182x 1x                   3x 1526x 338x                     3x 292x     3x 1198x 1185x 1410x 1410x     207x 207x                   20x 20x   1183x           1183x           3x 1514x 1514x 316x   1198x 1198x           5926x 5926x     3x 292x       292x       3x 552x 552x 552x 1819x 1819x 1819x 209x 209x   1610x 1282x             15x 15x   1267x       1267x             1267x   328x 328x       552x     3x 13x   13x 13x 142x 142x 142x 6x 6x 136x 13x     13x 13x   123x 123x       13x 13x     3x 1527x 1527x 1527x 1527x 1526x 1526x 217x       3x 1198x     1198x 2x 1196x 13x 1183x 18x 18x 1165x 18x 18x 1147x 77x 77x 1070x 31x 31x 1039x 211x 211x 828x 41x 41x 13x 13x   28x 787x 10x 10x 777x 27x 27x 750x 50x 50x 700x 50x 50x 650x 74x 74x 576x 24x 24x   552x     3x  
import {
  Token,
  nameTok,
  colonTok,
  dotTok,
  starTok,
  bangTok,
  bracketLTok,
  bracketRTok,
  bracketDRTok,
  expandTok,
  parenLTok,
  parenRTok,
  commaTok,
  eofTok,
  ignoreTok,
  braceLTok,
  braceRTok,
  bracketDLTok,
} from './tokens'
import { bracketDContext, Context } from './contexts'
 
const nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/
 
const fullCharCodeAtPos = (input: string, pos: number) => {
  const code = input.charCodeAt(pos)
  if (code <= 0xd7ff || code >= 0xe000) return code
 
  const next = input.charCodeAt(pos + 1)
  return (code << 10) + next - 0x35fdc00
}
 
const isRewordCode = (code: number) =>
  code === 42 ||
  code === 46 ||
  code === 33 ||
  code === 91 ||
  code === 93 ||
  code === 40 ||
  code === 41 ||
  code === 44 ||
  code === 58 ||
  code === 126 ||
  code === 123 ||
  code === 125
 
const getError = (message?: string, props?: any) => {
  const err = new Error(message)
  Object.assign(err, props)
  return err
}
 
const slice = (string: string, start: number, end: number) => {
  let str = ''
  for (let i = start; i < end; i++) {
    let ch = string.charAt(i)
    if (ch !== '\\') {
      str += ch
    }
  }
  return str
}
 
export class Tokenizer {
  public input: string
  public state: {
    context: Context[]
    type: Token
    pos: number
    value?: any
  }
  public type_: Token
  constructor(input: string) {
    this.input = input
    this.state = {
      context: [],
      type: null,
      pos: 0,
    }
    this.type_ = null
  }
 
  curContext() {
    return this.state.context[this.state.context.length - 1]
  }
 
  includesContext(context: Context) {
    for (let len = this.state.context.length - 1; len >= 0; len--) {
      if (this.state.context[len] === context) {
        return true
      }
    }
    return false
  }
 
  unexpect(type?: Token) {
    type = type || this.state.type
    return getError(
      `Unexpect token "${type.flag}" in ${this.state.pos} char.`,
      {
        pos: this.state.pos,
      }
    )
  }
 
  expectNext(type?: Token, next?: Token) {
    if (type && type.expectNext) {
      if (next && !type.expectNext.call(this, next)) {
        throw getError(
          `Unexpect token "${next.flag}" token should not be behind "${type.flag}" token.(${this.state.pos}th char)`,
          {
            pos: this.state.pos,
          }
        )
      }
    }
  }
 
  expectPrev(type?: Token, prev?: Token) {
    if (type && type.expectPrev) {
      Iif (prev && !type.expectPrev.call(this, prev)) {
        throw getError(
          `Unexpect token "${type.flag}" should not be behind "${prev.flag}"(${this.state.pos}th char).`,
          {
            pos: this.state.pos,
          }
        )
      }
    }
  }
 
  match(type?: Token) {
    return this.state.type === type
  }
 
  skipSpace() {
    if (this.curContext() === bracketDContext) return
    loop: while (this.state.pos < this.input.length) {
      const ch = this.input.charCodeAt(this.state.pos)
      switch (ch) {
        case 32:
        case 160:
          ++this.state.pos
          break
 
        case 13:
          if (this.input.charCodeAt(this.state.pos + 1) === 10) {
            ++this.state.pos
          }
 
        case 10:
        case 8232:
        case 8233:
          ++this.state.pos
          break
        default:
          Iif (
            (ch > 8 && ch < 14) ||
            (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch)))
          ) {
            ++this.state.pos
          } else {
            break loop
          }
      }
    }
  }
 
  next() {
    this.type_ = this.state.type
    if (this.input.length <= this.state.pos) {
      return this.finishToken(eofTok)
    }
    this.skipSpace()
    this.readToken(
      this.getCode(),
      this.state.pos > 0 ? this.getCode(this.state.pos - 1) : -Infinity
    )
  }
 
  getCode(pos = this.state.pos) {
    return fullCharCodeAtPos(this.input, pos)
  }
 
  eat(type) {
    Iif (this.match(type)) {
      this.next()
      return true
    } else {
      return false
    }
  }
 
  readKeyWord() {
    let startPos = this.state.pos,
      string = ''
    while (true) {
      let code = this.getCode()
      let prevCode = this.getCode(this.state.pos - 1)
      if (this.input.length === this.state.pos) {
        string = slice(this.input, startPos, this.state.pos + 1)
        break
      }
      if (!isRewordCode(code) || prevCode === 92) {
        if (
          code === 32 ||
          code === 160 ||
          code === 10 ||
          code === 8232 ||
          code === 8233
        ) {
          string = slice(this.input, startPos, this.state.pos)
          break
        }
        Iif (code === 13 && this.input.charCodeAt(this.state.pos + 1) === 10) {
          string = slice(this.input, startPos, this.state.pos)
          break
        }
        Iif (
          (code > 8 && code < 14) ||
          (code >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(code)))
        ) {
          string = slice(this.input, startPos, this.state.pos)
          break
        }
        this.state.pos++
      } else {
        string = slice(this.input, startPos, this.state.pos)
        break
      }
    }
 
    this.finishToken(nameTok, string)
  }
 
  readIngoreString() {
    let startPos = this.state.pos,
      prevCode,
      string = ''
    while (true) {
      let code = this.getCode()
      Iif (this.state.pos >= this.input.length) break
      if ((code === 91 || code === 93) && prevCode === 92) {
        this.state.pos++
        prevCode = ''
      } else if (code == 93 && prevCode === 93) {
        string = this.input
          .slice(startPos, this.state.pos - 1)
          .replace(/\\([\[\]])/g, '$1')
        this.state.pos++
        break
      } else {
        this.state.pos++
        prevCode = code
      }
    }
 
    this.finishToken(ignoreTok, string)
    this.finishToken(bracketDRTok)
  }
 
  finishToken(type: Token, value?: any) {
    const preType = this.state.type
    this.state.type = type
    if (value !== undefined) this.state.value = value
    this.expectNext(preType, type)
    this.expectPrev(type, preType)
    if (type.updateContext) {
      type.updateContext.call(this, preType)
    }
  }
 
  readToken(code: number, prevCode: number) {
    Iif (prevCode === 92) {
      return this.readKeyWord()
    }
    if (this.input.length <= this.state.pos) {
      this.finishToken(eofTok)
    } else if (this.curContext() === bracketDContext) {
      this.readIngoreString()
    } else if (code === 123) {
      this.state.pos++
      this.finishToken(braceLTok)
    } else if (code === 125) {
      this.state.pos++
      this.finishToken(braceRTok)
    } else if (code === 42) {
      this.state.pos++
      this.finishToken(starTok)
    } else if (code === 33) {
      this.state.pos++
      this.finishToken(bangTok)
    } else if (code === 46) {
      this.state.pos++
      this.finishToken(dotTok)
    } else if (code === 91) {
      this.state.pos++
      if (this.getCode() === 91) {
        this.state.pos++
        return this.finishToken(bracketDLTok)
      }
      this.finishToken(bracketLTok)
    } else if (code === 126) {
      this.state.pos++
      this.finishToken(expandTok)
    } else if (code === 93) {
      this.state.pos++
      this.finishToken(bracketRTok)
    } else if (code === 40) {
      this.state.pos++
      this.finishToken(parenLTok)
    } else if (code === 41) {
      this.state.pos++
      this.finishToken(parenRTok)
    } else if (code === 44) {
      this.state.pos++
      this.finishToken(commaTok)
    } else if (code === 58) {
      this.state.pos++
      this.finishToken(colonTok)
    } else {
      this.readKeyWord()
    }
  }
}