-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_import.go
More file actions
96 lines (80 loc) · 3.15 KB
/
parse_import.go
File metadata and controls
96 lines (80 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package twig
import (
"fmt"
"strings"
)
func (p *Parser) parseImport(parser *Parser) (Node, error) {
// Use debug logging if enabled
if IsDebugEnabled() && debugger.level >= DebugVerbose {
tokenIndex := parser.tokenIndex - 2
LogVerbose("Parsing import, tokens available:")
for i := 0; i < 10 && tokenIndex+i < len(parser.tokens); i++ {
token := parser.tokens[tokenIndex+i]
LogVerbose(" Token %d: Type=%d, Value=%q, Line=%d", i, token.Type, token.Value, token.Line)
}
}
// Get the line number of the import token
importLine := parser.tokens[parser.tokenIndex-2].Line
// Check for incorrectly tokenized import syntax
if parser.tokenIndex < len(parser.tokens) &&
parser.tokens[parser.tokenIndex].Type == TOKEN_NAME &&
strings.Contains(parser.tokens[parser.tokenIndex].Value, " as ") {
// Special handling for combined syntax like "path.twig as alias"
parts := strings.SplitN(parser.tokens[parser.tokenIndex].Value, " as ", 2)
if len(parts) == 2 {
templatePath := strings.TrimSpace(parts[0])
alias := strings.TrimSpace(parts[1])
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Found combined import syntax: template=%q, alias=%q", templatePath, alias)
}
// Create an expression node for the template path
var templateExpr Node
if strings.HasPrefix(templatePath, "\"") && strings.HasSuffix(templatePath, "\"") {
// It's already a quoted string
templateExpr = NewLiteralNode(templatePath[1:len(templatePath)-1], importLine)
} else {
// Create a string literal node
templateExpr = NewLiteralNode(templatePath, importLine)
}
// Skip to end of token
parser.tokenIndex++
// Expect block end
if parser.tokenIndex >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END_TRIM) {
return nil, fmt.Errorf("expected block end token after import statement at line %d", importLine)
}
parser.tokenIndex++
// Create import node
return NewImportNode(templateExpr, alias, importLine), nil
}
}
// Standard parsing path
// Get the template to import
templateExpr, err := parser.parseExpression()
if err != nil {
return nil, err
}
// Expect 'as' keyword
if parser.tokenIndex >= len(parser.tokens) ||
parser.tokens[parser.tokenIndex].Type != TOKEN_NAME ||
parser.tokens[parser.tokenIndex].Value != "as" {
return nil, fmt.Errorf("expected 'as' after template path at line %d", importLine)
}
parser.tokenIndex++
// Get the alias name
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected identifier after 'as' at line %d", importLine)
}
alias := parser.tokens[parser.tokenIndex].Value
parser.tokenIndex++
// Expect block end
if parser.tokenIndex >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END_TRIM) {
return nil, fmt.Errorf("expected block end token after import statement at line %d", importLine)
}
parser.tokenIndex++
// Create import node
return NewImportNode(templateExpr, alias, importLine), nil
}