Skip to content

Commit

Permalink
fix: raw identifiers in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Jan 20, 2024
1 parent dd3f915 commit 5680281
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 49 deletions.
6 changes: 3 additions & 3 deletions internal/rust/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ func (l *Language) ParseExports(file *rust_grammar.File) (*language.ExportsEntri
})
} else {
exports = append(exports, language.ExportEntry{
Names: []language.ExportName{{Original: use.Name.Original, Alias: use.Name.Alias}},
Names: []language.ExportName{{Original: string(use.Name.Original), Alias: string(use.Name.Alias)}},
Path: path,
})
}
}
case stmt.Pub != nil:
exports = append(exports, language.ExportEntry{
Names: []language.ExportName{{Original: stmt.Pub.Name}},
Names: []language.ExportName{{Original: string(stmt.Pub.Name)}},
Path: file.Path,
})
case stmt.Mod != nil && stmt.Mod.Pub:
exports = append(exports, language.ExportEntry{
Names: []language.ExportName{{Original: stmt.Mod.Name}},
Names: []language.ExportName{{Original: string(stmt.Mod.Name)}},
Path: file.Path,
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/rust/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ func (l *Language) ParseImports(file *rust_grammar.File) (*language.ImportsResul
})
} else {
imports = append(imports, language.ImportEntry{
Names: []string{use.Name.Original},
Names: []string{string(use.Name.Original)},
Path: id,
})
}
}
} else if stmt.Mod != nil && !stmt.Mod.Local {
names := []string{stmt.Mod.Name}
names := []string{string(stmt.Mod.Name)}

thisDir := filepath.Dir(file.Path)

var modPath string
if p := filepath.Join(thisDir, stmt.Mod.Name+".rs"); utils.FileExists(p) {
if p := filepath.Join(thisDir, string(stmt.Mod.Name)+".rs"); utils.FileExists(p) {
modPath = p
} else if p = filepath.Join(thisDir, stmt.Mod.Name, "mod.rs"); utils.FileExists(p) {
} else if p = filepath.Join(thisDir, string(stmt.Mod.Name), "mod.rs"); utils.FileExists(p) {
modPath = p
} else {
errors = append(errors, fmt.Errorf("could not find mod %s while looking in dir %s", stmt.Mod.Name, thisDir))
Expand Down
10 changes: 5 additions & 5 deletions internal/rust/mod_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ func makeModTree(mainPath string, name string, parent *ModTree) (*ModTree, error
for _, stmt := range file.Statements {
if stmt.Mod != nil {
if stmt.Mod.Local {
modTree.Children[stmt.Mod.Name] = &ModTree{
Name: stmt.Mod.Name,
modTree.Children[string(stmt.Mod.Name)] = &ModTree{
Name: string(stmt.Mod.Name),
Path: mainPath,
}
} else {
var modPath string
if p := filepath.Join(searchPath, stmt.Mod.Name+".rs"); utils.FileExists(p) {
if p := filepath.Join(searchPath, string(stmt.Mod.Name)+".rs"); utils.FileExists(p) {
modPath = p
} else if p = filepath.Join(searchPath, stmt.Mod.Name, "mod.rs"); utils.FileExists(p) {
} else if p = filepath.Join(searchPath, string(stmt.Mod.Name), "mod.rs"); utils.FileExists(p) {
modPath = p
} else {
return nil, fmt.Errorf(`could not find mod "%s" in path "%s"`, stmt.Mod.Name, searchPath)
}
modTree.Children[stmt.Mod.Name], err = makeModTree(modPath, stmt.Mod.Name, modTree)
modTree.Children[string(stmt.Mod.Name)], err = makeModTree(modPath, string(stmt.Mod.Name), modTree)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/rust/rust_grammar/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
{"PathSep", `::`},
{"Punct", `[,{}()]`},
{"String", `"(?:\\.|[^"])*"`},
{"Ident", `[_$a-zA-Z][_$a-zA-Z0-9]*`},
{"Ident", `(r#)?[_$a-zA-Z][_$a-zA-Z0-9]*`},
{"Comment", `//.*|/\*(.|\n)*?\*/`},
{"Whitespace", `\s+`},
{"ANY", `.`},
Expand Down
21 changes: 18 additions & 3 deletions internal/rust/rust_grammar/mod.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
//nolint:govet
package rust_grammar

import (
"strings"
)

type Ident string

func (i *Ident) Capture(values []string) error {
if strings.HasPrefix(values[0], "r#") {
*i = Ident(values[0][2:])
} else {
*i = Ident(values[0])
}
return nil
}

type Mod struct {
Pub bool `@"pub"? ("(" (Ident | PathSep)* ")")? "mod"`
Name string `@Ident`
Local bool `@"{"?`
Pub bool `@"pub"? ("(" (Ident | PathSep)* ")")? "mod"`
Name Ident `@Ident`
Local bool `@"{"?`
}
15 changes: 11 additions & 4 deletions internal/rust/rust_grammar/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ func TestMod(t *testing.T) {
{
Name: "mod mod",
ExpectedMods: []Mod{{
Name: "mod",
Name: Ident("mod"),
}},
},
{
Name: "pub mod mod",
ExpectedMods: []Mod{{
Pub: true,
Name: "mod",
Name: Ident("mod"),
}},
},
{
Name: "pub mod r#async",
ExpectedMods: []Mod{{
Pub: true,
Name: Ident("async"),
}},
},
{
Name: "pub(in crate::this) mod mod",
ExpectedMods: []Mod{{
Pub: true,
Name: "mod",
Name: Ident("mod"),
}},
},
{
Name: "mod mod {}",
ExpectedMods: []Mod{{
Name: "mod",
Name: Ident("mod"),
Local: true,
}},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/rust/rust_grammar/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
package rust_grammar

type Pub struct {
Name string `"pub" ("(" (Ident | PathSep)* ")")? "unsafe"? "async"? ("fn" | "struct" | "trait" | "enum" | "type" | "static" | "const") @Ident`
Name Ident `"pub" ("(" (Ident | PathSep)* ")")? "unsafe"? "async"? ("fn" | "struct" | "trait" | "enum" | "type" | "static" | "const") @Ident`
}
22 changes: 11 additions & 11 deletions internal/rust/rust_grammar/pub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,61 @@ func TestPub(t *testing.T) {
{
Name: "pub fn my_function",
ExpectedPub: []Pub{{
Name: "my_function",
Name: Ident("my_function"),
}},
},
{
Name: "pub unsafe fn my_function",
ExpectedPub: []Pub{{
Name: "my_function",
Name: Ident("my_function"),
}},
},
{
Name: "pub trait my_trait",
ExpectedPub: []Pub{{
Name: "my_trait",
Name: Ident("my_trait"),
}},
},
{
Name: "pub struct my_struct",
ExpectedPub: []Pub{{
Name: "my_struct",
Name: Ident("my_struct"),
}},
},
{
Name: "pub enum my_enum",
ExpectedPub: []Pub{{
Name: "my_enum",
Name: Ident("my_enum"),
}},
},
{
Name: "pub type my_type",
ExpectedPub: []Pub{{
Name: "my_type",
Name: Ident("my_type"),
}},
},
{
Name: "pub(crate) fn my_function and a lot of shit after",
ExpectedPub: []Pub{{
Name: "my_function",
Name: Ident("my_function"),
}},
},
{
Name: "pub async fn my_function ",
ExpectedPub: []Pub{{
Name: "my_function",
Name: Ident("my_function"),
}},
},
{
Name: "pub static VAR",
ExpectedPub: []Pub{{
Name: "VAR",
Name: Ident("VAR"),
}},
},
{
Name: "pub const VAR",
ExpectedPub: []Pub{{
Name: "VAR",
Name: Ident("VAR"),
}},
},
{
Expand All @@ -81,7 +81,7 @@ func TestPub(t *testing.T) {
{
Name: "' pub struct MyStruct '",
ExpectedPub: []Pub{{
Name: "MyStruct",
Name: Ident("MyStruct"),
}},
},
}
Expand Down
10 changes: 6 additions & 4 deletions internal/rust/rust_grammar/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package rust_grammar

type Name struct {
Original string `@Ident`
Alias string `("as" @Ident)?`
Original Ident `@Ident`
Alias Ident `("as" @Ident)?`
}

type UsePath struct {
PathSlices []string `(@Ident PathSep)*`
PathSlices []Ident `(@Ident PathSep)*`
All bool `(@ALL |`
Name *Name ` @@ |`
UsePaths []*UsePath ` "{" @@ ("," @@)* "}" )`
Expand All @@ -27,7 +27,9 @@ type FlattenUse struct {

func flatten(node *UsePath, pathSlices []string, isPub bool) []FlattenUse {
currentSlices := pathSlices
currentSlices = append(currentSlices, node.PathSlices...)
for _, pathSlice := range node.PathSlices {
currentSlices = append(currentSlices, string(pathSlice))
}
switch {
case node.All:
return []FlattenUse{
Expand Down
26 changes: 13 additions & 13 deletions internal/rust/rust_grammar/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,49 @@ func TestUse(t *testing.T) {
Name: "use something::something;",
ExpectedUse: []FlattenUse{{
PathSlices: []string{"something"},
Name: Name{Original: "something"},
Name: Name{Original: Ident("something")},
}},
},
{
Name: "use something::something as another;",
ExpectedUse: []FlattenUse{{
PathSlices: []string{"something"},
Name: Name{Original: "something", Alias: "another"},
Name: Name{Original: Ident("something"), Alias: Ident("another")},
}},
},
{
Name: "pub use something::something;",
ExpectedUse: []FlattenUse{{
Pub: true,
PathSlices: []string{"something"},
Name: Name{Original: "something"},
Name: Name{Original: Ident("something")},
}},
},
{
Name: "pub ( crate) use something ::something ;",
ExpectedUse: []FlattenUse{{
Pub: true,
PathSlices: []string{"something"},
Name: Name{Original: "something"},
Name: Name{Original: Ident("something")},
}},
},
{
Name: "use something::{something};",
ExpectedUse: []FlattenUse{{
PathSlices: []string{"something"},
Name: Name{Original: "something"},
Name: Name{Original: Ident("something")},
}},
},
{
Name: "use something::{one, OrAnother};",
ExpectedUse: []FlattenUse{
{
PathSlices: []string{"something"},
Name: Name{Original: "one"},
Name: Name{Original: Ident("one")},
},
{
PathSlices: []string{"something"},
Name: Name{Original: "OrAnother"},
Name: Name{Original: Ident("OrAnother")},
},
},
},
Expand All @@ -66,19 +66,19 @@ func TestUse(t *testing.T) {
ExpectedUse: []FlattenUse{
{
PathSlices: []string{"something"},
Name: Name{Original: "one", Alias: "two"},
Name: Name{Original: Ident("one"), Alias: Ident("two")},
},
{
PathSlices: []string{"something"},
Name: Name{Original: "OrAnother"},
Name: Name{Original: Ident("OrAnother")},
},
},
},
{
Name: "use one::very_long::veeery_long::path::something;",
ExpectedUse: []FlattenUse{{
PathSlices: []string{"one", "very_long", "veeery_long", "path"},
Name: Name{Original: "something"},
Name: Name{Original: Ident("something")},
}},
},
{
Expand All @@ -94,12 +94,12 @@ func TestUse(t *testing.T) {
{
Pub: true,
PathSlices: []string{"crate", "ast"},
Name: Name{Original: "Node"},
Name: Name{Original: Ident("Node")},
},
{
Pub: true,
PathSlices: []string{"crate", "ast"},
Name: Name{Original: "Operator"},
Name: Name{Original: Ident("Operator")},
},
},
},
Expand All @@ -108,7 +108,7 @@ func TestUse(t *testing.T) {
ExpectedUse: []FlattenUse{
{
PathSlices: []string{"crate", "one"},
Name: Name{Original: "One"},
Name: Name{Original: Ident("One")},
},
{
PathSlices: []string{"crate", "another"},
Expand Down

0 comments on commit 5680281

Please sign in to comment.