From d1d247b0036824bb086a638f5499735b14b55bf5 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Tue, 21 Jul 2015 12:15:01 +0100 Subject: [PATCH] Allow to specify exacly one input file and at most one output and header file. --- re2c/bootstrap/src/conf/parse_opts.cc | 19 +++++---- re2c/src/codegen/output.cc | 3 +- re2c/src/conf/opt.cc | 60 ++++++++++++++++++++------- re2c/src/conf/opt.h | 8 ++-- re2c/src/conf/parse_opts.re | 17 +++++--- 5 files changed, 73 insertions(+), 34 deletions(-) diff --git a/re2c/bootstrap/src/conf/parse_opts.cc b/re2c/bootstrap/src/conf/parse_opts.cc index f0707db0..70ecf3f6 100644 --- a/re2c/bootstrap/src/conf/parse_opts.cc +++ b/re2c/bootstrap/src/conf/parse_opts.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 0.14.3 on Sun Jul 19 12:06:48 2015 */ +/* Generated by re2c 0.14.3 on Tue Jul 21 12:00:08 2015 */ #include #include "src/conf/msg.h" @@ -89,7 +89,7 @@ yy7: { goto opt_long; } yy9: ++YYCURSOR; - { opts.source (""); goto opt; } + { if (!opts.source ("")) return EXIT_FAIL; goto opt; } yy11: ++YYCURSOR; { goto end; } @@ -101,7 +101,7 @@ yy14: goto yy13; } ++YYCURSOR; - { opts.source (*argv); goto opt; } + { if (!opts.source (*argv)) return EXIT_FAIL; goto opt; } } @@ -466,7 +466,7 @@ yy131: yych = *++YYCURSOR; if (yych >= 0x01) goto yy89; ++YYCURSOR; - { goto opt_header; } + { YYCURSOR = *++argv; goto opt_header; } yy143: yych = *++YYCURSOR; if (yych == 'n') goto yy184; @@ -605,7 +605,7 @@ yy204: yych = *++YYCURSOR; if (yych >= 0x01) goto yy89; ++YYCURSOR; - { goto opt_output; } + { YYCURSOR = *++argv; goto opt_output; } yy211: yych = *++YYCURSOR; if (yych == '-') goto yy223; @@ -1009,7 +1009,7 @@ yy387: goto yy386; } ++YYCURSOR; - { opts.output (*argv); goto opt; } + { if (!opts.output (*argv)) return EXIT_FAIL; goto opt; } } @@ -1071,7 +1071,7 @@ yy396: goto yy395; } ++YYCURSOR; - { opts.type_header (*argv); goto opt; } + { if (!opts.type_header (*argv)) return EXIT_FAIL; goto opt; } } @@ -1288,6 +1288,11 @@ yy477: end: + if (!opts.source_file) + { + error ("no source file\n"); + return EXIT_FAIL; + } if (!cFlag && opts.header_file) { error ("can only output a header file when using -c switch\n"); diff --git a/re2c/src/codegen/output.cc b/re2c/src/codegen/output.cc index 30adfa58..b2461531 100644 --- a/re2c/src/codegen/output.cc +++ b/re2c/src/codegen/output.cc @@ -60,8 +60,9 @@ OutputFile::OutputFile (const char * fn) bool OutputFile::open () { - if (strcmp (file_name, "") == 0) + if (file_name == NULL) { + file_name = ""; file = stdout; } else diff --git a/re2c/src/conf/opt.cc b/re2c/src/conf/opt.cc index 561a51be..9773fbf6 100644 --- a/re2c/src/conf/opt.cc +++ b/re2c/src/conf/opt.cc @@ -1,3 +1,4 @@ +#include "src/conf/msg.h" #include "src/conf/opt.h" namespace re2c @@ -129,22 +130,6 @@ void Opt::skeleton () flag_skeleton = true; } -void Opt::source (const char * s) -{ - source_file = s; -} - -void Opt::output (const char * s) -{ - output_file = s; -} - -void Opt::type_header (const char * s) -{ - tFlag = true; - header_file = s; -} - void Opt::encoding_policy (Enc::policy_t p) { encoding.setPolicy (p); @@ -160,6 +145,49 @@ void Opt::empty_class (empty_class_policy_t p) empty_class_policy = p; } +bool Opt::source (const char * s) +{ + if (source_file) + { + error ("multiple source files: %s, %s\n", source_file, s); + return false; + } + else + { + source_file = s; + return true; + } +} + +bool Opt::output (const char * s) +{ + if (output_file) + { + error ("multiple output files: %s, %s\n", output_file, s); + return false; + } + else + { + output_file = s; + return true; + } +} + +bool Opt::type_header (const char * s) +{ + if (header_file) + { + error ("multiple header files: %s, %s\n", header_file, s); + return false; + } + else + { + tFlag = true; + header_file = s; + return true; + } +} + bool Opt::ecb () { return encoding.set (Enc::EBCDIC); diff --git a/re2c/src/conf/opt.h b/re2c/src/conf/opt.h index 6bc202f3..95409a6e 100644 --- a/re2c/src/conf/opt.h +++ b/re2c/src/conf/opt.h @@ -19,7 +19,7 @@ struct Opt Opt () : source_file (NULL) - , output_file ("") + , output_file (NULL) , header_file (NULL) {} @@ -37,12 +37,12 @@ struct Opt void case_insensitive (); void case_inverted (); void skeleton (); - void source (const char * s); - void output (const char * s); - void type_header (const char * s); void encoding_policy (Enc::policy_t); void input (InputAPI::type_t); void empty_class (empty_class_policy_t); + bool source (const char * s); + bool output (const char * s); + bool type_header (const char * s); bool ecb (); bool unicode (); bool wide_chars (); diff --git a/re2c/src/conf/parse_opts.re b/re2c/src/conf/parse_opts.re index dadbe819..dc62c025 100644 --- a/re2c/src/conf/parse_opts.re +++ b/re2c/src/conf/parse_opts.re @@ -40,8 +40,8 @@ opt: "--" end { goto end; } - "-" end { opts.source (""); goto opt; } - filename end { opts.source (*argv); goto opt; } + "-" end { if (!opts.source ("")) return EXIT_FAIL; goto opt; } + filename end { if (!opts.source (*argv)) return EXIT_FAIL; goto opt; } "-" { goto opt_short; } "--" { goto opt_long; } @@ -109,8 +109,8 @@ opt_long: "wide-chars" end { if (!opts.wide_chars ()) { error_encoding (); return EXIT_FAIL; } goto opt; } "utf-16" end { if (!opts.utf_16 ()) { error_encoding (); return EXIT_FAIL; } goto opt; } "utf-8" end { if (!opts.utf_8 ()) { error_encoding (); return EXIT_FAIL; } goto opt; } - "output" end { goto opt_output; } - "type-header" end { goto opt_header; } + "output" end { YYCURSOR = *++argv; goto opt_output; } + "type-header" end { YYCURSOR = *++argv; goto opt_header; } "encoding-policy" end { goto opt_encoding_policy; } "input" end { goto opt_input; } "empty-class" end { goto opt_empty_class; } @@ -124,7 +124,7 @@ opt_output: error ("bad argument to option -o, --output: %s\n", *argv); return EXIT_FAIL; } - filename end { opts.output (*argv); goto opt; } + filename end { if (!opts.output (*argv)) return EXIT_FAIL; goto opt; } */ opt_header: @@ -134,7 +134,7 @@ opt_header: error ("bad argument to option -t, --type-header: %s\n", *argv); return EXIT_FAIL; } - filename end { opts.type_header (*argv); goto opt; } + filename end { if (!opts.type_header (*argv)) return EXIT_FAIL; goto opt; } */ opt_encoding_policy: @@ -176,6 +176,11 @@ opt_empty_class: */ end: + if (!opts.source_file) + { + error ("no source file\n"); + return EXIT_FAIL; + } if (!cFlag && opts.header_file) { error ("can only output a header file when using -c switch\n"); -- 2.50.1