Skip to content

Commit

Permalink
Merge pull request #1340 from GaijinEntertainment/string_builder
Browse files Browse the repository at this point in the history
string builder without s\r errors
  • Loading branch information
borisbat authored Oct 31, 2024
2 parents ba39dcb + 053ee81 commit 5c82b75
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 196 deletions.
15 changes: 15 additions & 0 deletions examples/test/misc/hello_world.das
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ def test_typeinfo( arg : int ) {
debug(c);
}

/////////////////
// string builder

[export]
def test_string_builder ( arg,arg2:int ) {
let a = "";
debug(a);
let b = "hello, world!";
debug(b);
let c = "arg={arg}\n";
debug(c);
let d = "{arg} + {arg2} = {arg+arg2}";
debug(d);
}

///////
// main

Expand Down
166 changes: 83 additions & 83 deletions src/parser/ds2_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,83 +1006,83 @@ static const yytype_uint8 yytranslate[] =
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_int16 yyrline[] =
{
0, 524, 524, 525, 530, 531, 532, 533, 534, 535,
536, 537, 538, 539, 540, 541, 542, 546, 552, 553,
554, 558, 559, 563, 581, 582, 583, 584, 588, 592,
597, 606, 614, 630, 635, 643, 643, 682, 700, 704,
707, 711, 717, 726, 729, 735, 736, 740, 744, 745,
749, 752, 758, 764, 767, 773, 774, 778, 779, 780,
789, 790, 794, 795, 801, 802, 803, 804, 805, 809,
815, 821, 827, 835, 845, 854, 861, 862, 863, 864,
865, 866, 870, 875, 883, 884, 885, 889, 890, 891,
892, 893, 894, 895, 896, 902, 905, 911, 914, 920,
921, 922, 926, 939, 957, 960, 968, 979, 990, 1001,
1004, 1011, 1015, 1022, 1023, 1027, 1028, 1029, 1033, 1036,
1043, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065,
1066, 1067, 1068, 1069, 1070, 1071, 1072, 1073, 1074, 1075,
1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085,
1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095,
1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105,
1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115,
1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125,
1126, 1127, 1128, 1129, 1130, 1135, 1153, 1154, 1155, 1159,
1165, 1165, 1182, 1186, 1197, 1210, 1211, 1212, 1213, 1214,
1215, 1216, 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224,
1225, 1226, 1227, 1231, 1236, 1242, 1248, 1249, 1253, 1257,
1264, 1265, 1276, 1280, 1283, 1291, 1291, 1291, 1297, 1300,
1304, 1308, 1315, 1321, 1325, 1329, 1332, 1335, 1343, 1346,
1354, 1360, 1361, 1362, 1366, 1367, 1371, 1372, 1376, 1381,
1389, 1395, 1407, 1410, 1416, 1416, 1416, 1419, 1419, 1419,
1424, 1424, 1424, 1432, 1432, 1432, 1438, 1448, 1459, 1474,
1477, 1483, 1484, 1491, 1502, 1503, 1504, 1508, 1509, 1510,
1511, 1515, 1520, 1528, 1529, 1533, 1540, 1544, 1550, 1551,
1552, 1553, 1554, 1555, 1556, 1560, 1561, 1562, 1563, 1564,
1565, 1566, 1567, 1568, 1569, 1570, 1571, 1572, 1573, 1574,
1575, 1576, 1577, 1578, 1582, 1589, 1601, 1606, 1616, 1620,
1627, 1630, 1630, 1630, 1635, 1635, 1635, 1648, 1652, 1656,
1661, 1668, 1668, 1668, 1675, 1679, 1688, 1692, 1695, 1701,
1702, 1703, 1704, 1705, 1706, 1707, 1708, 1709, 1710, 1711,
1712, 1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721,
1722, 1723, 1724, 1725, 1726, 1727, 1728, 1729, 1730, 1731,
1732, 1733, 1734, 1735, 1736, 1742, 1743, 1744, 1745, 1746,
1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768,
1769, 1770, 1773, 1776, 1777, 1780, 1780, 1780, 1783, 1788,
1792, 1796, 1796, 1796, 1801, 1804, 1808, 1808, 1808, 1813,
1816, 1817, 1818, 1819, 1820, 1821, 1822, 1823, 1824, 1826,
1830, 1834, 1835, 1836, 1837, 1838, 1839, 1840, 1844, 1848,
1852, 1856, 1860, 1864, 1868, 1872, 1876, 1883, 1884, 1888,
1889, 1890, 1894, 1895, 1899, 1900, 1901, 1905, 1906, 1910,
1921, 1924, 1924, 1943, 1942, 1956, 1955, 1971, 1980, 1990,
1991, 1995, 1998, 2007, 2008, 2012, 2015, 2018, 2034, 2043,
2044, 2048, 2051, 2054, 2068, 2069, 2073, 2079, 2085, 2088,
2092, 2101, 2102, 2103, 2107, 2108, 2112, 2119, 2124, 2133,
2139, 2150, 2153, 2158, 2169, 2172, 2172, 2192, 2193, 2197,
2198, 2199, 2203, 2206, 2206, 2224, 2228, 2235, 2238, 2251,
2268, 2269, 2270, 2275, 2275, 2301, 2305, 2306, 2307, 2311,
2321, 2324, 2330, 2335, 2330, 2350, 2351, 2355, 2356, 2360,
2366, 2367, 2371, 2372, 2373, 2377, 2380, 2386, 2391, 2386,
2405, 2412, 2417, 2426, 2432, 2443, 2444, 2445, 2446, 2447,
2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2456, 2457,
2458, 2459, 2460, 2461, 2462, 2463, 2464, 2465, 2466, 2467,
2468, 2469, 2473, 2474, 2475, 2476, 2477, 2478, 2479, 2480,
2484, 2495, 2499, 2506, 2518, 2525, 2534, 2539, 2549, 2562,
2562, 2562, 2575, 2579, 2586, 2590, 2594, 2598, 2605, 2608,
2626, 2627, 2628, 2629, 2630, 2630, 2630, 2634, 2639, 2646,
2646, 2653, 2657, 2661, 2666, 2671, 2676, 2681, 2685, 2689,
2694, 2698, 2702, 2707, 2707, 2707, 2713, 2720, 2720, 2720,
2725, 2725, 2725, 2731, 2731, 2731, 2736, 2740, 2740, 2740,
2745, 2745, 2745, 2754, 2758, 2758, 2758, 2763, 2763, 2763,
2772, 2776, 2776, 2776, 2781, 2781, 2781, 2790, 2790, 2790,
2796, 2796, 2796, 2805, 2808, 2819, 2835, 2840, 2845, 2835,
2870, 2875, 2881, 2870, 2906, 2911, 2916, 2906, 2946, 2947,
2948, 2949, 2950, 2954, 2961, 2968, 2974, 2980, 2987, 2994,
3000, 3010, 3018, 3023, 3030, 3031, 3035, 3035, 3035, 3043,
3043, 3043, 3050, 3050, 3050, 3057, 3057, 3057, 3068, 3074,
3080, 3086, 3086, 3086, 3096, 3104, 3104, 3104, 3114, 3114,
3114, 3124, 3124, 3124, 3134, 3142, 3142, 3142, 3150, 3157,
3157, 3157, 3167, 3170, 3176, 3184, 3192, 3200, 3213, 3214,
3218, 3219, 3224, 3227, 3230
0, 527, 527, 528, 533, 534, 535, 536, 537, 538,
539, 540, 541, 542, 543, 544, 545, 549, 555, 556,
557, 561, 562, 566, 584, 585, 586, 587, 591, 595,
600, 609, 617, 633, 638, 646, 646, 685, 703, 707,
710, 714, 720, 729, 732, 738, 739, 743, 747, 748,
752, 755, 761, 767, 770, 776, 777, 781, 782, 783,
792, 793, 797, 798, 804, 805, 806, 807, 808, 812,
818, 824, 830, 838, 848, 857, 864, 865, 866, 867,
868, 869, 873, 878, 886, 887, 888, 892, 893, 894,
895, 896, 897, 898, 899, 905, 908, 914, 917, 923,
924, 925, 929, 942, 960, 963, 971, 982, 993, 1004,
1007, 1014, 1018, 1025, 1026, 1030, 1031, 1032, 1036, 1039,
1046, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058,
1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068,
1069, 1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077, 1078,
1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088,
1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098,
1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108,
1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118,
1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128,
1129, 1130, 1131, 1132, 1133, 1138, 1156, 1157, 1158, 1162,
1168, 1168, 1185, 1189, 1200, 1213, 1214, 1215, 1216, 1217,
1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227,
1228, 1229, 1230, 1234, 1239, 1245, 1251, 1252, 1256, 1260,
1267, 1268, 1279, 1283, 1286, 1294, 1294, 1294, 1300, 1303,
1307, 1311, 1318, 1324, 1328, 1332, 1335, 1338, 1346, 1349,
1357, 1363, 1364, 1365, 1369, 1370, 1374, 1375, 1379, 1384,
1392, 1398, 1410, 1413, 1419, 1419, 1419, 1422, 1422, 1422,
1427, 1427, 1427, 1435, 1435, 1435, 1441, 1451, 1462, 1477,
1480, 1486, 1487, 1494, 1505, 1506, 1507, 1511, 1512, 1513,
1514, 1518, 1523, 1531, 1532, 1536, 1543, 1547, 1553, 1554,
1555, 1556, 1557, 1558, 1559, 1563, 1564, 1565, 1566, 1567,
1568, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577,
1578, 1579, 1580, 1581, 1585, 1592, 1604, 1609, 1619, 1623,
1630, 1633, 1633, 1633, 1638, 1638, 1638, 1651, 1655, 1659,
1664, 1671, 1671, 1671, 1678, 1682, 1691, 1695, 1698, 1704,
1705, 1706, 1707, 1708, 1709, 1710, 1711, 1712, 1713, 1714,
1715, 1716, 1717, 1718, 1719, 1720, 1721, 1722, 1723, 1724,
1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734,
1735, 1736, 1737, 1738, 1739, 1745, 1746, 1747, 1748, 1749,
1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771,
1772, 1773, 1776, 1779, 1780, 1783, 1783, 1783, 1786, 1791,
1795, 1799, 1799, 1799, 1804, 1807, 1811, 1811, 1811, 1816,
1819, 1820, 1821, 1822, 1823, 1824, 1825, 1826, 1827, 1829,
1833, 1837, 1838, 1839, 1840, 1841, 1842, 1843, 1847, 1851,
1855, 1859, 1863, 1867, 1871, 1875, 1879, 1886, 1887, 1891,
1892, 1893, 1897, 1898, 1902, 1903, 1904, 1908, 1909, 1913,
1924, 1927, 1927, 1946, 1945, 1959, 1958, 1974, 1983, 1993,
1994, 1998, 2001, 2010, 2011, 2015, 2018, 2021, 2037, 2046,
2047, 2051, 2054, 2057, 2071, 2072, 2076, 2082, 2088, 2091,
2095, 2104, 2105, 2106, 2110, 2111, 2115, 2122, 2127, 2136,
2142, 2153, 2156, 2161, 2172, 2175, 2175, 2195, 2196, 2200,
2201, 2202, 2206, 2209, 2209, 2227, 2231, 2238, 2241, 2254,
2271, 2272, 2273, 2278, 2278, 2304, 2308, 2309, 2310, 2314,
2324, 2327, 2333, 2338, 2333, 2353, 2354, 2358, 2359, 2363,
2369, 2370, 2374, 2375, 2376, 2380, 2383, 2389, 2394, 2389,
2408, 2415, 2420, 2429, 2435, 2446, 2447, 2448, 2449, 2450,
2451, 2452, 2453, 2454, 2455, 2456, 2457, 2458, 2459, 2460,
2461, 2462, 2463, 2464, 2465, 2466, 2467, 2468, 2469, 2470,
2471, 2472, 2476, 2477, 2478, 2479, 2480, 2481, 2482, 2483,
2487, 2498, 2502, 2509, 2521, 2528, 2537, 2542, 2552, 2565,
2565, 2565, 2578, 2582, 2589, 2593, 2597, 2601, 2608, 2611,
2629, 2630, 2631, 2632, 2633, 2633, 2633, 2637, 2642, 2649,
2649, 2656, 2660, 2664, 2669, 2674, 2679, 2684, 2688, 2692,
2697, 2701, 2705, 2710, 2710, 2710, 2716, 2723, 2723, 2723,
2728, 2728, 2728, 2734, 2734, 2734, 2739, 2743, 2743, 2743,
2748, 2748, 2748, 2757, 2761, 2761, 2761, 2766, 2766, 2766,
2775, 2779, 2779, 2779, 2784, 2784, 2784, 2793, 2793, 2793,
2799, 2799, 2799, 2808, 2811, 2822, 2838, 2843, 2848, 2838,
2873, 2878, 2884, 2873, 2909, 2914, 2919, 2909, 2949, 2950,
2951, 2952, 2953, 2957, 2964, 2971, 2977, 2983, 2990, 2997,
3003, 3013, 3021, 3026, 3033, 3034, 3038, 3038, 3038, 3046,
3046, 3046, 3053, 3053, 3053, 3060, 3060, 3060, 3071, 3077,
3083, 3089, 3089, 3089, 3099, 3107, 3107, 3107, 3117, 3117,
3117, 3127, 3127, 3127, 3137, 3145, 3145, 3145, 3153, 3160,
3160, 3160, 3170, 3173, 3179, 3187, 3195, 3203, 3216, 3217,
3221, 3222, 3227, 3230, 3233
};
#endif

Expand Down Expand Up @@ -5401,19 +5401,19 @@ YYLTYPE yylloc = yyloc_default;
break;

case 24: /* character_sequence: STRING_CHARACTER */
{ (yyval.s) = new string(); *(yyval.s) += (yyvsp[0].ch); }
{ (yyval.s) = new string(); *(yyval.s) += (yyvsp[0].ch); }
break;

case 25: /* character_sequence: STRING_CHARACTER_ESC */
{ (yyval.s) = new string(); *(yyval.s) += "\\\\"; }
{ (yyval.s) = new string(); *(yyval.s) += "\\\\"; }
break;

case 26: /* character_sequence: character_sequence STRING_CHARACTER */
{ (yyval.s) = (yyvsp[-1].s); *(yyvsp[-1].s) += (yyvsp[0].ch); }
{ (yyval.s) = (yyvsp[-1].s); *(yyvsp[-1].s) += (yyvsp[0].ch); }
break;

case 27: /* character_sequence: character_sequence STRING_CHARACTER_ESC */
{ (yyval.s) = (yyvsp[-1].s); *(yyvsp[-1].s) += "\\\\"; }
{ (yyval.s) = (yyvsp[-1].s); *(yyvsp[-1].s) += "\\\\"; }
break;

case 28: /* string_constant: "start of the string" character_sequence "end of the string" */
Expand All @@ -5429,7 +5429,7 @@ YYLTYPE yylloc = yyloc_default;
break;

case 30: /* string_builder_body: string_builder_body character_sequence */
{
{
bool err;
auto esconst = unescapeString(*(yyvsp[0].s),&err);
if ( err ) das2_yyerror(scanner,"invalid escape sequence",tokAt(scanner,(yylsp[-1])), CompilationError::invalid_escape_sequence);
Expand All @@ -5441,7 +5441,7 @@ YYLTYPE yylloc = yyloc_default;
break;

case 31: /* string_builder_body: string_builder_body "{" expr "}" */
{
{
auto se = ExpressionPtr((yyvsp[-1].pExpression));
static_cast<ExprStringBuilder *>((yyvsp[-3].pExpression))->elements.push_back(se);
(yyval.pExpression) = (yyvsp[-3].pExpression);
Expand Down
5 changes: 1 addition & 4 deletions src/parser/ds2_parser.output
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Terminals unused in grammar
State 464 conflicts: 1 shift/reduce
State 471 conflicts: 1 shift/reduce
State 640 conflicts: 1 shift/reduce
State 814 conflicts: 2 shift/reduce


Grammar
Expand Down Expand Up @@ -23141,9 +23140,7 @@ State 814
STRING_CHARACTER shift, and go to state 211
STRING_CHARACTER_ESC shift, and go to state 212

STRING_CHARACTER [reduce using rule 29 (string_builder_body)]
STRING_CHARACTER_ESC [reduce using rule 29 (string_builder_body)]
$default reduce using rule 29 (string_builder_body)
$default reduce using rule 29 (string_builder_body)


State 815
Expand Down
15 changes: 9 additions & 6 deletions src/parser/ds2_parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@
%nonassoc '$' '@'
%nonassoc COLCOL

%right STRING_CHARACTER STRING_CHARACTER_ESC
%right BEGIN_STRING_EXPR END_STRING_EXPR

%%

program
Expand Down Expand Up @@ -578,10 +581,10 @@ module_declaration
;

character_sequence
: STRING_CHARACTER[char] { $$ = new string(); *$$ += $char; }
| STRING_CHARACTER_ESC { $$ = new string(); *$$ += "\\\\"; }
| character_sequence[elem] STRING_CHARACTER[char] { $$ = $elem; *$elem += $char; }
| character_sequence[elem] STRING_CHARACTER_ESC { $$ = $elem; *$elem += "\\\\"; }
: STRING_CHARACTER[char] { $$ = new string(); *$$ += $char; }
| STRING_CHARACTER_ESC { $$ = new string(); *$$ += "\\\\"; }
| character_sequence[elem] STRING_CHARACTER[char] %prec STRING_CHARACTER { $$ = $elem; *$elem += $char; }
| character_sequence[elem] STRING_CHARACTER_ESC %prec STRING_CHARACTER { $$ = $elem; *$elem += "\\\\"; }
;

string_constant
Expand All @@ -594,7 +597,7 @@ string_builder_body
$$->at = LineInfo(yyextra->g_FileAccessStack.back(),
yylloc.first_column,yylloc.first_line,yylloc.last_column,yylloc.last_line);
}
| string_builder_body[sb] character_sequence[sconst] {
| string_builder_body[sb] character_sequence[sconst] %prec STRING_CHARACTER {
bool err;
auto esconst = unescapeString(*$sconst,&err);
if ( err ) das2_yyerror(scanner,"invalid escape sequence",tokAt(scanner,@sb), CompilationError::invalid_escape_sequence);
Expand All @@ -603,7 +606,7 @@ string_builder_body
static_cast<ExprStringBuilder *>($sb)->elements.push_back(sc);
$$ = $sb;
}
| string_builder_body[sb] BEGIN_STRING_EXPR expr[subexpr] END_STRING_EXPR {
| string_builder_body[sb] BEGIN_STRING_EXPR expr[subexpr] END_STRING_EXPR %prec BEGIN_STRING_EXPR {
auto se = ExpressionPtr($subexpr);
static_cast<ExprStringBuilder *>($sb)->elements.push_back(se);
$$ = $sb;
Expand Down
Loading

0 comments on commit 5c82b75

Please sign in to comment.