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

Add rudimentary parser for Git config files #50

Merged
merged 6 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 14 additions & 3 deletions cli/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
(executables
(executable
(libraries cohttp-lwt-unix gitlab-unix cmdliner otoml)
(package lab)
(public_names lab)
(names main))
(public_name lab)
(modules main api config issue merge_request project user)
(name main))

(executable
(name test_gitconfig)
(modules_without_implementation gitconfig_types)
(modules gitconfig_lexer gitconfig_types gitconfig_parser test_gitconfig))

(ocamllex gitconfig_lexer)

(menhir
(modules gitconfig_parser))

(mdx
(files lab.md)
Expand Down
16 changes: 16 additions & 0 deletions cli/gitconfig_lexer.mll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
open Gitconfig_parser

exception Error of string
}

let whitespace = ['\t' ' ']*
let eol = '\n' | "\r\n"
let key = ['A'-'Z''a'-'z''0'-'9''_''-']+
let value = [^'\n''\r']+
let section_name = [^'['']']+

rule token = parse
| whitespace '[' (section_name as name) ']' whitespace eol { SECTIONHEADER name }
| whitespace (key as key) whitespace '=' whitespace (value as value) eol { KEYVAL (key,value) }
| eof { EOF }
24 changes: 24 additions & 0 deletions cli/gitconfig_parser.mly
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%{
%}

%token <Gitconfig_types.binding> KEYVAL
%token <string> SECTIONHEADER
%token EOF

%start config

%type <Gitconfig_types.t> config
%type <Gitconfig_types.binding> key_value

%%
config:
| section* EOF { $1 }

section:
| section_header key_value* { $1, $2 }

section_header:
| SECTIONHEADER { $1 }

key_value:
| KEYVAL { $1 }
5 changes: 5 additions & 0 deletions cli/gitconfig_types.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type binding = string * string (* Key/value pair *)

type section = string * binding list (* Section name and contents *)

type t = section list
OlivierNicole marked this conversation as resolved.
Show resolved Hide resolved
OlivierNicole marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions cli/test_gitconfig.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let print_config fmt config =
OlivierNicole marked this conversation as resolved.
Show resolved Hide resolved
OlivierNicole marked this conversation as resolved.
Show resolved Hide resolved
let open Format in
let rec print_bindings fmt = function
| [] -> ()
| (key, value) :: rem ->
fprintf fmt "\t%s = %s\n" key value;
print_bindings fmt rem
in
config |> List.iter (fun (name, bindings) ->
fprintf fmt "[%s]\n%a" name print_bindings bindings
)


let () =
let lexbuf = Lexing.from_channel stdin in
try
let config = Gitconfig_parser.config Gitconfig_lexer.token lexbuf in
print_config Format.std_formatter config
with
| Gitconfig_lexer.Error msg ->
Printf.fprintf stderr "%s%!" msg
| Gitconfig_parser.Error ->
Printf.fprintf stderr "At offset %d: syntax error.\n%!" (Lexing.lexeme_start lexbuf)
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ This library installs the JavaScript version, which uses [js_of_ocaml](http://oc
(synopsis "GitLab cli")
(description "Experimental GitLab cli in the style of GitHub's gh and hub commands."))

(using menhir 2.1)