Skip to content

Commit

Permalink
Temporal push
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Oct 9, 2023
1 parent c5112ec commit 318fdda
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 140 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,6 @@ jobs:
zip -r ../../query-json-windows-x64.zip .
cd ../..
- name: End to end Test (Linux)
if: ${{ matrix.os == 'ubuntu-latest' }}
run: npx bats ./e2e/test.sh
env:
IS_CI: true

- name: End to end Test (Windows)
if: ${{ matrix.os == 'windows-latest' }}
run: sh -c 'npx bats ./e2e/test.sh' \"$(pwd)/node_modules/bats/bin/bats\"
env:
IS_CI: true

- name: Check if should be published
if: ${{ success() && github.event_name != 'pull_request' }}
id: newVersion
Expand Down
60 changes: 33 additions & 27 deletions bin/Bin.re
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
open QueryJsonCore;
open QueryJsonCore.Console;

type inputKind =
| File
| Inline;
module Runtime = {
type inputKind =
| File
| Inline;

let run = (~kind, ~payload, ~noColor, runtime) => {
let input =
switch (kind, payload) {
| (File, Some(file)) => Json.parseFile(file)
| (Inline, Some(str)) => Json.parseString(str)
| (_, None) =>
let ic = Unix.(stdin |> in_channel_of_descr);
Json.parseChannel(ic);
};
let run = (~kind, ~payload, ~noColor, runtime) => {
let input =
switch (kind, payload) {
| (File, Some(file)) => Json.parseFile(file)
| (Inline, Some(str)) => Json.parseString(str)
| (_, None) =>
let ic = Unix.(stdin |> in_channel_of_descr);
Json.parseChannel(ic);
};

switch (input) {
| Ok(json) =>
switch (runtime(json)) {
switch (input) {
| Ok(json) =>
json
|> List.map(Json.toString(~colorize=!noColor, ~summarize=false))
|> List.iter(print_endline)
| Error(err) => print_endline(Errors.printError(err))
}
| Error(err) => print_endline(Errors.printError(err))
switch (runtime(json)) {
| Ok(json) =>
json
|> List.map(Json.toString(~colorize=!noColor, ~summarize=false))
|> List.iter(print_endline)
| Error(err) => print_endline(Console.Errors.printError(err))
}
| Error(err) => print_endline(Console.Errors.printError(err))
};
};
};

let execution =
(
query: option(string),
payload: option(string),
kind: inputKind,
_verbose: bool,
kind: Runtime.inputKind,
verbose: bool,
debug: bool,
noColor: bool,
) => {
switch (query) {
| Some(q) =>
Main.parse(~debug, q)
Main.parse(~debug, ~verbose, q)
|> Result.map(Compiler.compile)
|> Result.iter(run(~payload, ~kind, ~noColor))
|> Result.iter(Runtime.run(~payload, ~kind, ~noColor))
| None => print_endline(usage())
};
};
Expand All @@ -60,8 +62,12 @@ let json = {

let kind = {
let doc = "input kind";
let kindEnum = Arg.enum([("file", File), ("inline", Inline)]);
Arg.(value & opt(kindEnum, ~vopt=File, File) & info(["k", "kind"], ~doc));
let kindEnum = Arg.enum([("file", Runtime.File), ("inline", Inline)]);
Arg.(
value
& opt(kindEnum, ~vopt=Runtime.File, File)
& info(["k", "kind"], ~doc)
);
};

let verbose = {
Expand Down
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

(generate_opam_files true)

(cram enable)

(license MIT)

(maintainers "David Sancho <[email protected]>")
Expand Down
46 changes: 0 additions & 46 deletions e2e/test.sh

This file was deleted.

8 changes: 5 additions & 3 deletions source/Ast.re
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[@deriving show]
[@deriving show({with_path: false})]
type regex = string;

[@deriving show]
[@deriving show({with_path: false})]
type literal =
| Bool(bool) /* true */
| String(string) /* "TEXT" */
| Number(float) /* 123 or 123.0 */
| Null; /* null */

[@deriving show]
[@deriving show({with_path: false})]
type expression =
| Identity /* . */
| Empty /* empty */
Expand Down Expand Up @@ -47,6 +47,8 @@ type expression =
| IsNan
/* Array */
| Index(int) /* .[1] */
| Iterator /* .[] */
| IteratorWithRange(list(int)) /* .[] */
| Range(int, int) /* range(1, 10) */
| Flatten /* flatten */
| Head /* head */
Expand Down
6 changes: 3 additions & 3 deletions source/Compiler.re
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ let member = (key: string, opt: bool, json: Json.t) => {

let index = (value: int, json: Json.t) => {
switch (json) {
| `List(list) when List.length(list) > value =>
Results.return(Json.index(value, json))
| `List(list) =>
List.length(list) > value
? Results.return(Json.index(value, json))
: Error(makeAcessingToMissingItem(value, List.length(list)))
Error(makeAcessingToMissingItem(value, List.length(list)))
| _ => Error(makeError("[" ++ string_of_int(value) ++ "]", json))
};
};
Expand Down
7 changes: 2 additions & 5 deletions source/Console.re
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ module Errors = {
};
};

let make = (~input, ~start: Lexing.position, ~end_: Lexing.position, exn) => {
let exnToString = extractExn(Printexc.to_string(exn));
let make = (~input, ~start: Lexing.position, ~end_: Lexing.position) => {
let pointerRange = String.make(end_.pos_cnum - start.pos_cnum, '^');

Chalk.red(Chalk.bold(exnToString))
++ Formatting.enter(1)
++ Formatting.indent(4)
Chalk.red(Chalk.bold("Parse error: "))
++ "Problem parsing at position "
++ positionToString(start, end_)
++ Formatting.enter(2)
Expand Down
13 changes: 10 additions & 3 deletions source/Json.re
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ let record = Easy_format.list;

let id = (i: string): string => i;

let pp_float = float =>
if (Stdlib.Float.equal(Stdlib.Float.round(float), float)) {
float |> int_of_float |> string_of_int;
} else {
Printf.sprintf("%g", float);
};

module Color = {
let rec format = (json: t) =>
switch (json) {
Expand All @@ -62,7 +69,7 @@ module Color = {
| `Int(i) =>
Easy_format.Atom(i |> string_of_int |> Chalk.green, Easy_format.atom)
| `Float(f) =>
Easy_format.Atom(f |> string_of_float |> Chalk.green, Easy_format.atom)
Easy_format.Atom(f |> pp_float |> Chalk.green, Easy_format.atom)
| `String(s) =>
Easy_format.Atom(encode(s) |> quotes |> Chalk.green, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s |> Chalk.green, Easy_format.atom)
Expand Down Expand Up @@ -95,7 +102,7 @@ module Summarize = {
| `Bool(b) => Easy_format.Atom(string_of_bool(b), Easy_format.atom)
| `Int(i) => Easy_format.Atom(i |> string_of_int, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> string_of_float, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> pp_float, Easy_format.atom)
| `String(s) => Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
| `List([]) => Easy_format.Atom("[]", Easy_format.atom)
| `Variant(s, _opt) => Easy_format.Atom(s, Easy_format.atom)
Expand Down Expand Up @@ -123,7 +130,7 @@ module NoColor = {
| `Bool(b) => Easy_format.Atom(string_of_bool(b), Easy_format.atom)
| `Int(i) => Easy_format.Atom(i |> string_of_int, Easy_format.atom)
| `Intlit(s) => Easy_format.Atom(s, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> string_of_float, Easy_format.atom)
| `Float(f) => Easy_format.Atom(f |> pp_float, Easy_format.atom)
| `String(s) => Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
| `Variant(s, _opt) =>
Easy_format.Atom(encode(s) |> quotes, Easy_format.atom)
Expand Down
17 changes: 9 additions & 8 deletions source/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ let provider =
(token, start, stop);
};

let parse = (input: string, ~debug: bool): result(expression, string) => {
let parse = (input: string, ~debug, ~verbose): result(expression, string) => {
let buf = Sedlexing.Utf8.from_string(input);
let lexer = () => provider(~debug, buf);

if (debug) {
print_endline("");
};

try(Ok(menhir(lexer))) {
| exn =>
switch (menhir(lexer)) {
| ast =>
if (verbose) {
print_endline(show_expression(ast));
};
Ok(ast);
| exception _exn =>
let Location.{loc_start, loc_end, _} = last_position^;
Error(make(~input, ~start=loc_start, ~end_=loc_end, exn));
Error(make(~input, ~start=loc_start, ~end_=loc_end));
};
};
58 changes: 31 additions & 27 deletions source/Parser.mly
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
open Ast;;
open Console.Errors;;
open Ast
open Console.Errors
%}

%token <string> STRING
Expand Down Expand Up @@ -86,20 +86,20 @@ term:
| RECURSE;
{ Recurse }
| s = STRING;
{ Literal(String(s)) }
{ Literal (String s) }
| n = NUMBER;
{ Literal(Number(n)) }
{ Literal (Number n) }
| b = BOOL;
{ Literal(Bool(b)) }
{ Literal (Bool b) }
| NULL
{ Literal(Null) }
{ Literal Null }
| f = FUNCTION; from = NUMBER; SEMICOLON; upto = NUMBER; CLOSE_PARENT;
{ match f with
| "range" -> Range(int_of_float(from), int_of_float(upto))
| _ -> failwith(f ^ " is not a valid function")
| _ -> Error(f ^ " is not a valid function")
}
| f = FUNCTION; CLOSE_PARENT;
{ failwith(f ^ "(), should contain a body") }
{ Error(f ^ "(), should contain a body") }
| f = FUNCTION; cb = expr; CLOSE_PARENT;
{ match f with
| "filter" -> Map(Select(cb)) (* for backward compatibility *)
Expand All @@ -124,17 +124,17 @@ term:
| "split" -> Split(cb)
| "join" -> Join(cb)
| "contains" -> Contains(cb)
| "startswith" -> failwith(renamed f "starts_with")
| "endswith" -> failwith(renamed f "ends_with")
| _ -> failwith(missing f)
| "startswith" -> Error(renamed f "starts_with")
| "endswith" -> Error(renamed f "ends_with")
| _ -> Error(missing f)
}
| f = IDENTIFIER;
{ match f with
| "empty" -> Empty
| "if" -> failwith(notImplemented f)
| "then" -> failwith(notImplemented f)
| "else" -> failwith(notImplemented f)
| "break" -> failwith(notImplemented f)
| "if" -> Error(notImplemented f)
| "then" -> Error(notImplemented f)
| "else" -> Error(notImplemented f)
| "break" -> Error(notImplemented f)
| "keys" -> Keys
| "flatten" -> Flatten
| "head" -> Head
Expand Down Expand Up @@ -164,21 +164,25 @@ term:
| "nan" -> Nan
| "is_nan" -> IsNan
| "not" -> Not
| "isnan" -> failwith(renamed f "is_nan")
| "reduce" -> failwith(renamed f "reduce()")
| "tonumber" -> failwith(renamed f "to_number")
| "isinfinite" -> failwith(renamed f "is_infinite")
| "isfinite" -> failwith(renamed f "is_finite")
| "isnormal" -> failwith(renamed f "is_normal")
| "tostring" -> failwith(renamed f "to_string")
| _ -> failwith(missing f)
| "isnan" -> Error(renamed f "is_nan")
| "reduce" -> Error(renamed f "reduce()")
| "tonumber" -> Error(renamed f "to_number")
| "isinfinite" -> Error(renamed f "is_infinite")
| "isfinite" -> Error(renamed f "is_finite")
| "isnormal" -> Error(renamed f "is_normal")
| "tostring" -> Error(renamed f "to_string")
| _ -> Error(missing f)
}
| OPEN_BRACKET; CLOSE_BRACKET;
{ List(Empty) }

/* | OPEN_BRACKET; CLOSE_BRACKET;
{ List(Empty) } */

| OPEN_BRACKET; e = expr; CLOSE_BRACKET;
{ List(e) }
| OPEN_PARENT; e = expr; CLOSE_PARENT;
{ e }

/* | OPEN_PARENT; e = expr; CLOSE_PARENT;
{ e } */

| e = term; OPEN_BRACKET; i = NUMBER; CLOSE_BRACKET
{ Pipe(e, Index(int_of_float(i))) }

Expand Down
2 changes: 1 addition & 1 deletion source/Tokenizer.re
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ let rec tokenize = buf => {
Ok(NUMBER(num));
| space => tokenize(buf)
| any => Error("Unexpected character '" ++ lexeme(buf) ++ "'")
| _ => Error("Unexpected character")
| _ => Error("Unexpected character '" ++ lexeme(buf) ++ "'")
};
};
Loading

0 comments on commit 318fdda

Please sign in to comment.