Skip to content

Commit

Permalink
Change the suffix of a description file from -description.json to -re…
Browse files Browse the repository at this point in the history
…port.json
  • Loading branch information
nihei9 committed May 10, 2022
1 parent dd5fd33 commit 0ff00d2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/vartan/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
44 changes: 22 additions & 22 deletions cmd/vartan/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -51,23 +51,23 @@ 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
}

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()

Expand All @@ -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 }}
Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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++
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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++
Expand Down
4 changes: 2 additions & 2 deletions grammar/parsing_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion spec/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 0ff00d2

Please sign in to comment.