update parse format

master
Jordan Orelli 10 years ago
parent 88c013b2bc
commit 2dc6b618f0

@ -85,13 +85,12 @@ func (n *rootNode) String() string {
} }
func (n *rootNode) pretty(w io.Writer, prefix string) error { func (n *rootNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%sroot{\n", prefix) fmt.Fprintf(w, "%sroot:\n", prefix)
for _, child := range n.children { for _, child := range n.children {
if err := child.pretty(w, prefix+indent); err != nil { if err := child.pretty(w, prefix+indent); err != nil {
return err return err
} }
} }
fmt.Fprintf(w, "%s}\n", prefix)
return nil return nil
} }
@ -121,8 +120,8 @@ func (n *commentNode) String() string {
} }
func (n *commentNode) pretty(w io.Writer, prefix string) error { func (n *commentNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%scomment:\n", prefix)
r := bufio.NewReader(strings.NewReader(n.body)) r := bufio.NewReader(strings.NewReader(n.body))
fmt.Fprintf(w, "%scomment{\n", prefix)
for { for {
line, err := r.ReadString('\n') line, err := r.ReadString('\n')
if err == io.EOF { if err == io.EOF {
@ -136,7 +135,6 @@ func (n *commentNode) pretty(w io.Writer, prefix string) error {
} }
fmt.Fprintf(w, "%s%s%s\n", prefix, indent, line) fmt.Fprintf(w, "%s%s%s\n", prefix, indent, line)
} }
fmt.Fprintf(w, "%s}\n", prefix)
return nil return nil
} }
@ -178,12 +176,13 @@ 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:\n", prefix, indent)
if err := n.value.pretty(w, fmt.Sprintf("%s%svalue: ", prefix, indent)); err != nil { fmt.Fprintf(w, "%s%s%s%s\n", prefix, indent, indent, n.name)
fmt.Fprintf(w, "%s%svalue:\n", prefix, indent)
if err := n.value.pretty(w, prefix+indent+indent); err != nil {
return err return err
} }
fmt.Fprintf(w, "%s}\n", prefix)
return nil return nil
} }
@ -211,7 +210,8 @@ func (s *stringNode) parse(p *parser) error {
} }
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\n", prefix, string(*s)) fmt.Fprintf(w, "%sstring:\n", prefix)
_, err := fmt.Fprintf(w, "%s%s%s\n", prefix, indent, string(*s))
return err return err
} }
@ -275,7 +275,7 @@ func (n *numberNode) pretty(w io.Writer, prefix string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(w, "%s%v\n", prefix, v) fmt.Fprintf(w, "%snumber:\n%s%s%v\n", prefix, prefix, indent, v)
return nil return nil
} }
@ -320,13 +320,12 @@ func (l *listNode) parse(p *parser) error {
} }
func (l *listNode) pretty(w io.Writer, prefix string) error { func (l *listNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%slist{\n", prefix) fmt.Fprintf(w, "%slist:\n", prefix)
for _, n := range *l { for _, n := range *l {
if err := n.pretty(w, prefix+indent); err != nil { if err := n.pretty(w, prefix+indent); err != nil {
return err return err
} }
} }
fmt.Fprintf(w, "%s}\n", prefix)
return nil return nil
} }

@ -1,2 +1 @@
root{ root:
}

@ -1,50 +1,73 @@
root{ root:
assign{ assign:
name: a_string name:
value: a string value a_string
} value:
assign{ string:
name: an_int a string value
value: 1 assign:
} name:
assign{ an_int
name: another_int value:
value: 9 number:
} 1
assign{ assign:
name: moar_int name:
value: -12 another_int
} value:
assign{ number:
name: a_float 9
value: 1 assign:
} name:
assign{ moar_int
name: another_float value:
value: -0.9 number:
} -12
assign{ assign:
name: extra_floaty name:
value: 1.2 a_float
} value:
assign{ number:
name: complex1 1
value: (1+1i) assign:
} name:
assign{ another_float
name: complex2 value:
value: (1+0i) number:
} -0.9
assign{ assign:
name: complex3 name:
value: (1.3+4.7i) extra_floaty
} value:
assign{ number:
name: complex4 1.2
value: (-4.2+8.9i) assign:
} name:
assign{ complex1
name: complex5 value:
value: (4.2-8.9i) number:
} (1+1i)
} assign:
name:
complex2
value:
number:
(1+0i)
assign:
name:
complex3
value:
number:
(1.3+4.7i)
assign:
name:
complex4
value:
number:
(-4.2+8.9i)
assign:
name:
complex5
value:
number:
(4.2-8.9i)

@ -1,10 +1,12 @@
root{ root:
assign{ assign:
name: items name:
value: list{ items
value: 1 value:
value: 2 list:
value: 3 number:
value: } 1
} number:
} 2
number:
3

Loading…
Cancel
Save