From 742c23d7c5d90b681dcbce92846d57042d2ea467 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 09:34:58 -0600 Subject: Add support for ints --- lib/ast_types.ml | 5 +++++ lib/fixture.ml | 7 +++++-- lib/lexer.mll | 5 +++++ lib/parser.mly | 6 ++++++ 4 files changed, 21 insertions(+), 2 deletions(-) (limited to 'lib') 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 +%token LBRACE +%token RBRACE %start 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) } ; -- cgit v1.2.3