Example #1
0
File: parse.go Project: albrow/calc
// E'2 -> "(" E ")"
func ep2(buf *token.Buffer, tree ast.Node) (newTree ast.Node, err error) {
	origPos := buf.Pos()
	defer func() {
		if err != nil {
			buf.MustSeek(origPos)
		}
	}()
	newTree = tree.Copy()
	expr := ast.New()
	newTree.AddChild(expr)
	if err := termOpenParen(buf); err != nil {
		return nil, err
	}
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	eTree, err := e(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	expr.AddChildren(eTree.Children())
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	if err := termCloseParen(buf); err != nil {
		return nil, err
	}
	return newTree, nil
}
Example #2
0
File: parse.go Project: albrow/calc
// E1 -> E' Op E
func e1(buf *token.Buffer, tree ast.Node) (newTree ast.Node, err error) {
	origPos := buf.Pos()
	defer func() {
		if err != nil {
			buf.MustSeek(origPos)
		}
	}()
	newTree = tree.Copy()
	epTree, err := ep(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	newTree.AddChildren(epTree.Children())
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	opNode, err := termOp(buf)
	if err != nil {
		return nil, err
	}
	newTree.AddChild(opNode)
	if buf.Pos() >= buf.Len() {
		return nil, io.EOF
	}
	eTree, err := e(buf, tree.Copy())
	if err != nil {
		return nil, err
	}
	newTree.AddChildren(eTree.Children())
	return newTree, nil
}