-
Notifications
You must be signed in to change notification settings - Fork 4
/
format.go
129 lines (111 loc) · 3.09 KB
/
format.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
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
package main
import (
"bytes"
"go/ast"
"go/format"
"go/parser"
"go/token"
"regexp"
"strings"
"unicode"
"golang.org/x/tools/go/ast/astutil"
)
// Format processes the source code.
func Format(src []byte) ([]byte, error) {
// Parse.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
return nil, err
}
// Process every comment as a formula.
transformed := commentreplace(f, formula)
// Format.
buf := bytes.NewBuffer(nil)
if err := format.Node(buf, fset, transformed); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// commentreplace applies repl function to the text of every comment under the root AST.
func commentreplace(root ast.Node, repl func(string) string) ast.Node {
return astutil.Apply(root, func(c *astutil.Cursor) bool {
switch n := c.Node().(type) {
case *ast.Comment:
c.Replace(&ast.Comment{
Slash: n.Slash,
Text: repl(n.Text),
})
case *ast.File:
for _, g := range n.Comments {
for _, comment := range g.List {
comment.Text = repl(comment.Text)
}
}
}
return true
}, nil)
}
// Fixed data structures required for formula processing.
var (
// Symbol replacer.
replacer *strings.Replacer
// Regular expressions for super/subscripts.
supregexp *regexp.Regexp
subregexp *regexp.Regexp
// Rune replacement maps.
super = map[rune]rune{}
sub = map[rune]rune{}
)
func init() {
// Build symbol replacer.
var oldnew []string
for symbol, r := range symbols {
oldnew = append(oldnew, symbol, string([]rune{r}))
}
replacer = strings.NewReplacer(oldnew...)
// Build super/subscript character classes and replacement maps.
var superclass, subclass []rune
for _, char := range chars {
if char.Super != None {
superclass = append(superclass, char.Char)
super[char.Char] = char.Super
}
if char.Sub != None {
subclass = append(subclass, char.Char)
sub[char.Char] = char.Sub
}
}
// Build regular expressions.
supregexp = regexp.MustCompile(`(\b[A-Za-z0-9]|[)\pL\pS^A-Za-z0-9])\^(\d+|\{` + charclass(superclass) + `+\}|` + charclass(superclass) + `\s)`)
subregexp = regexp.MustCompile(`(\b[A-Za-z]|\pS|\p{Greek})_(\d+\b|\{` + charclass(subclass) + `+\})`)
}
// charclass builds a regular expression character class from a list of runes.
func charclass(runes []rune) string {
return strings.ReplaceAll("["+string(runes)+"]", "-", `\-`)
}
// formula processes a formula in s, writing the result to w.
func formula(s string) string {
// Replace symbols.
s = replacer.Replace(s)
// Replace superscripts.
s = supregexp.ReplaceAllStringFunc(s, subsupreplacer(super))
// Replace subscripts.
s = subregexp.ReplaceAllStringFunc(s, subsupreplacer(sub))
return s
}
// subsupreplacer builds a replacement function that applies the repl rune map
// to a matched super/subscript.
func subsupreplacer(repl map[rune]rune) func(string) string {
return func(s string) string {
var runes []rune
for i, r := range s {
if i == 0 || unicode.IsSpace(r) {
runes = append(runes, r)
} else if repl[r] != None {
runes = append(runes, repl[r])
}
}
return string(runes)
}
}