Example #1
0
func (p *Parser) parseClassFields(c *ast.Class) *ast.Class {
	// Starting on BlockBegin
	c.Methods = make([]*ast.Method, 0)
	c.Properties = make([]*ast.Property, 0)
	for p.peek().Typ != token.BlockEnd {
		vis, _, _, abstract := p.parseClassMemberSettings()
		p.next()
		switch p.current.Typ {
		case token.Function:
			p.parseClassMethod(c, abstract, vis)
		case token.Var:
			p.expect(token.VariableOperator)
			fallthrough
		case token.VariableOperator:
			p.parseClassVariables(c, vis)
		case token.Const:
			p.parseClassConst(c)
		default:
			p.errorf("unexpected class member %v", p.current)
			return c
		}
	}
	p.expect(token.BlockEnd)
	return c
}
Example #2
0
func (p *Parser) parseClassMethod(c *ast.Class, abstract bool, vis ast.Visibility) {
	if abstract {
		f := p.parseFunctionDefinition()
		m := &ast.Method{
			Visibility:   vis,
			FunctionStmt: &ast.FunctionStmt{FunctionDefinition: f},
		}
		c.Methods = append(c.Methods, m)
		p.expect(token.StatementEnd)
	} else {
		c.Methods = append(c.Methods, &ast.Method{
			Visibility:   vis,
			FunctionStmt: p.parseFunctionStmt(true),
		})
	}
}
Example #3
0
func (p *Parser) parseClassConst(c *ast.Class) {
	constant := &ast.Constant{}
	p.expect(token.Identifier)
	constant.Name = p.current.Val
	if p.peek().Typ == token.AssignmentOperator {
		p.expect(token.AssignmentOperator)
		constant.Value = p.parseNextExpression()
	}
	c.Constants = append(c.Constants, constant)
	p.expect(token.StatementEnd)
}
Example #4
0
func (p *Parser) parseClassVariables(c *ast.Class, vis ast.Visibility) {
	for {
		p.expect(token.Identifier)
		prop := &ast.Property{
			Visibility: vis,
			Name:       "$" + p.current.Val,
		}
		if p.peek().Typ == token.AssignmentOperator {
			p.expect(token.AssignmentOperator)
			prop.Initialization = p.parseNextExpression()
		}
		c.Properties = append(c.Properties, prop)
		if p.accept(token.StatementEnd) {
			break
		}
		p.expect(token.Comma)
		p.expect(token.VariableOperator)
	}
}
Example #5
0
func (p *Parser) parseClassFields(c *ast.Class) *ast.Class {
	// Starting on BlockBegin
	c.Methods = make([]*ast.Method, 0)
	c.Properties = make([]*ast.Property, 0)
	for p.peek().Typ != token.BlockEnd {
		vis, _, _, abstract := p.parseClassMemberSettings()
		p.next()
		switch p.current.Typ {
		case token.Function:
			if abstract {
				f := p.parseFunctionDefinition()
				m := &ast.Method{
					Visibility:   vis,
					FunctionStmt: &ast.FunctionStmt{FunctionDefinition: f},
				}
				c.Methods = append(c.Methods, m)
				p.expect(token.StatementEnd)
			} else {
				c.Methods = append(c.Methods, &ast.Method{
					Visibility:   vis,
					FunctionStmt: p.parseFunctionStmt(true),
				})
			}
		case token.Var:
			p.expect(token.VariableOperator)
			fallthrough
		case token.VariableOperator:
			for {
				p.expect(token.Identifier)
				prop := &ast.Property{
					Visibility: vis,
					Name:       "$" + p.current.Val,
				}
				if p.peek().Typ == token.AssignmentOperator {
					p.expect(token.AssignmentOperator)
					prop.Initialization = p.parseNextExpression()
				}
				c.Properties = append(c.Properties, prop)
				if p.accept(token.StatementEnd) {
					break
				}
				p.expect(token.Comma)
				p.expect(token.VariableOperator)
			}
		case token.Const:
			constant := &ast.Constant{}
			p.expect(token.Identifier)
			constant.Name = p.current.Val
			if p.peek().Typ == token.AssignmentOperator {
				p.expect(token.AssignmentOperator)
				constant.Value = p.parseNextExpression()
			}
			c.Constants = append(c.Constants, constant)
			p.expect(token.StatementEnd)
		default:
			p.errorf("unexpected class member %v", p.current)
			return c
		}
	}
	p.expect(token.BlockEnd)
	return c
}