escape reserved AWK and our names: prefix conflicting variables with _ v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 11 May 2019 22:55:21 +0200
branchv_0
changeset 24 c805c968b7ed
parent 23 bc20d499a458
child 25 13a1e1134797
escape reserved AWK and our names: prefix conflicting variables with _
src/AwkHandler.h
--- a/src/AwkHandler.h	Sat May 11 22:47:49 2019 +0200
+++ b/src/AwkHandler.h	Sat May 11 22:55:21 2019 +0200
@@ -145,10 +145,11 @@
 	}
 
 	string_t a2v(const string_t& attributeName) {
-		// FIXME: escape reserved names; prefix with _ ?
 		// cat awkgram.y | awk -v FS='\\{"|",' -v ORS='|' '/static const struct token tokentab/, /\};/ { if (/^\{/) { print $2} }'
-		// BEGIN|BEGINFILE|END|ENDFILE|adump|and|asort|asorti|atan2|bindtextdomain|break|case|close|compl|continue|cos|dcgettext|dcngettext|default|delete|do|else|eval|exit|exp|fflush|for|func|function|gensub|getline|gsub|if|in|include|index|int|intdiv0|isarray|length|load|log|lshift|match|mktime|namespace|next|nextfile|or|patsplit|print|printf|rand|return|rshift|sin|split|sprintf|sqrt|srand|stopme|strftime|strtonum|sub|substr|switch|system|systime|tolower|toupper|typeof|while|xor
-		return attributeName;
+		std::wregex awkReservedNames(L"BEGIN|BEGINFILE|END|ENDFILE|adump|and|asort|asorti|atan2|bindtextdomain|break|case|close|compl|continue|cos|dcgettext|dcngettext|default|delete|do|else|eval|exit|exp|fflush|for|func|function|gensub|getline|gsub|if|in|include|index|int|intdiv0|isarray|length|load|log|lshift|match|mktime|namespace|next|nextfile|or|patsplit|print|printf|rand|return|rshift|sin|split|sprintf|sqrt|srand|stopme|strftime|strtonum|sub|substr|switch|system|systime|tolower|toupper|typeof|while|xor");
+		std::wregex trReservedNames(L"_escape|_unescape|_readVariables|_writeVariables|record");
+		if (regex_match(attributeName, awkReservedNames) || regex_match(attributeName, trReservedNames)) return a2v(L"_" + attributeName);
+		else return attributeName;
 	}
 
 	string_t escapeAwkValue(const string_t& value) {