-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
523 lines (459 loc) · 12.7 KB
/
parser.go
File metadata and controls
523 lines (459 loc) · 12.7 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright 2025-2026 : Nawa Manusitthipol
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Package boothfile provides parsing and compilation of Boothfiles into Dockerfiles.
//
// Boothfile is a higher-level DSL that compiles to Dockerfiles, aimed at simplifying
// CodingBooth configuration by hiding boilerplate and providing intent-based syntax.
//
// Example Boothfile:
//
// # syntax=codingbooth/boothfile:1
// setup python 3.12
// install pip django
//
// This compiles to a full Dockerfile with the required CodingBooth prologue.
package boothfile
import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
)
// SyntaxVersion is the expected syntax directive version.
const SyntaxVersion = "codingbooth/boothfile:1"
// SyntaxDirective is the full syntax line expected at the start of a Boothfile.
const SyntaxDirective = "# syntax=" + SyntaxVersion
// CommandType represents the type of a parsed command.
type CommandType int
const (
CommandUnknown CommandType = iota
CommandComment
CommandBlank
CommandRun
CommandRunHeredoc
CommandCopy
CommandEnv
CommandWorkdir
CommandExpose
CommandLabel
CommandArg
CommandSetup
CommandInstall
CommandDocker // Escape hatch
)
// String returns a string representation of the command type.
func (ct CommandType) String() string {
switch ct {
case CommandComment:
return "comment"
case CommandBlank:
return "blank"
case CommandRun:
return "run"
case CommandRunHeredoc:
return "run-heredoc"
case CommandCopy:
return "copy"
case CommandEnv:
return "env"
case CommandWorkdir:
return "workdir"
case CommandExpose:
return "expose"
case CommandLabel:
return "label"
case CommandArg:
return "arg"
case CommandSetup:
return "setup"
case CommandInstall:
return "install"
case CommandDocker:
return "DOCKER"
default:
return "unknown"
}
}
// HeredocMode represents how heredoc content should be joined.
type HeredocMode int
const (
HeredocVerbatim HeredocMode = iota // Pass through as Docker heredoc
HeredocAndJoin // Join lines with &&
HeredocSemiJoin // Join lines with ;
)
// String returns a string representation of the heredoc mode.
func (hm HeredocMode) String() string {
switch hm {
case HeredocVerbatim:
return "verbatim"
case HeredocAndJoin:
return "and-join"
case HeredocSemiJoin:
return "semi-join"
default:
return "unknown"
}
}
// Command represents a parsed Boothfile command.
type Command struct {
Type CommandType
LineNumber int
Raw string // Original line(s) from the file
Args []string // Parsed arguments
// For heredoc commands
HeredocMode HeredocMode
HeredocDelimiter string
HeredocContent []string
}
// ParseError represents an error that occurred during parsing.
type ParseError struct {
LineNumber int
Message string
Hint string
}
// Error implements the error interface.
func (e ParseError) Error() string {
if e.Hint != "" {
return fmt.Sprintf("Boothfile:%d: %s\nHint: %s", e.LineNumber, e.Message, e.Hint)
}
return fmt.Sprintf("Boothfile:%d: %s", e.LineNumber, e.Message)
}
// ParseResult contains the result of parsing a Boothfile.
type ParseResult struct {
Commands []Command
Errors []ParseError
Warnings []ParseError
}
// HasErrors returns true if there were parsing errors.
func (pr ParseResult) HasErrors() bool {
return len(pr.Errors) > 0
}
// HasWarnings returns true if there were parsing warnings.
func (pr ParseResult) HasWarnings() bool {
return len(pr.Warnings) > 0
}
// Parser parses Boothfiles into structured commands.
type Parser struct {
strict bool
}
// NewParser creates a new Boothfile parser.
func NewParser() *Parser {
return &Parser{strict: false}
}
// NewStrictParser creates a new Boothfile parser in strict mode.
// In strict mode, warnings are treated as errors.
func NewStrictParser() *Parser {
return &Parser{strict: true}
}
// Regex patterns for parsing
var (
// Matches: run <<END, run &&<<END, run ;<<END
heredocStartPattern = regexp.MustCompile(`^run\s*(&&|;)?<<(\w+)\s*(?:#.*)?$`)
// Matches command and rest of line
commandPattern = regexp.MustCompile(`^(\w+)\s*(.*)$`)
// Matches inline comment
inlineCommentPattern = regexp.MustCompile(`^(.*?)\s*#.*$`)
)
// Parse parses a Boothfile from a reader.
func (p *Parser) Parse(r io.Reader) ParseResult {
result := ParseResult{
Commands: make([]Command, 0),
Errors: make([]ParseError, 0),
Warnings: make([]ParseError, 0),
}
scanner := bufio.NewScanner(r)
lineNumber := 0
syntaxFound := false
for scanner.Scan() {
lineNumber++
line := scanner.Text()
startLineNumber := lineNumber
// Handle backslash line continuation:
// If a line ends with '\' (ignoring trailing whitespace), the next line
// is joined with a single space separating them.
for {
trimmed := strings.TrimRight(line, " \t")
if !strings.HasSuffix(trimmed, `\`) {
break
}
// Strip the trailing backslash and any whitespace before it
line = strings.TrimRight(trimmed[:len(trimmed)-1], " \t")
if !scanner.Scan() {
break
}
lineNumber++
nextLine := strings.TrimLeft(scanner.Text(), " \t")
line = line + " " + nextLine
}
// First non-blank, non-comment line must be syntax directive
if !syntaxFound {
trimmed := strings.TrimSpace(line)
if trimmed == "" {
result.Commands = append(result.Commands, Command{
Type: CommandBlank,
LineNumber: startLineNumber,
Raw: line,
})
continue
}
// Check for syntax directive
if strings.HasPrefix(trimmed, "# syntax=") {
syntaxFound = true
if trimmed != SyntaxDirective {
result.Errors = append(result.Errors, ParseError{
LineNumber: startLineNumber,
Message: fmt.Sprintf("Invalid syntax directive. Expected: %s", SyntaxDirective),
Hint: "The first non-blank line must be exactly: " + SyntaxDirective,
})
}
result.Commands = append(result.Commands, Command{
Type: CommandComment,
LineNumber: startLineNumber,
Raw: line,
})
continue
}
// Not a syntax directive - error
result.Errors = append(result.Errors, ParseError{
LineNumber: startLineNumber,
Message: "Missing syntax directive",
Hint: "Boothfile must start with: " + SyntaxDirective,
})
syntaxFound = true // Continue parsing anyway
}
// Parse the line
cmd, err := p.parseLine(line, startLineNumber, scanner, &lineNumber)
if err != nil {
result.Errors = append(result.Errors, *err)
continue
}
if cmd != nil {
result.Commands = append(result.Commands, *cmd)
}
}
if scanErr := scanner.Err(); scanErr != nil {
result.Errors = append(result.Errors, ParseError{
LineNumber: lineNumber,
Message: fmt.Sprintf("Error reading file: %v", scanErr),
})
}
return result
}
// ParseString parses a Boothfile from a string.
func (p *Parser) ParseString(content string) ParseResult {
return p.Parse(strings.NewReader(content))
}
// parseLine parses a single line (or multi-line for heredocs).
func (p *Parser) parseLine(line string, lineNumber int, scanner *bufio.Scanner, currentLine *int) (*Command, *ParseError) {
trimmed := strings.TrimSpace(line)
// Blank line
if trimmed == "" {
return &Command{
Type: CommandBlank,
LineNumber: lineNumber,
Raw: line,
}, nil
}
// Full-line comment
if strings.HasPrefix(trimmed, "#") {
return &Command{
Type: CommandComment,
LineNumber: lineNumber,
Raw: line,
}, nil
}
// Check for heredoc start
if heredocMatch := heredocStartPattern.FindStringSubmatch(trimmed); heredocMatch != nil {
return p.parseHeredoc(line, lineNumber, heredocMatch, scanner, currentLine)
}
// Check for DOCKER escape hatch (must be uppercase)
if strings.HasPrefix(trimmed, "DOCKER ") {
return &Command{
Type: CommandDocker,
LineNumber: lineNumber,
Raw: line,
Args: []string{strings.TrimPrefix(trimmed, "DOCKER ")},
}, nil
}
// Parse regular command
return p.parseCommand(trimmed, lineNumber, line)
}
// parseHeredoc parses a heredoc block.
func (p *Parser) parseHeredoc(startLine string, startLineNumber int, match []string, scanner *bufio.Scanner, currentLine *int) (*Command, *ParseError) {
// match[1] = mode (empty, "&&", or ";")
// match[2] = delimiter
modeStr := match[1]
delimiter := match[2]
var mode HeredocMode
switch modeStr {
case "&&":
mode = HeredocAndJoin
case ";":
mode = HeredocSemiJoin
default:
mode = HeredocVerbatim
}
content := make([]string, 0)
rawLines := []string{startLine}
for scanner.Scan() {
*currentLine++
line := scanner.Text()
rawLines = append(rawLines, line)
if strings.TrimSpace(line) == delimiter {
return &Command{
Type: CommandRunHeredoc,
LineNumber: startLineNumber,
Raw: strings.Join(rawLines, "\n"),
HeredocMode: mode,
HeredocDelimiter: delimiter,
HeredocContent: content,
}, nil
}
content = append(content, line)
}
// EOF without closing delimiter
return nil, &ParseError{
LineNumber: startLineNumber,
Message: fmt.Sprintf("Unclosed heredoc block. Expected closing delimiter: %s", delimiter),
Hint: "The closing delimiter must appear alone on its own line.",
}
}
// parseCommand parses a regular (non-heredoc) command.
func (p *Parser) parseCommand(trimmed string, lineNumber int, raw string) (*Command, *ParseError) {
// Remove inline comment for parsing (but keep in raw)
withoutComment := trimmed
if idx := strings.Index(trimmed, "#"); idx > 0 {
// Check if # is inside quotes (simple heuristic)
beforeHash := trimmed[:idx]
if strings.Count(beforeHash, `"`)%2 == 0 && strings.Count(beforeHash, `'`)%2 == 0 {
withoutComment = strings.TrimSpace(beforeHash)
}
}
match := commandPattern.FindStringSubmatch(withoutComment)
if match == nil {
return nil, &ParseError{
LineNumber: lineNumber,
Message: fmt.Sprintf("Invalid command syntax: %s", trimmed),
}
}
cmdName := strings.ToLower(match[1])
argsStr := match[2]
// Map command name to type
cmdType := p.mapCommandType(cmdName)
if cmdType == CommandUnknown {
// Check for common typos
suggestion := p.suggestCommand(cmdName)
hint := ""
if suggestion != "" {
hint = fmt.Sprintf("Did you mean '%s'?", suggestion)
}
return nil, &ParseError{
LineNumber: lineNumber,
Message: fmt.Sprintf("Unknown command: %s", cmdName),
Hint: hint,
}
}
// Parse arguments based on command type
args := p.parseArgs(argsStr, cmdType)
return &Command{
Type: cmdType,
LineNumber: lineNumber,
Raw: raw,
Args: args,
}, nil
}
// mapCommandType maps a command name to its type.
func (p *Parser) mapCommandType(name string) CommandType {
switch name {
case "run":
return CommandRun
case "copy":
return CommandCopy
case "env":
return CommandEnv
case "workdir":
return CommandWorkdir
case "expose":
return CommandExpose
case "label":
return CommandLabel
case "arg":
return CommandArg
case "setup":
return CommandSetup
case "install":
return CommandInstall
default:
return CommandUnknown
}
}
// suggestCommand returns a suggestion for a misspelled command.
func (p *Parser) suggestCommand(name string) string {
commands := []string{"run", "copy", "env", "workdir", "expose", "label", "arg", "setup", "install"}
for _, cmd := range commands {
// Simple Levenshtein-like check: if most characters match
if len(name) > 0 && len(cmd) > 0 {
if strings.HasPrefix(cmd, name[:1]) && abs(len(cmd)-len(name)) <= 2 {
// Check character overlap
matches := 0
for i := 0; i < len(name) && i < len(cmd); i++ {
if name[i] == cmd[i] {
matches++
}
}
if matches >= len(name)-2 || matches >= len(cmd)-2 {
return cmd
}
}
}
}
return ""
}
// parseArgs parses the argument string for a command.
func (p *Parser) parseArgs(argsStr string, cmdType CommandType) []string {
argsStr = strings.TrimSpace(argsStr)
if argsStr == "" {
return []string{}
}
// For most commands, split on whitespace
// But preserve quoted strings
return splitArgs(argsStr)
}
// splitArgs splits an argument string, respecting quotes.
func splitArgs(s string) []string {
var args []string
var current strings.Builder
inQuote := false
quoteChar := rune(0)
for _, r := range s {
switch {
case (r == '"' || r == '\'') && !inQuote:
inQuote = true
quoteChar = r
current.WriteRune(r)
case r == quoteChar && inQuote:
inQuote = false
quoteChar = 0
current.WriteRune(r)
case (r == ' ' || r == '\t') && !inQuote:
if current.Len() > 0 {
args = append(args, current.String())
current.Reset()
}
default:
current.WriteRune(r)
}
}
if current.Len() > 0 {
args = append(args, current.String())
}
return args
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}