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

View File

@ -24,6 +24,20 @@ func TestLex(t *testing.T) {
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",
[]int{

View File

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

View File

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

View File

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