diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2016-07-16 12:21:27 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2016-07-16 12:21:27 +0300 |
commit | 529fed25f92e04d694c30b3fc6bc8645e2703f4e (patch) | |
tree | c575001b35a43ba8f325bf7af60d7b9fb8b8b32a | |
parent | 6201e61fb932dbe6153f92ede836e07247d04b7c (diff) | |
download | grecs-529fed25f92e04d694c30b3fc6bc8645e2703f4e.tar.gz grecs-529fed25f92e04d694c30b3fc6bc8645e2703f4e.tar.bz2 |
Disable adjacent string concatenation.
The adjacent string concatenation feature doesn't work well with
multiple-argument statements. The common example (found in GNU
pies) is the "env" statement:
env "PATH=/sbin:$PATH" "PRELOAD=true"
The intent was to pass it two arguments, but in fact they get
concatenated into one.
To control this, the grecs_parser_options variable is introduced.
If it has the GRECS_OPTION_QUOTED_STRING_CONCAT bit set, adjacent
string concatenation is enabled. By default it is disabled.
The GRECS_OPTION_ADJUST_STRING_LOCATIONS bit controls the way the
quoted sring locations are computed. If it is set, the beginning of
the string is counted at the first character after the opening double
quote, and its end at the character immediately preceding the closing
double quote. Otherwise, both double-quote characters are included in
the location (the default).
The change is backward incompatible.
* doc/grecs-syntax.texi: Update.
* include/grecs/parser.h (grecs_adjust_string_locations): Remove.
(grecs_parser_options): New extern.
* src/grecs-lex.l: QSTRING is returned only if
GRECS_OPTION_QUOTED_STRING_CONCAT option is set.
The GRECS_OPTION_ADJUST_STRING_LOCATIONS option controls string locus
adjustment.
* src/parser.c (grecs_adjust_string_locations): Remove.
(grecs_parser_options): New variable.
* tests/Makefile.am: Add strcat.at, stradj.at
* tests/testsuite.at: Likewise.
* tests/gcffmt.c: New options -strcat and -stradj
* tests/stradj.at: New test case.
* tests/strcat.at: New test case.
-rw-r--r-- | doc/grecs-syntax.texi | 15 | ||||
-rw-r--r-- | include/grecs/parser.h | 5 | ||||
-rw-r--r-- | src/grecs-lex.l | 10 | ||||
-rw-r--r-- | src/parser.c | 2 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/gcffmt.c | 7 | ||||
-rw-r--r-- | tests/stradj.at | 39 | ||||
-rw-r--r-- | tests/strcat.at | 31 | ||||
-rw-r--r-- | tests/testsuite.at | 4 |
9 files changed, 106 insertions, 9 deletions
diff --git a/doc/grecs-syntax.texi b/doc/grecs-syntax.texi index a7738d6..2ddedea 100644 --- a/doc/grecs-syntax.texi +++ b/doc/grecs-syntax.texi @@ -108,15 +108,18 @@ The default include search path is: @end enumerate @noindent where @var{prefix} is the installation prefix. @c FIXME: Uncomment this, if necessary: -@c FIXME: New directories can be appended in front of it using @option{-I} -@c FIXME: (@option{--include-directory}) command line option -@c FIXME: (@pxref{Preprocessor, include-directory}). +@ignore +New directories can be appended in front of it using @option{-I} +(@option{--include-directory}) command line option +(@pxref{Preprocessor, include-directory}). +} +@end ignore @kwindex #include_once @item #include_once <@var{file}> @itemx #include_once @var{file} Same as @code{#include}, except that, if the @var{file} has already been included, it will not be included again. @@ -213,23 +216,29 @@ physical lines, e.g.: @end group @end smallexample If the character following a backslash is not one of those specified above, the backslash is ignored and a warning is issued. +@c FIXME: If grecs_parser_options variable has +@c FIXME: GRECS_OPTION_QUOTED_STRING_CONCAT bit set, then the +@c FIXME: following holds: + +@ignore Two or more adjacent quoted strings are concatenated, which gives another way to split long strings over several lines to improve readability. The following fragment produces the same result as the example above: @smallexample @group "a long string may be" " split over several lines" @end group @end smallexample +@end ignore @anchor{here-document} @item Here-document @cindex here-document A @dfn{here-document} is a special construct that allows to introduce strings of text containing embedded newlines. diff --git a/include/grecs/parser.h b/include/grecs/parser.h index 00878f4..41b3ae4 100644 --- a/include/grecs/parser.h +++ b/include/grecs/parser.h @@ -25,15 +25,18 @@ typedef struct grecs_node *(*grecs_parser_t)(const char *name, int trace); extern int grecs_trace_flags; extern grecs_parser_t grecs_parser_fun; extern int grecs_default_port; extern struct grecs_locus_point grecs_current_locus_point; extern grecs_locus_t grecs_locus; -extern int grecs_adjust_string_locations; extern int grecs_error_count; +#define GRECS_OPTION_ADJUST_STRING_LOCATIONS 0x01 +#define GRECS_OPTION_QUOTED_STRING_CONCAT 0x02 +extern int grecs_parser_options; + /* Main entry point */ struct grecs_node *grecs_parse(const char *name); void grecs_gram_trace(int n); void grecs_lex_trace(int n); /* Parsers: */ diff --git a/src/grecs-lex.l b/src/grecs-lex.l index 113ee88..f7be62f 100644 --- a/src/grecs-lex.l +++ b/src/grecs-lex.l @@ -53,12 +53,16 @@ static void multiline_begin(char *); static void multiline_add(char *); static char *multiline_strip_tabs(char *text); static int ident(void); static int isemptystr(int off); static void qstring_locus_fixup(void); +#define qstring() \ + ((grecs_parser_options & GRECS_OPTION_QUOTED_STRING_CONCAT) \ + ? QSTRING : STRING) + #undef YY_INPUT #define YY_INPUT(buf,result,max_size) \ do { \ if (grecs_preprocessor) \ result = fread(buf, 1, max_size, yyin); \ else \ @@ -113,13 +117,13 @@ P [1-9][0-9]* return STRING; } /* Quoted strings */ \"[^\\"\n]*\" { grecs_line_begin(); grecs_line_add(yytext + 1, yyleng - 2); yylval.string = grecs_line_finish(); qstring_locus_fixup(); - return QSTRING; } + return qstring(); } \"[^\\"\n]*\\\n { BEGIN(STR); grecs_line_begin(); grecs_line_acc_grow_unescape_last(yytext + 1, yyleng - 1, &yylloc); grecs_locus_point_advance_line(grecs_current_locus_point); } @@ -135,13 +139,13 @@ P [1-9][0-9]* &yylloc); } <STR>[^\\"\n]*\" { BEGIN(INITIAL); if (yyleng > 1) grecs_line_add(yytext, yyleng - 1); yylval.string = grecs_line_finish(); qstring_locus_fixup(); - return QSTRING; } + return qstring(); } /* Multiline strings */ "<<"(-" "?)?\\?{ID}[ \t]*#.*\n | "<<"(-" "?)?\\?{ID}[ \t]*"//".*\n | "<<"(-" "?)?\\?{ID}[ \t]*\n | "<<"(-" "?)?\"{ID}\"[ \t]*#.*\n | "<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n | @@ -345,13 +349,13 @@ ident() return IDENT; } static void qstring_locus_fixup() { - if (grecs_adjust_string_locations) { + if (grecs_parser_options & GRECS_OPTION_ADJUST_STRING_LOCATIONS) { yylloc.beg.col++; yylloc.end.col--; } } grecs_value_t * diff --git a/src/parser.c b/src/parser.c index 1ad43c1..ed99a7a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -23,13 +23,13 @@ #include "grecs.h" int grecs_error_count = 0; int grecs_default_port = 0; int grecs_trace_flags = 0; -int grecs_adjust_string_locations = 0; +int grecs_parser_options = 0; #ifndef GRECS_DEFAULT_PARSER # define GRECS_DEFAULT_PARSER grecs_grecs_parser #endif struct grecs_node *(*grecs_parser_fun)(const char *name, int trace) = diff --git a/tests/Makefile.am b/tests/Makefile.am index 9bf28da..65f590a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -118,12 +118,14 @@ TESTSUITE_AT = \ reduce01.at\ reduce02.at\ reduce03.at\ set.at\ sort00.at\ sort01.at\ + stradj.at\ + strcat.at\ testsuite.at\ vercmp.at\ wordsplit.at\ @GRECS_DISTCK_AT@ TESTSUITE = $(srcdir)/testsuite diff --git a/tests/gcffmt.c b/tests/gcffmt.c index 8e378f9..170f788 100644 --- a/tests/gcffmt.c +++ b/tests/gcffmt.c @@ -91,13 +91,14 @@ main(int argc, char **argv) char *progname = argv[0]; char *file = NULL; struct grecs_node *tree; int flags = GRECS_NODE_FLAG_DEFAULT; int reduce = 0; int sort = 0; - + + grecs_parser_options = 0; while (--argc) { char *arg = *++argv; if (strcmp(arg, "-locus") == 0) flags |= GRECS_NODE_FLAG_LOCUS; else if (strcmp(arg, "-nopath") == 0) flags &= ~GRECS_NODE_FLAG_PATH; @@ -125,12 +126,16 @@ main(int argc, char **argv) else if (strncmp(arg, "-include=", 9) == 0) grecs_preproc_add_include_dir(arg+9); else if (strcmp(arg, "-x") == 0) grecs_gram_trace(1); else if (strcmp(arg, "-X") == 0) grecs_lex_trace(1); + else if (strcmp(arg, "-strcat") == 0) + grecs_parser_options |= GRECS_OPTION_QUOTED_STRING_CONCAT; + else if (strcmp(arg, "-stradj") == 0) + grecs_parser_options |= GRECS_OPTION_ADJUST_STRING_LOCATIONS; else if (arg[0] == '-') usage(progname, stderr, 1); else { file = arg; --argc; break; diff --git a/tests/stradj.at b/tests/stradj.at new file mode 100644 index 0000000..186078a --- /dev/null +++ b/tests/stradj.at @@ -0,0 +1,39 @@ +# This file is part of grecs -*- Autotest -*- +# Copyright (C) 2016 Sergey Poznyakoff +# +# Grecs is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Grecs is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Grecs. If not, see <http://www.gnu.org/licenses/>. + +AT_SETUP([String location adjustment]) +AT_KEYWORDS([format option stradj]) + +AT_DATA([input.conf], +[ident name; +option "string"; +]) + +AT_CHECK([echo "Default" +gcffmt -nopath -locus input.conf +echo "Adjustment" +gcffmt -nopath -locus -stradj input.conf +], +[0], +[Default +input.conf:1.7-10: "name" +input.conf:2.8-15: "string" +Adjustment +input.conf:1.7-10: "name" +input.conf:2.9-14: "string" +]) + +AT_CLEANUP diff --git a/tests/strcat.at b/tests/strcat.at new file mode 100644 index 0000000..b5f703a --- /dev/null +++ b/tests/strcat.at @@ -0,0 +1,31 @@ +# This file is part of grecs -*- Autotest -*- +# Copyright (C) 2016 Sergey Poznyakoff +# +# Grecs is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# Grecs is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Grecs. If not, see <http://www.gnu.org/licenses/>. + +AT_SETUP([String concatenation]) +AT_KEYWORDS([format option strcat concat]) + +AT_DATA([input.conf], +[option id "a" " string"; +]) + +AT_CHECK([gcffmt input.conf +gcffmt -strcat input.conf], +[0], +[.option: "id" "a" " string" +.option: "id" "a string" +]) + +AT_CLEANUP diff --git a/tests/testsuite.at b/tests/testsuite.at index ec94ff0..cb7f1a5 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -51,12 +51,16 @@ m4_include([wordsplit.at]) AT_BANNER([Formats]) m4_include([format00.at]) m4_include([format01.at]) m4_include([format02.at]) +AT_BANNER([Options]) +m4_include([stradj.at]) +m4_include([strcat.at]) + AT_BANNER([Enumeration]) m4_include([enum.at]) AT_BANNER([Peek a node]) m4_include([peek00.at]) m4_include([peek01.at]) |