Skip to content

Commit

Permalink
Merge pull request #43 from ReconfigureIO/feature/position-information
Browse files Browse the repository at this point in the history
Add position information to statements, expressions, and declarations.
  • Loading branch information
patrickt authored Feb 17, 2017
2 parents 57fb361 + 8db5769 commit 23357ff
Show file tree
Hide file tree
Showing 23 changed files with 1,828 additions and 865 deletions.
25 changes: 22 additions & 3 deletions cmd/goblin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"github.com/ReconfigureIO/goblin"
"go/ast"
"go/parser"
"go/token"
"os"
Expand All @@ -15,6 +16,7 @@ var version string = "unspecified"

func main() {
versionFlag := flag.Bool("v", false, "display goblin version")
builtinDumpFlag := flag.Bool("builtin-dump", false, "use go/ast to dump the file, not JSON")
fileFlag := flag.String("file", "", "file to parse")
stmtFlag := flag.String("stmt", "", "statement to parse")
exprFlag := flag.String("expr", "", "expression to parse")
Expand All @@ -27,14 +29,31 @@ func main() {
println(version)
return
} else if *fileFlag != "" {
file, err := os.Open(*fileFlag)
if err != nil {
panic(err)
}
info, err := file.Stat()
if err != nil {
panic(err)
}

size := info.Size()
file.Close()

fset.AddFile(*fileFlag, -1, int(size))

f, err := parser.ParseFile(fset, *fileFlag, nil, parser.ParseComments)
if err != nil {
panic(err)
}

// Inspect the AST and print all identifiers and literals.
val, _ := goblin.DumpFile(f, fset)
os.Stdout.Write(val)
if *builtinDumpFlag {
ast.Print(fset, f)
} else {
val, _ := goblin.DumpFile(f, fset)
os.Stdout.Write(val)
}
} else if *exprFlag != "" {
val, _ := json.Marshal(goblin.TestExpr(*exprFlag))
os.Stdout.Write(val)
Expand Down
22 changes: 1 addition & 21 deletions fixtures/expressions/addition/addition.json
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
{
"type": "expression",
"right": {
"value": {
"value": "matic",
"kind": "ident"
},
"type": "identifier",
"kind": "expression"
},
"operator": "+",
"left": {
"value": {
"value": "ill",
"kind": "ident"
},
"type": "identifier",
"kind": "expression"
},
"kind": "binary"
}
{"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"}
76 changes: 53 additions & 23 deletions fixtures/expressions/chancast/chan.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
{
"type": "cast",
"kind": "expression",
"coerced-to": {
"kind": "type",
"type": "chan",
"direction": "both",
"value": {
"kind": "type",
"type": "identifier",
"value": {
"kind": "ident",
"value": "int"
}
}
},
"target": {
"kind": "expression",
"type": "identifier",
"value": {
"kind": "ident",
"value": "foo"
}
}
"type" : "cast",
"target" : {
"kind" : "expression",
"position" : {
"line" : 0,
"offset" : 0,
"column" : 0,
"filename" : ""
},
"value" : {
"position" : {
"filename" : "",
"offset" : 0,
"column" : 0,
"line" : 0
},
"kind" : "ident",
"value" : "foo"
},
"type" : "identifier"
},
"position" : {
"line" : 0,
"filename" : "",
"offset" : 0,
"column" : 0
},
"kind" : "expression",
"coerced-to" : {
"value" : {
"type" : "identifier",
"value" : {
"kind" : "ident",
"position" : {
"line" : 0,
"filename" : "",
"offset" : 0,
"column" : 0
},
"value" : "int"
},
"kind" : "type",
"position" : {
"line" : 0,
"filename" : "",
"column" : 0,
"offset" : 0
}
},
"direction" : "both",
"kind" : "type",
"type" : "chan"
}
}
66 changes: 48 additions & 18 deletions fixtures/expressions/doublequalifier/double.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
{
"kind": "expression",
"type": "selector",
"target": {
"kind": "expression",
"type": "identifier",
"qualifier": {
"kind": "ident",
"value": "foo"
},
"value": {
"kind": "ident",
"value": "bar"
}
},
"field": {
"kind": "ident",
"value": "baz"
}
"kind" : "expression",
"field" : {
"value" : "baz",
"kind" : "ident",
"position" : {
"filename" : "",
"offset" : 0,
"line" : 0,
"column" : 0
}
},
"type" : "selector",
"target" : {
"position" : {
"column" : 0,
"filename" : "",
"offset" : 0,
"line" : 0
},
"type" : "identifier",
"value" : {
"value" : "bar",
"kind" : "ident",
"position" : {
"column" : 0,
"filename" : "",
"line" : 0,
"offset" : 0
}
},
"kind" : "expression",
"qualifier" : {
"position" : {
"filename" : "",
"offset" : 0,
"line" : 0,
"column" : 0
},
"value" : "foo",
"kind" : "ident"
}
},
"position" : {
"offset" : 0,
"line" : 0,
"filename" : "",
"column" : 0
}
}
43 changes: 31 additions & 12 deletions fixtures/expressions/logicalor/logicalor.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
{
"kind": "binary",
"left": {
"kind": "literal",
"type": "BOOL",
"value": "false"
},
"operator": "||",
"right": {
"kind": "literal",
"type": "BOOL",
"value": "true"
}
"position" : {
"offset" : 0,
"line" : 0,
"column" : 0,
"filename" : ""
},
"right" : {
"type" : "BOOL",
"value" : "true",
"kind" : "literal",
"position" : {
"offset" : 0,
"column" : 0,
"line" : 0,
"filename" : ""
}
},
"left" : {
"type" : "BOOL",
"position" : {
"offset" : 0,
"filename" : "",
"column" : 0,
"line" : 0
},
"value" : "false",
"kind" : "literal"
},
"kind" : "binary",
"operator" : "||",
"type" : "expression"
}
Loading

0 comments on commit 23357ff

Please sign in to comment.