UNPKG

4.36 kBJavaScriptView Raw
1class TokenBuffer extends Array {
2 constructor (items) {
3 typeof items === 'number' ? super(items) : super()
4
5 // if (Array.isArray(items)) {
6 // items.forEach((item) => this.push(item))
7 // }
8 // this.index = 0
9 // this.indentionSize = 2
10 }
11
12 find (type, value, index = 0) {
13 for (let i = index; i < this.length; i++) {
14 if (this.match(type, value, i)) {
15 return i
16 }
17 }
18
19 return -1
20 }
21
22 match (type, value, index = 0) {
23 const token = this[index]
24 // console.log('MATCH INDEX', token, type, value, index)
25 if (!token) {
26 return false
27 }
28
29 if (type === 'indention' && value) {
30 switch (value) {
31 case '>=': return token.value >= token.indention
32 case '>': return token.value > token.indention
33 case '==': return token.value === token.indention
34 case '<': return token.value < token.indention
35 case '<=': return token.value <= token.indention
36 }
37
38 return false
39 }
40
41 if (value && Array.isArray(value)) {
42 return value.indexOf(token.value) >= 0
43 } else if (value && value instanceof RegExp) {
44 return value.test(token.value)
45 } else if (value && token.value !== value) {
46 return false
47 }
48
49 return Array.isArray(type)
50 ? type.some((t) => this.matchType(token, t))
51 : this.matchType(token, type)
52 }
53
54 matchType (token, type) {
55 if (type === 'keyword') {
56 return token.type === 'identifier' && token.isKeyword
57 }
58
59 return token.type === type
60 }
61
62 getIndention () {
63 return this.length ? this[0].indention : 0
64 }
65
66 // match (type, value, offset) {
67 // return this.expect(type, value, this.index + offset)
68 // }
69
70 // lastIndention (mode, indention) {
71 // indention = Math.max(indention, 0)
72 //
73 // const currentIndention = this.getIndention()
74 // // console.log('INDENTION CHECK', currentIndention, mode, indention)
75 //
76 // if (mode === 'eq') {
77 // return currentIndention === indention
78 // } else if (mode === 'lt') {
79 // return currentIndention < indention
80 // } else if (mode === 'lte') {
81 // return currentIndention <= indention
82 // } else if (mode === 'gt') {
83 // return currentIndention > indention
84 // } else if (mode === 'gte') {
85 // return currentIndention >= indention
86 // } else {
87 // throw new Error(`Wrong mode param! '${mode}'`)
88 // }
89 // }
90
91 // isIndention (mode, indention, curIndention) {
92 // if (typeof indention === 'object') {
93 // if (indention.type !== 'indention') {
94 // return false
95 // }
96 //
97 // indention = indention.value
98 // }
99 //
100 // indention = Math.max(indention, 0)
101 // const token = this.current()
102 //
103 // if (curIndention === undefined && token && token.type !== 'indention') {
104 // return false
105 // }
106 //
107 // const currentIndention = curIndention || (token === null ? 0 : token.value)
108 // // console.log('INDENTION CHECK', currentIndention, mode, indention)
109 //
110 // if (mode === 'eq') {
111 // return currentIndention === indention
112 // } else if (mode === 'lt') {
113 // return currentIndention < indention
114 // } else if (mode === 'lte') {
115 // return currentIndention <= indention
116 // } else if (mode === 'gt') {
117 // return currentIndention > indention
118 // } else if (mode === 'gte') {
119 // return currentIndention >= indention
120 // } else {
121 // throw new Error(`Wrong mode param! '${mode}'`)
122 // }
123 // }
124 //
125 // print (msg) {
126 // const startIndex = Math.max(this.index - 2, 0)
127 // const endIndex = Math.min(startIndex + 5, this.length)
128 // // console.log('Len:', this.length)
129 // // console.log('Start:', startIndex)
130 // // console.log('End:', endIndex)
131 // // console.log('Index:', this.index)
132 // // console.log('Stack', this)
133 // const items = this.slice(startIndex, endIndex)
134 // if (msg) {
135 // console.log(` + ${msg}`)
136 // }
137 //
138 // // console.log('Items:', items)
139 // items.forEach((item, index) => {
140 // const arrow = this.index - startIndex === index ? '>' : ' '
141 // console.log(`${arrow}| ${item.type} ${item.value}`)
142 // })
143 // }
144 //
145 // lastItem () {
146 // return this.index >= this.length - 1
147 // }
148 //
149 // changeType (type) {
150 // this[this.index].type = type
151 // }
152}
153
154module.exports = TokenBuffer