1 /* |
|
2 Copyright 2012-2014 Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <cstdio> |
|
30 #include "main.h" |
|
31 #include "format.h" |
|
32 #include "lexer.h" |
|
33 |
|
34 // ============================================================================= |
|
35 // |
|
36 static void draw_position (const string& fmt, int pos) |
|
37 { |
|
38 string rep (fmt); |
|
39 rep.replace ("\n", "↵"); |
|
40 rep.replace ("\t", "⇥"); |
|
41 |
|
42 fprintf (stderr, "%s\n", rep.chars()); |
|
43 |
|
44 for (int x = 0; x < pos; ++x) |
|
45 fprintf (stderr, "-"); |
|
46 |
|
47 fprintf (stderr, "^\n"); |
|
48 } |
|
49 |
|
50 // ============================================================================= |
|
51 // |
|
52 string format_args (const list<format_arg>& args) |
|
53 { |
|
54 const string& fmtstr = args[0].as_string(); |
|
55 assert (args.size() >= 1); |
|
56 |
|
57 if (args.size() == 1) |
|
58 return args[0].as_string(); |
|
59 |
|
60 string fmt = fmtstr; |
|
61 string out; |
|
62 int pos = 0; |
|
63 |
|
64 while ((pos = fmt.first ("%", pos)) != -1) |
|
65 { |
|
66 if (fmt[pos + 1] == '%') |
|
67 { |
|
68 fmt.replace (pos, 2, "%"); |
|
69 pos++; |
|
70 continue; |
|
71 } |
|
72 |
|
73 int ofs = 1; |
|
74 char mod = '\0'; |
|
75 |
|
76 // handle modifiers |
|
77 if (fmt[pos + ofs] == 's' || fmt[pos + ofs] == 'x') |
|
78 { |
|
79 mod = fmt[pos + ofs]; |
|
80 ofs++; |
|
81 } |
|
82 |
|
83 if (!isdigit (fmt[pos + ofs])) |
|
84 { |
|
85 fprintf (stderr, "bad format string, expected digit with optional " |
|
86 "modifier after '%%':\n"); |
|
87 draw_position (fmt, pos); |
|
88 return fmt; |
|
89 } |
|
90 |
|
91 int i = fmt[pos + ofs] - '0'; |
|
92 |
|
93 if (i >= args.size()) |
|
94 { |
|
95 fprintf (stderr, "format arg #%d used but not defined: %s\n", i, fmtstr.chars()); |
|
96 return fmt; |
|
97 } |
|
98 |
|
99 string repl = args[i].as_string(); |
|
100 |
|
101 switch (mod) |
|
102 { |
|
103 case 's': repl = (repl == "1") ? "" : "s"; break; |
|
104 case 'd': repl.sprintf ("%d", repl[0]); break; |
|
105 case 'x': repl.sprintf ("0x%X", repl.to_long()); break; |
|
106 default: break; |
|
107 } |
|
108 |
|
109 fmt.replace (pos, 1 + ofs, repl); |
|
110 pos += repl.length(); |
|
111 } |
|
112 |
|
113 return fmt; |
|
114 } |
|
115 |
|
116 // ============================================================================= |
|
117 // |
|
118 void print_args (FILE* fp, const list<format_arg>& args) |
|
119 { |
|
120 string out = format_args (args); |
|
121 fprintf (fp, "%s", out.chars()); |
|
122 } |
|
123 |
|
124 // ============================================================================= |
|
125 // |
|
126 void do_error (string msg) |
|
127 { |
|
128 lexer* lx = lexer::get_current_lexer(); |
|
129 string fileinfo; |
|
130 |
|
131 if (lx != null && lx->has_valid_token()) |
|
132 { |
|
133 lexer::token* tk = lx->get_token(); |
|
134 fileinfo = format ("%1:%2:%3: ", tk->file, tk->line, tk->column); |
|
135 } |
|
136 |
|
137 throw script_error (fileinfo + msg); |
|
138 } |
|