From 68de927be04607e6573a5439e19449941defb5f8 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 12:13:49 -0600 Subject: Fix bug with naming --- bin/main.ml | 1 - lib/fixture.ml | 2 +- lib/lexer.mll | 6 +++--- lib/parser.mly | 36 +++++++++++++++++++++--------------- test/test_parser.ml | 16 ++++++++-------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 43ce56b..0f9972d 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -54,7 +54,6 @@ let file = Arg.(required & pos 0 (some string) None & info [] ~docv:"PROGRAM" ~doc) -(* TODO: Support option to set output folder *) let output_folder = let doc = "Set output folder" in Arg.( diff --git a/lib/fixture.ml b/lib/fixture.ml index 94c39a9..daf172e 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -135,7 +135,7 @@ let json_of_generated_fixtures fixtures = "[" ^ String.concat ", " (List.map json_of_row rows) ^ "]" -(* TODO: Should export not only as strings *) +(* TODO: Should export correct types, not only strings *) let%test "json_of_generated_fixtures" = let result = [ [ "id"; "name" ]; [ "1234"; "John" ] ] |> json_of_generated_fixtures in diff --git a/lib/lexer.mll b/lib/lexer.mll index 6e9b975..c6f2b93 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -17,10 +17,10 @@ let many_newline = newline+ rule read = parse + | "uuidv4" { UUIDV4 } + | "name" { NAME } + | "int" { INTSYMBOL } | id { IDENTIFIER (Lexing.lexeme lexbuf) } - | "_uuidv4" { UUIDV4 } - | "_name" { NAME } - | "_int" { INTSYMBOL } | "(" { LBRACE } | ")" { RBRACE } | '"' { read_string (Buffer.create 20) lexbuf } diff --git a/lib/parser.mly b/lib/parser.mly index 8a666c6..19fa44e 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -22,30 +22,36 @@ prog: | e = expr; EOF { Some e } ; +id_or_other: + | IDENTIFIER { $1 } + | NAME { "name" } + | UUIDV4 { "uuidv4" } + + expr: - | tbl = IDENTIFIER; LBRACE; amount = INT; RBRACE; COLON; r = row; NEWLINE; e = expr { Table (tbl, amount, r, e) } - | tbl = IDENTIFIER; LBRACE; amount = INT; RBRACE; COLON; r = row; SEMICOLON; e = expr { Table (tbl,amount, r, e) } - | tbl = IDENTIFIER; LBRACE; amount = INT; RBRACE; COLON; r = row { Table (tbl,amount, r, End) } + | tbl = id_or_other; LBRACE; amount = INT; RBRACE; COLON; r = row; NEWLINE; e = expr { Table (tbl, amount, r, e) } + | tbl = id_or_other; LBRACE; amount = INT; RBRACE; COLON; r = row; SEMICOLON; e = expr { Table (tbl,amount, r, e) } + | tbl = id_or_other; LBRACE; amount = INT; RBRACE; COLON; r = row { Table (tbl,amount, r, End) } ; row: - | row_title = IDENTIFIER; parent = IDENTIFIER; DOT; parent_id = IDENTIFIER; COMMA; r = row { Foreign (parent,parent_id,row_title, r) } - | row_title = IDENTIFIER; parent = IDENTIFIER; DOT; parent_id = IDENTIFIER; { Foreign (parent,parent_id,row_title, End) } + | row_title = id_or_other; parent = id_or_other; DOT; parent_id = id_or_other; COMMA; r = row { Foreign (parent,parent_id,row_title, r) } + | row_title = id_or_other; parent = id_or_other; DOT; parent_id = id_or_other; { Foreign (parent,parent_id,row_title, End) } - | row_title = IDENTIFIER; UUIDV4; COMMA; r = row { Uuidv4 (row_title, r) } - | row_title = IDENTIFIER; UUIDV4 { Uuidv4 (row_title, End) } + | row_title = id_or_other; UUIDV4; COMMA; r = row { Uuidv4 (row_title, r) } + | row_title = id_or_other; UUIDV4 { Uuidv4 (row_title, End) } - | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE; COMMA; r = row { List (row_title, union, r) } - | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE { List (row_title, union, End) } + | row_title = id_or_other; LBRACE; union = union_fields; RBRACE; COMMA; r = row { List (row_title, union, r) } + | row_title = id_or_other; LBRACE; union = union_fields; RBRACE { List (row_title, union, End) } - | row_title = IDENTIFIER; NAME; COMMA; r = row { Name (row_title, r) } - | row_title = IDENTIFIER; NAME { Name (row_title, End) } + | row_title = id_or_other; NAME; COMMA; r = row { Name (row_title, r) } + | row_title = id_or_other; NAME { Name (row_title, End) } - | row_title = IDENTIFIER; const = STRING; COMMA; r = row { Const (row_title, const, r) } - | row_title = IDENTIFIER; const = STRING { Const (row_title, const, End) } + | row_title = id_or_other; const = STRING; COMMA; r = row { Const (row_title, const, r) } + | row_title = id_or_other; const = STRING { Const (row_title, const, End) } - | row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE;COMMA; r = row { Int (row_title,min,max,r) } - | row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE; { Int (row_title,min,max,End) } + | row_title = id_or_other; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE;COMMA; r = row { Int (row_title,min,max,r) } + | row_title = id_or_other; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE; { Int (row_title,min,max,End) } ; union_fields: diff --git a/test/test_parser.ml b/test/test_parser.ml index 80e9d12..003086a 100644 --- a/test/test_parser.ml +++ b/test/test_parser.ml @@ -13,32 +13,32 @@ let test_parse s () = let rows_suite = - [ "Can parse uuidv4", `Quick, test_parse "user (1): id _uuidv4" + [ "Can parse uuidv4", `Quick, test_parse "user (1): id uuidv4" ; ( "Can parse multiple tables" , `Quick - , test_parse "user (1): id _uuidv4\npurchases (1): id _uuidv4" ) - ; "Supports names", `Quick, test_parse "purchases (1): name _name" + , test_parse "user (1): id uuidv4\npurchases (1): id uuidv4" ) + ; "Supports names", `Quick, test_parse "purchases (1): name name" ; "Supports const", `Quick, test_parse "purchases (1): name \"hello\"" - ; "Supports ints with min/max", `Quick, test_parse "purchases (1): name _int(1,20)" + ; "Supports ints with min/max", `Quick, test_parse "purchases (1): name int(1,20)" ; ( "Supports a list of potential values" , `Quick , test_parse "purchases (1): name (\"a\",\"b\",\"c\")" ) ; ( "Supports setting amount of fixture to generate" , `Quick - , test_parse "purchases (5): name _int(1,20)" ) + , test_parse "purchases (5): name int(1,20)" ) ] let relations_suite = [ ( "Can reference other tables" , `Quick - , test_parse "user (2): id _uuidv4\npurchases (3): id _uuidv4, userid user.id" ) + , test_parse "user (2): id uuidv4\npurchases (3): id uuidv4, userid user.id" ) ; ( "Can use ; as a separator" , `Quick - , test_parse "user (2): id _uuidv4; purchases (5): id _uuidv4" ) + , test_parse "user (2): id uuidv4; purchases (5): id uuidv4" ) ; ( "Supports multiple newlines" , `Quick - , test_parse "user (2): id _uuidv4\n\n\n\n\npurchases (5): id _uuidv4" ) + , test_parse "user (2): id uuidv4\n\n\n\n\npurchases (5): id uuidv4" ) ] -- cgit v1.2.3