MCPcopy Create free account
hub / github.com/Moli-X/Resources / parser

Function parser

Script/Parser.js:3204–3267  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

3202 }
3203
3204 function parser(str) {
3205 var regLevel = regex["regLevel"];
3206 var invalidLine = regex["invalidLine"];
3207 var lines = str.split("\n");
3208 var m;
3209 var level = 0, curLevel = 0;
3210
3211 var blocks = [];
3212
3213 var result = new Block(-1);
3214 var currentBlock = new Block(0);
3215 result.addChild(currentBlock);
3216 var levels = [];
3217 var line = "";
3218
3219 blocks.push(currentBlock);
3220 levels.push(level);
3221
3222 for(var i = 0, len = lines.length; i < len; ++i) {
3223 line = lines[i];
3224
3225 if(line.match(invalidLine)) {
3226 continue;
3227 }
3228
3229 if(m = regLevel.exec(line)) {
3230 level = m[1].length;
3231 } else
3232 level = 0;
3233
3234 if(level > curLevel) {
3235 var oldBlock = currentBlock;
3236 currentBlock = new Block(level);
3237 oldBlock.addChild(currentBlock);
3238 blocks.push(currentBlock);
3239 levels.push(level);
3240 } else if(level < curLevel) {
3241 var added = false;
3242
3243 var k = levels.length - 1;
3244 for(; k >= 0; --k) {
3245 if(levels[k] == level) {
3246 currentBlock = new Block(level);
3247 blocks.push(currentBlock);
3248 levels.push(level);
3249 if(blocks[k].parent!= null)
3250 blocks[k].parent.addChild(currentBlock);
3251 added = true;
3252 break;
3253 }
3254 }
3255
3256 if(!added) {
3257 errors.push("Error: Invalid indentation at line " + i + ": " + line);
3258 return;
3259 }
3260 }
3261

Callers 1

YAMLFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected