-
Notifications
You must be signed in to change notification settings - Fork 1
/
glyph_serial.js
208 lines (183 loc) · 5.94 KB
/
glyph_serial.js
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
const glyphMatrix = require('./glyph_matrix.js');
class Glyph {
constructor() {
this.arch = [];
this.part = [];
}
toStringLite() {
if(this.part.length==0) return '';
let oo = '';
for(let i=0; i<this.part.length; i++) {
if((typeof this.part[i]) == 'object') {
oo += this.part[i].toStringLite();
} else if ((typeof this.part[i]) == 'string') {
oo += this.part[i];
}
}
return oo;
}
/*
toString() {
if(this.part.length==0) return '';
let oo = '';
let rightBrack = '';
let archSet = new Set(this.arch);
if(archSet.size==0) {
oo += this.part[0].toString();
} else if(archSet.has('^')||archSet.has('<')||archSet.has('2')||archSet.has('3')||archSet.has('4')) {
oo += this.arch.join('') + '(' + this.part[0].toString(); rightBrack=')';
} else if(archSet.has('@')||(!archSet.has('_')&&archSet.has('%'))) {
oo += this.part[0].toString();
if(this.arch.join('')=='%') oo += this.arch.join('');
oo += '{';
rightBrack='}';
} else if(archSet.has('?')) {
oo += this.arch.join('');
oo += '[';
oo += this.part[0].toString();
rightBrack=']';
} else if(archSet.has('~')||archSet.has('!')||archSet.has('#')||archSet.has('$')||archSet.has('_')) {
oo += this.arch.join('');
oo += this.part[0].toString();
} else {
if((typeof this.part[0])!='undefined') oo += this.part[0].toString();
rightBrack='';
}
for(let i=1; i<this.part.length; i++) {
oo += this.part[i].toString();
}
return oo += rightBrack;
}
//*/
toString() {
if(this.part.length==0) return '';
let oo = '';
let rightBrack = '';
switch(this.arch.join('')) {
case '' : oo += this.part[0].toString(); break;
case '2': case '2^': case '2<': case '3': case '4':
case '^': case '<': case '-<': case '-^' :
oo += this.arch.join('') + '(' + this.part[0].toString(); rightBrack=')'; break;
case '@' :
case '%' :
oo += this.part[0].toString();
if(this.arch.join('')=='%') oo += this.arch.join('');
oo += '{';
rightBrack='}';
break;
case '?':
oo += this.arch.join('');
oo += '[';
oo += this.part[0].toString();
rightBrack=']';
break;
case '~': case '!': case '#':
case '$':
oo += this.arch.join('');
oo += this.part[0].toString(); break;
default:
oo += this.arch.join('');
//console.log(typeof this.part[0]); console.log(this.part[0]);
if((typeof this.part[0])!='undefined')
oo += this.part[0].toString(); rightBrack='';
break;
}
for(let i=1; i<this.part.length; i++) {
oo += this.part[i].toString();
}
return oo += rightBrack;
}
static parse(s) {
// if(s instanceof Glyph) return s;
let retGlyph = new Glyph();
if(s==null) return retGlyph;
// if((typeof s)!='string' && (typeof s)!='object') return retGlyph;
//console.log(typeof s[Symbol.iterator] === 'function');
//console.log(Symbol.iterator in Object(s));
let oo = []; for(let o of s) oo.push(o); //不能使用 s.split('') //例:s = '㗰𠳝'; 分解單字出錯
//let oo = Array.from(s);
const op = '?~!@#$%^&*_-<234';
const opbrackets = '{[()]}?~!@#$%^&*_-<234';
const leftBrackets = '{[(';
const rightBrackets = ')]}';
function getRightBrackets(s) {
switch(s) {
case '(' : return ')';
case '[' : return ']';
case '{' : return '}';
default: return '';
}
}
while(oo.length>0) {
if(opbrackets.indexOf(oo[0])==-1 && oo.length>0) {
retGlyph.part.push(oo.shift());
continue;
}
if(op.indexOf(oo[0])!=-1 || leftBrackets.indexOf(oo[0])!=-1) {
let subGlyph = new Glyph();
while(op.indexOf(oo[0])!=-1) subGlyph.arch.push(oo.shift());
while(rightBrackets.indexOf(oo[0])!=-1) oo.shift(); //去掉右括號
if(opbrackets.indexOf(oo[0])==-1 && oo.length>0) {
subGlyph.part.push(oo.shift());
if(subGlyph.arch.includes('*')) {
subGlyph.arch.splice(subGlyph.arch.indexOf('*'),1);
subGlyph.arch.push('@');
subGlyph.part.push(retGlyph.part.pop());
}
retGlyph.part.push(subGlyph);
continue;
} else if(leftBrackets.indexOf(oo[0])!=-1) {
let leftBracket = oo.shift();
let rightBracket = getRightBrackets(leftBracket);
let matchBracket = 1;
while(oo.length>0 && matchBracket>0) {
if(leftBracket.indexOf(oo[0])!=-1) matchBracket++;
else if(rightBracket.indexOf(oo[0])!=-1) matchBracket--;
subGlyph.part.push(oo.shift());
}
subGlyph.part = Glyph.parse(subGlyph.part).part; //對括號內的元素作遞歸操作
let archSet = new Set(subGlyph.arch);
if(archSet.has('-')) {
archSet.delete('-');
subGlyph.part.push(subGlyph.part.shift());
}
if(leftBracket.indexOf('{')!=-1) {
if(!(archSet.has('%')||archSet.has('*'))) {
archSet.add('@');
subGlyph.part.unshift(retGlyph.part.pop());
} else if(archSet.has('%')) {
subGlyph.part.unshift(retGlyph.part.pop());
}
} else if(leftBracket.indexOf('[')!=-1) {
archSet.add('?');
}
if(archSet.has('*')) { //星號構造符的特殊性:後置前並包含前符。
archSet.delete('*');
let tmpGlyph = new Glyph();
tmpGlyph.arch.push('@');
subGlyph.arch = Array.from(archSet);
if(subGlyph.arch.length==0 && subGlyph.part.length>1) subGlyph.arch.push('?');
tmpGlyph.part.push(subGlyph);
tmpGlyph.part.push(retGlyph.part.pop());
retGlyph.part.push(tmpGlyph);
continue;
}
{
subGlyph.arch = Array.from(archSet);
retGlyph.part.push(subGlyph);
}
continue;
}
}
oo.shift(); //去掉右括號
}
if(retGlyph.arch.length==0 && retGlyph.part.length==1 && retGlyph.part[0] instanceof Glyph) {
// retGlyph = retGlyph.part[0];
}
return retGlyph;
}
static parseThinner(s) {
return Glyph.parse(Glyph.parse(s).toString());
}
}
module.exports = Glyph;