首页 > 其他 > 详细

code转tokens

时间:2019-07-09 20:00:41      阅读:117      评论:0      收藏:0      [点我收藏+]
const fs=require(‘fs‘);
const code=fs.readFileSync(‘../code.js‘).toString()

const tokens=[]
let obj={
start:0,
end:0,
value:null,
line:1,
pos:0
}
function cut(start,end){
if(end-start>0&&end<code.length+1){
tokens.push({
start:start,
end:end,
value:code.substring(start,end),
})
}
}
function start(n=1) {
obj.pos=obj.pos+n;
obj.start=obj.pos;
}
//封闭符号
const closeSymbol=[
//注释
[‘//‘,function (code,pos) {
if(pos===code.length-1||code[pos+1]===‘\n‘||code[pos+1]===‘\r‘){
return 0;
}
return -1;
}],
[‘/*‘,‘*/‘],
[‘<!--‘,‘-->‘],
//正则
[function (code,pos) {
if(code[pos]===‘/‘){
let pre=pos-1
while(code[pre]===‘ ‘){
pre--;
}
if(Punctuation.indexOf(code[pre])>-1||Operators.indexOf(code[pre])>-1){
return 0
}
}
return -1;

},function (code,pos) {
if(code[pos]===‘/‘){
let post=pos+1
while(!(code[post]===‘;‘||code[post]===‘\n‘||post===code.length-1)){
post++;
}
return post-pos;
}
return -1;
}],
[‘"‘,‘"‘],
["‘","‘"],
[‘<‘,‘/>‘],
[‘<‘,‘>‘],

];
//分隔符
const Punctuation=[
‘...‘,
‘{|‘,
‘|}‘,
‘::‘,
‘=>‘,
‘${‘,
‘(‘,
‘)‘,
‘,‘,
‘@‘,
‘:‘,
‘[‘,
‘.‘,
‘?‘,
‘]‘,
‘{‘,
‘`‘,
‘}‘,
‘;‘
];
//运算符
const Operators=[
‘!==‘,
‘===‘,
‘!=‘,
‘**‘,
‘-=‘,
‘++‘,
‘--‘,
‘||‘,
‘&&‘,
‘>>‘,
‘<<‘,
‘+=‘,
‘==‘,
‘+‘,
‘&‘,
‘>‘,
‘^‘,
‘|‘,
‘<‘,
‘-‘,
‘%‘,
‘*‘,
‘/‘,
‘=‘
];

//字符串,封闭符号
function isCloseSymbol(code,pos,arr) {
let isClose=false;
let index=-1;
for(let i=0;i<arr.length;i++){
let match=true;
if(typeof arr[i][0]===‘function‘){
const num=arr[i][0](code,pos);
if(num===-1){
match=false;
}
}else{
for(let j=0;j<arr[i][0].length;j++){
if(arr[i][0].charCodeAt(j)!==code.charCodeAt(pos+j)){
match=false;
break;
}
}
}

if(match){
index=i;
break;
}
}

if(index!==-1){

const arr2=arr[index];
let pos2=0;
for(let i=pos+arr2[0].length;i<code.length;i++){
if(code.charCodeAt(i-1)===92){
continue;
}
let match=true;
if(typeof arr2[1]===‘function‘){
const num=arr2[1](code,i);
if(num===-1){
match=false;
}else{
pos2=i+num;
}

}else {
for (let j = 0; j < arr2[1].length; j++) {
if (arr2[1].charCodeAt(j) !== code.charCodeAt(i + j)) {
match = false;
break;
}
}
pos2=i+arr2[1].length;
}
if(match){
cut(obj.start,obj.pos);
cut(obj.pos,pos2);
start(pos2-obj.pos)
isClose=true;
break;
}
}

}
return isClose;

}
//空符号
function isEmpty(code,pos) {
const arr=[‘\n‘,‘\r‘,‘ ‘]
let index=-1;
for(let i=0;i<arr.length;i++){
let match=true;
for(let j=0;j<arr[i].length;j++){
if(arr[i].charCodeAt(j)!==code.charCodeAt(pos+j)){
match=false;
break;
}
}
if(match){
index=i;
break;
}
}
if(index!==-1){
cut(obj.start,obj.pos);
const len=arr[index].length
if(arr[index]===‘\n‘){
obj.line++;
}
// cut(obj.pos,obj.pos+len);
start(len)
}
return index!==-1;
}

//分割符号
function isSplitSymbol(code,pos,arr) {
let index=-1;
for(let i=0;i<arr.length;i++){
let match=true;
for(let j=0;j<arr[i].length;j++){
if(arr[i].charCodeAt(j)!==code.charCodeAt(pos+j)){
match=false;
break;
}
}
if(match){
index=i;
break;
}
}
if(index!==-1){
cut(obj.start,obj.pos);
const len=arr[index].length
cut(obj.pos,obj.pos+len);
start(len)
}
return index!==-1;
}

//code转tokens
function CodeToTokens(code){
console.log(code)
while (obj.pos<code.length) {

if(isEmpty(code,obj.pos)||isCloseSymbol(code,obj.pos,closeSymbol)||isSplitSymbol(code,obj.pos,Operators)||isSplitSymbol(code,obj.pos,Punctuation)){
console.log(21)
}else{
obj.pos++
}
}
cut(obj.start,obj.pos);

console.log(tokens)
}

module.exports=CodeToTokens;

console.log(CodeToTokens(code))

code转tokens

原文:https://www.cnblogs.com/caoke/p/11159643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!