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 1 commit
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
32 changes: 32 additions & 0 deletions src/reason_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,31 @@ let remove_underscores s =
| c -> Bytes.set b dst c; remove (src + 1) (dst + 1)
in remove 0 0

let remove_numbers s =
let l = String.length s in
let b = Bytes.create l in
let rec remove src dst =
if src >= l then
if dst >= l then s else Bytes.sub_string b 0 dst
else
match s.[src] with
| '0' .. '9' -> remove (src + 1) dst
| c -> Bytes.set b dst c; remove (src + 1) (dst + 1)
in remove 0 0

let remove_chars s =
let l = String.length s in
let b = Bytes.create l in
let rec remove src dst =
if src >= l then
if dst >= l then s else Bytes.sub_string b 0 dst
else
match s.[src] with
| 'A' .. 'z' -> remove (src + 1) dst
| c -> Bytes.set b dst c; remove (src + 1) (dst + 1)
in remove 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 +346,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 +386,11 @@ rule token = parse
| "=?"
(* Need special label extractor? *)
{ OPTIONAL_NO_DEFAULT }
| postfix_chars
{
let l = Lexing.lexeme lexbuf in
POSTFIX (remove_numbers l, int_of_string (remove_chars l))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: instead of using two functions, is it possible to just do one pass and return both number part and string part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

}
| lowercase identchar *
{ let s = Lexing.lexeme lexbuf in
try Hashtbl.find keyword_table s
Expand Down
Loading