Skip to content

Instantly share code, notes, and snippets.

@dennwc

dennwc/bblfsh.go Secret

Created June 15, 2017 19:21
Show Gist options
  • Save dennwc/8d6a69db88cf89438541a49a26e81ad6 to your computer and use it in GitHub Desktop.
Save dennwc/8d6a69db88cf89438541a49a26e81ad6 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/bblfsh/sdk/protocol"
"google.golang.org/grpc"
"context"
"os"
"flag"
"io/ioutil"
"log"
"time"
"fmt"
)
func checkErr(err error) {
if err != nil {
panic(err)
}
}
var (
f_lang = flag.String("lang", "", "programming language")
)
func main() {
flag.Parse()
if flag.NArg() == 0 {
log.Println("no files specified")
os.Exit(1)
}
cc, err := grpc.Dial("localhost:9432", grpc.WithInsecure())
checkErr(err)
defer cc.Close()
cli := protocol.NewProtocolServiceClient(cc)
ctx := context.TODO()
for _, arg := range flag.Args() {
log.Println("processing", arg)
data, err := ioutil.ReadFile(arg)
checkErr(err)
cctx, cancel := context.WithTimeout(ctx, time.Second*10)
res, err := cli.ParseUAST(cctx, &protocol.ParseUASTRequest{
Filename:arg,
Language: *f_lang,
Content: string(data),
})
select {
case <-cctx.Done():
log.Println("timeout on", arg)
continue
default:
cancel()
checkErr(err)
if res.Status != protocol.Ok {
checkErr(fmt.Errorf("status: %v: %v", res.Status, res.Errors))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment