1 Unlimited macro string literal length and single-quoted strings
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.
22 Fixed adjacent string literal merging to allow concatenation with "".
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
33 @@ -1945,15 +1945,16 @@ Macro Language
34 conditionally, such as the body of a loop, are surrounded by curly braces
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
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
45 + to be continued. Note that a comment with a backslash at the end is treated
46 + as a continuation in this way too.
51 The NEdit macro language recognizes only three data types, dynamic character
52 @@ -1971,16 +1972,18 @@ Macro Language
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:
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
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
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
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);
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);
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)
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 */
154 - static char replace[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
160 /* return end of input at the end of the string */
161 @@ -644,119 +639,14 @@ static int yylex(void)
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
176 - if (*InPtr == '\"') {
177 - char string[MAX_STRING_CONST_LEN], *p = string;
180 - while (*InPtr != '\0' && *InPtr != '\"' && *InPtr != '\n') {
181 - if (p >= string + MAX_STRING_CONST_LEN) {
185 - if (*InPtr == '\\') {
188 - if (*InPtr == '\n') {
192 - if (*InPtr == 'x') {
193 - /* a hex introducer */
195 - const char *hexDigits = "0123456789abcdef";
198 - if (*InPtr == '\0' ||
199 - (hexD = strchr(hexDigits, tolower(*InPtr))) == NULL) {
203 - hexValue = hexD - hexDigits;
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);
211 - if (hexValue != 0) {
212 - *p++ = (char)hexValue;
215 - InPtr = backslash + 1; /* just skip the backslash */
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') {
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
239 - if ('0' <= *InPtr && *InPtr <= '7' &&
241 - /* third digit is acceptable */
243 - octValue = (octValue << 3) + octD - '0';
246 - if (octValue != 0) {
247 - *p++ = (char)octValue;
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 */
258 - for (i=0; escape[i]!='\0'; i++) {
259 - if (escape[i] == *InPtr) {
265 - /* if we get here, we didn't recognise the character after
266 - the backslash: just copy it next time round the loop */
274 - yylval.sym = InstallStringConstSymbol(string);
276 + /* Process quoted strings */
278 + if (*InPtr == '\"' || *InPtr == '\'') {
279 + return scanString();
282 /* process remaining two character tokens or return single char as token */
285 @@ -856,10 +746,181 @@ static Symbol *matchesActionRoutine(char
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
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;
314 + char stopper, first_stopper = *startPtr;
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 */
322 + static char replace[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
325 + if (first_stopper != '\"' && first_stopper != '\'')
326 + return yyerror("expected a string");
328 + for (scan = 0; scan < 2; ++scan)
331 + stopper = first_stopper;
332 + handleBackslash = (stopper == '\"');
335 + while (*InPtr != '\0' && *InPtr != '\n') {
336 + if (*InPtr == stopper) {
337 + char *endPtr = InPtr++;
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 == '\"');
345 + InPtr = endPtr; /* no further string: restore position */
349 + else if (handleBackslash && *InPtr == '\\') {
352 + if (*InPtr == '\n') { /* allows newline to be skipped */
356 + if (*InPtr == 'x') {
357 + /* a hex introducer */
359 + const char *hexDigits = "0123456789abcdef";
362 + if (*InPtr == '\0')
364 + if ((hexD = strchr(hexDigits, tolower(*InPtr))) == NULL) {
365 + SCANSTRING_WRITE_TO_STRING(p, len, 'x');
368 + hexValue = hexD - hexDigits;
370 + if (*InPtr == '\0')
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);
377 + if (hexValue != 0) {
378 + SCANSTRING_WRITE_TO_STRING(p, len, (char)hexValue);
381 + InPtr = backslash + 1; /* just skip the backslash */
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') {
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
405 + if ('0' <= *InPtr && *InPtr <= '7' &&
407 + /* third digit is acceptable */
409 + octValue = (octValue << 3) + octD - '0';
412 + if (octValue != 0) {
413 + SCANSTRING_WRITE_TO_STRING(p, len, (char)octValue);
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 */
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]);
432 + /* if we get here, we didn't recognise the character after
433 + the backslash: just copy it next time round the loop */
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) {
443 + /* this was the size measurement and validation */
444 + p = string = AllocString(len);
447 + /* OK: string now contains our string text */
448 + InPtr++; /* skip past stopper */
449 + yylval.sym = InstallStringConstSymbol(string);
454 + /* failure: end quote doesn't match start quote */
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