Skip to content

Commit

Permalink
Reason V4 [Stacked Diff 1/n #2605] [Allow multiple versions of Reason]
Browse files Browse the repository at this point in the history
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:
  • Loading branch information
jordwalke authored and anmonteiro committed Apr 25, 2023
1 parent fc97ab2 commit 70705a3
Show file tree
Hide file tree
Showing 67 changed files with 1,052 additions and 680 deletions.
24 changes: 21 additions & 3 deletions docs/RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,32 @@ and `rtop.json` respectively in the repo root, you would run that script after
committing/bumping some versions:


**IMPORTANT: Update The Version Numbers In Packages:**
1. Make sure the version number in `esy.json` and `reason.json` is the new
version number for the release.
2. Make sure the file
[../../src/reason-version/reason_version.ml](../../src/reason-version/reason_version.ml)
also has that same version number that `refmt` has:

```sh
git checkout -b MYRELEASE origin/master
git rebase origin/master
vim -O esy.json reason.json
# Then edit the version number accordingly on BOTH files. With that same VERSION do:
version=3.5.0 make pre_release
vim -O esy.json reason.json src/reason-version/reason_version.ml

# Edit version field in jsons, and make sure reason_version has the new version
# let package_version = {
# major = 3;
# minor = 7;
# patch = 0;
# }

git commit -m "Bump version"
git push origin HEAD:PullRequestForVersion # Commit these version bumps

```

**Perform The Release:**
```sh
node ./scripts/esy-prepublish.js ./reason.json ./rtop.json

# Then publish. For example:
Expand Down
3 changes: 2 additions & 1 deletion src/reason-parser/dune
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@
reason.ocaml-migrate-parsetree
menhirLib
reason.easy_format
ppxlib))
ppxlib
reason.version))
17 changes: 11 additions & 6 deletions src/reason-parser/reason_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ type attributesPartition = {
uncurried : bool
}

let is_stylistic_attr = function
| { attr_name = {txt="reason.raw_literal"}; _}
(* Consider warnings to be "stylistic" attributes - attributes that do not
* affect printing *)
| { attr_name = {txt="ocaml.ppwarn"}; _}
| { attr_name = {txt="reason.preserve_braces"}; _} -> true
| _ -> false


(** Partition attributes into kinds *)
let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attributesPartition =
match attrs with
Expand All @@ -35,10 +44,7 @@ let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attrib
| ({ attr_name = {txt="ocaml.doc" | "ocaml.text"}; _} as doc)::atTl when partDoc = true ->
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
{partition with docAttrs=doc::partition.docAttrs}
| ({ attr_name = {txt="reason.raw_literal"}; _} as attr) :: atTl ->
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
| ({ attr_name = {txt="reason.preserve_braces"}; _} as attr) :: atTl ->
| attr :: atTl when is_stylistic_attr attr ->
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
| atHd :: atTl ->
Expand All @@ -62,8 +68,7 @@ let extract_raw_literal attrs =

let without_stylistic_attrs attrs =
let rec loop acc = function
| attr :: rest when (partitionAttributes [attr]).stylisticAttrs != [] ->
loop acc rest
| attr :: rest when is_stylistic_attr attr -> loop acc rest
| [] -> List.rev acc
| attr :: rest -> loop (attr :: acc) rest
in
Expand Down
15 changes: 15 additions & 0 deletions src/reason-parser/reason_declarative_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ rule token state = parse
{ SHARPEQUAL }
| "#" operator_chars+
{ SHARPOP (lexeme_operator lexbuf) }
(* File name / line number source mapping # n string\n *)
| "#" [' ' '\t']* (['0'-'9']+ as num) [' ' '\t']*
("\"" ([^ '\010' '\013' '"' ] * as name) "\"")?
[^ '\010' '\013'] * newline
Expand Down Expand Up @@ -552,24 +553,38 @@ rule token state = parse
}
| "[|<"
{ set_lexeme_length lexbuf 2;
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
LBRACKETBAR
}
(* allow parsing of <div /></Component> *)
| "/></" uppercase_or_lowercase+
{ (* allow parsing of <div asd=1></div> *)
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
set_lexeme_length lexbuf 2;
SLASHGREATER
}
| "></" uppercase_or_lowercase+
{ (* allow parsing of <div asd=1></div> *)
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
set_lexeme_length lexbuf 1;
GREATER
}
| "><" uppercase_or_lowercase+
{ (* allow parsing of <div><span> *)
(* TODO: See if decompose_token in Reason_single_parser.ml would work better for this *)
set_lexeme_length lexbuf 1;
GREATER
}
| "[@reason.version " (['0'-'9']+ as major) '.' (['0'-'9']+ as minor) (('.' ['0'-'9']+)? as _patch) ']' {
(* Special case parsing of attribute so that we can special case its
* parsing. Parses x.y.z even though it is not valid syntax otherwise -
* just gracefully remove the last number. The parser will ignore this
* attribute when parsed, and instead record its presence, and then inject
* the attribute into the footer of the file. Then the printer will ensure
* it is formatted at the top of the file, ideally after the first file
* floating doc comment. *)
VERSION_ATTRIBUTE (int_of_string major, int_of_string minor)
}
| "[@" { LBRACKETAT }
| "[%" { LBRACKETPERCENT }
| "[%%" { LBRACKETPERCENTPERCENT }
Expand Down
Loading

0 comments on commit 70705a3

Please sign in to comment.