From 884a72950981d57409c175430e49a6ae4f0db644 Mon Sep 17 00:00:00 2001 From: ScottDowne Date: Fri, 12 Feb 2010 10:23:14 -0800 Subject: [PATCH] Ticket #226: Parsing indifferent to string content --- processing.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/processing.js b/processing.js index cd358d8e0..90aae4851 100644 --- a/processing.js +++ b/processing.js @@ -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 + // 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)/, ""); + } + // 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"); @@ -287,6 +296,48 @@ // Convert 3.0f to just 3.0 aCode = aCode.replace(/(\d+)f/g, "$1"); + // replaces all masked strings from 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("(.*)(\)(.*)", "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; };