Пример #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
}
Пример #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),
		})
	}
}
Пример #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)
}
Пример #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)
	}
}
Пример #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
}