-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf.go
78 lines (65 loc) · 1.87 KB
/
ebnf.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
package build
import (
"github.com/peter-mount/go-build/core"
"github.com/peter-mount/go-build/util/makefile"
"github.com/peter-mount/go-build/util/makefile/target"
"github.com/peter-mount/go-build/util/meta"
"github.com/peter-mount/go-script/parser"
"html"
"os"
"path/filepath"
"strings"
)
// EBNF generates EBNF and railroad diagrams of the parser
type EBNF struct {
Encoder *core.Encoder `kernel:"inject"`
Build *core.Build `kernel:"inject"`
Ebnf *string `kernel:"flag,ebnf,Output Parser EBNF"`
}
func (s *EBNF) Start() error {
s.Build.Documentation(0, s.documentation)
if *s.Ebnf != "" {
return s.ebnf()
}
return nil
}
func (s *EBNF) documentation(_ makefile.Builder, target target.Builder, _ *meta.Meta) {
srcDir := "documentation/go-script/parser"
ebnf := filepath.Join(srcDir, "ebnf.txt")
target.PhonyTarget(ebnf).
MkDir(srcDir).
Echo("GEN EBNF", ebnf).
BuildTool("-ebnf", ebnf, "-d", *s.Encoder.Dest)
}
func (s *EBNF) ebnf() error {
p := parser.New()
ebnf := p.EBNF()
err := os.MkdirAll(filepath.Dir(*s.Ebnf), 0755)
// The raw ebnf file. Not needed in the final documentation
// as we include it below, but needed for the railroad diagrams
if err == nil {
err = os.WriteFile(*s.Ebnf, []byte(ebnf), 0644)
}
// the html page forming part of the documentation
if err == nil {
err = os.WriteFile(
filepath.Join(
filepath.Dir(*s.Ebnf),
strings.ReplaceAll(filepath.Base(*s.Ebnf), ".txt", ".html"),
),
[]byte(`---
editingNote: "This page is generated, any changes will be lost"
type: "manual"
title: "Extended Backus-Naur Form"
titleClass: section
linkTitle: "EBNF"
weight: 1
description: "Extended Backus-Naur Form of the scripting language"
---
<p>This is the Extended Backus-Naur Form (EBNF) description of the scripting language:</p>
<div class="sourceCode">`+html.EscapeString(ebnf)+`</div>
`),
0644)
}
return err
}