hcl/json/parse.y

174 lines
2.1 KiB
Plaintext
Raw Normal View History

2014-08-02 18:38:41 +00:00
// This is the yacc input for creating the parser for HCL JSON.
%{
package json
import (
"fmt"
"strconv"
2014-08-11 23:23:28 +00:00
"github.com/hashicorp/hcl/hcl"
2014-08-02 18:38:41 +00:00
)
%}
%union {
num int
str string
2014-08-11 23:23:28 +00:00
obj *hcl.Object
objlist []*hcl.Object
2014-08-02 18:38:41 +00:00
}
%type <num> int
2014-08-11 23:23:28 +00:00
%type <obj> number object pair value
%type <objlist> array elements members
%type <str> frac
2014-08-02 18:38:41 +00:00
%token <num> NUMBER
%token <str> COLON COMMA IDENTIFIER EQUAL NEWLINE STRING
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET
%token <str> TRUE FALSE NULL MINUS PERIOD
2014-08-02 18:38:41 +00:00
%%
top:
object
{
2014-08-11 23:23:28 +00:00
jsonResult = $1
2014-08-02 18:38:41 +00:00
}
object:
LEFTBRACE members RIGHTBRACE
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeObject,
2014-08-12 03:58:20 +00:00
Value: hcl.ObjectList($2).Flat(),
2014-08-11 23:23:28 +00:00
}
2014-08-02 18:38:41 +00:00
}
| LEFTBRACE RIGHTBRACE
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{Type: hcl.ValueTypeObject}
2014-08-02 18:38:41 +00:00
}
members:
pair
{
2014-08-11 23:23:28 +00:00
$$ = []*hcl.Object{$1}
2014-08-02 18:38:41 +00:00
}
2014-08-05 04:53:39 +00:00
| members COMMA pair
2014-08-02 18:38:41 +00:00
{
2014-08-05 04:53:39 +00:00
$$ = append($1, $3)
2014-08-02 18:38:41 +00:00
}
pair:
STRING COLON value
{
2014-08-11 23:23:28 +00:00
$3.Key = $1
$$ = $3
2014-08-02 18:38:41 +00:00
}
value:
STRING
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeString,
2014-08-02 18:38:41 +00:00
Value: $1,
}
}
| number
2014-08-02 18:38:41 +00:00
{
$$ = $1
2014-08-02 18:38:41 +00:00
}
| object
{
$$ = $1
}
| array
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeList,
Value: $1,
}
2014-08-02 18:38:41 +00:00
}
| TRUE
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeBool,
2014-08-02 18:38:41 +00:00
Value: true,
}
}
| FALSE
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeBool,
2014-08-02 18:38:41 +00:00
Value: false,
}
}
| NULL
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeNil,
2014-08-02 18:38:41 +00:00
Value: nil,
}
}
array:
LEFTBRACKET RIGHTBRACKET
{
2014-08-11 23:23:28 +00:00
$$ = nil
2014-08-02 18:38:41 +00:00
}
| LEFTBRACKET elements RIGHTBRACKET
{
2014-08-11 23:23:28 +00:00
$$ = $2
2014-08-02 18:38:41 +00:00
}
elements:
value
{
2014-08-11 23:23:28 +00:00
$$ = []*hcl.Object{$1}
2014-08-02 18:38:41 +00:00
}
2014-08-05 04:53:39 +00:00
| elements COMMA value
2014-08-02 18:38:41 +00:00
{
2014-08-05 04:53:39 +00:00
$$ = append($1, $3)
2014-08-02 18:38:41 +00:00
}
number:
int
{
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeInt,
Value: $1,
}
}
| int frac
{
fs := fmt.Sprintf("%d.%s", $1, $2)
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
panic(err)
}
2014-08-11 23:23:28 +00:00
$$ = &hcl.Object{
Type: hcl.ValueTypeFloat,
Value: f,
}
}
int:
MINUS int
{
$$ = $2 * -1
}
| NUMBER
{
$$ = $1
}
frac:
PERIOD NUMBER
{
$$ = strconv.FormatInt(int64($2), 10)
}
2014-08-02 18:38:41 +00:00
%%