From b8924e6beccc8e2a7fdf9f3d229f9ef40b98c1e5 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Tue, 24 Nov 2020 08:22:33 -0500 Subject: [PATCH] no-filter-in-filter validation from C to Go --- go/cases-to-do.txt | 3 ++- go/reg-test/cases/case-no-filter-in-filter.sh | 2 ++ .../expected/case-no-filter-in-filter.sh.out | 7 +++++++ go/src/miller/dsl/cst/validate.go | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 go/reg-test/cases/case-no-filter-in-filter.sh create mode 100644 go/reg-test/expected/case-no-filter-in-filter.sh.out diff --git a/go/cases-to-do.txt b/go/cases-to-do.txt index 66a174e69..2a76138c6 100644 --- a/go/cases-to-do.txt +++ b/go/cases-to-do.txt @@ -2,7 +2,8 @@ BUG: rrv ./reg-test/cases/case-c-dsl-context-specific-validation.sh -* subr and more ... also needs check for filter statement within filter commands +* also needs check for filter statement within filter commands +* subr and more rrv ./reg-test/cases/case-c-dsl-forbind-typedecl.sh rrv ./reg-test/cases/case-c-dsl-localvar-typedecl.sh diff --git a/go/reg-test/cases/case-no-filter-in-filter.sh b/go/reg-test/cases/case-no-filter-in-filter.sh new file mode 100644 index 000000000..38b1e4243 --- /dev/null +++ b/go/reg-test/cases/case-no-filter-in-filter.sh @@ -0,0 +1,2 @@ +run_mlr put 'filter NR > 2' $indir/s.dkvp +mlr_expect_fail filter 'filter NR > 2' $indir/s.dkvp diff --git a/go/reg-test/expected/case-no-filter-in-filter.sh.out b/go/reg-test/expected/case-no-filter-in-filter.sh.out new file mode 100644 index 000000000..f590c4997 --- /dev/null +++ b/go/reg-test/expected/case-no-filter-in-filter.sh.out @@ -0,0 +1,7 @@ + +mlr put filter NR > 2 ./reg-test/input/s.dkvp +a=wye,b=wye,i=3,x=0.20460330576630303,y=0.33831852551664776 +a=eks,b=wye,i=4,x=0.38139939387114097,y=0.13418874328430463 + +mlr filter filter NR > 2 ./reg-test/input/s.dkvp + diff --git a/go/src/miller/dsl/cst/validate.go b/go/src/miller/dsl/cst/validate.go index 829b6b5c0..111e60826 100644 --- a/go/src/miller/dsl/cst/validate.go +++ b/go/src/miller/dsl/cst/validate.go @@ -39,6 +39,7 @@ func ValidateAST( for _, astChild := range ast.RootNode.Children { return validateASTAux( astChild, + isFilter, atTopLevel, inLoop, inBeginOrEnd, @@ -57,6 +58,7 @@ func ValidateAST( // ---------------------------------------------------------------- func validateASTAux( astNode *dsl.ASTNode, + isFilter bool, atTopLevel bool, inLoop bool, inBeginOrEnd bool, @@ -66,6 +68,7 @@ func validateASTAux( isAssignmentLHS bool, isUnset bool, ) error { + nextIsFilter := isFilter nextAtTopLevel := false nextInLoop := inLoop nextInBeginOrEnd := inBeginOrEnd @@ -74,6 +77,16 @@ func validateASTAux( nextIsAssignmentLHS := isAssignmentLHS nextIsUnset := isUnset + if astNode.Type == dsl.NodeTypeFilterStatement { + if isFilter { + return errors.New( + "Miller: filter expressions must not also contain the \"filter\" keyword.", + ) + } + nextIsFilter = true + nextInUDS = true + } + // Check: begin/end/func/subr must be at top-level if astNode.Type == dsl.NodeTypeBeginBlock { if !atTopLevel { @@ -206,10 +219,6 @@ func validateASTAux( } } - // Check: ENV disallowed at left-hand side of assignment? or nah? - // Needs to be supported for system() ... - // * TODO - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Treewalk @@ -219,6 +228,7 @@ func validateASTAux( nextIsUnset = astNode.Type == dsl.NodeTypeUnset err := validateASTAux( astChild, + nextIsFilter, nextAtTopLevel, nextInLoop, nextInBeginOrEnd,