Skip to content

Commit

Permalink
Fix parsing record fields with .piq-positional
Browse files Browse the repository at this point in the history
See allow_omit_label_invalid.piq for details
  • Loading branch information
alavrik committed Sep 27, 2019
1 parent 79f2ebd commit ea41b08
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
13 changes: 10 additions & 3 deletions piqilib/piqobj_of_piq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,17 @@ and parse_option_by_type ~try_mode o x =
| _ ->
None
)
| _, Some t ->
(* TODO: do not support this behavior by default, only when
* piq-allow-omit-label is specified for options
*
* TODO: unify .piq-allow-omit-label and .piq-positional
*)
| None, Some t when o.piq_alias = None ->
let do_parse () =
let obj = Some (parse_obj t x ?piq_format:o.piq_format) in
Some O.({t = o; obj = obj})
in
match C.unalias t, x with
(match C.unalias t, x with
| `bool, `bool _

| `int, `int _
Expand Down Expand Up @@ -693,7 +698,9 @@ and parse_option_by_type ~try_mode o x =
| `binary, `string (s, _) when Piq_lexer.is_ascii_string s -> do_parse ()
| _ ->
None

)
| _, Some _ -> (* either name or piq_alias are defined *)
None


and parse_name_option o name =
Expand Down
3 changes: 3 additions & 0 deletions tests/piq/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ test:
$(PIQI) check --type options/v options_invalid.piq; test $$? -eq 1
$(PIQI) check --type options/v options_valid.piq

$(PIQI) check allow_omit_label_invalid.piq; test $$? -eq 1
$(PIQI) check allow_omit_label_valid.piq


clean:
rm -f *.pib *.json
Expand Down
27 changes: 27 additions & 0 deletions tests/piq/allow_omit_label_invalid.piq
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:piqi [
.module m

.record [
.name r

.field [
.name f
.type v
.piq-positional true
]
]

.variant [
.name v

.option [
.piq-alias oo
.type string
]
]
]

:m/r [
% this is invalid, because there is .piq-alias in option definition
"foo"
]
50 changes: 50 additions & 0 deletions tests/piq/allow_omit_label_valid.piq
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:piqi [
.module m

.record [
.name r

.field [
.name f
.type v
.piq-positional true
]
]

.record [
.name rr

.field [
.name f
.type vv
.piq-positional true
]
]

.variant [
.name v

.option [
.piq-alias oo
.type string
]
]

.variant [
.name vv

.option [
.type string
]
]
]

:m/r [
% this is valid, because of .piq-positional
.oo "foo"
]

:m/rr [
% this is valid, because of a) .piq-positional and b) there is no .name nor .piq-alias in option definition
"foo"
]

0 comments on commit ea41b08

Please sign in to comment.