diff options
author | Marc Coquand <marc@mccd.space> | 2023-12-26 09:34:58 -0600 |
---|---|---|
committer | Marc Coquand <marc@mccd.space> | 2023-12-26 09:57:20 -0600 |
commit | 742c23d7c5d90b681dcbce92846d57042d2ea467 (patch) | |
tree | b6cf10333e15bc7b748744f7c7af805ca77f222c | |
parent | 8ac0e27adc8d14a77427351dede1757999c8c709 (diff) | |
download | fixgen-742c23d7c5d90b681dcbce92846d57042d2ea467.tar.gz fixgen-742c23d7c5d90b681dcbce92846d57042d2ea467.tar.bz2 fixgen-742c23d7c5d90b681dcbce92846d57042d2ea467.zip |
Add support for ints
Diffstat (limited to '')
-rw-r--r-- | lib/ast_types.ml | 5 | ||||
-rw-r--r-- | lib/fixture.ml | 7 | ||||
-rw-r--r-- | lib/lexer.mll | 5 | ||||
-rw-r--r-- | lib/parser.mly | 6 | ||||
-rw-r--r-- | test/test_parser.ml | 1 |
5 files changed, 22 insertions, 2 deletions
diff --git a/lib/ast_types.ml b/lib/ast_types.ml index 1c5de60..7b4f445 100644 --- a/lib/ast_types.ml +++ b/lib/ast_types.ml @@ -4,6 +4,7 @@ module Fixture = Fixture type ast_row = | Uuidv4 of string * ast_row | Name of string * ast_row + | Int of string * int * int * ast_row | Foreign of string * string * string * ast_row (* parent, row, child_name, row *) | End @@ -21,6 +22,9 @@ let rec print_row = function | Foreign (p, r, c, next_row) -> printf "Foreign(%s.%s, %s)," p r c; print_row next_row + | Int (s, min, max, r) -> + printf "Int(%s, %d, %d)," s min max; + print_row r | Name (s, r) -> printf "Name(%s)," s; print_row r @@ -55,6 +59,7 @@ let rec ast_row_to_fixtures = function | Foreign (p, r, c, next_row) -> CRow (p, r, fun l -> Fixture.Foreign (c, l)) :: ast_row_to_fixtures next_row | Name (s, r) -> PRow (Fixture.Name s) :: ast_row_to_fixtures r + | Int (s, min, max, r) -> PRow (Fixture.Int (s, min, max)) :: ast_row_to_fixtures r | End -> [] diff --git a/lib/fixture.ml b/lib/fixture.ml index 189d2fb..8806692 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -22,12 +22,13 @@ let names = let random_name () = List.nth names (Random.int (List.length names)) let%test "random_name" = List.mem (random_name ()) names -(* Support arbitrary amount *) -(* Support names *) +(* TODO: Support arbitrary amount *) +(* TODO: Support names *) type t = | Name of string | Uuidv4 of string | Foreign of (string * string list) + | Int of string * int * int (* (Name, foreign ids to pick from) *) let add_name name fixtures = Name name :: fixtures @@ -46,6 +47,7 @@ let generate_fixture fixture = | Name _ -> random_name () | Uuidv4 _ -> Uuidm.v `V4 |> Uuidm.to_string | Foreign (_, reference) -> random_value_in_list reference + | Int (_, min, max) -> Random.int (max - min) + min |> string_of_int let%test "generate_uuid" = @@ -57,6 +59,7 @@ let id_of_fixture fixture = | Name id -> id | Uuidv4 id -> id | Foreign (id, _) -> id + | Int (id, _, _) -> id let rec replicate element n = diff --git a/lib/lexer.mll b/lib/lexer.mll index 769192b..fed77ab 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -8,6 +8,7 @@ let digit = ['0'-'9'] let frac = '.' digit* let exp = ['e' 'E'] ['-' '+']? digit+ let float = digit* frac? exp? +let int = digit+ let white = [' ' '\t']+ let newline = '\r' | '\n' | "\r\n" let id = ['a'-'z' 'A'-'Z' '-']* @@ -19,6 +20,10 @@ rule read = | id { IDENTIFIER (Lexing.lexeme lexbuf) } | "_uuidv4" { UUIDV4 } | "_name" { NAME } + | "_int" { INTSYMBOL } + | "<" { LBRACE } + | ">" { RBRACE } + | int { INT (Lexing.lexeme lexbuf |> int_of_string) } | white { read lexbuf } | ":" { COLON } | ";" { SEMICOLON } diff --git a/lib/parser.mly b/lib/parser.mly index 3d57961..ab9b697 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -10,6 +10,10 @@ %token COLON %token DOT %token EOF +%token INTSYMBOL +%token <int> INT +%token LBRACE +%token RBRACE %start <ast_table option > prog %% @@ -30,4 +34,6 @@ row: | row_title = IDENTIFIER; UUIDV4 { Uuidv4 (row_title, End) } | row_title = IDENTIFIER; NAME; COMMA; r = row { Name (row_title, r) } | row_title = IDENTIFIER; NAME { Name (row_title, 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) } ; diff --git a/test/test_parser.ml b/test/test_parser.ml index c677869..941ac56 100644 --- a/test/test_parser.ml +++ b/test/test_parser.ml @@ -19,6 +19,7 @@ let rows_suite = , test_parse "user: id _uuidv4\npurchases: id _uuidv4" ) ; "Resolves conflicting names", `Quick, test_parse "purchases: name _uuidv4" ; "Supports names", `Quick, test_parse "purchases: name _name" + ; "Supports ints with min/max", `Quick, test_parse "purchases: name _int<1,20>" ] |