broke lists and objects, made strings nodes

fixing an old flaw in the design, namely that assignment nodes have
values that are just interface{} instead of actual parse nodes
master
Jordan Orelli 10 years ago
parent 41cfb120dc
commit 9c317ec358

@ -143,7 +143,7 @@ func (n *commentNode) eval(ctx map[string]interface{}) (interface{}, error) {
type assignmentNode struct {
name string
value interface{}
value node
}
func (n *assignmentNode) Type() nodeType {
@ -177,7 +177,9 @@ func (n *assignmentNode) String() string {
func (n *assignmentNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%sassign{\n", prefix)
fmt.Fprintf(w, "%s%sname: %s\n", prefix, indent, n.name)
fmt.Fprintf(w, "%s%svalue: %v\n", prefix, indent, n.value)
if err := n.value.pretty(w, fmt.Sprintf("%s%svalue: ", prefix, indent)); err != nil {
return err
}
fmt.Fprintf(w, "%s}\n", prefix)
return nil
}
@ -192,16 +194,21 @@ func (n *assignmentNode) eval(ctx map[string]interface{}) (interface{}, error) {
type stringNode string
func (s stringNode) Type() nodeType {
func (s *stringNode) Type() nodeType {
return n_string
}
func (s *stringNode) parse(p *parser) error {
t := p.next()
if t.t != t_string {
return fmt.Errorf("unexpected %s while looking for string token", t.t)
}
*s = stringNode(t.s)
return nil
}
func (s *stringNode) pretty(w io.Writer, prefix string) error {
_, err := fmt.Fprintf(w, "%s%s", prefix, string(*s))
_, err := fmt.Fprintf(w, "%s%s\n", prefix, string(*s))
return err
}

@ -73,23 +73,27 @@ func (p *parser) ensureNext(tt tokenType, context string) error {
// parse the next value. This is to be executed in a context where we know we
// want something that is a value to come next, such as after an equals sign.
func (p *parser) parseValue() (interface{}, error) {
func (p *parser) parseValue() (node, error) {
for {
t := p.next()
t := p.peek()
switch t.t {
case t_error:
return nil, fmt.Errorf("parse error: saw lex error when looking for value: %v", t.s)
case t_eof:
return nil, fmt.Errorf("parse error: unexpected eof when looking for value")
case t_string:
return t.s, nil
case t_real_number, t_imaginary_number:
p.unread(t)
return p.number()
case t_list_start:
return p.parseList(make(list, 0, 4))
case t_object_start:
return p.parseObject(make(object))
n := new(stringNode)
if err := n.parse(p); err != nil {
return nil, err
}
return n, nil
// case t_real_number, t_imaginary_number:
// p.unread(t)
// return p.number()
// case t_list_start:
// return p.parseList(make(list, 0, 4))
// case t_object_start:
// return p.parseObject(make(object))
default:
return nil, fmt.Errorf("parse error: unexpected %v token while looking for value", t.t)
}

Loading…
Cancel
Save