Example #1
0
File: code.go Project: xet7/gerb
func createCodeTag(p *core.Parser) (*core.Codes, 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(elseBytes) {
				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() == '%' {
			if p.ConsumeIf(trimCloseBytes) {
				return &core.Codes{true, codes}, nil
			}
			if p.ConsumeIf(closeBytes) {
				return &core.Codes{false, codes}, nil
			}
		}
	}
}