Skip to content

Commit 23357ff

Browse files
authored
Merge pull request #43 from ReconfigureIO/feature/position-information
Add position information to statements, expressions, and declarations.
2 parents 57fb361 + 8db5769 commit 23357ff

File tree

23 files changed

+1828
-865
lines changed

23 files changed

+1828
-865
lines changed

cmd/goblin/main.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"flag"
66
"github.com/ReconfigureIO/goblin"
7+
"go/ast"
78
"go/parser"
89
"go/token"
910
"os"
@@ -15,6 +16,7 @@ var version string = "unspecified"
1516

1617
func main() {
1718
versionFlag := flag.Bool("v", false, "display goblin version")
19+
builtinDumpFlag := flag.Bool("builtin-dump", false, "use go/ast to dump the file, not JSON")
1820
fileFlag := flag.String("file", "", "file to parse")
1921
stmtFlag := flag.String("stmt", "", "statement to parse")
2022
exprFlag := flag.String("expr", "", "expression to parse")
@@ -27,14 +29,31 @@ func main() {
2729
println(version)
2830
return
2931
} else if *fileFlag != "" {
32+
file, err := os.Open(*fileFlag)
33+
if err != nil {
34+
panic(err)
35+
}
36+
info, err := file.Stat()
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
size := info.Size()
42+
file.Close()
43+
44+
fset.AddFile(*fileFlag, -1, int(size))
45+
3046
f, err := parser.ParseFile(fset, *fileFlag, nil, parser.ParseComments)
3147
if err != nil {
3248
panic(err)
3349
}
3450

35-
// Inspect the AST and print all identifiers and literals.
36-
val, _ := goblin.DumpFile(f, fset)
37-
os.Stdout.Write(val)
51+
if *builtinDumpFlag {
52+
ast.Print(fset, f)
53+
} else {
54+
val, _ := goblin.DumpFile(f, fset)
55+
os.Stdout.Write(val)
56+
}
3857
} else if *exprFlag != "" {
3958
val, _ := json.Marshal(goblin.TestExpr(*exprFlag))
4059
os.Stdout.Write(val)
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
{
2-
"type": "expression",
3-
"right": {
4-
"value": {
5-
"value": "matic",
6-
"kind": "ident"
7-
},
8-
"type": "identifier",
9-
"kind": "expression"
10-
},
11-
"operator": "+",
12-
"left": {
13-
"value": {
14-
"value": "ill",
15-
"kind": "ident"
16-
},
17-
"type": "identifier",
18-
"kind": "expression"
19-
},
20-
"kind": "binary"
21-
}
1+
{"kind":"binary","left":{"kind":"expression","position":{"column":0,"filename":"","line":0,"offset":0},"type":"identifier","value":{"kind":"ident","position":{"column":0,"filename":"","line":0,"offset":0},"value":"ill"}},"operator":"+","position":{"column":0,"filename":"","line":0,"offset":0},"right":{"kind":"expression","position":{"column":0,"filename":"","line":0,"offset":0},"type":"identifier","value":{"kind":"ident","position":{"column":0,"filename":"","line":0,"offset":0},"value":"matic"}},"type":"expression"}
Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
{
2-
"type": "cast",
3-
"kind": "expression",
4-
"coerced-to": {
5-
"kind": "type",
6-
"type": "chan",
7-
"direction": "both",
8-
"value": {
9-
"kind": "type",
10-
"type": "identifier",
11-
"value": {
12-
"kind": "ident",
13-
"value": "int"
14-
}
15-
}
16-
},
17-
"target": {
18-
"kind": "expression",
19-
"type": "identifier",
20-
"value": {
21-
"kind": "ident",
22-
"value": "foo"
23-
}
24-
}
2+
"type" : "cast",
3+
"target" : {
4+
"kind" : "expression",
5+
"position" : {
6+
"line" : 0,
7+
"offset" : 0,
8+
"column" : 0,
9+
"filename" : ""
10+
},
11+
"value" : {
12+
"position" : {
13+
"filename" : "",
14+
"offset" : 0,
15+
"column" : 0,
16+
"line" : 0
17+
},
18+
"kind" : "ident",
19+
"value" : "foo"
20+
},
21+
"type" : "identifier"
22+
},
23+
"position" : {
24+
"line" : 0,
25+
"filename" : "",
26+
"offset" : 0,
27+
"column" : 0
28+
},
29+
"kind" : "expression",
30+
"coerced-to" : {
31+
"value" : {
32+
"type" : "identifier",
33+
"value" : {
34+
"kind" : "ident",
35+
"position" : {
36+
"line" : 0,
37+
"filename" : "",
38+
"offset" : 0,
39+
"column" : 0
40+
},
41+
"value" : "int"
42+
},
43+
"kind" : "type",
44+
"position" : {
45+
"line" : 0,
46+
"filename" : "",
47+
"column" : 0,
48+
"offset" : 0
49+
}
50+
},
51+
"direction" : "both",
52+
"kind" : "type",
53+
"type" : "chan"
54+
}
2555
}
Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
{
2-
"kind": "expression",
3-
"type": "selector",
4-
"target": {
5-
"kind": "expression",
6-
"type": "identifier",
7-
"qualifier": {
8-
"kind": "ident",
9-
"value": "foo"
10-
},
11-
"value": {
12-
"kind": "ident",
13-
"value": "bar"
14-
}
15-
},
16-
"field": {
17-
"kind": "ident",
18-
"value": "baz"
19-
}
2+
"kind" : "expression",
3+
"field" : {
4+
"value" : "baz",
5+
"kind" : "ident",
6+
"position" : {
7+
"filename" : "",
8+
"offset" : 0,
9+
"line" : 0,
10+
"column" : 0
11+
}
12+
},
13+
"type" : "selector",
14+
"target" : {
15+
"position" : {
16+
"column" : 0,
17+
"filename" : "",
18+
"offset" : 0,
19+
"line" : 0
20+
},
21+
"type" : "identifier",
22+
"value" : {
23+
"value" : "bar",
24+
"kind" : "ident",
25+
"position" : {
26+
"column" : 0,
27+
"filename" : "",
28+
"line" : 0,
29+
"offset" : 0
30+
}
31+
},
32+
"kind" : "expression",
33+
"qualifier" : {
34+
"position" : {
35+
"filename" : "",
36+
"offset" : 0,
37+
"line" : 0,
38+
"column" : 0
39+
},
40+
"value" : "foo",
41+
"kind" : "ident"
42+
}
43+
},
44+
"position" : {
45+
"offset" : 0,
46+
"line" : 0,
47+
"filename" : "",
48+
"column" : 0
49+
}
2050
}
Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
{
2-
"kind": "binary",
3-
"left": {
4-
"kind": "literal",
5-
"type": "BOOL",
6-
"value": "false"
7-
},
8-
"operator": "||",
9-
"right": {
10-
"kind": "literal",
11-
"type": "BOOL",
12-
"value": "true"
13-
}
2+
"position" : {
3+
"offset" : 0,
4+
"line" : 0,
5+
"column" : 0,
6+
"filename" : ""
7+
},
8+
"right" : {
9+
"type" : "BOOL",
10+
"value" : "true",
11+
"kind" : "literal",
12+
"position" : {
13+
"offset" : 0,
14+
"column" : 0,
15+
"line" : 0,
16+
"filename" : ""
17+
}
18+
},
19+
"left" : {
20+
"type" : "BOOL",
21+
"position" : {
22+
"offset" : 0,
23+
"filename" : "",
24+
"column" : 0,
25+
"line" : 0
26+
},
27+
"value" : "false",
28+
"kind" : "literal"
29+
},
30+
"kind" : "binary",
31+
"operator" : "||",
32+
"type" : "expression"
1433
}

0 commit comments

Comments
 (0)