-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcopy_paste.go
83 lines (68 loc) · 1.49 KB
/
copy_paste.go
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
/*
* Copyright (c) Cherri
*/
package main
import (
"fmt"
"regexp"
"strings"
"github.com/electrikmilk/args-parser"
)
var pasteables map[string]string
var copyPasteRegex = regexp.MustCompile(`copy .+ \{`)
func parseCopyPastes() {
if !copyPasteRegex.MatchString(contents) {
return
}
pasteables = make(map[string]string)
for char != -1 {
switch {
case isChar('/'):
collectComment()
case tokenAhead(Copy):
advance()
collectCopy()
case tokenAhead(Paste):
advance()
pasteCopy()
continue
}
advance()
}
resetParse()
if args.Using("debug") {
printPasteablesDebug()
}
}
func collectCopy() {
var startLine = lineIdx
var identifier = collectIdentifier()
if _, found := pasteables[identifier]; found {
parserError(fmt.Sprintf("Duplication declaration of copy/paste '%s'", identifier))
}
advanceUntil('{')
advance()
var contents = collectObject()
for i := startLine; i <= lineIdx; i++ {
lines[i] = ""
}
pasteables[identifier] = strings.TrimSpace(contents)
}
func pasteCopy() {
var identifier = collectIdentifier()
if contents, found := pasteables[identifier]; found {
lines[lineIdx] = contents
} else {
parserError(fmt.Sprintf("Unable to paste undefined copy '%s'", identifier))
}
}
func printPasteablesDebug() {
fmt.Println(ansi("### COPY/PASTE ###", bold) + "\n")
for identifier, contents := range pasteables {
fmt.Println("identifier:", identifier+"()")
fmt.Println("contents:")
fmt.Println(contents)
fmt.Println("(end)")
fmt.Print("\n")
}
}