aboutsummaryrefslogtreecommitdiff
path: root/cmd/generator/main.go
blob: 3edcef5d360abfd7a2b047ddb96e6d07977ffd66 (plain) (blame)
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
package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"
	"text/template"

	"github.com/nihei9/maleeni/ucd"
)

func main() {
	err := gen()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}
}

func gen() error {
	var propValAliases *ucd.PropertyValueAliases
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/PropertyValueAliases.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		propValAliases, err = ucd.ParsePropertyValueAliases(resp.Body)
		if err != nil {
			return err
		}
	}
	var unicodeData *ucd.UnicodeData
	{
		resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt")
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		unicodeData, err = ucd.ParseUnicodeData(resp.Body, propValAliases)
		if err != nil {
			return err
		}
	}
	tmpl, err := template.ParseFiles("../compiler/ucd_table.go.tmpl")
	if err != nil {
		return err
	}
	var b strings.Builder
	err = tmpl.Execute(&b, struct {
		GeneratorName        string
		UnicodeData          *ucd.UnicodeData
		PropertyValueAliases *ucd.PropertyValueAliases
	}{
		GeneratorName:        "generator/main.go",
		UnicodeData:          unicodeData,
		PropertyValueAliases: propValAliases,
	})
	if err != nil {
		return err
	}
	f, err := os.OpenFile("../compiler/ucd_table.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		return err
	}
	defer f.Close()
	fmt.Fprintf(f, b.String())
	return nil
}