Skip to content

Commit

Permalink
Ticket processing-js#226: Parsing indifferent to string content
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDowne committed Feb 12, 2010
1 parent 051a398 commit 884a729
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion processing.js
Expand Up @@ -75,8 +75,17 @@

// Parse Processing (Java-like) syntax to JavaScript syntax with Regex
Processing.parse = function parse(aCode, p) {

// Saves all strings into an array
// masks all strings into <STRING n>
// to be replaced with the array strings after parsing is finishes
var strings = aCode.match(/(["'])(\\\1|.)*?(\1)/g);
for ( var i = 0; /(["'])(\\\1|.)*?(\1)/.test(aCode); i++){
aCode = aCode.replace(/(["'])(\\\1|.)*?(\1)/, "<STRING " + i + ">");
}

// Remove end-of-line comments
aCode = aCode.replace(/\/\/ .*\n/g, "\n");
aCode = aCode.replace(/\/\/.*\n/g, "\n");

// Weird parsing errors with %
aCode = aCode.replace(/([^\s])%([^\s])/g, "$1 % $2");
Expand Down Expand Up @@ -287,6 +296,48 @@
// Convert 3.0f to just 3.0
aCode = aCode.replace(/(\d+)f/g, "$1");

// replaces all masked strings from <STRING n> to the appropriate string contained in the strings array
if (strings != null){

for( var i = 0; l = i < strings.length; i++ ){
aCode = aCode.replace(new RegExp("(.*)(\<STRING " + i + "\>)(.*)", "g"), function(all, quoteStart, match, quoteEnd){

var returnString = all, notString = true, quoteType = "", escape = false;

for (var x = 0; x < quoteStart.length; x++){

if (notString){

if (quoteStart.charAt(x) == "\"" || quoteStart.charAt(x) == "'"){
quoteType = quoteStart.charAt(x);
notString = false;
}
}
else{

if (!escape){

if (quoteStart.charAt(x) == "\\"){
escape = true;
}
else if (quoteStart.charAt(x) == quoteType){
notString = true;
quoteType = "";
}
}
else{ escape = false; }
}
}

if(notString){ // Match is not inside a string
returnString = quoteStart + strings[i] + quoteEnd;
}

return returnString;
});
}
}

return aCode;
};

Expand Down

0 comments on commit 884a729

Please sign in to comment.