Disallow objects in lists

This commit is contained in:
Mitchell Hashimoto 2014-08-01 11:18:47 -07:00
parent 1150448982
commit 038acdae90
5 changed files with 32 additions and 21 deletions

15
lex.go
View File

@ -3,7 +3,6 @@ package hcl
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
"strconv" "strconv"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -56,6 +55,8 @@ func (x *hclLex) Lex(yylval *hclSymType) int {
} }
switch c { switch c {
case ':':
return COLON
case ',': case ',':
return COMMA return COMMA
case '=': case '=':
@ -143,20 +144,12 @@ func (x *hclLex) lexId(yylval *hclSymType) int {
break break
} }
// If this isn't a character we want in an ID, return out. if unicode.IsSpace(c) {
// One day we should make this a regexp.
if c != '_' &&
c != '-' &&
c != '.' &&
c != '*' &&
!unicode.IsLetter(c) &&
!unicode.IsNumber(c) {
x.backup() x.backup()
break break
} }
if _, err := b.WriteRune(c); err != nil { if _, err := b.WriteRune(c); err != nil {
log.Printf("ERR: %s", err)
return lexEOF return lexEOF
} }
} }
@ -211,7 +204,6 @@ func (x *hclLex) lexString(yylval *hclSymType) int {
} }
if _, err := b.WriteRune(c); err != nil { if _, err := b.WriteRune(c); err != nil {
log.Printf("ERR: %s", err)
return lexEOF return lexEOF
} }
} }
@ -259,7 +251,6 @@ func (x *hclLex) backup() {
// createErr records the given error // createErr records the given error
func (x *hclLex) createErr(msg string) { func (x *hclLex) createErr(msg string) {
x.err = fmt.Errorf("Line %d, column %d: %s", x.line, x.col, msg) x.err = fmt.Errorf("Line %d, column %d: %s", x.line, x.col, msg)
log.Printf("parse error: %s", x.err)
} }
// The parser calls this method on a parse error. // The parser calls this method on a parse error.

View File

@ -24,6 +24,20 @@ func TestLex(t *testing.T) {
lexEOF, lexEOF,
}, },
}, },
{
"assign_colon.hcl",
[]int{
IDENTIFIER, EQUAL, LEFTBRACKET, LEFTBRACE,
STRING, COLON, LEFTBRACE,
STRING, COLON, LEFTBRACE, RIGHTBRACE, COMMA,
STRING, COLON, LEFTBRACKET,
NUMBER, COMMA, NUMBER, COMMA, STRING,
RIGHTBRACKET, COMMA,
RIGHTBRACE,
RIGHTBRACE, RIGHTBRACKET,
lexEOF,
},
},
{ {
"list.hcl", "list.hcl",
[]int{ []int{

View File

@ -19,7 +19,7 @@ package hcl
%type <str> blockId %type <str> blockId
%token <num> NUMBER %token <num> NUMBER
%token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING %token <str> COLON COMMA IDENTIFIER EQUAL NEWLINE STRING
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET %token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET
%% %%
@ -128,11 +128,7 @@ list:
} }
listitem: listitem:
object NUMBER
{
$$ = $1
}
| NUMBER
{ {
$$ = LiteralNode{ $$ = LiteralNode{
Type: ValueTypeInt, Type: ValueTypeInt,

View File

@ -11,6 +11,10 @@ func TestParse(t *testing.T) {
Name string Name string
Err bool Err bool
}{ }{
{
"assign_colon.hcl",
true,
},
{ {
"comment.hcl", "comment.hcl",
false, false,
@ -32,12 +36,12 @@ func TestParse(t *testing.T) {
false, false,
}, },
{ {
"assign_deep.hcl", "complex.hcl",
false, false,
}, },
{ {
"complex.hcl", "assign_deep.hcl",
false, true,
}, },
} }

View File

@ -0,0 +1,6 @@
resource = [{
"foo": {
"bar": {},
"baz": [1, 2, "foo"],
}
}]