31 #include "Format.h" |
31 #include "Format.h" |
32 #include "Lexer.h" |
32 #include "Lexer.h" |
33 |
33 |
34 // ============================================================================= |
34 // ============================================================================= |
35 // |
35 // |
36 static void FormatError (String fmtstr, const String errdescribe, int pos) |
36 static void formatError (String fmtstr, const String errdescribe, int pos) |
37 { |
37 { |
38 fmtstr.Replace ("\n", " "); |
38 fmtstr.replace ("\n", " "); |
39 fmtstr.Replace ("\t", " "); |
39 fmtstr.replace ("\t", " "); |
40 String errmsg ("With format string:\n" + fmtstr + "\n"); |
40 String errmsg ("With format string:\n" + fmtstr + "\n"); |
41 |
41 |
42 for (int x = 0; x < pos; ++x) |
42 for (int x = 0; x < pos; ++x) |
43 errmsg += "-"; |
43 errmsg += "-"; |
44 |
44 |
45 errmsg += "^\n" + errdescribe; |
45 errmsg += "^\n" + errdescribe; |
46 throw std::logic_error (errmsg.STDString()); |
46 throw std::logic_error (errmsg.stdString()); |
47 } |
47 } |
48 |
48 |
49 // ============================================================================= |
49 // ============================================================================= |
50 // |
50 // |
51 String FormatArgs (const String& fmtstr, const std::vector<String>& args) |
51 String formatArgs (const String& fmtstr, const std::vector<String>& args) |
52 { |
52 { |
53 String fmt = fmtstr; |
53 String fmt = fmtstr; |
54 String out; |
54 String out; |
55 int pos = 0; |
55 int pos = 0; |
56 |
56 |
57 while ((pos = fmt.FirstIndexOf ("%", pos)) != -1) |
57 while ((pos = fmt.firstIndexOf ("%", pos)) != -1) |
58 { |
58 { |
59 if (fmt[pos + 1] == '%') |
59 if (fmt[pos + 1] == '%') |
60 { |
60 { |
61 fmt.Replace (pos, 2, "%"); |
61 fmt.replace (pos, 2, "%"); |
62 pos++; |
62 pos++; |
63 continue; |
63 continue; |
64 } |
64 } |
65 |
65 |
66 int ofs = 1; |
66 int ofs = 1; |
72 mod = fmt[pos + ofs]; |
72 mod = fmt[pos + ofs]; |
73 ofs++; |
73 ofs++; |
74 } |
74 } |
75 |
75 |
76 if (!isdigit (fmt[pos + ofs])) |
76 if (!isdigit (fmt[pos + ofs])) |
77 FormatError (fmtstr, "bad format string, expected digit with optional " |
77 formatError (fmtstr, "bad format string, expected digit with optional " |
78 "modifier after '%%'", pos); |
78 "modifier after '%%'", pos); |
79 |
79 |
80 int i = fmt[pos + ofs] - '0'; |
80 int i = fmt[pos + ofs] - '0'; |
81 |
81 |
82 if (i > static_cast<signed> (args.size())) |
82 if (i > static_cast<signed> (args.size())) |
83 FormatError (fmtstr, String ("Format argument #") + i + " used but not defined.", pos); |
83 formatError (fmtstr, String ("Format argument #") + i + " used but not defined.", pos); |
84 |
84 |
85 String replacement = args[i - 1]; |
85 String replacement = args[i - 1]; |
86 |
86 |
87 switch (mod) |
87 switch (mod) |
88 { |
88 { |
89 case 's': replacement = (replacement == "1") ? "" : "s"; break; |
89 case 's': replacement = (replacement == "1") ? "" : "s"; break; |
90 case 'd': replacement.SPrintf ("%d", replacement[0]); break; |
90 case 'd': replacement.sprintf ("%d", replacement[0]); break; |
91 case 'x': replacement.SPrintf ("0x%X", replacement.ToLong()); break; |
91 case 'x': replacement.sprintf ("0x%X", replacement.toLong()); break; |
92 default: break; |
92 default: break; |
93 } |
93 } |
94 |
94 |
95 fmt.Replace (pos, 1 + ofs, replacement); |
95 fmt.replace (pos, 1 + ofs, replacement); |
96 pos += replacement.Length(); |
96 pos += replacement.length(); |
97 } |
97 } |
98 |
98 |
99 return fmt; |
99 return fmt; |
100 } |
100 } |
101 |
101 |
102 // ============================================================================= |
102 // ============================================================================= |
103 // |
103 // |
104 void Error (String msg) |
104 void error (String msg) |
105 { |
105 { |
106 Lexer* lx = Lexer::GetCurrentLexer(); |
106 Lexer* lx = Lexer::getCurrentLexer(); |
107 String fileinfo; |
107 String fileinfo; |
108 |
108 |
109 if (lx != null && lx->HasValidToken()) |
109 if (lx != null && lx->hasValidToken()) |
110 { |
110 { |
111 Lexer::TokenInfo* tk = lx->Token(); |
111 Lexer::TokenInfo* tk = lx->token(); |
112 fileinfo = Format ("%1:%2:%3: ", tk->file, tk->line, tk->column); |
112 fileinfo = format ("%1:%2:%3: ", tk->file, tk->line, tk->column); |
113 } |
113 } |
114 |
114 |
115 throw std::runtime_error (fileinfo + msg); |
115 throw std::runtime_error (fileinfo + msg); |
116 } |
116 } |