OSDN Git Service

エンコードが正常になるように変換。
[simplecms/utakata.git] / sublexer_impl.h
1 #ifndef _SUBLEXER_IMPL_H_
2 #define _SUBLEXER_IMPL_H_
3
4 #include <string>
5
6 #include "sublexer.h"
7 #include "smart_ptr.h"
8 #include "utf8.h"
9 #include "lexeme.h"
10 #include "utf8_string.h"
11
12
13 namespace utakata {
14
15     namespace sublexer {
16
17         class LexException : public std::exception
18         {
19         public:
20             LexException(size_t pos, std::string str);
21             virtual ~LexException() throw() {}
22             const char* what() const throw();
23
24         private:
25
26             size_t pos_;
27             std::string str_;
28         };
29
30         class FirstLexer : public ISubLexer
31         {
32             // lex関数の内部で最初に実行される。
33             // ここから返されない場合は、一文字の構文構成文字が返されるということになる。
34         public:
35             FirstLexer() {}
36             virtual ~FirstLexer() {}
37
38             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
39                                            smart_ptr<ISubLexer>& next);
40
41         private:
42
43             // lex関数中で利用される、雑多になった字句解析関数の分割
44             smart_ptr<lexeme::ILexeme> lex_(const utakata::utf8_string::UTF8String& str,
45                                             smart_ptr<utakata::utf8::UTF8InputStream> stream,
46                                             smart_ptr<ISubLexer>& next);
47
48         };
49
50         class StringLexer : public ISubLexer
51         {
52             // 文字列だと判断された場合に実行されるsublexer。
53             // 文字列が不正に終わったりしている場合には、その時点で例
54             // 外を発行する。
55         public:
56             StringLexer() {}
57             virtual ~StringLexer() {}
58
59             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
60                                            smart_ptr<ISubLexer>& next);
61         };
62
63         class OneLineCommentLexer : public ISubLexer
64         {
65             // コメントを返す。
66             // と言っても、;から開始されるコメントは完全に無視されるので、lexeme
67             // を保存する必要すらない。
68         public:
69
70             OneLineCommentLexer(){}
71             virtual ~OneLineCommentLexer(){}
72
73             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
74                                            smart_ptr<ISubLexer>& next);
75         };
76
77         class NestedCommentLexer : public ISubLexer
78         {
79             // コメントを返す。
80             // と言っても、;から開始されるコメントは完全に無視されるので、lexeme
81             // を保存する必要すらない。
82         public:
83
84             NestedCommentLexer(){}
85             virtual ~NestedCommentLexer(){}
86
87             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
88                                            smart_ptr<ISubLexer>& next);
89         };
90
91         class CharactorLexer : public ISubLexer
92         {
93             // 文字名、及び16進数による文字指定の解析を行なう。
94             // #\文字名、#\x<hex>;の両方を解釈する。
95         public:
96
97             CharactorLexer(){}
98             virtual ~CharactorLexer(){}
99
100             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
101                                            smart_ptr<ISubLexer>& next);
102
103         };
104
105         class NumberLexer : public ISubLexer
106         {
107             // 数値を解析する。
108             // ここで解析する数値は、<number>に相当する。
109             const unsigned char BINARY;
110             const unsigned char OCTET;
111             const unsigned char DECIMAL;
112             const unsigned char HEX;
113         public:
114
115             NumberLexer(const utakata::utf8_string::UTF8String& str);
116             virtual ~NumberLexer(){}
117
118             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
119                                            smart_ptr<ISubLexer>& next);
120         private:
121
122             smart_ptr<lexeme::ILexeme> innerLex_(smart_ptr<utakata::utf8::UTF8InputStream> stream,
123                                                  smart_ptr<ISubLexer>& next,
124                                                  const utakata::utf8_string::UTF8String& str);
125
126
127             // 渡した文字列からprefixを抽出して返す。
128             unsigned char getPrefix_(const utakata::utf8_string::UTF8String& str);
129             // 正確性を抽出して返す。
130             void checkExactness_(const utakata::utf8_string::UTF8String& str); 
131
132         private:
133             
134             smart_ptr<utakata::utf8_string::UTF8String> str_;
135
136             bool exact_;        // 正確性を設定する。
137             unsigned char prefix_;       // prefixを設定する。
138             
139         };
140
141         class IdentifierLexer : public ISubLexer
142         {
143             // コメントを返す。
144             // と言っても、;から開始されるコメントは完全に無視されるので、lexeme
145             // を保存する必要すらない。
146         public:
147
148             IdentifierLexer(const utakata::utf8_string::UTF8String& str);
149             virtual ~IdentifierLexer(){}
150
151             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
152                                            smart_ptr<ISubLexer>& next);
153
154         private:
155
156             smart_ptr<utakata::utf8_string::UTF8String> str_;
157         };
158
159         class BooleanLexer : public ISubLexer
160         {
161             // #t、または#fを返す。
162             // いずれかの次には、必ず区切り文字がなければならない。
163         public:
164
165             BooleanLexer(const utakata::utf8_string::UTF8Char& ch);
166             virtual ~BooleanLexer(){}
167
168             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
169                                            smart_ptr<ISubLexer>& next);
170
171         private:
172
173             smart_ptr<utakata::utf8_string::UTF8Char> ch_;
174         };
175
176         class ByteVectorLexer : public ISubLexer
177         {
178             // #vu8であるかどうかを検査する。
179             // vu8ではない場合には、エラーとなる。
180         public:
181
182             ByteVectorLexer(const utakata::utf8_string::UTF8Char& ch);
183             virtual ~ByteVectorLexer(){}
184
185             smart_ptr<lexeme::ILexeme> lex(smart_ptr<utakata::utf8::UTF8InputStream> stream,
186                                            smart_ptr<ISubLexer>& next);
187
188         private:
189
190             smart_ptr<utakata::utf8_string::UTF8Char> ch_;
191         };
192
193     };
194
195 };
196
197
198 #endif /* _SUBLEXER_IMPL_H_ */