This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from jonludlam/better-parser
Better parser
- Loading branch information
Showing
11 changed files
with
437 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# OASIS_START | ||
# DO NOT EDIT (digest: 64a1fd59a37fc080f5125673cfa49c01) | ||
# DO NOT EDIT (digest: 621d71e2d7947006d1c33a3bc5856582) | ||
Bootvar | ||
Parse_argv | ||
# OASIS_STOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# OASIS_START | ||
# DO NOT EDIT (digest: 64a1fd59a37fc080f5125673cfa49c01) | ||
# DO NOT EDIT (digest: 621d71e2d7947006d1c33a3bc5856582) | ||
Bootvar | ||
Parse_argv | ||
# OASIS_STOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(* | ||
* Copyright (c) 2016 Citrix Systems Inc | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
open Astring | ||
|
||
(* Split string into whitespace-separated substrings, | ||
taking into account quoting *) | ||
|
||
let parse s = | ||
let skip_white s = String.Sub.drop | ||
~max:Sys.max_string_length | ||
~sat:Char.Ascii.is_white s in | ||
|
||
let split s = | ||
let rec inner in_quoted s so_far acc = | ||
let is_data = function | ||
| '\\' -> false | ||
| '"' -> false | ||
| c when Char.Ascii.is_white c -> in_quoted | ||
| _ -> true in | ||
|
||
let data,rem = String.Sub.span | ||
~sat:is_data | ||
~max:Sys.max_string_length s in | ||
|
||
match String.Sub.head rem with | ||
| Some c when Char.Ascii.is_white c -> | ||
let so_far = List.rev (data :: so_far) in | ||
inner in_quoted (skip_white rem) [] ((String.Sub.concat so_far)::acc) | ||
| Some '"' -> | ||
let so_far = data :: so_far in | ||
inner (not in_quoted) (String.Sub.tail rem) so_far acc | ||
| Some '\\' -> | ||
let rem = String.Sub.tail rem in | ||
begin match String.Sub.head rem with | ||
| Some c -> | ||
let so_far' = String.(sub (of_char c)) :: data :: so_far in | ||
inner in_quoted (String.Sub.tail rem) so_far' acc | ||
| None -> | ||
`Error "Invalid escaping at end of string" | ||
end | ||
| Some c -> | ||
let e = Printf.sprintf "Something went wrong in the argv parser: Matched '%c'" c in | ||
`Error e | ||
| None -> | ||
let so_far = List.rev (data :: so_far) in | ||
`Ok (List.map (String.Sub.to_string) (List.rev ((String.Sub.concat so_far) :: acc))) | ||
in | ||
inner false s [] [] | ||
in | ||
match split (String.sub s |> skip_white) with | ||
| `Ok s -> `Ok (List.filter (fun s -> String.length s > 0) s) | ||
| `Error _ as e -> e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
(* | ||
* Copyright (c) 2016 Citrix Systems Inc | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
* | ||
*) | ||
|
||
open OUnit | ||
|
||
let verbose = ref false | ||
|
||
let log = Printf.ksprintf (fun s -> if !verbose then Printf.fprintf stdout "%s\n%!" s) | ||
|
||
let tests = | ||
[ "foo bar baz", `Ok ["foo"; "bar"; "baz"]; | ||
"foo \"bar\" baz", `Ok ["foo"; "bar"; "baz"]; | ||
"f\\\ oo b\\\"r baz", `Ok ["f oo"; "b\"r"; "baz"]; | ||
"foo bar\"bie\"boo baz", `Ok ["foo"; "barbieboo"; "baz"]; | ||
" ", `Ok [] | ||
] | ||
|
||
let test_parse () = | ||
List.iter (fun (input, expected) -> | ||
log "checking '%s'" input; | ||
let result = Parse_argv.parse input in | ||
if result <> expected | ||
then begin | ||
let tostr l = | ||
match l with | ||
| `Ok list -> | ||
String.concat "," | ||
(List.map (fun x -> Printf.sprintf "'%s'" x) list) | ||
| `Error e -> Printf.sprintf "Error: %s" e | ||
in | ||
Printf.fprintf stderr "Error - failed to parse. Got:\n%s\nExpected\n%s\n" | ||
(tostr result) (tostr expected) | ||
end; | ||
assert (result=expected)) tests | ||
|
||
let negative_test () = | ||
let str = " \\" in | ||
log "Testing parse of %s (should fail)" str; | ||
match Parse_argv.parse str with | ||
| `Ok x -> assert false | ||
| `Error _ -> () | ||
|
||
let _ = | ||
Arg.parse [ | ||
"-verbose", Arg.Unit (fun _ -> verbose := true), "Run in verbose mode"; | ||
] (fun x -> Printf.fprintf stderr "Ignoring argument: %s" x) | ||
"Test argv parser"; | ||
|
||
let suite = "parser" >::: [ | ||
"test parse" >:: test_parse; | ||
"negative test" >:: negative_test; | ||
] in | ||
run_test_tt ~verbose:!verbose suite | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.