Allow all kinds of string literals as object field names

This commit is contained in:
Stanisław Barzowski 2017-09-29 17:45:25 -04:00 committed by Dave Cunningham
parent 80ce6ac112
commit 33377907ec
3 changed files with 68 additions and 55 deletions

View File

@ -372,7 +372,8 @@ func (p *parser) parseObjectRemainder(tok *token) (ast.Node, *token, error) {
first = false
switch next.kind {
case tokenBracketL, tokenIdentifier, tokenStringDouble, tokenStringSingle, tokenStringBlock:
case tokenBracketL, tokenIdentifier, tokenStringDouble, tokenStringSingle,
tokenStringBlock, tokenVerbatimStringDouble, tokenVerbatimStringSingle:
var kind ast.ObjectFieldKind
var expr1 ast.Node
var id *ast.Identifier
@ -380,30 +381,10 @@ func (p *parser) parseObjectRemainder(tok *token) (ast.Node, *token, error) {
case tokenIdentifier:
kind = ast.ObjectFieldID
id = (*ast.Identifier)(&next.data)
case tokenStringDouble:
case tokenStringDouble, tokenStringSingle,
tokenStringBlock, tokenVerbatimStringDouble, tokenVerbatimStringSingle:
kind = ast.ObjectFieldStr
expr1 = &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(next.loc),
Value: next.data,
Kind: ast.StringDouble,
}
case tokenStringSingle:
kind = ast.ObjectFieldStr
expr1 = &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(next.loc),
Value: next.data,
Kind: ast.StringSingle,
}
case tokenStringBlock:
kind = ast.ObjectFieldStr
expr1 = &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(next.loc),
Value: next.data,
Kind: ast.StringBlock,
BlockIndent: next.stringBlockIndent,
}
// TODO(sbarzowski) are verbatim string literals allowed here?
// if so, maybe it's time we extracted string literal creation somewhere...
expr1 = tokenStringToAst(next)
default:
kind = ast.ObjectFieldExpr
var err error
@ -680,6 +661,44 @@ func (p *parser) parseArray(tok *token) (ast.Node, error) {
}, nil
}
func tokenStringToAst(tok *token) *ast.LiteralString {
switch tok.kind {
case tokenStringSingle:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringSingle,
}
case tokenStringDouble:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringDouble,
}
case tokenStringBlock:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringBlock,
BlockIndent: tok.stringBlockIndent,
}
case tokenVerbatimStringDouble:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.VerbatimStringDouble,
}
case tokenVerbatimStringSingle:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.VerbatimStringSingle,
}
default:
panic(fmt.Sprintf("Not a string token %#+v", tok))
}
}
func (p *parser) parseTerminal() (ast.Node, error) {
tok := p.pop()
switch tok.kind {
@ -722,37 +741,9 @@ func (p *parser) parseTerminal() (ast.Node, error) {
Value: num,
OriginalString: tok.data,
}, nil
case tokenStringSingle:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringSingle,
}, nil
case tokenStringDouble:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringDouble,
}, nil
case tokenStringBlock:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.StringBlock,
BlockIndent: tok.stringBlockIndent,
}, nil
case tokenVerbatimStringDouble:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.VerbatimStringDouble,
}, nil
case tokenVerbatimStringSingle:
return &ast.LiteralString{
NodeBase: ast.NewNodeBaseLoc(tok.loc),
Value: tok.data,
Kind: ast.VerbatimStringSingle,
}, nil
case tokenStringDouble, tokenStringSingle,
tokenStringBlock, tokenVerbatimStringDouble, tokenVerbatimStringSingle:
return tokenStringToAst(tok), nil
case tokenFalse:
return &ast.LiteralBoolean{
NodeBase: ast.NewNodeBaseLoc(tok.loc),

View File

@ -0,0 +1,9 @@
{
"expr, as complex as you want": true,
"identifier": true,
"str_block \\n\n": true,
"str_double ' \n": true,
"str_single \" \n": true,
"str_verbatim_double \" '' \\n": true,
"str_verbatim_single \"\" ' \\n": true
}

View File

@ -0,0 +1,13 @@
{
identifier: true,
"str_double ' \n": true,
'str_single " \n': true,
|||
str_block \n
|||: true,
@"str_verbatim_double "" '' \n": true,
@'str_verbatim_single "" '' \n': true,
["expr, " + "as complex as you want"]: true,
local local_field = true,
assert true
}