Пример #1
0
Файл: if.go Проект: xet7/gerb
func ElseFactory(p *core.Parser) (core.Code, error) {
	code := &ElseCode{NormalContainer: new(core.NormalContainer)}
	if p.SkipSpaces() == 'i' && p.ConsumeIf([]byte("if")) {
		if p.TagContains(';') {
			assignment, err := p.ReadAssignment()
			if err != nil {
				return nil, err
			}
			code.assignment = assignment
			if p.SkipSpaces() != ';' {
				return nil, p.Error("else if assignment should be followed by a semicolon")
			}
			p.Next()
		}
		verifiable, err := p.ReadConditionGroup(false)
		if err != nil {
			return nil, err
		}
		code.verifiable = verifiable
		if p.SkipSpaces() != '{' {
			return nil, p.Error("Missing openening brace for else if statement")
		}
	} else {
		code.verifiable = core.TrueCondition //else case
		if p.SkipSpaces() != '{' {
			return nil, p.Error("Missing openening brace for else statement")
		}
	}
	p.Next()
	return code, nil
}
Пример #2
0
func createCodeTag(p *core.Parser) ([]core.Code, error) {
	codes := make([]core.Code, 0, 1)
	for {
		token, err := p.ReadToken()
		if err != nil {
			return nil, err
		}
		length := len(token)
		if length == 0 {
			if err := p.ReadCloseTag(); err != nil {
				return nil, err
			}
			return nil, nil
		}

		var code core.Code
		if token == "}" {
			if p.SkipSpaces() == 'e' && p.ConsumeIf([]byte("else")) {
				code, err = ElseFactory(p)
			} else {
				code = endScope
			}
		} else if factory, ok := CodeFactories[token]; ok {
			code, err = factory(p)
		} else {
			p.Backwards(length)
			code, err = p.ReadAssignment()
		}
		if err != nil {
			return nil, err
		}
		codes = append(codes, code)
		if p.SkipSpaces() == '%' && p.ConsumeIf([]byte("%>")) {
			return codes, nil
		}
	}
}
Пример #3
0
Файл: for.go Проект: xet7/gerb
func RangedForFactory(p *core.Parser) (core.Code, error) {
	code := &RangedForCode{NormalContainer: new(core.NormalContainer)}
	tokens, err := p.ReadTokenList()
	if err != nil {
		return nil, err
	}
	if len(tokens) != 2 {
		return nil, p.Error("Invalid for loop, ranged loop should have two variables")
	}
	code.tokens = tokens
	c := p.SkipSpaces()
	if c == ':' {
		c = p.Next()
	}
	if c != '=' {
		return nil, p.Error("Invalid for loop, ranged loop expecting assignment operator")
	}
	p.Next()

	if p.SkipSpaces() != 'r' || p.ConsumeIf([]byte("range")) == false {
		return nil, p.Error("invalid for loop, expected 'range' keyword")
	}

	value, err := p.ReadValue()
	if err != nil {
		return nil, err
	}
	code.value = value
	if p.SkipSpaces() != '{' {
		return nil, p.Error("Missing openening brace for if statement")
	}
	p.Next()

	return code, nil

}