Canola  0.8.D001
lib/lexer.h
Go to the documentation of this file.
00001 //
00002 // canola - canon canola 1614p emulator
00003 // Copyright (C) 2011, 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, version 3, as
00007 // published by the Free Software Foundation.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program. If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #ifndef LIB_LEXER_H
00019 #define LIB_LEXER_H
00020 
00021 #include <lib/ac/stdio.h>
00022 #include <string>
00023 #include <list>
00024 #include <map>
00025 #include <boost/shared_ptr.hpp>
00026 
00027 #include <lib/format_printf.h>
00028 #include <lib/location.h>
00029 #include <lib/search_path.h>
00030 
00031 class calculator; // forward
00032 class lexer_functor; // forward
00033 
00040 class lexer
00041 {
00042 public:
00046     virtual ~lexer();
00047 
00051     lexer(const std::string &filename, calculator &subject);
00052 
00061     bool read_and_execute(void);
00062 
00071     bool read_binary_and_execute(void);
00072 
00082     void test_mode_set(bool state = true);
00083 
00088     static void search_path_append(const std::string &dir);
00089 
00090 private:
00096     calculator &subject;
00097 
00103     class source
00104     {
00105     public:
00106         typedef boost::shared_ptr<source> pointer;
00107 
00112         ~source();
00113 
00123         static pointer create(const std::string &filename, calculator &subject);
00124 
00131         int getch(void);
00132 
00141         void ungetch(int c);
00142 
00147         void set_line_number(int n);
00148 
00153         void error(const char *fmt, ...)                    FORMAT_PRINTF(2, 3);
00154 
00159         void error_v(const char *fmt, va_list)              FORMAT_PRINTF(2, 0);
00160 
00161         location::pointer get_location(void) const;
00162 
00163     private:
00173         source(const std::string &filename, calculator &subject);
00174 
00179         std::string filename;
00180 
00185         calculator &subject;
00186 
00192         FILE *fp;
00193 
00199         int line_number;
00200 
00207         bool prev_was_newline;
00208 
00213         long position;
00214 
00219         void close(void);
00220 
00228         std::list<unsigned char> pushback;
00229     };
00230 
00235     std::list<source::pointer> src;
00236 
00241     bool test_mode;
00242 
00247     void close(void);
00248 
00257     bool next_is(char c);
00258 
00267     bool next_is(const char *s);
00268 
00274     int getch(void);
00275 
00283     void ungetch(int c);
00284 
00293     void error(const char *fmt, ...)                        FORMAT_PRINTF(2, 3);
00294 
00302     void open_include_file(const std::string &filename);
00303 
00308     static search_path include_search_path;
00309 
00310     typedef boost::shared_ptr<lexer_functor> functor_pointer;
00311     typedef std::map<std::string, functor_pointer> names_t;
00312 
00317     names_t names;
00318 
00323     void load_names(void);
00324 
00329     location::pointer get_location(void) const;
00330 
00338     location::pointer get_location(const location::pointer &start) const;
00339 
00345     bool card_properties_active;
00346 
00354     void process_comment(const std::string &text);
00355 
00361     bool ok;
00362 
00367     lexer();
00368 
00373     lexer(const lexer &);
00374 
00379     lexer &operator=(const lexer &);
00380 };
00381 
00382 #endif // LIB_LEXER_H