| Line | |
|---|
| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_TLATE_SCANNER_HH |
|---|
| 21 | #define GUARD_TLATE_SCANNER_HH |
|---|
| 22 | |
|---|
| 23 | #include <boost/shared_ptr.hpp> |
|---|
| 24 | #include <iostream> |
|---|
| 25 | #include <list> |
|---|
| 26 | |
|---|
| 27 | namespace tlate |
|---|
| 28 | { |
|---|
| 29 | class scanner |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | enum token { END, INVALID, |
|---|
| 33 | LITERAL, ID, OBRACE, CBRACE, |
|---|
| 34 | OPAREN, CPAREN, COMMA, PERIOD, |
|---|
| 35 | DEF, ELSE, FOR, IF, IN |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | scanner(boost::shared_ptr<std::istream> is, std::string fname); |
|---|
| 39 | scanner(std::string fname); |
|---|
| 40 | |
|---|
| 41 | token peek() const; |
|---|
| 42 | std::string peekValue() const; |
|---|
| 43 | std::string peekFileName() const; |
|---|
| 44 | size_t peekLineNo() const; |
|---|
| 45 | token get(); |
|---|
| 46 | |
|---|
| 47 | private: |
|---|
| 48 | void setup_include(); |
|---|
| 49 | void next(); |
|---|
| 50 | void next_(); |
|---|
| 51 | bool next_lit(); |
|---|
| 52 | bool next_dir(); |
|---|
| 53 | bool next_eof(); |
|---|
| 54 | bool next_comment(); |
|---|
| 55 | bool do_include(); |
|---|
| 56 | |
|---|
| 57 | std::string include_prefix; |
|---|
| 58 | std::string fname; |
|---|
| 59 | size_t lineno; |
|---|
| 60 | boost::shared_ptr<std::istream> is; |
|---|
| 61 | enum mode { LIT, DIR } m; |
|---|
| 62 | token cur; |
|---|
| 63 | std::string strval; |
|---|
| 64 | |
|---|
| 65 | struct saved_item { |
|---|
| 66 | std::string fname; |
|---|
| 67 | size_t lineno; |
|---|
| 68 | boost::shared_ptr<std::istream> is; |
|---|
| 69 | mode m; |
|---|
| 70 | token cur; |
|---|
| 71 | std::string strval; |
|---|
| 72 | }; |
|---|
| 73 | std::list<saved_item> safe; |
|---|
| 74 | |
|---|
| 75 | void push(); |
|---|
| 76 | void pop(); |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | inline scanner::token scanner::peek() const |
|---|
| 80 | { |
|---|
| 81 | return cur; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | inline scanner::token scanner::get() |
|---|
| 85 | { |
|---|
| 86 | token rv = cur; |
|---|
| 87 | next(); |
|---|
| 88 | return rv; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | inline std::string scanner::peekValue() const |
|---|
| 92 | { |
|---|
| 93 | return strval; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | inline std::string scanner::peekFileName() const |
|---|
| 97 | { |
|---|
| 98 | return fname; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | inline size_t scanner::peekLineNo() const |
|---|
| 102 | { |
|---|
| 103 | return lineno; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | #endif |
|---|