1
2
3
4
5
6 #include <sngcpp/parser/SourceLineParser.hpp>
7 #include <sngcpp/lexer/CppTokenLexer.hpp>
8 #include <sngcpp/lexer/CppTokenLexerTokens.hpp>
9 #include <soulng/util/TextUtils.hpp>
10 #include <soulng/util/Unicode.hpp>
11
12 using namespace soulng::util;
13 using namespace soulng::unicode;
14
15 void ParseSourceLine(const std::u32string& sourceLine, sngcpp::ast::SourceCodeWriter& sourceCodeWriter, bool& inBlockComment)
16 {
17
18
19 std::u32string line(sourceLine);
20 line.append(U"\n");
21 CppTokenLexer lexer(line, "", 0);
22 lexer.inBlockComment = inBlockComment;
23 lexer.SetSeparatorChar('\n');
24 lexer.writer = &sourceCodeWriter;
25 ++lexer;
26 int i = 0;
27 while (*lexer != END)
28 {
29 switch (*lexer)
30 {
31 case WS: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteSpaces(token.match.ToString()); break; }
32 case LINECOMMENT: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteLineComment(token.match.ToString()); return; }
33 case BLOCKCOMMENT:
34 {
35 soulng::lexer::Token token = lexer.GetToken(i);
36 sourceCodeWriter.WriteBlockComment(token.match.ToString());
37 if (!soulng::util::EndsWith(ToUtf8(token.match.ToString()), "*/"))
38 {
39 inBlockComment = true;
40 return;
41 }
42 break;
43 }
44 case BLOCKCOMMENTLINE:
45 {
46 soulng::lexer::Token token = lexer.GetToken(i);
47 sourceCodeWriter.WriteBlockComment(token.match.ToString());
48 if (token.match.ToString().find(U"*/") != std::u32string::npos)
49 {
50 inBlockComment = false;
51 }
52 return;
53 }
54 case KEYWORD: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteKeyword(token.match.ToString()); break; }
55 case ID: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteIdentifier(token.match.ToString()); break; }
56 case CHARLIT: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteChar(token.match.ToString()); break; }
57 case STRINGLIT: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteString(token.match.ToString()); break; }
58 case NUMBER: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteNumber(token.match.ToString()); break; }
59 case PP: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WritePP(token.match.ToString()); break; }
60 case OTHER: { soulng::lexer::Token token = lexer.GetToken(i); sourceCodeWriter.WriteOther(token.match.ToString()); break; }
61 }
62 ++lexer;
63 ++i;
64 }
65 }