| ... | @@ -1972,16 +1972,7 @@ fn transStringLiteral( | ... | @@ -1972,16 +1972,7 @@ fn transStringLiteral( |
| 1972 | const bytes_ptr = stmt.getString_bytes_begin_size(&len); | 1972 | const bytes_ptr = stmt.getString_bytes_begin_size(&len); |
| 1973 | const str = bytes_ptr[0..len]; | 1973 | const str = bytes_ptr[0..len]; |
| 1974 | | 1974 | |
| 1975 | var char_buf: [4]u8 = undefined; | 1975 | const token = try appendTokenFmt(rp.c, .StringLiteral, "\"{Z}\"", .{str}); |
| 1976 | len = 0; | | |
| 1977 | for (str) |c| len += escapeChar(c, &char_buf).len; | | |
| 1978 | | | |
| 1979 | const buf = try rp.c.arena.alloc(u8, len + "\"\"".len); | | |
| 1980 | buf[0] = '"'; | | |
| 1981 | writeEscapedString(buf[1..], str); | | |
| 1982 | buf[buf.len - 1] = '"'; | | |
| 1983 | | | |
| 1984 | const token = try appendToken(rp.c, .StringLiteral, buf); | | |
| 1985 | const node = try rp.c.arena.create(ast.Node.OneToken); | 1976 | const node = try rp.c.arena.create(ast.Node.OneToken); |
| 1986 | node.* = .{ | 1977 | node.* = .{ |
| 1987 | .base = .{ .tag = .StringLiteral }, | 1978 | .base = .{ .tag = .StringLiteral }, |
| ... | @@ -1999,41 +1990,6 @@ fn transStringLiteral( | ... | @@ -1999,41 +1990,6 @@ fn transStringLiteral( |
| 1999 | } | 1990 | } |
| 2000 | } | 1991 | } |
| 2001 | | 1992 | |
| 2002 | fn escapedStringLen(s: []const u8) usize { | | |
| 2003 | var len: usize = 0; | | |
| 2004 | var char_buf: [4]u8 = undefined; | | |
| 2005 | for (s) |c| len += escapeChar(c, &char_buf).len; | | |
| 2006 | return len; | | |
| 2007 | } | | |
| 2008 | | | |
| 2009 | fn writeEscapedString(buf: []u8, s: []const u8) void { | | |
| 2010 | var char_buf: [4]u8 = undefined; | | |
| 2011 | var i: usize = 0; | | |
| 2012 | for (s) |c| { | | |
| 2013 | const escaped = escapeChar(c, &char_buf); | | |
| 2014 | mem.copy(u8, buf[i..], escaped); | | |
| 2015 | i += escaped.len; | | |
| 2016 | } | | |
| 2017 | } | | |
| 2018 | | | |
| 2019 | // Returns either a string literal or a slice of `buf`. | | |
| 2020 | fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 { | | |
| 2021 | return switch (c) { | | |
| 2022 | '\"' => "\\\"", | | |
| 2023 | '\'' => "\\'", | | |
| 2024 | '\\' => "\\\\", | | |
| 2025 | '\n' => "\\n", | | |
| 2026 | '\r' => "\\r", | | |
| 2027 | '\t' => "\\t", | | |
| 2028 | // Handle the remaining escapes Zig doesn't support by turning them | | |
| 2029 | // into their respective hex representation | | |
| 2030 | else => if (std.ascii.isCntrl(c)) | | |
| 2031 | std.fmt.bufPrint(char_buf, "\\x{x:0>2}", .{c}) catch unreachable | | |
| 2032 | else | | |
| 2033 | std.fmt.bufPrint(char_buf, "{c}", .{c}) catch unreachable, | | |
| 2034 | }; | | |
| 2035 | } | | |
| 2036 | | | |
| 2037 | fn transCCast( | 1993 | fn transCCast( |
| 2038 | rp: RestorePoint, | 1994 | rp: RestorePoint, |
| 2039 | scope: *Scope, | 1995 | scope: *Scope, |
| ... | @@ -2922,8 +2878,7 @@ fn transCharLiteral( | ... | @@ -2922,8 +2878,7 @@ fn transCharLiteral( |
| 2922 | if (val > 255) | 2878 | if (val > 255) |
| 2923 | break :blk try transCreateNodeInt(rp.c, val); | 2879 | break :blk try transCreateNodeInt(rp.c, val); |
| 2924 | } | 2880 | } |
| 2925 | var char_buf: [4]u8 = undefined; | 2881 | const token = try appendTokenFmt(rp.c, .CharLiteral, "'{Z}'", .{@intCast(u8, val)}); |
| 2926 | const token = try appendTokenFmt(rp.c, .CharLiteral, "'{}'", .{escapeChar(@intCast(u8, val), &char_buf)}); | | |
| 2927 | const node = try rp.c.arena.create(ast.Node.OneToken); | 2882 | const node = try rp.c.arena.create(ast.Node.OneToken); |
| 2928 | node.* = .{ | 2883 | node.* = .{ |
| 2929 | .base = .{ .tag = .CharLiteral }, | 2884 | .base = .{ .tag = .CharLiteral }, |
| ... | @@ -5247,23 +5202,8 @@ fn isZigPrimitiveType(name: []const u8) bool { | ... | @@ -5247,23 +5202,8 @@ fn isZigPrimitiveType(name: []const u8) bool { |
| 5247 | mem.eql(u8, name, "c_ulonglong"); | 5202 | mem.eql(u8, name, "c_ulonglong"); |
| 5248 | } | 5203 | } |
| 5249 | | 5204 | |
| 5250 | fn isValidZigIdentifier(name: []const u8) bool { | | |
| 5251 | for (name) |c, i| { | | |
| 5252 | switch (c) { | | |
| 5253 | '_', 'a'...'z', 'A'...'Z' => {}, | | |
| 5254 | '0'...'9' => if (i == 0) return false, | | |
| 5255 | else => return false, | | |
| 5256 | } | | |
| 5257 | } | | |
| 5258 | return true; | | |
| 5259 | } | | |
| 5260 | | | |
| 5261 | fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { | 5205 | fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { |
| 5262 | if (!isValidZigIdentifier(name) or std.zig.Token.getKeyword(name) != null) { | 5206 | return appendTokenFmt(c, .Identifier, "{z}", .{name}); |
| 5263 | return appendTokenFmt(c, .Identifier, "@\"{}\"", .{name}); | | |
| 5264 | } else { | | |
| 5265 | return appendTokenFmt(c, .Identifier, "{}", .{name}); | | |
| 5266 | } | | |
| 5267 | } | 5207 | } |
| 5268 | | 5208 | |
| 5269 | fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { | 5209 | fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { |