Debian Codegraph @

main.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Gocc is LR1 parser generator for go written in go. The generator uses a BNF with very easy to use SDT rules.
16 // Please see https://github.com/goccmack/gocc/ for more documentation.
17 package main
18
19 import (
20 "bytes"
21 "flag"
22 "fmt"
23 "os"
24 "path"
25 "strings"
26
27 "github.com/goccmack/gocc/internal/ast"
28 "github.com/goccmack/gocc/internal/config"
29 "github.com/goccmack/gocc/internal/frontend/parser"
30 "github.com/goccmack/gocc/internal/frontend/scanner"
31 "github.com/goccmack/gocc/internal/frontend/token"
32 "github.com/goccmack/gocc/internal/io"
33 genLexer "github.com/goccmack/gocc/internal/lexer/gen/golang"
34 lexItems "github.com/goccmack/gocc/internal/lexer/items"
35 "github.com/goccmack/gocc/internal/parser/first"
36 genParser "github.com/goccmack/gocc/internal/parser/gen"
37 lr1Action "github.com/goccmack/gocc/internal/parser/lr1/action"
38 lr1Items "github.com/goccmack/gocc/internal/parser/lr1/items"
39 "github.com/goccmack/gocc/internal/parser/symbols"
40 outToken "github.com/goccmack/gocc/internal/token"
41 genToken "github.com/goccmack/gocc/internal/token/gen"
42 genUtil "github.com/goccmack/gocc/internal/util/gen"
43 "github.com/goccmack/gocc/internal/util/md"
44 )
45
46 func main() {
47 flag.Usage = usage
48 cfg, err := config.New()
49 if err != nil {
50 fmt.Printf("Error reading configuration: %s\n", err)
51 flag.Usage()
52 }
53
54 if cfg.Verbose() {
55 cfg.PrintParams()
56 }
57
58 if cfg.Help() {
59 flag.Usage()
60 }
61
62 scanner := &scanner.Scanner{}
63 srcBuffer := getSource(cfg)
64
65 scanner.Init(srcBuffer, token.FRONTENDTokens)
66 parser := parser.NewParser(parser.ActionTable, parser.GotoTable, parser.ProductionsTable, token.FRONTENDTokens)
67 grammar, err := parser.Parse(scanner)
68 if err != nil {
69 fmt.Printf("Parse error: %s\n", err)
70 os.Exit(1)
71 }
72
73 g := grammar.(*ast.Grammar)
74
75 gSymbols := symbols.NewSymbols(g)
76 if cfg.Verbose() {
77 writeTerminals(gSymbols, cfg)
78 }
79
80 var tokenMap *outToken.TokenMap
81
82 gSymbols.Add(g.LexPart.TokenIds()...)
83 g.LexPart.UpdateStringLitTokens(gSymbols.ListStringLitSymbols())
84 lexSets := lexItems.GetItemSets(g.LexPart)
85 if cfg.Verbose() {
86 io.WriteFileString(path.Join(cfg.OutDir(), "lexer_sets.txt"), lexSets.String())
87 }
88 tokenMap = outToken.NewTokenMap(gSymbols.ListTerminals())
89 if !cfg.NoLexer() {
90 genLexer.Gen(cfg.Package(), cfg.OutDir(), g.LexPart.Header.SDTLit, lexSets, tokenMap, cfg)
91 }
92
93 if g.SyntaxPart != nil {
94 firstSets := first.GetFirstSets(g, gSymbols)
95 if cfg.Verbose() {
96 io.WriteFileString(path.Join(cfg.OutDir(), "first.txt"), firstSets.String())
97 }
98
99 lr1Sets := lr1Items.GetItemSets(g, gSymbols, firstSets)
100 if cfg.Verbose() {
101 io.WriteFileString(path.Join(cfg.OutDir(), "LR1_sets.txt"), lr1Sets.String())
102 }
103
104 conflicts := genParser.Gen(cfg.Package(), cfg.OutDir(), g.SyntaxPart.Header.SDTLit, g.SyntaxPart.ProdList, gSymbols, lr1Sets, tokenMap, cfg)
105 handleConflicts(conflicts, lr1Sets.Size(), cfg, g.SyntaxPart.ProdList)
106 }
107
108 genToken.Gen(cfg.Package(), cfg.OutDir(), tokenMap)
109 genUtil.Gen(cfg.OutDir())
110 }
111
112 func getSource(cfg config.Config) []byte {
113 if strings.HasSuffix(cfg.SourceFile(), ".md") {
114 str, err := md.GetSource(cfg.SourceFile())
115 if err != nil {
116 fmt.Println(err)
117 os.Exit(1)
118 }
119 return []byte(str)
120 }
121 srcBuffer, err := os.ReadFile(cfg.SourceFile())
122 if err != nil {
123 fmt.Println(err)
124 os.Exit(1)
125 }
126 return srcBuffer
127 }
128
129 func usage() {
130 fmt.Fprintf(os.Stderr, "usage: gocc flags bnf_file\n\n")
131 fmt.Fprintf(os.Stderr, " bnf_file: contains the BNF grammar\n\n")
132 fmt.Fprintf(os.Stderr, "Flags:\n")
133 flag.PrintDefaults()
134 os.Exit(1)
135 }
136
137 func handleConflicts(conflicts map[int]lr1Items.RowConflicts, numSets int, cfg config.Config, prods ast.SyntaxProdList) {
138 if len(conflicts) <= 0 {
139 return
140 }
141 fmt.Printf("%d LR-1 conflicts \n", len(conflicts))
142 if cfg.Verbose() {
143 io.WriteFileString(path.Join(cfg.OutDir(), "LR1_conflicts.txt"), conflictString(conflicts, numSets, prods))
144 }
145 if !cfg.AutoResolveLRConf() {
146 os.Exit(1)
147 }
148 }
149
150 func conflictString(conflicts map[int]lr1Items.RowConflicts, numSets int, prods ast.SyntaxProdList) string {
151 w := new(strings.Builder)
152 fmt.Fprintf(w, "%d LR-1 conflicts: \n", len(conflicts))
153 for i := 0; i < numSets; i++ {
154 if cnf, exist := conflicts[i]; exist {
155 fmt.Fprintf(w, "\tS%d\n", i)
156 for sym, conflicts := range cnf {
157 fmt.Fprintf(w, "\t\tsymbol: %s\n", sym)
158 for _, cflct := range conflicts {
159 switch c := cflct.(type) {
160 case lr1Action.Reduce:
161 fmt.Fprintf(w, "\t\t\tReduce(%d:%s)\n", c, prods[c])
162 case lr1Action.Shift:
163 fmt.Fprintf(w, "\t\t\t%s\n", cflct)
164 default:
165 panic(fmt.Sprintf("unexpected type of action: %s", cflct))
166 }
167 }
168 }
169 }
170 }
171 return w.String()
172 }
173
174 func writeTerminals(gSymbols *symbols.Symbols, cfg config.Config) {
175 buf := new(bytes.Buffer)
176 for _, t := range gSymbols.ListTerminals() {
177 fmt.Fprintf(buf, "%s\n", t)
178 }
179 io.WriteFile(path.Join(cfg.OutDir(), "terminals.txt"), buf.Bytes())
180 }