-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (85 loc) · 2.52 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"log"
"os"
"github.com/artheus/swaggable/antlr/parser"
"github.com/artheus/swaggable/mapping"
"github.com/artheus/swaggable/swagger"
yaml "gopkg.in/yaml.v2"
"github.com/antlr/antlr4/runtime/Go/antlr"
)
type TreeShapeListener struct {
*parser.BaseSwaggableParserListener
}
var components map[string]*swagger.Component = make(map[string]*swagger.Component)
var baseElements map[string]*mapping.BaseElement = make(map[string]*mapping.BaseElement)
func NewTreeShapeListener() *TreeShapeListener {
return new(TreeShapeListener)
}
func (this *TreeShapeListener) EnterObjDeclaration(ctx *parser.ObjDeclarationContext) {
var comp = mapping.CreateComponent(ctx)
components[comp.Name] = comp
}
func (this *TreeShapeListener) EnterEnumDeclaration(ctx *parser.EnumDeclarationContext) {
var enum = mapping.CreateEnum(ctx)
components[enum.Name] = enum
}
func (this *TreeShapeListener) EnterBaseDeclaration(ctx *parser.BaseDeclarationContext) {
var base = mapping.CreateBase(ctx)
baseElements[base.Name] = base
}
func (s *TreeShapeListener) ExitProgram(ctx *parser.ProgramContext) {
// Base extends Base
for _, b := range baseElements {
for extIndex, ext := range b.GetExtends() {
if base, ok := baseElements[ext]; ok {
for _, baseProp := range base.GetProperties() {
b.AddProperty(baseProp)
}
b.DeleteExtends(extIndex)
}
}
}
// Component extends Base
for i, comp := range components {
for extIndex, ext := range comp.GetExtends() {
if base, ok := baseElements[ext]; ok {
for _, baseProp := range base.GetProperties() {
comp.AddProperty(baseProp)
}
comp.DeleteExtends(extIndex)
}
}
extComp := comp.CompileExtends()
if extComp != nil {
components[i] = extComp
}
}
root := swagger.NewRoot()
root.Components.AddSchemas(components)
// CRUD paths for components
for _, comp := range components {
for _, prop := range comp.GetProperties() {
if prop.Indexed {
root.AddPath(swagger.CreateGetAndDeleteByIndexPath(comp, prop))
}
}
root.AddPath(swagger.CreateUpdateAndCreatePath(comp))
}
d, err := yaml.Marshal(&root)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Println(string(d))
}
func main() {
input, _ := antlr.NewFileStream(os.Args[1])
lexer := parser.NewSwaggableLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewSwaggableParser(stream)
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
p.BuildParseTrees = true
tree := p.Program()
antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)
}