diff --git a/go/src/miller/dsl/ast.go b/go/src/miller/dsl/ast.go index 83e691eba..6445fe865 100644 --- a/go/src/miller/dsl/ast.go +++ b/go/src/miller/dsl/ast.go @@ -3,6 +3,7 @@ package dsl import ( "errors" "fmt" + "strings" "miller/parsing/token" ) @@ -127,16 +128,20 @@ func NewASTNodeStripDollarPlease(itok interface{}, nodeType TNodeType) (*ASTNode return NewASTNodeNestable(newToken, nodeType), nil } -// Likewise for the leading/trailing double quotes on string literals. +// Likewise for the leading/trailing double quotes on string literals. Also, +// since string literals can have backslash-escaped double-quotes like +// "...\"...\"...", we also unbackslash here. func NewASTNodeStripDoubleQuotePairPlease( itok interface{}, nodeType TNodeType, ) (*ASTNode, error) { oldToken := itok.(*token.Token) n := len(oldToken.Lit) + contents := string(oldToken.Lit[1 : n-1]) + contents = strings.ReplaceAll(contents, "\\\"", "\"") newToken := &token.Token{ Type: oldToken.Type, - Lit: oldToken.Lit[1 : n-1], + Lit: []byte(contents), Pos: oldToken.Pos, } return NewASTNodeNestable(newToken, nodeType), nil diff --git a/go/src/miller/parsing/mlr.bnf b/go/src/miller/parsing/mlr.bnf index 85ce26197..5cd8534c3 100644 --- a/go/src/miller/parsing/mlr.bnf +++ b/go/src/miller/parsing/mlr.bnf @@ -662,7 +662,9 @@ MlrvalOrFunction // As with '$' on md_token_field_name, so too for md_token_string_literal we // get LR-1 conflicts if we attempt to put the double quotes here. Hence the -// quote-stripper AST method. +// quote-stripper AST method. Also, since string literals can have +// backslash-escaped double-quotes like "...\"...\"...", we also unbackslash +// in the same method. MlrvalOrFunction : md_token_string_literal << dsl.NewASTNodeStripDoubleQuotePairPlease( $0, diff --git a/go/u/try-cst b/go/u/try-cst index a82cad8a5..4275d0996 100755 --- a/go/u/try-cst +++ b/go/u/try-cst @@ -96,6 +96,8 @@ echo; run_mlr --from u/s.dkvp --idkvp --opprint put '$z = true || %%%panic%%%' echo; run_mlr --from u/s.dkvp --idkvp --opprint put '$z = true ? 4 : %%%panic%%%' echo; run_mlr --from u/s.dkvp --idkvp --opprint put '$z = false ? %%%panic%%% : 5' +echo; run_mlr --from u/s.dkvp --idkvp --opprint put '$z = "abc\"def\"ghi"' + echo; run_mlr --from u/s.dkvp --idkvp --opprint put -v '$i += 2' echo; run_mlr --from u/s.dkvp --idkvp --opprint put -v '$i *= 2' echo; run_mlr --from u/s.dkvp --idkvp --opprint put -v '$i /= 2' diff --git a/go/u/try-cst.out b/go/u/try-cst.out index 3a965dbae..f5c6bce87 100644 --- a/go/u/try-cst.out +++ b/go/u/try-cst.out @@ -774,6 +774,15 @@ wye wye 3 0.20460330576630303 0.33831852551664776 5 eks wye 4 0.38139939387114097 0.13418874328430463 5 +---------------------------------------------------------------- +mlr --from u/s.dkvp --idkvp --opprint put $z = "abc\"def\"ghi" +a b i x y z +pan pan 1 0.3467901443380824 0.7268028627434533 abc"def"ghi +eks pan 2 0.7586799647899636 0.5221511083334797 abc"def"ghi +wye wye 3 0.20460330576630303 0.33831852551664776 abc"def"ghi +eks wye 4 0.38139939387114097 0.13418874328430463 abc"def"ghi + + ---------------------------------------------------------------- mlr --from u/s.dkvp --idkvp --opprint put -v $i += 2 DSL EXPRESSION: