aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/homemade/scl/parser.go
blob: 0304a00ac5ae49f7197f334bb92074bf298cfcb3 (plain)
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package scl

import (
	"fmt"
	"path/filepath"
	"strings"

	"github.com/hashicorp/hcl"
	hclparser "github.com/hashicorp/hcl/hcl/parser"
)

const (
	builtinMixinBody    = "__body__"
	builtinMixinInclude = "include"
	hclIndentSize       = 2
	noMixinParamValue   = "_"
)

/*
A Parser takes input in the form of filenames, variables values and include
paths, and transforms any SCL into HCL. Generally, a program will only call
Parse() for one file (the configuration file for that project) but it can be
called on any number of files, each of which will add to the Parser's HCL
output.

Variables and includes paths are global for all files parsed; that is, if you
Parse() multiple files, each of them will have access to the same set of
variables and use the same set of include paths. The parser variables are part
of the top-level scope: if a file changes them while it's being parsed, the
next file will have the same variable available with the changed value.
Similarly, if a file declares a new variable or mixin on the root scope, then
the next file will be able to access it. This can become confusing quickly,
so it's usually best to parse only one file and let it explicitly include
and other files at the SCL level.

SCL is an auto-documenting language, and the documentation is obtained using
the Parser's Documentation() function. Only mixins are currently documented.
Unlike the String() function, the documentation returned for Documentation()
only includes the nominated file.
*/
type Parser interface {
	Parse(fileName string) error
	Documentation(fileName string) (MixinDocs, error)
	SetParam(name, value string)
	AddIncludePath(name string)
	String() string
}

type parser struct {
	fs           FileSystem
	rootScope    *scope
	output       []string
	indent       int
	includePaths []string
}

/*
NewParser creates a new, standard Parser given a FileSystem. The most common FileSystem is
the DiskFileSystem, but any will do. The parser opens all files and reads all
includes using the FileSystem provided.
*/
func NewParser(fs FileSystem) (Parser, error) {

	p := &parser{
		fs:        fs,
		rootScope: newScope(),
	}

	return p, nil
}

func (p *parser) SetParam(name, value string) {
	p.rootScope.setVariable(name, value)
}

func (p *parser) AddIncludePath(name string) {
	p.includePaths = append(p.includePaths, name)
}

func (p *parser) String() string {
	return strings.Join(p.output, "\n")
}

func (p *parser) Parse(fileName string) error {

	lines, err := p.scanFile(fileName)

	if err != nil {
		return err
	}

	if err := p.parseTree(lines, newTokeniser(), p.rootScope); err != nil {
		return err
	}

	return nil
}

func (p *parser) Documentation(fileName string) (MixinDocs, error) {

	docs := MixinDocs{}

	lines, err := p.scanFile(fileName)

	if err != nil {
		return docs, err
	}

	if err := p.parseTreeForDocumentation(lines, newTokeniser(), &docs); err != nil {
		return docs, err
	}

	return docs, nil
}

func (p *parser) scanFile(fileName string) (lines scannerTree, err error) {

	f, _, err := p.fs.ReadCloser(fileName)

	if err != nil {
		return lines, fmt.Errorf("Can't read %s: %s", fileName, err)
	}

	defer f.Close()

	lines, err = newScanner(f, fileName).scan()

	if err != nil {
		return lines, fmt.Errorf("Can't scan %s: %s", fileName, err)
	}

	return
}

func (p *parser) isValid(hclString string) error {

	e := hcl.Decode(&struct{}{}, hclString)

	if pe, ok := e.(*hclparser.PosError); ok {
		return pe.Err
	} else if pe != nil {
		return pe
	}

	return nil
}

func (p *parser) indentedValue(literal string) string {
	return fmt.Sprintf("%s%s", strings.Repeat(" ", p.indent*hclIndentSize), literal)
}

func (p *parser) writeLiteralToOutput(scope *scope, literal string, block bool) error {

	literal, err := scope.interpolateLiteral(literal)

	if err != nil {
		return err
	}

	line := p.indentedValue(literal)

	if block {

		if err := p.isValid(line + "{}"); err != nil {
			return err
		}

		line += " {"
		p.indent++

	} else {

		if hashCommentMatcher.MatchString(line) {
			// Comments are passed through directly
		} else if err := p.isValid(line + "{}"); err == nil {
			line = line + "{}"
		} else if err := p.isValid(line); err != nil {
			return err
		}
	}

	p.output = append(p.output, line)

	return nil
}

func (p *parser) endBlock() {
	p.indent--
	p.output = append(p.output, p.indentedValue("}"))
}

func (p *parser) err(branch *scannerLine, e string, args ...interface{}) error {
	return fmt.Errorf("[%s] %s", branch.String(), fmt.Sprintf(e, args...))
}

func (p *parser) parseTree(tree scannerTree, tkn *tokeniser, scope *scope) error {

	for _, branch := range tree {

		tokens, err := tkn.tokenise(branch)

		if err != nil {
			return p.err(branch, err.Error())
		}

		if len(tokens) > 0 {

			token := tokens[0]

			switch token.kind {

			case tokenLiteral:

				if err := p.parseLiteral(branch, tkn, token, scope); err != nil {
					return err
				}

			case tokenVariableAssignment:

				value, err := scope.interpolateLiteral(tokens[1].content)

				if err != nil {
					return err
				}

				scope.setVariable(token.content, value)

			case tokenVariableDeclaration:

				value, err := scope.interpolateLiteral(tokens[1].content)

				if err != nil {
					return err
				}

				scope.setArgumentVariable(token.content, value)

			case tokenConditionalVariableAssignment:

				value, err := scope.interpolateLiteral(tokens[1].content)

				if err != nil {
					return err
				}

				if v := scope.variable(token.content); v == "" {
					scope.setArgumentVariable(token.content, value)
				}

			case tokenMixinDeclaration:
				if err := p.parseMixinDeclaration(branch, tokens, scope); err != nil {
					return err
				}

			case tokenFunctionCall:
				if err := p.parseFunctionCall(branch, tkn, tokens, scope.clone()); err != nil {
					return err
				}

			case tokenCommentStart, tokenCommentEnd, tokenLineComment:
				// Do nothing

			default:
				return p.err(branch, "Unexpected token: %s (%s)", token.kind, branch.content)
			}
		}
	}

	return nil
}

func (p *parser) parseTreeForDocumentation(tree scannerTree, tkn *tokeniser, docs *MixinDocs) error {

	comments := []string{}

	resetComments := func() {
		comments = []string{}
	}

	for _, branch := range tree {

		tokens, err := tkn.tokenise(branch)

		if err != nil {
			return p.err(branch, err.Error())
		}

		if len(tokens) > 0 {

			token := tokens[0]

			switch token.kind {
			case tokenLineComment, tokenCommentEnd:
				// Do nothing

			case tokenCommentStart:
				p.parseBlockComment(branch.children, &comments, branch.line, 0)

			case tokenMixinDeclaration:

				if token.content[0] == '_' {
					resetComments()
					continue
				}

				doc := MixinDoc{
					Name:      token.content,
					File:      branch.file,
					Line:      branch.line,
					Reference: branch.String(),
					Signature: string(branch.content),
					Docs:      strings.Join(comments, "\n"),
				}

				// Clear comments
				resetComments()

				// Store the mixin docs and empty the running comment
				if err := p.parseTreeForDocumentation(branch.children, tkn, &doc.Children); err != nil {
					return err
				}

				*docs = append(*docs, doc)

			default:
				resetComments()
				if err := p.parseTreeForDocumentation(branch.children, tkn, docs); err != nil {
					return err
				}
			}
		}
	}

	return nil
}

func (p *parser) parseBlockComment(tree scannerTree, comments *[]string, line, indentation int) error {

	for _, branch := range tree {

		// Re-add missing blank lines
		if line == 0 {
			line = branch.line
		} else {
			if line != branch.line-1 {
				*comments = append(*comments, "")
			}
			line = branch.line
		}

		*comments = append(*comments, strings.Repeat(" ", indentation*4)+string(branch.content))

		if err := p.parseBlockComment(branch.children, comments, line, indentation+1); err != nil {
			return nil
		}
	}

	return nil
}

func (p *parser) parseLiteral(branch *scannerLine, tkn *tokeniser, token token, scope *scope) error {

	children := len(branch.children) > 0

	if err := p.writeLiteralToOutput(scope, token.content, children); err != nil {
		return p.err(branch, err.Error())
	}

	if children {

		if err := p.parseTree(branch.children, tkn, scope.clone()); err != nil {
			return err
		}

		p.endBlock()
	}

	return nil
}

func (p *parser) parseMixinDeclaration(branch *scannerLine, tokens []token, scope *scope) error {

	i := 0
	literalExpected := false
	optionalArgStart := false

	var (
		arguments []token
		defaults  []string
		current   token
	)

	// Make sure that only variables are given as arguments
	for _, v := range tokens[1:] {

		switch v.kind {

		case tokenLiteral:
			if !literalExpected {
				return p.err(branch, "Argument declaration %d [%s]: Unexpected literal", i, v.content)
			}

			value := v.content

			// Underscore literals are 'no values' in mixin
			// declarations
			if value == noMixinParamValue {
				value = ""
			}

			arguments = append(arguments, current)
			defaults = append(defaults, value)
			literalExpected = false

		case tokenVariableAssignment:
			optionalArgStart = true
			literalExpected = true
			current = token{
				kind:    tokenVariable,
				content: v.content,
				line:    v.line,
			}
			i++

		case tokenVariable:

			if optionalArgStart {
				return p.err(branch, "Argument declaration %d [%s]: A required argument can't follow an optional argument", i, v.content)
			}

			arguments = append(arguments, v)
			defaults = append(defaults, "")
			i++

		default:
			return p.err(branch, "Argument declaration %d [%s] is not a variable or a variable assignment", i, v.content)
		}
	}

	if literalExpected {
		return p.err(branch, "Expected a literal in mixin signature")
	}

	if a, d := len(arguments), len(defaults); a != d {
		return p.err(branch, "Expected eqaual numbers of arguments and defaults (a:%d,d:%d)", a, d)
	}

	scope.setMixin(tokens[0].content, branch, arguments, defaults)

	return nil
}

func (p *parser) parseFunctionCall(branch *scannerLine, tkn *tokeniser, tokens []token, scope *scope) error {

	// Handle built-ins
	if tokens[0].content == builtinMixinBody {
		return p.parseBodyCall(branch, tkn, scope)
	} else if tokens[0].content == builtinMixinInclude {
		return p.parseIncludeCall(branch, tokens, scope)
	}

	// Make sure the mixin exists in the scope
	mx, err := scope.mixin(tokens[0].content)

	if err != nil {
		return p.err(branch, err.Error())
	}

	args, err := p.extractValuesFromArgTokens(branch, tokens[1:], scope)

	if err != nil {
		return p.err(branch, err.Error())
	}

	// Add in the defaults
	if l := len(args); l < len(mx.defaults) {
		args = append(args, mx.defaults[l:]...)
	}

	// Check the argument counts
	if r, g := len(mx.arguments), len(args); r != g {
		return p.err(branch, "Wrong number of arguments for %s (required %d, got %d)", tokens[0].content, r, g)
	}

	// Set the argument values
	for i := 0; i < len(mx.arguments); i++ {
		scope.setArgumentVariable(mx.arguments[i].name, args[i])
	}

	// Set an anchor branch for the __body__ built-in
	scope.branch = branch
	scope.branchScope = scope.parent

	// Call the function!
	return p.parseTree(mx.declaration.children, tkn, scope)
}

func (p *parser) parseBodyCall(branch *scannerLine, tkn *tokeniser, scope *scope) error {

	if scope.branchScope == nil {
		return p.err(branch, "Unexpected error: No parent scope somehow!")
	}

	if scope.branch == nil {
		return p.err(branch, "Unexpected error: No anchor branch!")
	}

	s := scope.branchScope.clone()
	s.mixins = scope.mixins
	s.variables = scope.variables // FIXME Merge?

	return p.parseTree(scope.branch.children, tkn, s)
}

func (p *parser) includeGlob(name string, branch *scannerLine) error {

	name = strings.TrimSuffix(strings.Trim(name, `"'`), ".scl") + ".scl"

	vendorPath := []string{filepath.Join(filepath.Dir(branch.file), "vendor")}
	vendorPath = append(vendorPath, p.includePaths...)

	var paths []string

	for _, ip := range vendorPath {

		ipaths, err := p.fs.Glob(ip + "/" + name)

		if err != nil {
			return err
		}

		if len(ipaths) > 0 {
			paths = ipaths
			break
		}
	}

	if len(paths) == 0 {

		var err error
		paths, err = p.fs.Glob(name)

		if err != nil {
			return err
		}
	}

	if len(paths) == 0 {
		return fmt.Errorf("Can't read %s: no files found", name)
	}

	for _, path := range paths {
		if err := p.Parse(path); err != nil {
			return fmt.Errorf(err.Error())
		}
	}

	return nil
}

func (p *parser) parseIncludeCall(branch *scannerLine, tokens []token, scope *scope) error {

	args, err := p.extractValuesFromArgTokens(branch, tokens[1:], scope)

	if err != nil {
		return p.err(branch, err.Error())
	}

	for _, v := range args {

		if err := p.includeGlob(v, branch); err != nil {
			return p.err(branch, err.Error())
		}
	}

	return nil
}

func (p *parser) extractValuesFromArgTokens(branch *scannerLine, tokens []token, scope *scope) ([]string, error) {

	var args []string

	for _, v := range tokens {
		switch v.kind {

		case tokenLiteral:

			value, err := scope.interpolateLiteral(v.content)

			if err != nil {
				return args, err
			}

			args = append(args, value)

		case tokenVariable:

			value := scope.variable(v.content)

			if value == "" {
				return args, fmt.Errorf("Variable $%s is not declared in this scope", v.content)
			}

			args = append(args, value)

		default:
			return args, fmt.Errorf("Invalid token type for function argument: %s (%s)", v.kind, branch.content)
		}
	}

	return args, nil
}