4 #include "utf8_string.h"
7 #include "sublexer_impl.h"
8 #include "lexeme_impl.h"
10 using namespace utakata;
12 smart_ptr<sublexer::ISubLexer> sublexer::CFirstLex::lex(const utf8_string::CUTF8Char& ch,
13 const utf8_string::CUTF8String& str,
14 smart_ptr<utf8::CUTF8InputStream>& stream)
16 // chにはlexerから渡された、今回読みだした文字が渡されている。
18 smart_ptr<sublexer::ISubLexer> ret;
19 // ()[]`',.は構文解析の重要な要素となるため、このまま返す。
20 if (ch.toUTF16Code() == '(' || ch.toUTF16Code() == '[')
22 lexeme_ = lexeme::makeOpenParen();
25 if (ch.toUTF16Code() == ')' || ch.toUTF16Code() == ']')
27 lexeme_ = lexeme::makeCloseParen();
30 if (ch.toUTF16Code() == '`')
32 lexeme_ = lexeme::makeBackQuote();
35 if (ch.toUTF16Code() == '\'')
37 lexeme_ = lexeme::makeQuote();
40 if (ch.toUTF16Code() == '.')
42 lexeme_ = lexeme::makeDot();
45 if (ch.toUTF16Code() == ',' || ch.toUTF16Code() == '#')
47 // ,や#の場合、次に別の文字が続く可能性があるため、NextLexemeLexerを
49 ret.add(new sublexer::CNextLexemeLexer());
52 if (ch.toUTF16Code() == '"')
54 // 先頭が"の場合、stringと判断される。
55 ret.add(new sublexer::CStringLexer());
58 // 基本的に判定できないことはありえない。
62 smart_ptr<lexeme::ILexeme> sublexer::CFirstLex::getLexeme()
64 return smart_ptr<lexeme::ILexeme>();
67 //================================================================================
69 smart_ptr<sublexer::ISubLexer> sublexer::CStringLexer::lex(
70 const utf8_string::CUTF8Char& ch,
71 const utf8_string::CUTF8String& str,
72 smart_ptr<utf8::CUTF8InputStream>& stream
75 return smart_ptr<ISubLexer>();
78 smart_ptr<lexeme::ILexeme> sublexer::CStringLexer::getLexeme()
84 //================================================================================
86 smart_ptr<sublexer::ISubLexer> sublexer::CNextLexemeLexer::lex(
87 const utf8_string::CUTF8Char& ch,
88 const utf8_string::CUTF8String& str,
89 smart_ptr<utf8::CUTF8InputStream>& stream
93 smart_ptr<sublexer::ISubLexer> ret;
94 // 二文字目が特殊であるようなデータを判別するためのlexer。
97 return checkSecondChar(ch, str, stream);
101 return smart_ptr<ISubLexer>();
104 smart_ptr<lexeme::ILexeme> sublexer::CNextLexemeLexer::getLexeme()
109 smart_ptr<sublexer::ISubLexer> sublexer::CNextLexemeLexer::checkSecondChar(
110 const utf8_string::CUTF8Char& ch,
111 const utf8_string::CUTF8String& str,
112 smart_ptr<utf8::CUTF8InputStream>& stream
115 smart_ptr<sublexer::ISubLexer> ret;
117 if (str[0].toUTF16Code() == '#')
120 if (ch.toUTF16Code() == '\'')
122 lexeme_ = lexeme::makeQuasiQuote();
124 else if (ch.toUTF16Code() == '`')
126 lexeme_ = lexeme::makeQuasiBackQuote();
128 else if (ch.toUTF16Code() == ',')
130 // これもまた次の文字で確定するため、さらに次の文字をチェッ
132 ret.add(new CNextLexemeLexer());
134 else if (ch.toUTF16Code() == '(')
136 lexeme_ = lexeme::makeByteVectorOpen();
138 else if (ch.toUTF16Code() == '!')
140 // detumコメント(こう呼んでおく)の開始
141 lexeme_ = lexeme::makeStartDetumComment();
143 else if (ch.toUTF16Code() == '|')
146 lexeme_ = lexeme::makeStartNestedComment();
148 else if (ch.toUTF16Code() == 't' || ch.toUTF16Code() == 'T')
151 lexeme_ = lexeme::makeTrue();
153 else if (ch.toUTF16Code() == 'f' || ch.toUTF16Code() == 'F')
156 lexeme_ = lexeme::makeFalse();
158 else if (ch.toUTF16Code() == '\\')
160 // 文字名かhexかどうかの判別のため、さらに次の文字を判定す
162 ret.add(new CNextLexemeLexer());
165 else if (str[0].toUTF16Code() == '|')
167 // 一文字目がこれの場合には、ネストコメントの末尾しかない。
168 if (ch.toUTF16Code() == '#')
170 lexeme_ = lexeme::makeEndNextedComment();
172 // ネストコメント以外というのはとりあえずありえないため、
174 lexeme_ = lexeme::makeIllegalToken(str + ch);