allow backslash-escaped double quotes in string literals, part 2

This commit is contained in:
John Kerl 2020-09-08 08:52:41 -04:00
parent 5c067dab82
commit 83e6dfd708
4 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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,

View file

@ -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'

View file

@ -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: