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 { type assignmentNode struct {
name string name string
value interface{} value node
} }
func (n *assignmentNode) Type() nodeType { func (n *assignmentNode) Type() nodeType {
@ -177,7 +177,9 @@ func (n *assignmentNode) String() string {
func (n *assignmentNode) pretty(w io.Writer, prefix string) error { func (n *assignmentNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%sassign{\n", prefix) fmt.Fprintf(w, "%sassign{\n", prefix)
fmt.Fprintf(w, "%s%sname: %s\n", prefix, indent, n.name) 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) fmt.Fprintf(w, "%s}\n", prefix)
return nil return nil
} }
@ -192,16 +194,21 @@ func (n *assignmentNode) eval(ctx map[string]interface{}) (interface{}, error) {
type stringNode string type stringNode string
func (s stringNode) Type() nodeType { func (s *stringNode) Type() nodeType {
return n_string return n_string
} }
func (s *stringNode) parse(p *parser) error { 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 return nil
} }
func (s *stringNode) pretty(w io.Writer, prefix string) error { 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 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 // 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. // 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 { for {
t := p.next() t := p.peek()
switch t.t { switch t.t {
case t_error: case t_error:
return nil, fmt.Errorf("parse error: saw lex error when looking for value: %v", t.s) return nil, fmt.Errorf("parse error: saw lex error when looking for value: %v", t.s)
case t_eof: case t_eof:
return nil, fmt.Errorf("parse error: unexpected eof when looking for value") return nil, fmt.Errorf("parse error: unexpected eof when looking for value")
case t_string: case t_string:
return t.s, nil n := new(stringNode)
case t_real_number, t_imaginary_number: if err := n.parse(p); err != nil {
p.unread(t) return nil, err
return p.number() }
case t_list_start: return n, nil
return p.parseList(make(list, 0, 4)) // case t_real_number, t_imaginary_number:
case t_object_start: // p.unread(t)
return p.parseObject(make(object)) // return p.number()
// case t_list_start:
// return p.parseList(make(list, 0, 4))
// case t_object_start:
// return p.parseObject(make(object))
default: default:
return nil, fmt.Errorf("parse error: unexpected %v token while looking for value", t.t) return nil, fmt.Errorf("parse error: unexpected %v token while looking for value", t.t)
} }

Loading…
Cancel
Save