Skip to content

Commit fbb4ccd

Browse files
author
kuba--
committed
handle multiple source files
1 parent 40432c9 commit fbb4ccd

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<img src="logo.png" name="logo" width="200" />
2-
1+
<img src="logo.png" name="logo" width="200" />
2+
33

44

55
# uast2pl
@@ -10,10 +10,10 @@ It takes *UAST* node as an input and tries to produce equivalent prolog program.
1010
### Tools
1111
- `./cmd/uast2pl -f fib.py -o fib.py.pl`.
1212
Transforms source code into _uast_ (thanks to babelfish) and next transforms _uast_ into a prolog representation.
13-
```bash
13+
```
1414
Usage of ./uast2pl:
15-
-f string
16-
input file to parse
15+
-f value
16+
list of input source files
1717
-o string
1818
output file (by default stdio)
1919
-s string
@@ -22,14 +22,14 @@ Usage of ./uast2pl:
2222

2323
- `./cmd/qpl -f fib.py.pl -q "identifier([_, Name, [_, Start, _], _])."`.
2424
Embedded [_wam_](https://en.wikipedia.org/wiki/Warren_Abstract_Machine) lets query prolog DB.
25-
```bash
25+
```
2626
Usage of ./qpl:
2727
-f value
2828
list of input prolog files
29-
-o string
30-
output file (by default stdio)
3129
-q string
3230
prolog query
31+
-o string
32+
output file (by default stdio)
3333
```
3434

3535
### Example (extract identifiers)

cmd/uast2pl/main.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,39 @@ import (
55
"flag"
66
"io"
77
"os"
8+
"strings"
89

910
bblfsh "github.com/bblfsh/go-client/v4"
11+
1012
"github.com/mloncode/uast2pl"
1113
)
1214

15+
type stringList []string
16+
17+
func (l *stringList) String() string {
18+
return strings.Join(*l, ",")
19+
}
20+
21+
func (l *stringList) Set(value string) error {
22+
*l = append(*l, value)
23+
return nil
24+
}
25+
1326
var (
14-
input string
27+
files stringList
1528
host string
1629
output string
1730
)
1831

1932
func init() {
20-
flag.StringVar(&input, "f", "", "input file to parse")
33+
flag.Var(&files, "f", "list of input source files")
2134
flag.StringVar(&host, "s", "localhost:9432", "address:port of babelfish server")
2235
flag.StringVar(&output, "o", "", "output file (by default stdio)")
2336
}
2437

2538
func main() {
2639
flag.Parse()
27-
if input == "" {
40+
if len(files) == 0 {
2841
flag.Usage()
2942
os.Exit(1)
3043
}
@@ -35,11 +48,15 @@ func main() {
3548
}
3649
defer client.Close()
3750

51+
nodes := make([]bblfsh.Node, len(files))
3852
req := client.NewParseRequest()
39-
req = req.ReadFile(input)
40-
n, _, err := req.UAST()
41-
if err != nil {
42-
panic(err)
53+
for i, f := range files {
54+
req = req.ReadFile(f)
55+
n, _, err := req.UAST()
56+
if err != nil {
57+
panic(err)
58+
}
59+
nodes[i] = n
4360
}
4461

4562
var w io.Writer = os.Stdout
@@ -52,7 +69,7 @@ func main() {
5269
}
5370
}
5471

55-
if err = uast2pl.WriteNode(w, n); err != nil {
72+
if err = uast2pl.WriteNode(w, nodes...); err != nil {
5673
panic(err)
5774
}
5875
}

import.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% join is a private predicate - the same predicate as stdlib's append/3 or merge/3 (from list module).
1+
% join_ is a private predicate - the same predicate as stdlib's append/3 or merge/3 (from list module).
22
join_([], List, List).
33
join_([Head | Tail], List, [Head | NewList]) :- !, join_(Tail, List, NewList).
44

list.pl

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
merge([], L, L).
2-
merge([H | T], L, [H | LL]) :- !, merge(T, L, LL).
1+
% merge is the same predicate as stdlib's append/3
2+
merge([], List, List).
3+
merge([Head | Tail], List, [Head | NewList]) :- !, merge(Tail, List, NewList).
34

5+
% flatten is true if FlatList is a non-nested version of NestedList.
6+
flatten([Head | Tail], FlatList) :- !, flatten(Head, HeadFlatList), flatten(Tail, TailFlatList), merge(HeadFlatList, TailFlatList, FlatList).
7+
flatten(Elem, [Elem]) :- !.
48
flatten([], []) :- !.
5-
flatten([H | T], L) :- !, flatten(H, HL), flatten(T, TL), merge(HL, TL, L).
6-
flatten(L, [L]) :- !.

uast2pl.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ import (
1111
"github.com/bblfsh/sdk/v3/uast/role"
1212
)
1313

14-
// WriteNode writes bblfsh Node as prolog terms and predicates.
15-
func WriteNode(w io.Writer, n nodes.Node) error {
14+
// WriteNode writes bblfsh node(s) as prolog terms and predicates.
15+
func WriteNode(w io.Writer, n ...nodes.Node) (err error) {
1616
pl := &uast2pl{
1717
w: w,
1818
valuesMap: make(map[string]int),
1919
startEndsMap: make(map[uast.Position]int),
2020
rolesMap: make(map[role.Role]int),
2121
}
22-
_, err := pl.writeNode(n)
22+
23+
for _, ni := range n {
24+
if _, err = pl.writeNode(ni); err != nil {
25+
return err
26+
}
27+
}
2328

2429
err = pl.writeDeclaration(err, "value", "Val", pl.values)
2530
err = pl.writeDeclaration(err, "array", "[Arguments]", pl.arrays)
@@ -118,6 +123,7 @@ func (pl *uast2pl) writeValue(val nodes.Value) (string, error) {
118123
quote := ""
119124
if val.Kind() == nodes.KindString {
120125
quote = "'"
126+
v = strings.TrimSpace(v)
121127
}
122128

123129
i = len(pl.values)

0 commit comments

Comments
 (0)