Created
July 31, 2020 12:04
-
-
Save hube12/7d9809860a582b3138fe15a36744dad7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_pre_block(id) { | |
let pre_block = document.getElementById(id) | |
if (pre_block == null || pre_block.childElementCount === 0) { | |
return null; | |
} | |
let children = pre_block.childNodes | |
let res = "" | |
for (const child of children) { | |
if (child.nodeType === 3) { // text node | |
res += child.data.replace(/\\\*.*(?=\*\\)\*\\/g, "") | |
} | |
} | |
res = res.replace(/\/\*.*?(?=\*\/)\*\//g, "") | |
let final = "" | |
let removeSpaces = true; | |
for (const char of res) { | |
if (char === '"') { | |
removeSpaces = !removeSpaces | |
} | |
if (removeSpaces) { | |
if(char !== " "){ | |
final += char | |
} | |
}else{ | |
final += char | |
} | |
} | |
return parse_block(final,0)[0]; | |
} | |
// block:= '{' text ':' '{' (decl|block) (',' (decl|block))* '}' '}' | |
// text:= '"' .*? '"' | |
// decl:= text ':' | |
// ( '[' text (',' text )* ']' | |
// | text | |
// | block ) | |
// whitespace:=[ \r\n\t]* '/*' [ \r\n\t.(?=*/)]* '*/' | |
// final grammar, remove all whitespace (except in text before) | |
// block= '{' decl (',' decl)* ','? '}' | |
// decl= (text ':' ( text | array | block )) | block | |
// array= '[' text (',' text)* ','? ']' | |
// text='"' .*? '"' | |
function parse_block(string, index) { | |
if (string[index] !== '{') { | |
console.log("Error on outer block, missing first bracket") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
index++ | |
let acc = [] | |
while (true){ | |
let delc_r | |
delc_r = parse_decl(string, index); | |
if (delc_r === null) { | |
console.log("Error on decl") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
let decl = delc_r[0]; | |
acc.push(decl) | |
console.log(decl) | |
index = delc_r[1]; | |
if (string[index]===','){ | |
index++; | |
if (string[index]==='}'){ // this is to accomodate for a trailing comma | |
break | |
} | |
continue | |
} | |
break | |
} | |
if (string[index] !== '}') { | |
console.log("Error on outer block, missing last bracket") | |
console.log(string.slice(index - 10, index)) | |
return null; | |
} | |
index++; | |
return [acc, index] | |
} | |
function parse_decl(string, index) { | |
if (string[index]==='{'){ //standalone block | |
console.log("DEBUG: Standalone block") | |
let res=parse_block(string,index); | |
if (res==null){ | |
console.log("error on parsing block") | |
return null; | |
} | |
return [res[0],res[1]]; | |
} | |
console.log("DEBUG: Declaration: (decl|array|block) >") | |
let text_r = parse_text(string, index) | |
if (text_r === null) { | |
console.log("Error on text in decl") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
let text = text_r[0]; | |
index = text_r[1]; | |
if (string[index] !== ':') { | |
console.log("Error on outer block, missing colon") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
index++; | |
if (string[index] === '[') { | |
console.log("DEBUG: array") | |
index++; | |
let acc = [] | |
while (true){ | |
let text_r = parse_text(string, index); | |
if (text_r === null) { | |
console.log("Error on text") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
let text_array = text_r[0]; | |
acc.push(text_array) | |
index = text_r[1]; | |
if (string[index]===','){ | |
index++; | |
if (string[index]==='}'){ //this is to accomodate for trailing comma | |
break | |
} | |
continue | |
} | |
break | |
} | |
if (string[index]!==']'){ | |
console.log("Wrongfully terminated array"); | |
return null; | |
} | |
index++; | |
return [[text, acc], index] | |
} else if (string[index] === '{') { | |
console.log("DEBUG: block") | |
let res=parse_block(string,index); | |
if (res == null) { | |
console.log("Error on parsing block") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
return [[text, res[0]], res[1]] | |
}else{ | |
console.log("DEBUG: champ") | |
let text_r = parse_text(string, index); | |
if (text_r == null) { | |
console.log("Error on parsing text") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
return [[text,text_r[0]],text_r[1]] | |
} | |
} | |
function parse_text(string, index) { | |
if (string[index] !== '"') { | |
console.log("Error on parsing text") | |
console.log(string.slice(index, index + 10)) | |
return null; | |
} | |
index++ | |
let acc = '' | |
while (index < string.length) { | |
if (string[index] === '"') { | |
return [acc, index + 1] | |
} | |
acc += string[index] | |
index++; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment