Skip to content

Commit c738f75

Browse files
authored
Refactor jsx mode in parser (#7751)
* Add token dump printer * Remove ForwardSlash token * Document RESCRIPT_BSC_EXE for local usage * Process hypens in parse_module_long_ident for jsx * Update test snapshot * Add how to view tokens. * fmt * Clean up * Correct fragment range * Use legacy clean for analysis projects * Update analysis snapshot * Copilot review suggestion * Return original error directly * Add trailing hypen error * Mutate token on the call side. * Add changelog entry
1 parent fb162b2 commit c738f75

File tree

19 files changed

+335
-179
lines changed

19 files changed

+335
-179
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232

3333
#### :house: Internal
3434

35+
- Add token viewer to `res_parser`. https://github.com/rescript-lang/rescript/pull/7751
36+
- Refactor jsx mode in Scanner. https://github.com/rescript-lang/rescript/pull/7751
37+
3538
# 12.0.0-beta.4
3639

3740
#### :bug: Bug fix

CONTRIBUTING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,24 @@ make lib # Build compiler and standard library
126126
./cli/bsc.js myTestFile.res
127127
```
128128

129+
To view the tokens of a file run:
130+
131+
```sh
132+
dune exec res_parser -- -print tokens myTestFile.res
133+
```
134+
129135
To view the untyped tree of the file run:
130136

131137
```sh
132138
./cli/bsc.js -dparsetree myTestFile.res
133139
```
134140

141+
or
142+
143+
```sh
144+
dune exec res_parser -- -print ast -recover myTestFile.res
145+
```
146+
135147
To view the typed tree of the file run:
136148

137149
```sh
@@ -148,6 +160,16 @@ npm install
148160
npm link rescript
149161
```
150162

163+
#### Use Local BSC with Existing ReScript Installation
164+
165+
Alternatively, you can set the `RESCRIPT_BSC_EXE` environment variable to point to your locally compiled `bsc.exe`.
166+
167+
```sh
168+
RESCRIPT_BSC_EXE=your-rescript-repo/packages/@rescript/darwin-arm64/bin/bsc.exe npx rescript
169+
```
170+
171+
This will test the local compiler while still using the build system from the installed Node module.
172+
151173
### Running Automatic Tests
152174

153175
We provide different test suites for different levels of the compiler and build system infrastructure. Always make sure to locally build your compiler before running any tests.

compiler/syntax/cli/res_cli.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ end = struct
194194
("-recover", Arg.Unit (fun () -> recover := true), "Emit partial ast");
195195
( "-print",
196196
Arg.String (fun txt -> print := txt),
197-
"Print either binary, ml, ast, sexp, comments or res. Default: res" );
197+
"Print either binary, ml, ast, sexp, comments, tokens or res. Default: \
198+
res" );
198199
( "-width",
199200
Arg.Int (fun w -> width := w),
200201
"Specify the line length for the printer (formatter)" );
@@ -239,11 +240,12 @@ module CliArgProcessor = struct
239240
| "ast" -> Res_ast_debugger.print_engine
240241
| "sexp" -> Res_ast_debugger.sexp_print_engine
241242
| "comments" -> Res_ast_debugger.comments_print_engine
243+
| "tokens" -> Res_token_debugger.token_print_engine
242244
| "res" -> Res_driver.print_engine
243245
| target ->
244246
print_endline
245-
("-print needs to be either binary, ml, ast, sexp, comments or res. \
246-
You provided " ^ target);
247+
("-print needs to be either binary, ml, ast, sexp, comments, tokens \
248+
or res. You provided " ^ target);
247249
exit 1
248250
in
249251

@@ -256,7 +258,11 @@ module CliArgProcessor = struct
256258
let (Parser backend) = parsing_engine in
257259
(* This is the whole purpose of the Color module above *)
258260
Color.setup None;
259-
if process_interface then
261+
262+
(* Special case for tokens - bypass parsing entirely *)
263+
if target = "tokens" then
264+
print_engine.print_implementation ~width ~filename ~comments:[] []
265+
else if process_interface then
260266
let parse_result = backend.parse_interface ~for_printer ~filename in
261267
if parse_result.invalid then (
262268
backend.string_of_diagnostics ~source:parse_result.source

0 commit comments

Comments
 (0)