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
|
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
verr "error"
"grammar"
spec "spec/grammar"
"spec/grammar/parser"
)
var compileFlags = struct {
output *string
}{}
/*
func init() {
cmd := &cobra.Command{
Use: "compile",
Short: "Compile grammar you defined into a parsing table",
Example: ` vartan compile grammar.vartan -o grammar.json`,
Args: cobra.MaximumNArgs(1),
RunE: runCompile,
}
compileFlags.output = cmd.Flags().StringP("output", "o", "", "output file path (default stdout)")
rootCmd.AddCommand(cmd)
}
*/
func runCompile(args []string) (retErr error) {
var tmpDirPath string
defer func() {
if tmpDirPath == "" {
return
}
os.RemoveAll(tmpDirPath)
}()
var grmPath string
if len(args) > 0 {
grmPath = args[0]
}
defer func() {
if retErr != nil {
specErrs, ok := retErr.(verr.SpecErrors)
if ok {
for _, err := range specErrs {
if len(args) > 0 {
err.FilePath = grmPath
err.SourceName = grmPath
} else {
err.FilePath = grmPath
err.SourceName = "stdin"
}
}
}
}
}()
if grmPath == "" {
var err error
tmpDirPath, err = os.MkdirTemp("", "vartan-compile-*")
if err != nil {
return err
}
src, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
grmPath = filepath.Join(tmpDirPath, "stdin.vartan")
err = os.WriteFile(grmPath, src, 0600)
if err != nil {
return err
}
}
gram, report, err := readGrammar(grmPath)
if err != nil {
return err
}
err = writeCompiledGrammarAndReport(gram, report, *compileFlags.output)
if err != nil {
return fmt.Errorf("Cannot write an output files: %w", err)
}
var implicitlyResolvedCount int
for _, s := range report.States {
for _, c := range s.SRConflict {
if c.ResolvedBy == grammar.ResolvedByShift.Int() {
implicitlyResolvedCount++
}
}
for _, c := range s.RRConflict {
if c.ResolvedBy == grammar.ResolvedByProdOrder.Int() {
implicitlyResolvedCount++
}
}
}
if implicitlyResolvedCount > 0 {
fmt.Fprintf(os.Stdout, "%v conflicts\n", implicitlyResolvedCount)
}
return nil
}
func readGrammar(path string) (*spec.CompiledGrammar, *spec.Report, error) {
f, err := os.Open(path)
if err != nil {
return nil, nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err)
}
defer f.Close()
ast, err := parser.Parse(f)
if err != nil {
return nil, nil, err
}
b := grammar.GrammarBuilder{
AST: ast,
}
return b.Build(grammar.EnableReporting())
}
// writeCompiledGrammarAndReport writes a compiled grammar and a report to a files located at a specified path.
// This function selects one of the following output methods depending on how the path is specified.
//
// 1. When the path is a directory path, this function writes the compiled grammar and the report to
// <path>/<grammar-name>.json and <path>/<grammar-name>-report.json files, respectively.
// <grammar-name>-report.json as the output files.
// 2. When the path is a file path or a non-exitent path, this function asumes that the path represents a file
// path for the compiled grammar. Then it also writes the report in the same directory as the compiled grammar.
// The report file is named <grammar-name>.json.
// 3. When the path is an empty string, this function writes the compiled grammar to the stdout and writes
// the report to a file named <current-directory>/<grammar-name>-report.json.
func writeCompiledGrammarAndReport(cgram *spec.CompiledGrammar, report *spec.Report, path string) error {
cgramPath, reportPath, err := makeOutputFilePaths(cgram.Name, path)
if err != nil {
return err
}
{
var cgramW io.Writer
if cgramPath != "" {
cgramFile, err := os.OpenFile(cgramPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer cgramFile.Close()
cgramW = cgramFile
} else {
cgramW = os.Stdout
}
b, err := json.Marshal(cgram)
if err != nil {
return err
}
fmt.Fprintf(cgramW, "%v\n", string(b))
}
{
reportFile, err := os.OpenFile(reportPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer reportFile.Close()
b, err := json.Marshal(report)
if err != nil {
return err
}
fmt.Fprintf(reportFile, "%v\n", string(b))
}
return nil
}
func makeOutputFilePaths(gramName string, path string) (string, string, error) {
reportFileName := gramName + "-report.json"
if path == "" {
wd, err := os.Getwd()
if err != nil {
return "", "", err
}
return "", filepath.Join(wd, reportFileName), nil
}
fi, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return "", "", err
}
if os.IsNotExist(err) || !fi.IsDir() {
dir, _ := filepath.Split(path)
return path, filepath.Join(dir, reportFileName), nil
}
return filepath.Join(path, gramName+".json"), filepath.Join(path, reportFileName), nil
}
|