Skip to content

Commit 70705a3

Browse files
jordwalkeanmonteiro
authored andcommitted
Reason V4 [Stacked Diff 1/n #2605] [Allow multiple versions of Reason]
Summary:This allows multiple versions of Reason in a single project by inferring and recording the version of syntax used into the file in an attribute. The attribute allows us to switch the parser and lexer on the fly. This attribute is not the only way we can infer the version, and we can allow project level configuration, but this is the approach that is guaranteed to work with any build system or tooling. Test Plan: Reviewers: CC:
1 parent fc97ab2 commit 70705a3

File tree

67 files changed

+1052
-680
lines changed

Some content is hidden

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

67 files changed

+1052
-680
lines changed

docs/RELEASING.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,32 @@ and `rtop.json` respectively in the repo root, you would run that script after
1616
committing/bumping some versions:
1717

1818

19+
**IMPORTANT: Update The Version Numbers In Packages:**
20+
1. Make sure the version number in `esy.json` and `reason.json` is the new
21+
version number for the release.
22+
2. Make sure the file
23+
[../../src/reason-version/reason_version.ml](../../src/reason-version/reason_version.ml)
24+
also has that same version number that `refmt` has:
25+
1926
```sh
2027
git checkout -b MYRELEASE origin/master
2128
git rebase origin/master
22-
vim -O esy.json reason.json
23-
# Then edit the version number accordingly on BOTH files. With that same VERSION do:
24-
version=3.5.0 make pre_release
29+
vim -O esy.json reason.json src/reason-version/reason_version.ml
30+
31+
# Edit version field in jsons, and make sure reason_version has the new version
32+
# let package_version = {
33+
# major = 3;
34+
# minor = 7;
35+
# patch = 0;
36+
# }
37+
2538
git commit -m "Bump version"
2639
git push origin HEAD:PullRequestForVersion # Commit these version bumps
40+
41+
```
42+
43+
**Perform The Release:**
44+
```sh
2745
node ./scripts/esy-prepublish.js ./reason.json ./rtop.json
2846

2947
# Then publish. For example:

src/reason-parser/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,5 @@
125125
reason.ocaml-migrate-parsetree
126126
menhirLib
127127
reason.easy_format
128-
ppxlib))
128+
ppxlib
129+
reason.version))

src/reason-parser/reason_attributes.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ type attributesPartition = {
1212
uncurried : bool
1313
}
1414

15+
let is_stylistic_attr = function
16+
| { attr_name = {txt="reason.raw_literal"}; _}
17+
(* Consider warnings to be "stylistic" attributes - attributes that do not
18+
* affect printing *)
19+
| { attr_name = {txt="ocaml.ppwarn"}; _}
20+
| { attr_name = {txt="reason.preserve_braces"}; _} -> true
21+
| _ -> false
22+
23+
1524
(** Partition attributes into kinds *)
1625
let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attributesPartition =
1726
match attrs with
@@ -35,10 +44,7 @@ let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attrib
3544
| ({ attr_name = {txt="ocaml.doc" | "ocaml.text"}; _} as doc)::atTl when partDoc = true ->
3645
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
3746
{partition with docAttrs=doc::partition.docAttrs}
38-
| ({ attr_name = {txt="reason.raw_literal"}; _} as attr) :: atTl ->
39-
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
40-
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
41-
| ({ attr_name = {txt="reason.preserve_braces"}; _} as attr) :: atTl ->
47+
| attr :: atTl when is_stylistic_attr attr ->
4248
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
4349
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
4450
| atHd :: atTl ->
@@ -62,8 +68,7 @@ let extract_raw_literal attrs =
6268

6369
let without_stylistic_attrs attrs =
6470
let rec loop acc = function
65-
| attr :: rest when (partitionAttributes [attr]).stylisticAttrs != [] ->
66-
loop acc rest
71+
| attr :: rest when is_stylistic_attr attr -> loop acc rest
6772
| [] -> List.rev acc
6873
| attr :: rest -> loop (attr :: acc) rest
6974
in

src/reason-parser/reason_declarative_lexer.mll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ rule token state = parse
451451
{ SHARPEQUAL }
452452
| "#" operator_chars+
453453
{ SHARPOP (lexeme_operator lexbuf) }
454+
(* File name / line number source mapping # n string\n *)
454455
| "#" [' ' '\t']* (['0'-'9']+ as num) [' ' '\t']*
455456
("\"" ([^ '\010' '\013' '"' ] * as name) "\"")?
456457
[^ '\010' '\013'] * newline
@@ -552,24 +553,38 @@ rule token state = parse
552553
}
553554
| "[|<"
554555
{ set_lexeme_length lexbuf 2;
556+
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
555557
LBRACKETBAR
556558
}
557559
(* allow parsing of <div /></Component> *)
558560
| "/></" uppercase_or_lowercase+
559561
{ (* allow parsing of <div asd=1></div> *)
562+
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
560563
set_lexeme_length lexbuf 2;
561564
SLASHGREATER
562565
}
563566
| "></" uppercase_or_lowercase+
564567
{ (* allow parsing of <div asd=1></div> *)
568+
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
565569
set_lexeme_length lexbuf 1;
566570
GREATER
567571
}
568572
| "><" uppercase_or_lowercase+
569573
{ (* allow parsing of <div><span> *)
574+
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
570575
set_lexeme_length lexbuf 1;
571576
GREATER
572577
}
578+
| "[@reason.version " (['0'-'9']+ as major) '.' (['0'-'9']+ as minor) (('.' ['0'-'9']+)? as _patch) ']' {
579+
(* Special case parsing of attribute so that we can special case its
580+
* parsing. Parses x.y.z even though it is not valid syntax otherwise -
581+
* just gracefully remove the last number. The parser will ignore this
582+
* attribute when parsed, and instead record its presence, and then inject
583+
* the attribute into the footer of the file. Then the printer will ensure
584+
* it is formatted at the top of the file, ideally after the first file
585+
* floating doc comment. *)
586+
VERSION_ATTRIBUTE (int_of_string major, int_of_string minor)
587+
}
573588
| "[@" { LBRACKETAT }
574589
| "[%" { LBRACKETPERCENT }
575590
| "[%%" { LBRACKETPERCENTPERCENT }

0 commit comments

Comments
 (0)