Skip to content

Commit ff9bfa1

Browse files
gwansikkcometkim
andcommitted
feat: update
Co-authored-by: Hyeseong Kim <hey@hyeseong.kim>
1 parent 043b357 commit ff9bfa1

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed

compiler/syntax/src/res_core.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,27 @@ and parse_operand_expr ~context p =
21382138
(* pexp_loc = mkLoc startPos endPos *)
21392139
}
21402140

2141+
and parse_shift_operator (p : Parser.t) =
2142+
let start_pos = p.start_pos in
2143+
let end_pos = p.end_pos in
2144+
if p.token = LessThan && Scanner.is_left_shift p.scanner start_pos.pos_cnum
2145+
then (
2146+
Parser.next p;
2147+
{p with token = LeftShift; start_pos; prev_end_pos = end_pos})
2148+
else if
2149+
p.token = GreaterThan
2150+
&& Scanner.is_right_shift_unsigned p.scanner start_pos.pos_cnum
2151+
then (
2152+
Parser.next p;
2153+
Parser.next p;
2154+
{p with token = RightShiftUnsigned; start_pos; prev_end_pos = end_pos})
2155+
else if
2156+
p.token = GreaterThan && Scanner.is_right_shift p.scanner start_pos.pos_cnum
2157+
then (
2158+
Parser.next p;
2159+
{p with token = RightShift; start_pos; prev_end_pos = end_pos})
2160+
else p
2161+
21412162
(* a binary expression is an expression that combines two expressions with an
21422163
* operator. Examples:
21432164
* a + b
@@ -2150,6 +2171,11 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
21502171
| None -> parse_operand_expr ~context p
21512172
in
21522173
let rec loop a =
2174+
let p =
2175+
match p.token with
2176+
| LessThan | GreaterThan -> parse_shift_operator p
2177+
| _ -> p
2178+
in
21532179
let token = p.Parser.token in
21542180
let token_prec =
21552181
match token with
@@ -2179,6 +2205,11 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
21792205
Parser.leave_breadcrumb p (Grammar.ExprBinaryAfterOp token);
21802206
let start_pos = p.start_pos in
21812207
Parser.next p;
2208+
(* let p =
2209+
match p.token with
2210+
| LessThan | GreaterThan -> parse_shift_operator p
2211+
| _ -> p
2212+
in *)
21822213
let end_pos = p.prev_end_pos in
21832214
let token_prec =
21842215
(* exponentiation operator is right-associative *)

compiler/syntax/src/res_scanner.ml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -887,24 +887,23 @@ let rec scan scanner =
887887
next scanner;
888888
Token.Plus)
889889
| '>' -> (
890-
match (peek scanner, peek2 scanner) with
891-
| '>', '>' when not (in_diamond_mode scanner) ->
892-
next3 scanner;
893-
Token.GreaterThanGreaterThanGreaterThan
894-
| '>', _ when not (in_diamond_mode scanner) ->
895-
next2 scanner;
896-
Token.GreaterThanGreaterThan
897-
| '=', _ when not (in_diamond_mode scanner) ->
890+
match peek scanner with
891+
| '=' when not (in_diamond_mode scanner) ->
898892
next2 scanner;
899893
Token.GreaterEqual
900894
| _ ->
901895
next scanner;
902896
Token.GreaterThan)
903-
| '<' when not (in_jsx_mode scanner) -> (
897+
| '<' when not (in_diamond_mode scanner) -> (
904898
match peek scanner with
905-
| '<' ->
899+
| '=' ->
906900
next2 scanner;
907-
Token.LessThanLessThan
901+
Token.LessEqual
902+
| _ ->
903+
next scanner;
904+
Token.LessThan)
905+
| '<' when not (in_jsx_mode scanner) -> (
906+
match peek scanner with
908907
| '=' ->
909908
next2 scanner;
910909
Token.LessEqual
@@ -1037,9 +1036,6 @@ let reconsider_less_than scanner =
10371036
if scanner.ch == '/' then
10381037
let () = next scanner in
10391038
Token.LessThanSlash
1040-
else if scanner.ch == '<' then (
1041-
next scanner;
1042-
Token.LessThanLessThan)
10431039
else Token.LessThan
10441040

10451041
(* If an operator has whitespace around both sides, it's a binary operator *)
@@ -1058,3 +1054,26 @@ let is_binary_op src start_cnum end_cnum =
10581054
|| is_whitespace (String.unsafe_get src end_cnum)
10591055
in
10601056
left_ok && right_ok)
1057+
1058+
let is_left_shift scanner _start_cnum =
1059+
(* print_endline ("@@@@@ is_left_shift ch: " ^ (Char.escaped scanner.ch)); *)
1060+
(* print_endline ("@@@@@ is_left_shift cnum: " ^ (string_of_int _start_cnum)); *)
1061+
(* print_endline ("@@@@@ is_left_shift peek: " ^ (Char.escaped (peek scanner))); *)
1062+
match scanner.ch with
1063+
| '<' -> true
1064+
| _ -> false
1065+
1066+
let is_right_shift scanner _start_cnum =
1067+
(* print_endline ("@@@@@ is_right_shift ch: " ^ (Char.escaped scanner.ch)); *)
1068+
(* print_endline ("@@@@@ is_right_shift cnum: " ^ (string_of_int _start_cnum)); *)
1069+
(* print_endline ("@@@@@ is_right_shift peek: " ^ (Char.escaped (peek scanner))); *)
1070+
match scanner.ch with
1071+
| '>' -> true
1072+
| _ -> false
1073+
1074+
let is_right_shift_unsigned scanner _start_cnum =
1075+
(* print_endline ("@@@@@ is_right_shift_unsigned ch: " ^ (Char.escaped scanner.ch)); *)
1076+
(* print_endline ("@@@@@ is_right_shift_unsigned peek: " ^ (Char.escaped (peek scanner))); *)
1077+
match (scanner.ch, peek scanner) with
1078+
| '>', '>' -> true
1079+
| _ -> false

compiler/syntax/src/res_scanner.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ val scan : t -> Lexing.position * Lexing.position * Res_token.t
2626

2727
val is_binary_op : string -> int -> int -> bool
2828

29+
val is_left_shift : t -> int -> bool
30+
val is_right_shift : t -> int -> bool
31+
val is_right_shift_unsigned : t -> int -> bool
32+
2933
val set_jsx_mode : t -> unit
3034
val set_diamond_mode : t -> unit
3135
val pop_mode : t -> mode -> unit

compiler/syntax/src/res_token.ml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ type t =
5252
| ColonGreaterThan
5353
| GreaterThan
5454
| LessThan
55-
| LessThanLessThan
5655
| LessThanSlash
57-
| GreaterThanGreaterThan
58-
| GreaterThanGreaterThanGreaterThan
5956
| Hash
6057
| HashEqual
6158
| Assert
@@ -101,6 +98,9 @@ type t =
10198
| Try
10299
| DocComment of Location.t * string
103100
| ModuleComment of Location.t * string
101+
| LeftShift
102+
| RightShift
103+
| RightShiftUnsigned
104104

105105
let precedence = function
106106
| HashEqual | ColonEqual -> 1
@@ -110,13 +110,12 @@ let precedence = function
110110
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
111111
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
112112
5
113-
| Plus | PlusDot | Minus | MinusDot | PlusPlus | LessThanLessThan
114-
| GreaterThanGreaterThan | GreaterThanGreaterThanGreaterThan ->
115-
6
116-
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 7
117-
| Exponentiation -> 8
118-
| MinusGreater -> 9
119-
| Dot -> 10
113+
| LeftShift | RightShift | RightShiftUnsigned -> 6
114+
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 7
115+
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 8
116+
| Exponentiation -> 9
117+
| MinusGreater -> 10
118+
| Dot -> 11
120119
| _ -> 0
121120

122121
let to_string = function
@@ -170,10 +169,7 @@ let to_string = function
170169
| HashEqual -> "#="
171170
| GreaterThan -> ">"
172171
| LessThan -> "<"
173-
| LessThanLessThan -> "<<"
174172
| LessThanSlash -> "</"
175-
| GreaterThanGreaterThan -> ">>"
176-
| GreaterThanGreaterThanGreaterThan -> ">>>"
177173
| Asterisk -> "*"
178174
| AsteriskDot -> "*."
179175
| Exponentiation -> "**"
@@ -220,6 +216,9 @@ let to_string = function
220216
| Try -> "try"
221217
| DocComment (_loc, s) -> "DocComment " ^ s
222218
| ModuleComment (_loc, s) -> "ModuleComment " ^ s
219+
| LeftShift -> "<<"
220+
| RightShift -> ">>"
221+
| RightShiftUnsigned -> ">>>"
223222

224223
let keyword_table = function
225224
| "and" -> And

0 commit comments

Comments
 (0)