Skip to content

Commit d6c4a83

Browse files
committed
Add syntax sources directly to compiler repo
1 parent c1c3126 commit d6c4a83

File tree

2,001 files changed

+337933
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,001 files changed

+337933
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ coverage
5555
ppx_tools/
5656
vendor/ocaml/stdlib/target_camlheader
5757
vendor/ocaml/stdlib/target_camlheaderd
58-
reason/
5958
man/
6059
lib/ocaml
6160
*.tgz

res_syntax/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.DS_STORE
2+
*.cm*
3+
*.o
4+
*.s
5+
lib/*
6+
!lib/README.md
7+
.vscode/settings.json
8+
9+
# Dune
10+
_build
11+
12+
# Merlin
13+
.merlin
14+
15+
# Local opam switch
16+
_opam/
17+
18+
# Output dir for bootstrap script
19+
bootstrap/

res_syntax/.ocamlformat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
profile = default
2+
version = 0.22.4
3+
4+
field-space = tight-decl
5+
break-cases = toplevel
6+
module-item-spacing = preserve
7+
cases-exp-indent = 2
8+
space-around-arrays = false
9+
space-around-lists = false
10+
space-around-records = false
11+
space-around-variants = false

res_syntax/.ocamlformat-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
compiler-libs-406/*

res_syntax/CHANGELOG.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Master
2+
3+
> **Tags:**
4+
>
5+
> - :boom: [Breaking Change]
6+
> - :eyeglasses: [Spec Compliance]
7+
> - :rocket: [New Feature]
8+
> - :bug: [Bug Fix]
9+
> - :memo: [Documentation]
10+
> - :house: [Internal]
11+
> - :nail_care: [Polish]
12+
13+
#### :boom: Breaking Change
14+
15+
- Emit an error when a `@string` or `@int` attribute is used in a V4 component https://github.com/rescript-lang/rescript-compiler/issues/5724
16+
17+
#### :rocket: New Feature
18+
19+
- Add surface syntax for `async`/`await` https://github.com/rescript-lang/syntax/pull/600
20+
21+
- Initial support for JSX V4, still work in progress.
22+
23+
- :boom: when V4 is activated, at most one component is allowed for each module.
24+
25+
- Add support for empty record literal `{}` for records with only optional fields, and type definition of empty record (e.g. `type empty = {}`) https://github.com/rescript-lang/syntax/pull/632
26+
27+
- Support the use of spread anywhere in list creation (e.g. `list{...x, 1, ...y, ...z}). https://github.com/rescript-lang/syntax/pull/692
28+
29+
- Add support for the argument of `@react.component` to set a props type from the outside. https://github.com/rescript-lang/syntax/pull/699
30+
31+
#### :bug: Bug Fix
32+
33+
- Fix issue in formatting JSX spread props https://github.com/rescript-lang/syntax/pull/644
34+
- Fix pretty printer where it would print doc comments on the same line as other attributes https://github.com/rescript-lang/syntax/pull/642
35+
- Fix location issue in error messages with JSX V4 where the body of the component is an application https://github.com/rescript-lang/syntax/pull/633
36+
- Fix issue where the printer would omit attributes for `->` and `|>` https://github.com/rescript-lang/syntax/pull/629
37+
- Fix printing of optional fields in records https://github.com/rescript-lang/rescript-compiler/issues/5654
38+
- Fix printing of comments inside empty blocks https://github.com/rescript-lang/syntax/pull/647
39+
- Fix location issue in error messages with JSX V4 where the multiple props types are defined https://github.com/rescript-lang/syntax/pull/655
40+
- Fix location issue in make function in JSX V4 that breaks dead code elimination https://github.com/rescript-lang/syntax/pull/660
41+
- Fix parsing (hence pretty printing) of expressions with underscore `_` and comments.
42+
- Fix printing of comments inside JSX tag https://github.com/rescript-lang/syntax/pull/664
43+
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
44+
- Fix issue where the JSX prop has type annotation of the first class module https://github.com/rescript-lang/syntax/pull/666
45+
- Fix issue where a spread `...x` in non-last position would not be reported as syntax error https://github.com/rescript-lang/syntax/pull/673/
46+
- Fix issue where the formatter would delete `async` in a function with labelled arguments.
47+
- Fix several printing issues with `async` including an infinite loop https://github.com/rescript-lang/syntax/pull/680
48+
- Fix issue where certain JSX expressions would be formatted differenctly in compiler 10.1.0-rc.1 https://github.com/rescript-lang/syntax/issues/675
49+
- Fix issue where printing nested pipe discards await https://github.com/rescript-lang/syntax/issues/687
50+
- Fix issue where the JSX key type is not an optional string https://github.com/rescript-lang/syntax/pull/693
51+
- Fix issue where the JSX fragment without children build error https://github.com/rescript-lang/syntax/pull/704
52+
- Fix issue where async as an id cannot be used with application and labelled arguments https://github.com/rescript-lang/syntax/issues/707
53+
- Treat await as almost-unary operator weaker than pipe so `await foo->bar` means `await (foo->bar)` https://github.com/rescript-lang/syntax/pull/711
54+
55+
#### :eyeglasses: Spec Compliance
56+
57+
- Functions with consecutive dots now print as multiple arrow functions like in JavaScript.
58+
59+
#### :nail_care Polish
60+
61+
- Change the internal representation of props for the lowercase components to record. https://github.com/rescript-lang/syntax/pull/665
62+
- Change the payload of Pconst_char for type safety. https://github.com/rescript-lang/rescript-compiler/pull/5759
63+
- Specialize the printing of the rhs of a record field assignment for optional values `{x: ? e}` https://github.com/rescript-lang/syntax/issues/714
64+
65+
## ReScript 10.0
66+
67+
- Fix printing for inline nullary functor types [#477](https://github.com/rescript-lang/syntax/pull/477)
68+
- Fix stripping of quotes for empty poly variants [#474](https://github.com/rescript-lang/syntax/pull/474)
69+
- Implement syntax for arity zero vs arity one in uncurried application in [#139](https://github.com/rescript-lang/syntax/pull/139)
70+
- Fix parsing of first class module exprs as part of binary/ternary expr in [#256](https://github.com/rescript-lang/syntax/pull/256)
71+
- Fix formatter hanging on deeply nested function calls [#261](https://github.com/rescript-lang/syntax/issues/261)
72+
- Remove parsing of "import" and "export" which was never officially supported.
73+
74+
## ReScript 9.0.0
75+
76+
- Fix parsing of poly-var typexpr consisting of one tag-spec-first in [#254](https://github.com/rescript-lang/syntax/pull/254)
77+
- Implement new syntax for guards on pattern match cases in [#248](https://github.com/rescript-lang/syntax/pull/248)
78+
- Implement intelligent breaking for poly-var type expressions in [#246](https://github.com/rescript-lang/syntax/pull/246)
79+
- Improve indentation of fast pipe chain in let binding in [#244](https://github.com/rescript-lang/syntax/pull/244)
80+
- Improve printing of non-callback arguments in call expressions with callback in [#241](https://github.com/rescript-lang/syntax/pull/241/files)
81+
- Fix printing of constrained expressions in rhs of js objects [#240](https://github.com/rescript-lang/syntax/pull/240)
82+
- Improve printing of trailing comments under lhs of "pipe" expression in [#329](https://github.com/rescript-lang/syntax/pull/239/files)
83+
- Improve printing of jsx children and props with leading line comment in [#236](https://github.com/rescript-lang/syntax/pull/236)
84+
- Improve conversion of quoted strings from Reason in [#238](https://github.com/rescript-lang/syntax/pull/238)
85+
- Print attributes/extension without bs prefix where possible in [#230](https://github.com/rescript-lang/syntax/pull/230)
86+
- Cleanup gentype attribute printing [fe05e1051aa94b16f6993ddc5ba9651f89e86907](https://github.com/rescript-lang/syntax/commit/fe05e1051aa94b16f6993ddc5ba9651f89e86907)
87+
- Implement light weight syntax for poly-variants [f84c5760b3f743f65e934195c87fc06bf88bff75](https://github.com/rescript-lang/syntax/commit/f84c5760b3f743f65e934195c87fc06bf88bff75)
88+
- Fix bug in fast pipe conversion from Reason. [3d5f2daba5418b821c577ba03e2de1afb0dd66de](https://github.com/rescript-lang/syntax/commit/3d5f2daba5418b821c577ba03e2de1afb0dd66de)
89+
- Improve parsed AST when tilde is missing in arrow expr parameters. [e52a0c89ac39b578a2062ef15fae2be625962e1f](https://github.com/rescript-lang/syntax/commit/e52a0c89ac39b578a2062ef15fae2be625962e1f)
90+
- Improve parser diagnostics for missing tilde in labeled parameters. [a0d7689d5d2bfc31dc251e966ac33a3001200171](https://github.com/rescript-lang/syntax/commit/a0d7689d5d2bfc31dc251e966ac33a3001200171)
91+
- Improve printing of uncurried application with a huggable expression in [c8767215186982e171fe9f9101d518150a65f0d7](https://github.com/rescript-lang/syntax/commit/c8767215186982e171fe9f9101d518150a65f0d7)
92+
- Improve printing of uncurried arrow typexpr outcome printer in [4d953b668cf47358deccb8b730566f24de25b9ee](https://github.com/rescript-lang/syntax/commit/4d953b668cf47358deccb8b730566f24de25b9ee)
93+
- Remove support for nativeint syntax in [72d9b7034fc28f317672c94994b322bee520acca](https://github.com/rescript-lang/syntax/commit/72d9b7034fc28f317672c94994b322bee520acca)
94+
- Improve printing of poly variant typexprs with attributes in [bf6561b](https://github.com/rescript-lang/syntax/commit/bf6561bb5d84557b8b6cbbcd40078c39526af4af)
95+
96+
## ReScript 8.4.2 (December 11, 2020)
97+
98+
Released in https://github.com/rescript-lang/syntax/releases/tag/v8.4.2 as part of https://github.com/rescript-lang/rescript-compiler/releases/tag/8.4.2

res_syntax/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 ReScript
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

res_syntax/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
SHELL = /bin/bash
2+
3+
build:
4+
dune build
5+
6+
bootstrap: build
7+
dune exec -- bash ./scripts/bootstrap.sh
8+
9+
bench: build
10+
dune exec -- bench
11+
12+
test: build
13+
dune exec -- testrunner
14+
dune exec -- bash ./scripts/test.sh
15+
make reanalyze
16+
bash ./scripts/testok.sh
17+
18+
roundtrip-test: build
19+
dune exec -- testrunner
20+
ROUNDTRIP_TEST=1 dune exec -- bash ./scripts/test.sh
21+
make reanalyze
22+
bash ./scripts/testok.sh
23+
24+
reanalyze: build
25+
reanalyze.exe -set-exit-code -all-cmt _build/default -suppress testrunner,compiler-libs-406 -exclude-paths compiler-libs-406
26+
27+
format:
28+
dune build @fmt --auto-promote
29+
30+
checkformat:
31+
dune build @fmt
32+
33+
clean:
34+
dune clean
35+
36+
.PHONY: bench clean test roundtrip-test reanalyze bootstrap build-native

res_syntax/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# ReScript Syntax ![Tests](https://github.com/rescript-lang/syntax/workflows/CI/badge.svg)
2+
3+
Documentation: https://rescript-lang.org/docs/manual/latest/overview
4+
5+
This repo is the source of truth for the ReScript parser & printer. Issues go here.
6+
7+
**You don't need this repo to use the ReScript syntax**. This comes with ReScript >=8.1. This repo is for syntax developers.
8+
9+
## Contribute
10+
11+
### Why
12+
13+
A detailed discussion by Jonathan Blow and Casey Muratori on why you would hand-roll a parser for a production quality programming language
14+
[Discussion: Making Programming Language Parsers, etc](https://youtu.be/MnctEW1oL-E)
15+
16+
"One reason why I switched off these parser tools is that the promises didn't really materialize.
17+
The amount of work that I had to do change a yacc script from one language to a variant of that language
18+
was more than if I hand wrote the code myself.
19+
"
20+
J. Blow.
21+
22+
### Setup & Usage (For Repo Devs Only)
23+
24+
Required:
25+
26+
- OCaml 4.10 or later
27+
- Dune
28+
- Reanalyze
29+
- OS: macOS, Linux or Windows
30+
31+
```sh
32+
git clone https://github.com/rescript-lang/syntax.git
33+
cd syntax
34+
opam install . --deps-only --with-test
35+
make # or "dune build"
36+
```
37+
38+
This will produce the three binaries `rescript`, `tests` and `bench` (with `.exe` extension on Windows).
39+
40+
We only build production binaries, even in dev mode. No need for a separate dev binary when the build is fast enough. Plus, this encourages proper benchmarking of the (production) binary each diff.
41+
42+
After you make a change:
43+
44+
```sh
45+
make
46+
```
47+
48+
Run the core tests:
49+
50+
```sh
51+
make test
52+
```
53+
54+
Run the extended tests (not fully working on Windows yet):
55+
56+
```sh
57+
make roundtrip-test
58+
```
59+
60+
Those will tell you whether you've got a test output difference. If it's intentional, check them in.
61+
62+
Debug a file:
63+
64+
```sh
65+
# write code in test.res
66+
dune exec -- rescript test.res # test printer
67+
dune exec -- rescript -print ast test.res # print ast
68+
dune exec -- rescript -print comments test.res # print comment table
69+
dune exec -- rescript -print ml test.res # show ocaml code
70+
dune exec -- rescript -print res -width 80 test.res # test printer and change default print width
71+
```
72+
73+
Benchmark:
74+
75+
```sh
76+
make bench
77+
```
78+
79+
Enable stack trace:
80+
81+
```sh
82+
# Before you run the binary
83+
export OCAMLRUNPARAM="b"
84+
```
85+
86+
This is likely a known knowledge: add the above line into your shell rc file so that every shell startup you have OCaml stack trace enabled.
87+
88+
### Development Docs
89+
90+
#### Folder Structure
91+
92+
- `src` contains all the parser/printer source code. Don't change folder structure without notice; The [rescript-compiler](https://github.com/rescript-lang/rescript-compiler) repo uses this repo as a submodule and assumes `src`.
93+
- `benchmarks`, `cli` and `tests` contain the source code for the executables used for testing/benchmarking. These are not used by the [rescript-compiler](https://github.com/rescript-lang/rescript-compiler) repo.
94+
95+
#### Error Reporting Logic
96+
97+
Right now, ReScript's compiler's error reporting mechanism, for architectural reasons, is independent from this syntax repo's error reporting mechanism. However, we do want a unified look when they report the errors in the terminal. This is currently achieved by (carefully...) duplicating the error report logic from the compiler repo to here (or vice-versa; either way, just keep them in sync). The files to sync are the compiler repo's [super_location.ml](https://github.com/rescript-lang/rescript-compiler/blob/fcb21790dfb0592f609818df7790192061360631/jscomp/super_errors/super_location.ml) and [super_code_frame.ml](https://github.com/rescript-lang/rescript-compiler/blob/fcb21790dfb0592f609818df7790192061360631/jscomp/super_errors/super_code_frame.ml), into this repo's [res_diagnostics_printing_utils.ml](https://github.com/rescript-lang/syntax/blob/ec5cefb23b659b0a7be170ae0ad26f3fe8a05456/src/res_diagnostics_printing_utils.ml). A few notes:
98+
99+
- Some lines are lightly changed to fit this repo's needs; they're documented in the latter file.
100+
- Please keep these files lightweight and as dependency-less as possible, for easier syncing.
101+
- The syntax logic currently doesn't have warnings, only errors, and potentially more than one.
102+
- In the future, ideally, the error reporting logic would also be unified with GenType and Reanalyze's. It'd be painful to copy paste around all this reporting logic.
103+
- The errors are reported by the parser [here](https://github.com/rescript-lang/syntax/blob/ec5cefb23b659b0a7be170ae0ad26f3fe8a05456/src/res_diagnostics.ml#L146).
104+
- Our editor plugin parses the error report from the compiler and from the syntax [here](https://github.com/rescript-lang/rescript-vscode/blob/0dbf2eb9cdb0bd6d95be1aee88b73830feecb5cc/server/src/utils.ts#L129-L329).
105+
106+
### Example API usage
107+
108+
```ocaml
109+
let filename = "foo.res"
110+
let src = FS.readFile filename
111+
112+
let p =
113+
(* intended for ocaml compiler *)
114+
let mode = Res_parser.ParseForTypeChecker in
115+
(* if you want to target the printer use: let mode = Res_parser.Default in*)
116+
Res_parser.make ~mode src filename
117+
118+
let structure = Res_core.parseImplementation p
119+
let signature = Res_core.parseSpecification p
120+
121+
let () = match p.diagnostics with
122+
| [] -> () (* no problems *)
123+
| diagnostics -> (* parser contains problems *)
124+
Res_diagnostics.printReport diagnostics src
125+
```

0 commit comments

Comments
 (0)