Use comment variants and pull out mparser dependencies.

This commit is contained in:
Rijnard van Tonder 2019-04-23 02:23:20 -04:00 committed by GitHub
parent 4cf042413c
commit e2cca3e614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 112 additions and 105 deletions

View File

@ -1,28 +1,15 @@
open MParser
module Syntax = struct
open Types
include Generic.Syntax
let user_defined_delimiters =
Dyck.Syntax.user_defined_delimiters @
[ ("if", "fi")
; ("case", "esac")
]
@ Generic.Syntax.user_defined_delimiters
let escapable_string_literals =
[ {|"|}
; {|'|}
]
let escape_char =
'\\'
let raw_string_literals =
[]
let comment_parser s =
(Parsers.Comments.c_multiline
<|> Parsers.Comments.c_newline) s
let comment_parser =
[ Until_newline "#" ]
end
include Matcher.Make(Syntax)

View File

@ -1,22 +1,11 @@
open MParser
module Syntax = struct
open Types
include Generic.Syntax
let escapable_string_literals =
[ {|"|}
; {|'|}
let comment_parser =
[ Multiline ("/*", "*/")
; Until_newline "//"
]
let escape_char =
'\\'
let raw_string_literals =
[]
let comment_parser s =
(Parsers.Comments.c_multiline
<|> Parsers.Comments.c_newline) s
end
include Matcher.Make(Syntax)

18
lib/matchers/dyck.ml Normal file
View File

@ -0,0 +1,18 @@
module Syntax = struct
let user_defined_delimiters =
[ ("(", ")")
; ("{", "}")
; ("[", "]")
]
let escapable_string_literals = []
let escape_char =
'\\'
let raw_string_literals = []
let comment_parser = []
end
include Matcher.Make(Syntax)

4
lib/matchers/dyck.mli Normal file
View File

@ -0,0 +1,4 @@
open Types
module Syntax : Syntax.S
include Matcher.S

View File

@ -1,11 +1,6 @@
(** Dyck with common string literals. *)
module Syntax = struct
(** these are nestable. strings, on the other hand, are not
nestable without escapes *)
let user_defined_delimiters =
[ ("(", ")")
; ("{", "}")
; ("[", "]")
]
include Dyck.Syntax
let escapable_string_literals =
[ {|"|}
@ -14,10 +9,6 @@ module Syntax = struct
let escape_char =
'\\'
let raw_string_literals = []
let comment_parser = MParser.zero
end
include Matcher.Make(Syntax)

View File

@ -4,7 +4,6 @@ module Syntax = struct
let raw_string_literals =
[ ({|`|}, {|`|})
]
end
include Matcher.Make(Syntax)

View File

@ -1,17 +1,24 @@
module Syntax = struct
include Generic.Syntax
open Types
let user_defined_delimiters =
Generic.Syntax.user_defined_delimiters @
Dyck.Syntax.user_defined_delimiters @
[ ("<", ">")
]
let escapable_string_literals =
[ {|"|}
; {|'|}
]
let raw_string_literals = []
let escape_char =
'\\'
let comment_parser =
[ Multiline ("<!--", "-->")
]
end
include Matcher.Make(Syntax)

View File

@ -1,19 +1,15 @@
module Syntax = struct
open Types
include Dyck.Syntax
let user_defined_delimiters =
[ ("(", ")")
; ("{", "}")
; ("[", "]")
; ({|\if|}, {|\fi|})
Dyck.Syntax.user_defined_delimiters @
[ ({|\if|}, {|\fi|})
]
let escapable_string_literals = []
let escape_char =
'\\'
let raw_string_literals = []
let comment_parser s = Parsers.Comments.percentage_newline s
let comment_parser =
[ Until_newline "%"
]
end
include Matcher.Make(Syntax)

View File

@ -53,6 +53,28 @@ module Make (Syntax : Syntax.S) = struct
return (f ~contents ~left_delimiter ~right_delimiter))
|> choice
let comment_parser =
match Syntax.comment_parser with
| [] -> MParser.zero
| syntax ->
List.map syntax ~f:(function
| Multiline (left, right) ->
let module M =
Parsers.Comments.Multiline.Make(struct
let left = left
let right = right
end)
in
M.multiline_comment
| Until_newline start ->
let module M =
Parsers.Comments.Until_newline.Make(struct
let start = start
end)
in
M.until_newline_comment)
|> choice
let escapable_literal_grammar ~right_delimiter =
(attempt
(char Syntax.escape_char
@ -86,11 +108,11 @@ module Make (Syntax : Syntax.S) = struct
let generate_spaces_parser () =
(* at least a space followed by comments and spaces *)
(spaces1
>> many Syntax.comment_parser << spaces
>> many comment_parser << spaces
>>= fun result -> f result)
<|>
(* This case not covered by tests, may not be needed *)
(many1 Syntax.comment_parser << spaces >>= fun result -> f result)
(many1 comment_parser << spaces >>= fun result -> f result)
let sequence_chain (plist : ('c, Match.t) parser sexp_list) : ('c, Match.t) parser =
List.fold plist ~init:(return Unit) ~f:(>>)
@ -105,9 +127,9 @@ module Make (Syntax : Syntax.S) = struct
(** All code can have comments interpolated *)
let generate_string_token_parser str : ('c, _) parser =
many Syntax.comment_parser
many comment_parser
>> string str
>> many Syntax.comment_parser
>> many comment_parser
>>= fun result -> f result
let greedy_hole_parser _s =
@ -210,7 +232,7 @@ module Make (Syntax : Syntax.S) = struct
(* a parser that understands the hole matching cut off points happen at
delimiters *)
let rec nested_grammar s =
(Syntax.comment_parser
(comment_parser
<|> escapable_string_literal_parser (fun ~contents ~left_delimiter:_ ~right_delimiter:_ -> contents)
<|> raw_string_literal_parser (fun ~contents ~left_delimiter:_ ~right_delimiter:_ -> contents)
<|> delimsx
@ -359,7 +381,7 @@ module Make (Syntax : Syntax.S) = struct
(not_followed_by matcher "" >>
(
(* respect grammar but ignore contents up to a match *)
skip Syntax.comment_parser
skip comment_parser
<|> skip (escapable_string_literal_parser (fun ~contents:_ ~left_delimiter:_ ~right_delimiter:_ -> ()))
<|> skip (raw_string_literal_parser (fun ~contents:_ ~left_delimiter:_ ~right_delimiter:_ -> ()))
<|> skip any_char)

View File

@ -1,20 +1,14 @@
module Python = struct
open Types
include Generic.Syntax
let escapable_string_literals =
[ {|"|}
; {|'|}
]
let escape_char =
'\\'
let raw_string_literals =
[ ({|"""|}, {|"""|})
]
let comment_parser s =
Parsers.Comments.python_newline s
let comment_parser =
[ Until_newline "#"
]
end
include Matcher.Make(Python)

View File

@ -1,12 +1,16 @@
open Core
type comment_kind =
| Multiline of string * string
| Until_newline of string
module Syntax = struct
module type S = sig
val user_defined_delimiters : (string * string) list
val escapable_string_literals : string list
val escape_char : char
val raw_string_literals : (string * string) list
val comment_parser : (string, _) MParser.t
val comment_parser : comment_kind list
end
end

View File

@ -23,22 +23,9 @@ let non_nested_comment_delimiters from until s =
|>> to_string from until
) s
(** a parser for /* ... */ style block comments. *)
let c_multiline s =
non_nested_comment_delimiters "/*" "*/" s
let c_newline s =
(string "//" >> anything_excluding_newlines ~until:"\n"
|>> fun l -> "//"^(String.of_char_list l)) s
let python_newline s =
(string "#" >> anything_excluding_newlines ~until:"\n"
|>> fun l -> ("#"^String.of_char_list l)) s
let percentage_newline s =
(string "%" >> anything_excluding_newlines ~until:"\n"
|>> fun l -> ("%"^String.of_char_list l)) s
let until_newline start s =
(string start >> anything_excluding_newlines ~until:"\n"
|>> fun l -> start^(String.of_char_list l)) s
let any_newline comment_string s =
(string comment_string >> anything_excluding_newlines ~until:"\n" |>> fun l -> (comment_string^String.of_char_list l)) s
@ -72,3 +59,25 @@ let skip_nested_comments_inner from until s =
in
(comment_delimiters >>= fun _ ->
return ()) s
(** a parser for, e.g., /* ... */ style block comments. Non-nested. *)
module Multiline = struct
module type S = sig
val left : string
val right : string
end
module Make (M : S) = struct
let multiline_comment s = non_nested_comment_delimiters M.left M.right s
end
end
module Until_newline = struct
module type S = sig
val start : string
end
module Make (M : S) = struct
let until_newline_comment s = until_newline M.start s
end
end

View File

@ -1,13 +0,0 @@
(** C-style /* */ block comment parser *)
val c_multiline : (string, _) MParser.t
(** C++-style // line comment parser *)
val c_newline : (string, _) MParser.t
(** Python-style # line comment parser *)
val python_newline : (string, _) MParser.t
val percentage_newline : (string, _) MParser.t
(** Anything until newline *)
val any_newline : string -> (string, _) MParser.t