From 1453ab784dc40c4b4936e48dc5bdaae452138c7e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Oct 2015 16:09:28 -0500 Subject: [PATCH 1/2] Variable detection enhancements: In addition to parsing out commands and arguements, now also detect and record the variables used and modified on each line. Top level object has three children to track all variables, newed variables, and changed variables. --- lib/parse.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index dc185ca..8601962 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -158,7 +158,7 @@ function splitRoutinesAndArguments(inputLine) { } //Extracts Post-Conditionals from Routines, used by extractRoutines. -function extractPostConditional(inputObject) { +function extractPostConditional(inputObject, varObj, changedObj) { //Assign to new variable to truncate Routines. var tmpObject = inputObject; @@ -169,6 +169,10 @@ function extractPostConditional(inputObject) { if (tmpIndex > -1) { tmpObject[i].mPostConditional = tmpObject[i].mRoutine.substring(tmpIndex + 1); tmpObject[i].mRoutine = tmpObject[i].mRoutine.substring(0, tmpIndex); + + //jrg - extract variables + extractVars(tmpObject[i].mPostConditional,varObj,changedObj); + } } } @@ -180,9 +184,14 @@ function extractPostConditional(inputObject) { function extractRoutines(inputLine, inputObject) { var tmpFunction = {}; + var changedObj = {}; var tmpFunctionArray = []; + var varObj = {}; + var newedVarObj = {}; + var args,idx; //Divide Routines and Arguments. + inputLine = inputLine.replace(/^\s+/,""); //kill off any leading spaces that slipped through var splitLine = splitRoutinesAndArguments(inputLine); //Loop, even is function, odd is arguments. @@ -207,6 +216,27 @@ function extractRoutines(inputLine, inputObject) { } } + //jrg - extract variables + args = tmpFunction.mArguments; + //if it's a do, the peice before ( is the function. Don't consider that a variable + if (tmpFunction.mRoutine == "d") + { + idx = args.indexOf('('); + if (idx > 0) { + extractVars(args.substring(args.indexOf('(')+1,args.length),varObj,changedObj); + } + } + + else if (tmpFunction.mRoutine == "n") { + extractVars(args,newedVarObj,changedObj); //can't actually have changed vars here, we'll prevent the crash in case of typos + } + + else { + extractVars(args,varObj,changedObj); + } + + + tmpFunction.changedVars = changedObj; tmpFunctionArray.push(tmpFunction); tmpFunction = {}; @@ -224,13 +254,62 @@ function extractRoutines(inputLine, inputObject) { } //Extract and output Post Conditionals. - var postConditionFunctionArray = extractPostConditional(tmpFunctionArray); + var postConditionFunctionArray = extractPostConditional(tmpFunctionArray,varObj, changedObj); if (postConditionFunctionArray.length > 0) { inputObject.lineRoutines = postConditionFunctionArray; } + + inputObject.vars = varObj; + inputObject.newedVars = newedVarObj; + inputObject.changedVars = changedObj; return inputObject; } +//jrg - function to add variables + +function extractVars(str,obj,changedObj) { + var x, changed, ary, i, j; + str = str.replace(/"(.*?)"/g,""); //kill anything inside of quotes + var regex = /^[a-zA-Z].*$/; //ensure that we don't pick up constants or function calls + str = str.replace(/[-+*#_<>()!\[\]\\]/g,','); //mod out by all of the seperators except equal + + var statements = str.split(','); + for (i in statements) { + changed = false; + + x=statements[i].replace(/\'/g,""); //pick off negate + + if (x.indexOf('=') > 0) { + ary = x.split('='); + changedObj[ary[0]] = true; + + for (j in ary) { + if (regex.test(ary[j])) { + obj[ary[j].replace(':','')] = true; //select statements occasionally produce the trailing colon + } + } + + continue; + } + + if (x.charAt(0) == '.') { + changed = true; + } + + x = x.replace('.',''); //pick off reference + if (regex.test(x)) { + obj[x] = true; + if (changed) { + changedObj[x] = true; + } + } + } + + return obj; +} + + + //Export all routines. module.exports.extractLabel = extractLabel; module.exports.extractComment = extractComment; From 461d7d827c9ffe9aa725cfb56c9e0b3ebc51cef4 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 5 Nov 2015 16:32:25 -0600 Subject: [PATCH 2/2] Improvements and bug fixes in variable detection: -Now it doesn't get arrays and functions confused -Now knows how to detect things passed by indirection as variables --- lib/parse.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 8601962..476bb5a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -270,14 +270,17 @@ function extractRoutines(inputLine, inputObject) { function extractVars(str,obj,changedObj) { var x, changed, ary, i, j; str = str.replace(/"(.*?)"/g,""); //kill anything inside of quotes - var regex = /^[a-zA-Z].*$/; //ensure that we don't pick up constants or function calls + var regex = /^[a-zA-Z][^^]*$/; //ensure that we don't pick up constants or function calls + if (str.indexOf("=") == -1) { + str=str.substring(str.indexOf('(')+1,str.length); //no equals, so this is a function call. The function itself is not a variable + } str = str.replace(/[-+*#_<>()!\[\]\\]/g,','); //mod out by all of the seperators except equal var statements = str.split(','); for (i in statements) { changed = false; - x=statements[i].replace(/\'/g,""); //pick off negate + x=statements[i].replace(/[@\']/g,""); //pick off negate and indirection if (x.indexOf('=') > 0) { ary = x.split('='); @@ -310,6 +313,7 @@ function extractVars(str,obj,changedObj) { + //Export all routines. module.exports.extractLabel = extractLabel; module.exports.extractComment = extractComment;