aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2022-05-09 20:41:54 +0900
committerRyo Nihei <nihei.dev@gmail.com>2022-05-10 23:14:52 +0900
commit0ff00d27b2fd524c76fcfac1836b7aad8fe03069 (patch)
treefb52505656b0b9f66ac6efafa8a05f82ad54b015
parentAdd ordered symbol notation (diff)
downloadurubu-0ff00d27b2fd524c76fcfac1836b7aad8fe03069.tar.gz
urubu-0ff00d27b2fd524c76fcfac1836b7aad8fe03069.tar.xz
Change the suffix of a description file from -description.json to -report.json
-rw-r--r--README.md4
-rw-r--r--cmd/vartan/compile.go6
-rw-r--r--cmd/vartan/show.go44
-rw-r--r--grammar/grammar.go20
-rw-r--r--grammar/parsing_table.go4
-rw-r--r--spec/description.go2
6 files changed, 40 insertions, 40 deletions
diff --git a/README.md b/README.md
index 047f6c2..66cf005 100644
--- a/README.md
+++ b/README.md
@@ -115,10 +115,10 @@ When `vartan parse` command successfully parses the input data, it prints a CST
#### 3.2. Resolve conflicts
-`vartan compile` command also generates a description file having `-description.json` suffix along with a parsing table. This file describes each state in the parsing table in detail. If your grammar contains conflicts, see `Conflicts` and `States` sections of this file. Using `vartan show` command, you can see the description file in a readable format.
+`vartan compile` command also generates a report named `*-report.json`. This file describes each state in the parsing table in detail. If your grammar contains conflicts, see `Conflicts` and `States` sections of this file. Using `vartan show` command, you can see the report in a readable format.
```sh
-$ vartan show expr-description.json
+$ vartan show expr-report.json
```
### 4. Generate a parser
diff --git a/cmd/vartan/compile.go b/cmd/vartan/compile.go
index 69de14b..16eef16 100644
--- a/cmd/vartan/compile.go
+++ b/cmd/vartan/compile.go
@@ -107,14 +107,14 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
return err
}
- var descFileName string
+ var reportFileName string
{
_, grmFileName := filepath.Split(grmPath)
- descFileName = fmt.Sprintf("%v-description.json", strings.TrimSuffix(grmFileName, ".vr"))
+ reportFileName = fmt.Sprintf("%v-report.json", strings.TrimSuffix(grmFileName, ".vr"))
}
opts := []grammar.CompileOption{
- grammar.EnableDescription(descFileName),
+ grammar.EnableReporting(reportFileName),
}
switch strings.ToLower(*compileFlags.class) {
case "slr":
diff --git a/cmd/vartan/show.go b/cmd/vartan/show.go
index f6203f1..8094515 100644
--- a/cmd/vartan/show.go
+++ b/cmd/vartan/show.go
@@ -18,8 +18,8 @@ import (
func init() {
cmd := &cobra.Command{
Use: "show",
- Short: "Print a description file in readable format",
- Example: ` vartan show grammar-description.json`,
+ Short: "Print a report in a readable format",
+ Example: ` vartan show grammar-report.json`,
Args: cobra.ExactArgs(1),
RunE: runShow,
}
@@ -51,12 +51,12 @@ func runShow(cmd *cobra.Command, args []string) (retErr error) {
}
}()
- desc, err := readDescription(args[0])
+ report, err := readReport(args[0])
if err != nil {
return err
}
- err = writeDescription(os.Stdout, desc)
+ err = writeReport(os.Stdout, report)
if err != nil {
return err
}
@@ -64,10 +64,10 @@ func runShow(cmd *cobra.Command, args []string) (retErr error) {
return nil
}
-func readDescription(path string) (*spec.Description, error) {
+func readReport(path string) (*spec.Report, error) {
f, err := os.Open(path)
if err != nil {
- return nil, fmt.Errorf("Cannot open the description file %s: %w", path, err)
+ return nil, fmt.Errorf("Cannot open the report %s: %w", path, err)
}
defer f.Close()
@@ -76,16 +76,16 @@ func readDescription(path string) (*spec.Description, error) {
return nil, err
}
- desc := &spec.Description{}
- err = json.Unmarshal(d, desc)
+ report := &spec.Report{}
+ err = json.Unmarshal(d, report)
if err != nil {
return nil, err
}
- return desc, nil
+ return report, nil
}
-const descTemplate = `# Class
+const reportTemplate = `# Class
{{ .Class }}
@@ -127,20 +127,20 @@ const descTemplate = `# Class
{{ end -}}
{{ end }}`
-func writeDescription(w io.Writer, desc *spec.Description) error {
+func writeReport(w io.Writer, report *spec.Report) error {
termName := func(sym int) string {
- if desc.Terminals[sym].Alias != "" {
- return desc.Terminals[sym].Alias
+ if report.Terminals[sym].Alias != "" {
+ return report.Terminals[sym].Alias
}
- return desc.Terminals[sym].Name
+ return report.Terminals[sym].Name
}
nonTermName := func(sym int) string {
- return desc.NonTerminals[sym].Name
+ return report.NonTerminals[sym].Name
}
termAssoc := func(sym int) string {
- switch desc.Terminals[sym].Associativity {
+ switch report.Terminals[sym].Associativity {
case "l":
return "left"
case "r":
@@ -151,7 +151,7 @@ func writeDescription(w io.Writer, desc *spec.Description) error {
}
prodAssoc := func(prod int) string {
- switch desc.Productions[prod].Associativity {
+ switch report.Productions[prod].Associativity {
case "l":
return "left"
case "r":
@@ -162,10 +162,10 @@ func writeDescription(w io.Writer, desc *spec.Description) error {
}
fns := template.FuncMap{
- "printConflictSummary": func(desc *spec.Description) string {
+ "printConflictSummary": func(report *spec.Report) string {
var implicitlyResolvedCount int
var explicitlyResolvedCount int
- for _, s := range desc.States {
+ for _, s := range report.States {
for _, c := range s.SRConflict {
if c.ResolvedBy == grammar.ResolvedByShift.Int() {
implicitlyResolvedCount++
@@ -250,7 +250,7 @@ func writeDescription(w io.Writer, desc *spec.Description) error {
return fmt.Sprintf("%4v %v %v %v", prod.Number, prec, assoc, b.String())
},
"printItem": func(item spec.Item) string {
- prod := desc.Productions[item.Production]
+ prod := report.Productions[item.Production]
var b strings.Builder
fmt.Fprintf(&b, "%v →", nonTermName(prod.LHS))
@@ -327,12 +327,12 @@ func writeDescription(w io.Writer, desc *spec.Description) error {
},
}
- tmpl, err := template.New("").Funcs(fns).Parse(descTemplate)
+ tmpl, err := template.New("").Funcs(fns).Parse(reportTemplate)
if err != nil {
return err
}
- err = tmpl.Execute(w, desc)
+ err = tmpl.Execute(w, report)
if err != nil {
return err
}
diff --git a/grammar/grammar.go b/grammar/grammar.go
index a5d6cb7..0cde454 100644
--- a/grammar/grammar.go
+++ b/grammar/grammar.go
@@ -1206,15 +1206,15 @@ const (
)
type compileConfig struct {
- descriptionFileName string
- class Class
+ reportFileName string
+ class Class
}
type CompileOption func(config *compileConfig)
-func EnableDescription(fileName string) CompileOption {
+func EnableReporting(fileName string) CompileOption {
return func(config *compileConfig) {
- config.descriptionFileName = fileName
+ config.reportFileName = fileName
}
}
@@ -1342,31 +1342,31 @@ func Compile(gram *Grammar, opts ...CompileOption) (*spec.CompiledGrammar, error
return nil, err
}
- desc, err := b.genDescription(tab, gram)
+ report, err := b.genReport(tab, gram)
if err != nil {
return nil, err
}
- if config.descriptionFileName != "" {
- f, err := os.OpenFile(config.descriptionFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ if config.reportFileName != "" {
+ f, err := os.OpenFile(config.reportFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
defer f.Close()
- d, err := json.Marshal(desc)
+ d, err := json.Marshal(report)
if err != nil {
return nil, err
}
_, err = f.Write(d)
if err != nil {
- return nil, fmt.Errorf("failed to write a description file: %w", err)
+ return nil, fmt.Errorf("failed to write a report: %w", err)
}
}
var implicitlyResolvedCount int
- for _, s := range desc.States {
+ for _, s := range report.States {
for _, c := range s.SRConflict {
if c.ResolvedBy == ResolvedByShift.Int() {
implicitlyResolvedCount++
diff --git a/grammar/parsing_table.go b/grammar/parsing_table.go
index 1e3930b..57badd5 100644
--- a/grammar/parsing_table.go
+++ b/grammar/parsing_table.go
@@ -312,7 +312,7 @@ func (b *lrTableBuilder) resolveSRConflict(sym symbolNum, prod productionNum) (A
return ActionTypeReduce, ResolvedByPrec
}
-func (b *lrTableBuilder) genDescription(tab *ParsingTable, gram *Grammar) (*spec.Description, error) {
+func (b *lrTableBuilder) genReport(tab *ParsingTable, gram *Grammar) (*spec.Report, error) {
var terms []*spec.Terminal
{
termSyms := b.symTab.terminalSymbols()
@@ -552,7 +552,7 @@ func (b *lrTableBuilder) genDescription(tab *ParsingTable, gram *Grammar) (*spec
}
}
- return &spec.Description{
+ return &spec.Report{
Class: string(b.class),
Terminals: terms,
NonTerminals: nonTerms,
diff --git a/spec/description.go b/spec/description.go
index e82c509..4102455 100644
--- a/spec/description.go
+++ b/spec/description.go
@@ -65,7 +65,7 @@ type State struct {
RRConflict []*RRConflict `json:"rr_conflict"`
}
-type Description struct {
+type Report struct {
Class string `json:"class"`
Terminals []*Terminal `json:"terminals"`
NonTerminals []*NonTerminal `json:"non_terminals"`