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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// Package main implements a parser for Thrift files (https://thrift.apache.org/)
//
// It parses namespaces, exceptions, services, structs, consts, typedefs and enums, but is easily
// extensible to more.
//
// It also supports annotations and method throws.
package main
import (
"fmt"
"os"
"strings"
"github.com/alecthomas/kong"
"github.com/alecthomas/repr"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Namespace struct {
Pos lexer.Position
Language string `"namespace" @Ident`
Namespace string `@Ident ( @"." @Ident )*`
}
type Type struct {
Pos lexer.Position
Name string `@Ident ( @"." @Ident )*`
TypeOne *Type `( "<" @@ ( ","`
TypeTwo *Type ` @@ )? ">" )?`
}
type Annotation struct {
Pos lexer.Position
Key string `@Ident ( @"." @Ident )*`
Value *Literal `( "=" @@ )?`
}
type Field struct {
Pos lexer.Position
ID string `@Number ":"`
Requirement string `@( "optional" | "required" )?`
Type *Type `@@`
Name string `@Ident`
Default *Literal `( "=" @@ )?`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )? ";"?`
}
type Exception struct {
Pos lexer.Position
Name string `"exception" @Ident "{"`
Fields []*Field `@@ @@* "}"`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
}
type Struct struct {
Pos lexer.Position
Union bool `( "struct" | @"union" )`
Name string `@Ident "{"`
Fields []*Field `@@* "}"`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
}
type Argument struct {
Pos lexer.Position
ID string `@Number ":"`
Type *Type `@@`
Name string `@Ident`
}
type Throw struct {
Pos lexer.Position
ID string `@Number ":"`
Type *Type `@@`
Name string `@Ident`
}
type Method struct {
Pos lexer.Position
ReturnType *Type `@@`
Name string `@Ident`
Arguments []*Argument `"(" ( @@ ( "," @@ )* )? ")"`
Throws []*Throw `( "throws" "(" @@ ( "," @@ )* ")" )?`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
}
type Service struct {
Pos lexer.Position
Name string `"service" @Ident`
Extends string `( "extends" @Ident ( @"." @Ident )* )?`
Methods []*Method `"{" ( @@ ";"? )* "}"`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
}
// Literal is a "union" type, where only one matching value will be present.
type Literal struct {
Pos lexer.Position
Str *string ` @String`
Number *float64 `| @Number`
Bool *string `| @( "true" | "false" )`
Reference *string `| @Ident ( @"." @Ident )*`
Minus *Literal `| "-" @@`
List []*Literal `| "[" ( @@ ","? )* "]"`
Map []*MapItem `| "{" ( @@ ","? )* "}"`
}
func (l *Literal) GoString() string {
switch {
case l.Str != nil:
return fmt.Sprintf("%q", *l.Str)
case l.Number != nil:
return fmt.Sprintf("%v", *l.Number)
case l.Bool != nil:
return fmt.Sprintf("%v", *l.Bool)
case l.Reference != nil:
return fmt.Sprintf("%s", *l.Reference)
case l.Minus != nil:
return fmt.Sprintf("-%v", l.Minus)
case l.List != nil:
parts := []string{}
for _, e := range l.List {
parts = append(parts, e.GoString())
}
return fmt.Sprintf("[%s]", strings.Join(parts, ", "))
case l.Map != nil:
parts := []string{}
for _, e := range l.Map {
parts = append(parts, e.GoString())
}
return fmt.Sprintf("{%s}", strings.Join(parts, ", "))
}
panic("unsupported?")
}
type MapItem struct {
Pos lexer.Position
Key *Literal `@@ ":"`
Value *Literal `@@`
}
func (m *MapItem) GoString() string {
return fmt.Sprintf("%v: %v", m.Key, m.Value)
}
type Case struct {
Pos lexer.Position
Name string `@Ident`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
Value *Literal `( "=" @@ )? ( "," | ";" )?`
}
type Enum struct {
Pos lexer.Position
Name string `"enum" @Ident "{"`
Cases []*Case `@@* "}"`
Annotations []*Annotation `( "(" @@ ( "," @@ )* ")" )?`
}
type Typedef struct {
Pos lexer.Position
Type *Type `"typedef" @@`
Name string `@Ident`
}
type Const struct {
Pos lexer.Position
Type *Type `"const" @@`
Name string `@Ident`
Value *Literal `"=" @@ ";"?`
}
type Entry struct {
Pos lexer.Position
Includes []string ` "include" @String`
Namespaces []*Namespace `| @@`
Structs []*Struct `| @@`
Exceptions []*Exception `| @@`
Services []*Service `| @@`
Enums []*Enum `| @@`
Typedefs []*Typedef `| @@`
Consts []*Const `| @@`
}
// Thrift files consist of a set of top-level directives and definitions.
//
// The grammar
type Thrift struct {
Pos lexer.Position
Entries []*Entry `@@*`
}
var (
def = lexer.MustSimple([]lexer.SimpleRule{
{"Number", `\d+`},
{"Ident", `\w+`},
{"String", `"[^"]*"`},
{"Whitespace", `\s+`},
{"Punct", `[,.<>(){}=:]`},
{"Comment", `//.*`},
})
parser = participle.MustBuild[Thrift](
participle.Lexer(def),
participle.Unquote(),
participle.Elide("Whitespace"),
)
)
func main() {
var cli struct {
Gen bool `help:"Generate lexer."`
Files []string `help:"Thrift files."`
}
ctx := kong.Parse(&cli)
for _, file := range cli.Files {
r, err := os.Open(file)
ctx.FatalIfErrorf(err, "")
thrift, err := parser.Parse("", r)
ctx.FatalIfErrorf(err, "")
repr.Println(thrift)
}
}
|