Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
cli: support UAST mode parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Denys Smirnov <denys@sourced.tech>
  • Loading branch information
Denys Smirnov authored and dennwc committed Aug 28, 2018
1 parent 0032d4a commit 765dc68
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
27 changes: 21 additions & 6 deletions cmd/bblfsh-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
Host string `short:"a" long:"host" description:"Babelfish endpoint address" default:"localhost:9432"`
Language string `short:"l" long:"language" description:"language to parse (default: auto)"`
Query string `short:"q" long:"query" description:"XPath query applied to the resulting UAST"`
Mode string `short:"m" long:"mode" description:"UAST transformation mode: semantic, annotated, native"`
V2 bool `long:"v2" description:"return UAST in v2 format"`
}
args, err := flags.Parse(&opts)
Expand All @@ -50,21 +51,35 @@ func main() {

var ast interface{}
if opts.V2 {
nodes, _, err := client.NewParseRequestV2().
req := client.NewParseRequestV2().
Language(opts.Language).
Filename(filename).
ReadFile(filename).
UAST()
ReadFile(filename)
if opts.Mode != "" {
m, err := bblfsh.ParseMode(opts.Mode)
if err != nil {
fatalf("%v", err)
}
req = req.Mode(m)
}
nodes, _, err := req.UAST()
if err != nil {
fatalf("couldn't parse %s: %v", args[0], err)
}
ast = nodes
} else {
res, err := client.NewParseRequest().
req := client.NewParseRequest().
Language(opts.Language).
Filename(filename).
ReadFile(filename).
Do()
ReadFile(filename)
if opts.Mode != "" {
m, err := bblfsh.ParseMode(opts.Mode)
if err != nil {
fatalf("%v", err)
}
req = req.Mode(m)
}
res, err := req.Do()
if err != nil {
fatalf("couldn't parse %s: %v", args[0], err)
}
Expand Down
14 changes: 14 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ const (
Semantic = protocol2.Mode_Semantic
)

// Parse mode parses a UAST mode string to an enum value.
func ParseMode(mode string) (Mode, error) {
// TODO: define this function in SDK
switch mode {
case "native":
return Native, nil
case "annotated":
return Annotated, nil
case "semantic":
return Semantic, nil
}
return 0, fmt.Errorf("unsupported mode: %q", mode)
}

// Mode controls the level of transformation applied to UAST.
func (r *ParseRequestV2) Mode(mode Mode) *ParseRequestV2 {
r.internal.Mode = mode
Expand Down

0 comments on commit 765dc68

Please sign in to comment.