Skip to content

Commit

Permalink
WIP: Backport 5.2 changes to parser-standard
Browse files Browse the repository at this point in the history
TODO: Compiler libs shims are needed in Load_path and Builtin_attributes.
  • Loading branch information
Julow committed Jan 18, 2024
1 parent fe9f1f9 commit 40ec2ac
Show file tree
Hide file tree
Showing 6 changed files with 537 additions and 165 deletions.
18 changes: 12 additions & 6 deletions vendor/parser-standard/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module Typ = struct
let poly ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_poly (a, b))
let package ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_package (a, b))
let extension ?loc ?attrs a = mk ?loc ?attrs (Ptyp_extension a)
let open_ ?loc ?attrs mod_ident t = mk ?loc ?attrs (Ptyp_open (mod_ident, t))

let force_poly t =
match t.ptyp_desc with
Expand Down Expand Up @@ -102,9 +103,9 @@ module Typ = struct
Ptyp_object (List.map loop_object_field lst, o)
| Ptyp_class (longident, lst) ->
Ptyp_class (longident, List.map loop lst)
| Ptyp_alias(core_type, string) ->
check_variable var_names t.ptyp_loc string;
Ptyp_alias(loop core_type, string)
| Ptyp_alias(core_type, alias) ->
check_variable var_names alias.loc alias.txt;
Ptyp_alias(loop core_type, alias)
| Ptyp_variant(row_field_list, flag, lbl_lst_option) ->
Ptyp_variant(List.map loop_row_field row_field_list,
flag, lbl_lst_option)
Expand All @@ -114,6 +115,8 @@ module Typ = struct
Ptyp_poly(string_lst, loop core_type)
| Ptyp_package(longident,lst) ->
Ptyp_package(longident,List.map (fun (n,typ) -> (n,loop typ) ) lst)
| Ptyp_open (mod_ident, core_type) ->
Ptyp_open (mod_ident, loop core_type)
| Ptyp_extension (s, arg) ->
Ptyp_extension (s, arg)
in
Expand Down Expand Up @@ -178,8 +181,7 @@ module Exp = struct
let ident ?loc ?attrs a = mk ?loc ?attrs (Pexp_ident a)
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
let fun_ ?loc ?attrs a b c d = mk ?loc ?attrs (Pexp_fun (a, b, c, d))
let function_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_function a)
let function_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_function (a, b, c))
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))
Expand Down Expand Up @@ -213,7 +215,9 @@ module Exp = struct
mk ?loc ?attrs (Pexp_letop {let_; ands; body})
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
let unreachable ?loc ?attrs () = mk ?loc ?attrs Pexp_unreachable
let hole ?loc ?attrs () = mk ?loc ?attrs Pexp_hole
(* Added *)
let hole ?loc ?attrs () = mk ?loc ?attrs Pexp_hole
(* *)

let case lhs ?guard rhs =
{
Expand Down Expand Up @@ -259,7 +263,9 @@ module Mod = struct
let constraint_ ?loc ?attrs m mty = mk ?loc ?attrs (Pmod_constraint (m, mty))
let unpack ?loc ?attrs e = mk ?loc ?attrs (Pmod_unpack e)
let extension ?loc ?attrs a = mk ?loc ?attrs (Pmod_extension a)
(* Added *)
let hole ?loc ?attrs () = mk ?loc ?attrs Pmod_hole
(* *)
end

module Sig = struct
Expand Down
73 changes: 62 additions & 11 deletions vendor/parser-standard/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
(* Ensure that record patterns don't miss any field. *)
*)

[@@@ocaml.warning "-60"] module Str = Ast_helper.Str (* For ocamldep *)
[@@@ocaml.warning "+60"]

open Parsetree
open Ast_helper
open Location
Expand All @@ -45,6 +48,7 @@ type mapper = {
constant: mapper -> constant -> constant;
constructor_declaration: mapper -> constructor_declaration
-> constructor_declaration;
directive_argument: mapper -> directive_argument -> directive_argument;
expr: mapper -> expression -> expression;
extension: mapper -> extension -> extension;
extension_constructor: mapper -> extension_constructor
Expand All @@ -68,6 +72,8 @@ type mapper = {
signature_item: mapper -> signature_item -> signature_item;
structure: mapper -> structure -> structure;
structure_item: mapper -> structure_item -> structure_item;
toplevel_directive: mapper -> toplevel_directive -> toplevel_directive;
toplevel_phrase: mapper -> toplevel_phrase -> toplevel_phrase;
typ: mapper -> core_type -> core_type;
type_declaration: mapper -> type_declaration -> type_declaration;
type_extension: mapper -> type_extension -> type_extension;
Expand All @@ -76,9 +82,6 @@ type mapper = {
value_binding: mapper -> value_binding -> value_binding;
value_description: mapper -> value_description -> value_description;
with_constraint: mapper -> with_constraint -> with_constraint;
directive_argument: mapper -> directive_argument -> directive_argument;
toplevel_directive: mapper -> toplevel_directive -> toplevel_directive;
toplevel_phrase: mapper -> toplevel_phrase -> toplevel_phrase;
}

let map_fst f (x, y) = (f x, y)
Expand Down Expand Up @@ -147,14 +150,18 @@ module T = struct
object_ ~loc ~attrs (List.map (object_field sub) l) o
| Ptyp_class (lid, tl) ->
class_ ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
| Ptyp_alias (t, s) -> alias ~loc ~attrs (sub.typ sub t) s
| Ptyp_alias (t, s) ->
let s = map_loc sub s in
alias ~loc ~attrs (sub.typ sub t) s
| Ptyp_variant (rl, b, ll) ->
variant ~loc ~attrs (List.map (row_field sub) rl) b ll
| Ptyp_poly (sl, t) -> poly ~loc ~attrs
(List.map (map_loc sub) sl) (sub.typ sub t)
| Ptyp_package (lid, l) ->
package ~loc ~attrs (map_loc sub lid)
(List.map (map_tuple (map_loc sub) (sub.typ sub)) l)
| Ptyp_open (mod_ident, t) ->
open_ ~loc ~attrs (map_loc sub mod_ident) (sub.typ sub t)
| Ptyp_extension x -> extension ~loc ~attrs (sub.extension sub x)

let map_type_declaration sub
Expand Down Expand Up @@ -360,7 +367,9 @@ module M = struct
(sub.module_type sub mty)
| Pmod_unpack e -> unpack ~loc ~attrs (sub.expr sub e)
| Pmod_extension x -> extension ~loc ~attrs (sub.extension sub x)
(* Added *)
| Pmod_hole -> hole ~loc ~attrs ()
(* *)

let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
let open Str in
Expand Down Expand Up @@ -391,6 +400,35 @@ end
module E = struct
(* Value expressions for the core language *)

let map_function_param sub { pparam_loc = loc; pparam_desc = desc } =
let loc = sub.location sub loc in
let desc =
match desc with
| Pparam_val (lab, def, p) ->
Pparam_val
(lab,
map_opt (sub.expr sub) def,
sub.pat sub p)
| Pparam_newtype ty ->
Pparam_newtype (map_loc sub ty)
in
{ pparam_loc = loc; pparam_desc = desc }

let map_function_body sub body =
match body with
| Pfunction_body e ->
Pfunction_body (sub.expr sub e)
| Pfunction_cases (cases, loc, attributes) ->
let cases = sub.cases sub cases in
let loc = sub.location sub loc in
let attributes = sub.attributes sub attributes in
Pfunction_cases (cases, loc, attributes)

let map_constraint sub c =
match c with
| Pconstraint ty -> Pconstraint (sub.typ sub ty)
| Pcoerce (ty1, ty2) -> Pcoerce (map_opt (sub.typ sub) ty1, sub.typ sub ty2)

let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
let open Exp in
let loc = sub.location sub loc in
Expand All @@ -401,10 +439,11 @@ module E = struct
| Pexp_let (r, vbs, e) ->
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
(sub.expr sub e)
| Pexp_fun (lab, def, p, e) ->
fun_ ~loc ~attrs lab (map_opt (sub.expr sub) def) (sub.pat sub p)
(sub.expr sub e)
| Pexp_function pel -> function_ ~loc ~attrs (sub.cases sub pel)
| Pexp_function (ps, c, b) ->
function_ ~loc ~attrs
(List.map (map_function_param sub) ps)
(map_opt (map_constraint sub) c)
(map_function_body sub b)
| Pexp_apply (e, l) ->
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
| Pexp_match (e, pel) ->
Expand Down Expand Up @@ -469,7 +508,9 @@ module E = struct
(List.map (sub.binding_op sub) ands) (sub.expr sub body)
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
| Pexp_unreachable -> unreachable ~loc ~attrs ()
(* Added *)
| Pexp_hole -> hole ~loc ~attrs ()
(* *)

let map_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_loc} =
let open Exp in
Expand Down Expand Up @@ -865,11 +906,16 @@ module PpxContext = struct
}

let make ~tool_name () =
let Load_path.{ visible; hidden } = Load_path.get_paths () in
let fields =
[
lid "tool_name", make_string tool_name;
lid "include_dirs", make_list make_string !Clflags.include_dirs;
lid "load_path", make_list make_string (Load_path.get_paths ());
lid "include_dirs", make_list make_string (!Clflags.include_dirs);
lid "hidden_include_dirs",
make_list make_string (!Clflags.hidden_include_dirs);
lid "load_path",
make_pair (make_list make_string) (make_list make_string)
(visible, hidden);
lid "open_modules", make_list make_string !Clflags.open_modules;
lid "for_package", make_option make_string !Clflags.for_package;
lid "debug", make_bool !Clflags.debug;
Expand Down Expand Up @@ -938,6 +984,8 @@ module PpxContext = struct
tool_name_ref := get_string payload
| "include_dirs" ->
Clflags.include_dirs := get_list get_string payload
| "hidden_include_dirs" ->
Clflags.hidden_include_dirs := get_list get_string payload
| "load_path" ->
(* Duplicates Compmisc.auto_include, since we can't reference Compmisc
from this module. *)
Expand All @@ -948,7 +996,10 @@ module PpxContext = struct
let alert = Location.auto_include_alert in
Load_path.auto_include_otherlibs alert find_in_dir fn
in
Load_path.init ~auto_include (get_list get_string payload)
let visible, hidden =
get_pair (get_list get_string) (get_list get_string) payload
in
Load_path.init ~auto_include ~visible ~hidden
| "open_modules" ->
Clflags.open_modules := get_list get_string payload
| "for_package" ->
Expand Down
68 changes: 53 additions & 15 deletions vendor/parser-standard/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,34 @@ let get_stored_string () = Buffer.contents string_buffer
let store_string_char c = Buffer.add_char string_buffer c
let store_string_utf_8_uchar u = Buffer.add_utf_8_uchar string_buffer u
let store_string s = Buffer.add_string string_buffer s
let store_substring s ~pos ~len = Buffer.add_substring string_buffer s pos len

let store_lexeme lexbuf = store_string (Lexing.lexeme lexbuf)
let store_normalized_newline newline =
(* #12502: we normalize "\r\n" to "\n" at lexing time,
to avoid behavior difference due to OS-specific
newline characters in string literals.
(For example, Git for Windows will translate \n in versioned
files into \r\n sequences when checking out files on Windows. If
your code contains multiline quoted string literals, the raw
content of the string literal would be different between Git for
Windows users and all other users. Thanks to newline
normalization, the value of the literal as a string constant will
be the same no matter which programming tools are used.)
Many programming languages use the same approach, for example
Java, Javascript, Kotlin, Python, Swift and C++.
*)
(* Our 'newline' regexp accepts \r*\n, but we only wish
to normalize \r?\n into \n -- see the discussion in #12502.
All carriage returns except for the (optional) last one
are reproduced in the output. We implement this by skipping
the first carriage return, if any. *)
let len = String.length newline in
if len = 1
then store_string_char '\n'
else store_substring newline ~pos:1 ~len:(len - 1)

(* To store the position of the beginning of a string and comment *)
let string_start_loc = ref Location.none
Expand Down Expand Up @@ -338,7 +365,7 @@ let prepare_error loc = function
Location.error ~loc ~sub msg
| Keyword_as_label kwd ->
Location.errorf ~loc
"`%s' is a keyword, it cannot be used as label name" kwd
"%a is a keyword, it cannot be used as label name" Style.inline_code kwd
| Invalid_literal s ->
Location.errorf ~loc "Invalid literal %s" s
| Invalid_directive (dir, explanation) ->
Expand Down Expand Up @@ -403,6 +430,7 @@ let hex_float_literal =
('.' ['0'-'9' 'A'-'F' 'a'-'f' '_']* )?
(['p' 'P'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']* )?
let literal_modifier = ['G'-'Z' 'g'-'z']
let raw_ident_escape = "\\#"

rule token = parse
| ('\\' as bs) newline {
Expand All @@ -421,6 +449,8 @@ rule token = parse
| ".~"
{ error lexbuf
(Reserved_sequence (".~", Some "is reserved for use in MetaOCaml")) }
| "~" raw_ident_escape (lowercase identchar * as name) ':'
{ LABEL name }
| "~" (lowercase identchar * as name) ':'
{ check_label_name lexbuf name;
LABEL name }
Expand All @@ -429,12 +459,16 @@ rule token = parse
LABEL name }
| "?"
{ QUESTION }
| "?" raw_ident_escape (lowercase identchar * as name) ':'
{ OPTLABEL name }
| "?" (lowercase identchar * as name) ':'
{ check_label_name lexbuf name;
OPTLABEL name }
| "?" (lowercase_latin1 identchar_latin1 * as name) ':'
{ warn_latin1 lexbuf;
OPTLABEL name }
| raw_ident_escape (lowercase identchar * as name)
{ LIDENT name }
| lowercase identchar * as name
{ try Hashtbl.find keyword_table name
with Not_found -> LIDENT name }
Expand Down Expand Up @@ -493,7 +527,7 @@ rule token = parse
{ CHAR(char_for_octal_code lexbuf 3) }
| "\'\\" 'x' ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] "\'"
{ CHAR(char_for_hexadecimal_code lexbuf 3) }
| "\'" ("\\" _ as esc)
| "\'" ("\\" [^ '#'] as esc)
{ error lexbuf (Illegal_escape (esc, None)) }
| "\'\'"
{ error lexbuf Empty_character_literal }
Expand Down Expand Up @@ -676,9 +710,11 @@ and comment = parse
comment lexbuf }
| "\'\'"
{ store_lexeme lexbuf; comment lexbuf }
| "\'" newline "\'"
| "\'" (newline as nl) "\'"
{ update_loc lexbuf None 1 false 1;
store_lexeme lexbuf;
store_string_char '\'';
store_normalized_newline nl;
store_string_char '\'';
comment lexbuf
}
| "\'" [^ '\\' '\'' '\010' '\013' ] "\'"
Expand All @@ -699,9 +735,9 @@ and comment = parse
comment_start_loc := [];
error_loc loc (Unterminated_comment start)
}
| newline
| newline as nl
{ update_loc lexbuf None 1 false 0;
store_lexeme lexbuf;
store_normalized_newline nl;
comment lexbuf
}
| ident
Expand All @@ -712,9 +748,13 @@ and comment = parse
and string = parse
'\"'
{ lexbuf.lex_start_p }
| '\\' newline ([' ' '\t'] * as space)
| '\\' (newline as nl) ([' ' '\t'] * as space)
{ update_loc lexbuf None 1 false (String.length space);
if in_comment () then store_lexeme lexbuf;
if in_comment () then begin
store_string_char '\\';
store_normalized_newline nl;
store_string space;
end;
string lexbuf
}
| '\\' (['\\' '\'' '\"' 'n' 't' 'b' 'r' ' '] as c)
Expand Down Expand Up @@ -743,11 +783,9 @@ and string = parse
store_lexeme lexbuf;
string lexbuf
}
| newline
{ if not (in_comment ()) then
Location.prerr_warning (Location.curr lexbuf) Warnings.Eol_in_string;
update_loc lexbuf None 1 false 0;
store_lexeme lexbuf;
| newline as nl
{ update_loc lexbuf None 1 false 0;
store_normalized_newline nl;
string lexbuf
}
| eof
Expand All @@ -758,9 +796,9 @@ and string = parse
string lexbuf }
and quoted_string delim = parse
| newline
| newline as nl
{ update_loc lexbuf None 1 false 0;
store_lexeme lexbuf;
store_normalized_newline nl;
quoted_string delim lexbuf
}
| eof
Expand Down
Loading

0 comments on commit 40ec2ac

Please sign in to comment.