-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
chars.re
161 lines (158 loc) 路 4.72 KB
/
chars.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
open Grain_tests.TestFramework;
open Grain_tests.Runner;
open Grain_parsing.Location;
open Grain_middle_end.Anf_helper;
describe("chars", ({test, testSkip}) => {
let test_or_skip =
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;
let assertSnapshot = makeSnapshotRunner(test);
let assertCompileError = makeCompileErrorRunner(test);
let assertRun = makeRunner(test_or_skip);
let assertParseWithLocs = makeParseRunner(~keep_locs=true, test);
open Grain_parsing;
open Ast_helper;
let mk_loc =
(
file,
(start_line, start_col, start_bol),
(end_line, end_col, end_bol),
) => {
loc_start: {
pos_fname: file,
pos_lnum: start_line,
pos_bol: start_bol,
pos_cnum: start_col,
},
loc_end: {
pos_fname: file,
pos_lnum: end_line,
pos_bol: end_bol,
pos_cnum: end_col,
},
loc_ghost: false,
};
let char = (~loc=?, s) =>
Toplevel.expr(~loc?) @@ Expression.constant(~loc?, Constant.char(s));
assertRun("char1", "print('A')", "A\n");
assertSnapshot("char2", "'\\x41'");
assertSnapshot("char3", "'\\101'");
assertSnapshot("char4", "'\\u0041'");
assertSnapshot("char5", "'\\u{41}'");
assertSnapshot("char6", "'馃挴'");
assertSnapshot("char7", "'\\u{1F33E}'");
assertSnapshot("char8", "'\\u2728'");
assertRun("char_eq1", "print('馃尵' == '馃尵')", "true\n");
assertRun("char_eq2", "print('馃尵' == '馃挴')", "false\n");
assertRun(
"char_eq3",
"include \"char\" as Char; print(Char.fromCode(0x1F33E) == '馃尵')",
"true\n",
);
assertRun(
"char_eq4",
"include \"char\" as Char; print(Char.fromCode(0x1F33E) == '馃挴')",
"false\n",
);
assertRun("char_toString_escape1", {|print(('\\', 1))|}, "('\\\\', 1)\n");
assertRun("char_toString_escape2", {|print(('\b', 1))|}, "('\\b', 1)\n");
assertRun("char_toString_escape3", {|print(('\f', 1))|}, "('\\f', 1)\n");
assertRun("char_toString_escape4", {|print(('\n', 1))|}, "('\\n', 1)\n");
assertRun("char_toString_escape5", {|print(('
', 1))|}, "('\\n', 1)\n");
assertRun("char_toString_escape6", {|print(('\r', 1))|}, "('\\r', 1)\n");
assertRun("char_toString_escape7", {|print(('\t', 1))|}, "('\\t', 1)\n");
assertRun("char_toString_escape8", {|print(('\v', 1))|}, "('\\v', 1)\n");
assertRun("char_toString_escape9", {|print(('\'', 1))|}, "('\\'', 1)\n");
assertCompileError(
"char_illegal1",
"'abc'",
"This character literal contains multiple characters: 'abc'\nDid you mean to create the string \"abc\" instead?",
);
assertCompileError(
"char_illegal2",
{|'{"test": 1}'|},
{|This character literal contains multiple characters: '\{"test": 1\}'
Did you mean to create the string "\{\\"test\\": 1\}" instead?|},
);
assertCompileError(
"char_illegal3",
"''",
"This character literal contains no character. Did you mean to create an empty string \"\" instead?",
);
assertCompileError(
"unicode_err1",
"let x = '\\u{d800}'",
"Illegal unicode code point",
);
assertCompileError(
"unicode_err2",
"let x = '\\u{dfff}'",
"Illegal unicode code point",
);
assertCompileError(
"unicode_err3",
"let x = '\\u{110000}'",
"Illegal unicode code point",
);
// parse locations
assertParseWithLocs(
"char_loc_simple",
"module Test\n'a'",
{
compile_flags: Grain_tests.Test_utils.default_compile_flags,
module_name:
Location.mkloc(
"Test",
mk_loc("char_loc_simple", (1, 7, 0), (1, 11, 0)),
),
statements: [
char(
~loc=mk_loc("char_loc_simple", (2, 12, 12), (2, 15, 12)),
"a",
),
],
comments: [],
prog_loc: mk_loc("char_loc_simple", (1, 0, 0), (2, 15, 12)),
},
);
assertParseWithLocs(
"char_loc_code",
"module Test\n'\\u{1F3F4}'",
{
compile_flags: Grain_tests.Test_utils.default_compile_flags,
module_name:
Location.mkloc(
"Test",
mk_loc("char_loc_code", (1, 7, 0), (1, 11, 0)),
),
statements: [
char(
~loc=mk_loc("char_loc_code", (2, 12, 12), (2, 23, 12)),
"馃彺",
),
],
comments: [],
prog_loc: mk_loc("char_loc_code", (1, 0, 0), (2, 23, 12)),
},
);
assertParseWithLocs(
"char_loc_emoji",
"module Test\n'馃挴'",
{
compile_flags: Grain_tests.Test_utils.default_compile_flags,
module_name:
Location.mkloc(
"Test",
mk_loc("char_loc_emoji", (1, 7, 0), (1, 11, 0)),
),
statements: [
char(
~loc=mk_loc("char_loc_emoji", (2, 12, 12), (2, 15, 12)),
"馃挴",
),
],
comments: [],
prog_loc: mk_loc("char_loc_emoji", (1, 0, 0), (2, 15, 12)),
},
);
});