-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpython-import.go
More file actions
30 lines (24 loc) · 787 Bytes
/
python-import.go
File metadata and controls
30 lines (24 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main
import (
"fmt"
"github.com/alexflint/go-restructure"
)
var importRegexp = restructure.MustCompile(Import{}, restructure.Options{})
// Import matches "import foo" and "import foo as bar"
type Import struct {
_ struct{} `^import\s+`
Package restructure.Submatch `\w+`
Alias *AsName `?`
_ struct{} `$`
}
// AsName matches "as xyz"
type AsName struct {
_ struct{} `\s+as\s+`
Name restructure.Submatch `\w+`
}
func main() {
var imp Import
importRegexp.Find(&imp, "import foo as bar")
fmt.Printf("IMPORT %s (bytes %d...%d)\n", imp.Package.String(), imp.Package.Begin, imp.Package.End)
fmt.Printf(" AS %s (bytes %d...%d)\n", imp.Alias.Name.String(), imp.Alias.Name.Begin, imp.Alias.Name.End)
}