update to arrayReplacesArglist4.diff
[nedit-bw.git] / macroStringLiterals3.diff
bloba9c92f1e825df02a755fc5beb7a22154b7a24259
1 Unlimited macro string literal length and single-quoted strings
3 Available as a patch:
5 http://sourceforge.net/tracker/?func=detail&atid=311005&aid=1598271&group_id=11005
6 [ 1598271 ] Unlimited macro string literal length, single-quoted strings
7 macroStringLiterals.diff 2006-11-21
9 String literals are scanned twice, firstly to calculate their space
10 requirements, secondly to read their contents into allocated memory.
12 Separate string literals that follow one another are combined into one,
13 avoiding run-time concatenation of the pieces. Also single-quoted string
14 literals are allowed, within which backslash ('\') has no special
15 meaning (so you can't include a single-quote in a single-quoted string).
17 Note that a double-quoted string can be continued over multiple lines
18 by ending each line but the last with a backslash, like in C.
20 2006-11-21:
22 Fixed adjacent string literal merging to allow concatenation with "".
24 ---
26 doc/help.etx | 41 +++++---
27 source/parse.y | 291 ++++++++++++++++++++++++++++++++++-----------------------
28 2 files changed, 206 insertions(+), 126 deletions(-)
30 diff --quilt old/doc/help.etx new/doc/help.etx
31 --- old/doc/help.etx
32 +++ new/doc/help.etx
33 @@ -1945,15 +1945,16 @@ Macro Language
34 conditionally, such as the body of a loop, are surrounded by curly braces
35 "{}".
37 Blank lines and comments are also allowed. Comments begin with a "#" and end
38 with a newline, and can appear either on a line by themselves, or at the end
39 - of a statement.
40 + of a statement line.
42 Statements which are too long to fit on a single line may be split across
43 several lines, by placing a backslash "\" character at the end of each line
44 - to be continued.
45 + to be continued. Note that a comment with a backslash at the end is treated
46 + as a continuation in this way too.
49 3>Data Types
51 The NEdit macro language recognizes only three data types, dynamic character
52 @@ -1971,16 +1972,18 @@ Macro Language
53 a = -1
54 b = 1000
56 4>Character String Constants
58 - Character string constants are enclosed in double quotes. For example:
59 + Character string constants are enclosed in single or double quotes, but the
60 + start and end quotes must be the same character. For example:
62 a = "a string"
63 - dialog("Hi there!", "OK")
64 + dialog('Hi there!', "OK")
66 - Strings may also include C-language style escape sequences:
67 + A double-quoted string may also include C-language style escape
68 + sequences:
70 \\ Backslash \t Tab \f Form feed
71 \" Double quote \b Backspace \a Alert
72 \n Newline \r Carriage return \v Vertical tab
74 @@ -1994,32 +1997,48 @@ Macro Language
75 explicit newlines, and also buffers its output on a per-line basis:
77 t_print("a = " a "\n")
79 Other characters can be expressed as backslash-escape sequences in macro
80 - strings. The format is the same as for regular expressions, described in the
81 - paragraphs headed "Octal and Hex Escape Sequences" of the section
82 - "Metacharacters_", except that an octal escape sequence can start with any
83 - octal digit, not just 0, so the single character string "\0033" is the same
84 - as "\33", "\x1B" and "\e" (for an ASCII version of NEdit).
85 + double-quoted strings. The format is the same as for regular expressions,
86 + described in the paragraphs headed "Octal and Hex Escape Sequences" of the
87 + section "Metacharacters_", except that an octal escape sequence can start with
88 + any octal digit, not just 0, so the single character string "\0033" is the
89 + same as "\33", "\x1B" and "\e" (for an ASCII version of NEdit).
91 Note that if you want to define a regular expression in a macro string,
92 you need to "double-up" the backslashes for the metacharacters with
93 special meaning in regular expressions. For example, the expression
95 (?N(\s|/\*(?n(?:(?!\*/).)*)\*/|//.*\n|\n)+)
97 which matches whitespace or C/C++/Java-style comments, should be written as
98 - a macro string as
99 + a macro double-quoted string as
101 "(?N(\\s|/\\*(?n(?:(?!\\*/).)*)\\*/|//.*\n|\n)+)"
103 (The "\n"s towards the end add literal newline characters to the string. The
104 regular expression interpretation treats the newlines as themselves. It can
105 also interpret the sequence "\\n" as a newline, although the macro string here
106 would then contain a literal backslash followed by a lowercase `N'.)
108 + Alternatively, if you don't need special escapes or a single quote
109 + (apostrophe) in your string (true for this example), just turn the expression
110 + into a single-quoted string, as
112 + '(?N(\s|/\*(?n(?:(?!\*/).)*)\*/|//.*\n|\n)+)'
114 + Neighboring string literals (separated by whitespace or line continuations)
115 + are combined, as if by the concatenation operation before use. For example
117 + "The backslash '" '\' "' is an " \
118 + 'escape only in "double-quoted" strings' "\n"
120 + is treated as a single string ending with a newline character, looking like
122 + The backslash '\' is an escape only in "double-quoted" strings
125 3>Variables
127 Variable names must begin either with a letter (local variables), or a $
128 (global variables). Beyond the first character, variables may also contain
129 diff --quilt old/source/parse.y new/source/parse.y
130 --- old/source/parse.y
131 +++ new/source/parse.y
132 @@ -44,10 +44,11 @@ static int yylex(void);
133 int yyparse(void);
134 static int follow(char expect, int yes, int no);
135 static int follow2(char expect1, int yes1, char expect2, int yes2, int no);
136 static int follow_non_whitespace(char expect, int yes, int no);
137 static Symbol *matchesActionRoutine(char **inPtr);
138 +static int scanString(void);
140 static char *ErrMsg;
141 static char *InPtr;
142 extern Inst *LoopStack[]; /* addresses of break, cont stmts */
143 extern Inst **LoopStackPtr; /* to fill at the end of a loop */
144 @@ -571,16 +572,10 @@ static char skipWhitespace(void)
145 static int yylex(void)
147 int len;
148 Symbol *s;
149 static DataValue value = {NO_TAG, {0}};
150 - static char escape[] = "\\\"ntbrfave";
151 -#ifdef EBCDIC_CHARSET
152 - static char replace[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
153 -#else
154 - static char replace[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
155 -#endif
156 int result;
158 skipWhitespace();
160 /* return end of input at the end of the string */
161 @@ -644,119 +639,14 @@ static int yylex(void)
163 yylval.sym = s;
164 return SYMBOL;
167 - /* Process quoted strings with embedded escape sequences:
168 - For backslashes we recognise hexadecimal values with initial 'x' such
169 - as "\x1B"; octal value (upto 3 oct digits with a possible leading zero)
170 - such as "\33", "\033" or "\0033", and the C escapes: \", \', \n, \t, \b,
171 - \r, \f, \a, \v, and the added \e for the escape character, as for REs.
172 - Disallow hex/octal zero values (NUL): instead ignore the introductory
173 - backslash, eg "\x0xyz" becomes "x0xyz" and "\0000hello" becomes
174 - "0000hello". */
176 - if (*InPtr == '\"') {
177 - char string[MAX_STRING_CONST_LEN], *p = string;
178 - char *backslash;
179 - InPtr++;
180 - while (*InPtr != '\0' && *InPtr != '\"' && *InPtr != '\n') {
181 - if (p >= string + MAX_STRING_CONST_LEN) {
182 - InPtr++;
183 - continue;
185 - if (*InPtr == '\\') {
186 - backslash = InPtr;
187 - InPtr++;
188 - if (*InPtr == '\n') {
189 - InPtr++;
190 - continue;
192 - if (*InPtr == 'x') {
193 - /* a hex introducer */
194 - int hexValue = 0;
195 - const char *hexDigits = "0123456789abcdef";
196 - const char *hexD;
197 - InPtr++;
198 - if (*InPtr == '\0' ||
199 - (hexD = strchr(hexDigits, tolower(*InPtr))) == NULL) {
200 - *p++ = 'x';
202 - else {
203 - hexValue = hexD - hexDigits;
204 - InPtr++;
205 - /* now do we have another digit? only accept one more */
206 - if (*InPtr != '\0' &&
207 - (hexD = strchr(hexDigits,tolower(*InPtr))) != NULL){
208 - hexValue = hexD - hexDigits + (hexValue << 4);
209 - InPtr++;
211 - if (hexValue != 0) {
212 - *p++ = (char)hexValue;
214 - else {
215 - InPtr = backslash + 1; /* just skip the backslash */
218 - continue;
220 - /* the RE documentation requires \0 as the octal introducer;
221 - here you can start with any octal digit, but you are only
222 - allowed up to three (or four if the first is '0'). */
223 - if ('0' <= *InPtr && *InPtr <= '7') {
224 - if (*InPtr == '0') {
225 - InPtr++; /* octal introducer: don't count this digit */
227 - if ('0' <= *InPtr && *InPtr <= '7') {
228 - /* treat as octal - first digit */
229 - char octD = *InPtr++;
230 - int octValue = octD - '0';
231 - if ('0' <= *InPtr && *InPtr <= '7') {
232 - /* second digit */
233 - octD = *InPtr++;
234 - octValue = (octValue << 3) + octD - '0';
235 - /* now do we have another digit? can we add it?
236 - if value is going to be too big for char (greater
237 - than 0377), stop converting now before adding the
238 - third digit */
239 - if ('0' <= *InPtr && *InPtr <= '7' &&
240 - octValue <= 037) {
241 - /* third digit is acceptable */
242 - octD = *InPtr++;
243 - octValue = (octValue << 3) + octD - '0';
246 - if (octValue != 0) {
247 - *p++ = (char)octValue;
249 - else {
250 - InPtr = backslash + 1; /* just skip the backslash */
253 - else { /* \0 followed by non-digits: go back to 0 */
254 - InPtr = backslash + 1; /* just skip the backslash */
256 - continue;
258 - for (i=0; escape[i]!='\0'; i++) {
259 - if (escape[i] == *InPtr) {
260 - *p++ = replace[i];
261 - InPtr++;
262 - break;
265 - /* if we get here, we didn't recognise the character after
266 - the backslash: just copy it next time round the loop */
268 - else {
269 - *p++= *InPtr++;
272 - *p = '\0';
273 - InPtr++;
274 - yylval.sym = InstallStringConstSymbol(string);
275 - return STRING;
276 + /* Process quoted strings */
278 + if (*InPtr == '\"' || *InPtr == '\'') {
279 + return scanString();
282 /* process remaining two character tokens or return single char as token */
283 result = *InPtr++;
284 switch (result) {
285 @@ -856,10 +746,181 @@ static Symbol *matchesActionRoutine(char
286 *inPtr = c;
287 return s;
291 +** Process quoted string literals. These can be in single or double quotes.
292 +** A sequence of string literals separated by whitespace (see skipWhitespace())
293 +** are read as a single string.
295 +** Double-quoted string literals allow embedded escape sequences:
296 +** For backslashes we recognise hexadecimal values with initial 'x' such
297 +** as "\x1B"; octal value (upto 3 oct digits with a possible leading zero)
298 +** such as "\33", "\033" or "\0033", and the C escapes: \", \', \n, \t, \b,
299 +** \r, \f, \a, \v, and the added \e for the escape character, as for REs.
300 +** We disallow hex/octal zero values (NUL): instead ignore the introductory
301 +** backslash, eg "\x0xyz" becomes "x0xyz" and "\0000hello" becomes "0000hello".
302 +** An escaped newline is elided, and the string content continues on the next
303 +** source line.
305 +static int scanString(void)
307 +# define SCANSTRING_WRITE_TO_STRING(p, len, val) \
308 + do { char mc = (val); if (p) { *p++ = mc; } else { ++len; } } while (0)
310 + /* scan the string twice: once to get its size, then again to build it */
311 + char *startPtr = InPtr;
312 + char *p = NULL, *string = NULL;
313 + int len, scan, i;
314 + char stopper, first_stopper = *startPtr;
315 + char *backslash;
316 + int handleBackslash;
318 + static char escape[] = "\\\"ntbrfave";
319 +#ifdef EBCDIC_CHARSET
320 + static char replace[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
321 +#else
322 + static char replace[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
323 +#endif
325 + if (first_stopper != '\"' && first_stopper != '\'')
326 + return yyerror("expected a string");
328 + for (scan = 0; scan < 2; ++scan)
330 + InPtr = startPtr;
331 + stopper = first_stopper;
332 + handleBackslash = (stopper == '\"');
333 + len = 0;
334 + InPtr++;
335 + while (*InPtr != '\0' && *InPtr != '\n') {
336 + if (*InPtr == stopper) {
337 + char *endPtr = InPtr++;
338 + skipWhitespace();
339 + /* is this followed by another string literal? */
340 + if (*InPtr == '\"' || *InPtr == '\'') {
341 + stopper = *InPtr++; /* add it to the end of the first */
342 + handleBackslash = (stopper == '\"');
344 + else {
345 + InPtr = endPtr; /* no further string: restore position */
346 + break;
349 + else if (handleBackslash && *InPtr == '\\') {
350 + backslash = InPtr;
351 + InPtr++;
352 + if (*InPtr == '\n') { /* allows newline to be skipped */
353 + InPtr++;
354 + continue;
356 + if (*InPtr == 'x') {
357 + /* a hex introducer */
358 + int hexValue = 0;
359 + const char *hexDigits = "0123456789abcdef";
360 + const char *hexD;
361 + InPtr++;
362 + if (*InPtr == '\0')
363 + break;
364 + if ((hexD = strchr(hexDigits, tolower(*InPtr))) == NULL) {
365 + SCANSTRING_WRITE_TO_STRING(p, len, 'x');
367 + else {
368 + hexValue = hexD - hexDigits;
369 + InPtr++;
370 + if (*InPtr == '\0')
371 + break;
372 + /* now do we have another digit? only accept one more */
373 + if ((hexD = strchr(hexDigits,tolower(*InPtr))) != NULL){
374 + hexValue = hexD - hexDigits + (hexValue << 4);
375 + InPtr++;
377 + if (hexValue != 0) {
378 + SCANSTRING_WRITE_TO_STRING(p, len, (char)hexValue);
380 + else {
381 + InPtr = backslash + 1; /* just skip the backslash */
384 + continue;
386 + /* the RE documentation requires \0 as the octal introducer;
387 + here you can start with any octal digit, but you are only
388 + allowed up to three (or four if the first is '0'). */
389 + if ('0' <= *InPtr && *InPtr <= '7') {
390 + if (*InPtr == '0') {
391 + InPtr++; /* octal introducer: don't count this digit */
393 + if ('0' <= *InPtr && *InPtr <= '7') {
394 + /* treat as octal - first digit */
395 + char octD = *InPtr++;
396 + int octValue = octD - '0';
397 + if ('0' <= *InPtr && *InPtr <= '7') {
398 + /* second digit */
399 + octD = *InPtr++;
400 + octValue = (octValue << 3) + octD - '0';
401 + /* now do we have another digit? can we add it?
402 + if value is going to be too big for char (greater
403 + than 0377), stop converting now before adding the
404 + third digit */
405 + if ('0' <= *InPtr && *InPtr <= '7' &&
406 + octValue <= 037) {
407 + /* third digit is acceptable */
408 + octD = *InPtr++;
409 + octValue = (octValue << 3) + octD - '0';
412 + if (octValue != 0) {
413 + SCANSTRING_WRITE_TO_STRING(p, len, (char)octValue);
415 + else {
416 + InPtr = backslash + 1; /* just skip the backslash */
419 + else { /* \0 followed by non-digits: go back to 0 */
420 + InPtr = backslash + 1; /* just skip the backslash */
422 + continue;
424 + /* check for a valid c-style escape character */
425 + for (i = 0; escape[i] != '\0'; i++) {
426 + if (escape[i] == *InPtr) {
427 + SCANSTRING_WRITE_TO_STRING(p, len, replace[i]);
428 + InPtr++;
429 + break;
432 + /* if we get here, we didn't recognise the character after
433 + the backslash: just copy it next time round the loop */
435 + else {
436 + SCANSTRING_WRITE_TO_STRING(p, len, *InPtr++);
439 + /* terminate the string content */
440 + SCANSTRING_WRITE_TO_STRING(p, len, '\0');
441 + if (*InPtr == stopper) {
442 + if (!p) {
443 + /* this was the size measurement and validation */
444 + p = string = AllocString(len);
446 + else {
447 + /* OK: string now contains our string text */
448 + InPtr++; /* skip past stopper */
449 + yylval.sym = InstallStringConstSymbol(string);
450 + return STRING;
453 + else {
454 + /* failure: end quote doesn't match start quote */
455 + break;
458 + return yyerror("unterminated string");
462 ** Called by yacc to report errors (just stores for returning when
463 ** parsing is aborted. The error token action is to immediate abort
464 ** parsing, so this message is immediately reported to the caller
465 ** of ParseExpr)