Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement postfix parsing #953

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions formatTest/typeCheckedTests/expected_output/basics.re
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ let expectedPrecendence =

let expectedPrecendence =
1 \+ 1 \=== 1 \+ 1 && 1 \+ 1 !== 1 \+ 1;

type postfixType =
| Px int;

let postfix = 100Px;

type postfixRecord = {
height: postfixType,
width: postfixType
};

let postfix2 = {height: 50Px, width: 30Px};
14 changes: 14 additions & 0 deletions formatTest/typeCheckedTests/input/basics.re
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ let (\===) = (===);
let expectedPrecendence = 1 + 1 \=== 1 + 1 && 1 + 1 \!== 1 + 1;

let expectedPrecendence = 1 \+ 1 \=== 1 \+ 1 && 1 \+ 1 \!== 1 \+ 1;

type postfixType = Px int;

let postfix = 100Px;

type postfixRecord = {
height: postfixType,
width: postfixType
};

let postfix2 = {
height: 50Px,
width: 30Px
};
22 changes: 22 additions & 0 deletions src/reason_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ let remove_underscores s =
| c -> Bytes.set b dst c; remove (src + 1) (dst + 1)
in remove 0 0

let splitCharsNumbers s =
let l = String.length s in
let chars = Bytes.create l in
let numbers = Bytes.create l in
let rec split src nmb_dst char_dst =
if src >= l then (Bytes.sub_string numbers 0 nmb_dst, Bytes.sub_string chars 0 char_dst)
else
match s.[src] with
| '0' .. '9' -> Bytes.set numbers nmb_dst s.[src]; split (src + 1) (nmb_dst + 1) char_dst
| 'A' .. 'z' -> Bytes.set chars char_dst s.[src]; split (src + 1) nmb_dst (char_dst + 1)
| _ -> assert false
in
split 0 0 0

(* Update the current location with file name and line number. *)

let update_loc lexbuf file line absolute chars =
Expand Down Expand Up @@ -321,6 +335,8 @@ let float_literal =
('.' ['0'-'9' '_']* )?
(['e' 'E'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']*)?

let postfix_chars = decimal_literal uppercase identchar*

rule token = parse
| "\\" newline {
match !preprocessor with
Expand Down Expand Up @@ -359,6 +375,12 @@ rule token = parse
| "=?"
(* Need special label extractor? *)
{ OPTIONAL_NO_DEFAULT }
| postfix_chars
{
let l = Lexing.lexeme lexbuf in
let (n, c) = splitCharsNumbers l in
POSTFIX (c, int_of_string n)
}
| lowercase identchar *
{ let s = Lexing.lexeme lexbuf in
try Hashtbl.find keyword_table s
Expand Down
Loading