lexcaser.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : lexcaser                                                          *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/istring.h>
00016 #include <loggers/console_logger.h>
00017 #include <data_struct/static_memory_gremlin.h>
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 HOOPLE_STARTUP_CODE;
00023 
00024 console_logger out;
00025 
00026 void print_instructions_and_exit(const char *program_name)
00027 {
00028   out.log(istring("\n") +  program_name +
00029 ": This program modifies a lex file that is grabbed from standard input\n\
00030 by turning the characters in each quoted item that are lower case into a\n\
00031 token rule that accepts either case.\n\n\
00032 For example, the rule specified by:\n\n\
00033 \t\"pragma\"               { u; r(PRAGMA_); };\n\
00034 \n\nwould become:\n\n\
00035 \t[pP][rR][aA][gG][mM][aA]               { u; r(PRAGMA_); };\n\
00036 ");
00037   exit(1);
00038 }
00039 
00040 void modify_character(char to_modify)
00041 {
00042   istring to_up(to_modify, 1);
00043   to_up.to_upper();
00044   out.log(istring("[") + istring(to_modify, 1) + to_up + "]");
00045 }
00046 
00047 int main(int argc, char *argv[])
00048 {
00049   out.eol(log_base::NO_ENDING);  // set for proper ending.
00050 
00051   if (argc != 1) print_instructions_and_exit(argv[0]);
00052 
00053   enum states {
00054     CHECKING_FIRST_POSITION,
00055     ATE_A_QUOTE,
00056     NOW_MODIFYING,
00057     AFTER_FIRST_POSITION,
00058     EATING_RETURN
00059   };
00060   states state = CHECKING_FIRST_POSITION;
00061   int quote_eaten_on_this_line = false;
00062   while (!feof(stdin)) {
00063     byte current = getc(stdin);
00064     if (current == '\n') {
00065       state = EATING_RETURN;
00066       quote_eaten_on_this_line = false;
00067     } else if (current == byte(EOF)) break;
00068     switch (state) {
00069     case EATING_RETURN:
00070       out.log(istring(current, 1));
00071       state = CHECKING_FIRST_POSITION;
00072       break;
00073     case CHECKING_FIRST_POSITION:
00074       if (current == '"') state = ATE_A_QUOTE;
00075       else {
00076         state = AFTER_FIRST_POSITION;
00077         out.log(istring(current, 1));
00078       }
00079       break;
00080     case AFTER_FIRST_POSITION:
00081       if (current != '"') out.log(istring(current, 1));
00082       // the first quote after the initially eaten quote is not printed.
00083       else if (quote_eaten_on_this_line) quote_eaten_on_this_line = false;
00084       else out.log(istring(current, 1));
00085       break;
00086     case ATE_A_QUOTE:
00087       if ( (current <= 'z') && (current >= 'a') ) {
00088         quote_eaten_on_this_line = true;
00089         modify_character(current);
00090         state = NOW_MODIFYING;
00091       } else {
00092         state = AFTER_FIRST_POSITION;
00093         out.log(istring("\"") + istring(current, 1));
00094       }
00095       break;
00096     case NOW_MODIFYING:
00097       if ( (current <= 'z') && (current >= 'a') ) modify_character(current);
00098       else if (current == ' ') out.log(istring("\\") + istring(current, 1));
00099       else if (current == '_') out.log(istring(current, 1));
00100       else if (current == '"') {
00101         state = AFTER_FIRST_POSITION;
00102         // reset the status for that quote, since we are eating it here.
00103         quote_eaten_on_this_line = false;
00104       } else {
00105         state = AFTER_FIRST_POSITION;
00106         out.log(istring(current, 1));
00107       }
00108       break;
00109     }
00110   }
00111   return 0;
00112 }
00113 
00114 #ifdef __BUILD_STATIC_APPLICATION__
00115   // static dependencies found by buildor_gen_deps.sh:
00116   #include <basis/array.cpp>
00117   #include <basis/byte_array.cpp>
00118   #include <basis/callstack_tracker.cpp>
00119   #include <basis/chaos.cpp>
00120   #include <basis/convert_utf.cpp>
00121   #include <basis/definitions.cpp>
00122   #include <basis/earth_time.cpp>
00123   #include <basis/guards.cpp>
00124   #include <basis/istring.cpp>
00125   #include <basis/log_base.cpp>
00126   #include <basis/memory_checker.cpp>
00127   #include <basis/mutex.cpp>
00128   #include <basis/object_base.cpp>
00129   #include <basis/outcome.cpp>
00130   #include <basis/packable.cpp>
00131   #include <basis/portable.cpp>
00132   #include <basis/sequence.cpp>
00133   #include <basis/set.cpp>
00134   #include <basis/utility.cpp>
00135   #include <basis/version_record.cpp>
00136   #include <data_struct/amorph.cpp>
00137   #include <data_struct/bit_vector.cpp>
00138   #include <data_struct/byte_hasher.cpp>
00139   #include <data_struct/configurator.cpp>
00140   #include <data_struct/hash_table.cpp>
00141   #include <data_struct/pointer_hash.cpp>
00142   #include <data_struct/stack.cpp>
00143   #include <data_struct/static_memory_gremlin.cpp>
00144   #include <data_struct/string_hash.cpp>
00145   #include <data_struct/string_hasher.cpp>
00146   #include <data_struct/string_table.cpp>
00147   #include <data_struct/symbol_table.cpp>
00148   #include <data_struct/table_configurator.cpp>
00149   #include <loggers/console_logger.cpp>
00150   #include <loggers/file_logger.cpp>
00151   #include <loggers/locked_logger.cpp>
00152   #include <loggers/null_logger.cpp>
00153   #include <loggers/program_wide_logger.cpp>
00154   #include <opsystem/byte_filer.cpp>
00155   #include <opsystem/command_line.cpp>
00156   #include <opsystem/critical_events.cpp>
00157   #include <opsystem/directory.cpp>
00158   #include <opsystem/filename.cpp>
00159   #include <opsystem/ini_config.cpp>
00160   #include <opsystem/ini_parser.cpp>
00161   #include <opsystem/path_configuration.cpp>
00162   #include <opsystem/rendezvous.cpp>
00163   #include <textual/byte_format.cpp>
00164   #include <textual/parser_bits.cpp>
00165   #include <textual/string_manipulation.cpp>
00166   #include <textual/tokenizer.cpp>
00167 #endif // __BUILD_STATIC_APPLICATION__
00168 

Generated on Fri Nov 28 04:28:51 2008 for HOOPLE Libraries by  doxygen 1.5.1