Skip to content

Commit dd41d69

Browse files
committed
add access path for expr
1 parent 899a977 commit dd41d69

File tree

9 files changed

+121
-18
lines changed

9 files changed

+121
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ We welcome your feedback and contributions. Please feel free to submit pull requ
2222
- [Emmyrc Config](./docs/config/emmyrc_json_EN.md)
2323
- [Formatting Config](https://github.com/CppCXY/EmmyLuaCodeStyle/blob/master/README_EN.md)
2424

25-
## Install
26-
27-
TODO
28-
2925
## Build
3026

3127
```shell

crates/emmylua_doc_cli/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
This is a command line tool for generating EmmyLua doc from Lua source code.
44

5-
## Install
6-
7-
```shell
8-
cargo install emmylua_doc_cli
9-
```
10-
115
## Usage
126

137
```shell

crates/emmylua_parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "emmylua_parser"
3-
version = "0.9.0"
3+
version = "0.9.1"
44
edition = "2021"
55
authors = ["CppCXY"]
66
description = "A parser for EmmyLua and luals"

crates/emmylua_parser/src/syntax/node/lua/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
LuaAstToken, LuaIndexToken, LuaLiteralToken, LuaSyntaxNode, LuaSyntaxToken, LuaTokenKind,
99
};
1010

11-
use super::{LuaBlock, LuaCallArgList, LuaIndexKey, LuaParamList, LuaTableField};
11+
use super::{path_trait::PathTrait, LuaBlock, LuaCallArgList, LuaIndexKey, LuaParamList, LuaTableField};
1212

1313
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1414
pub enum LuaExpr {
@@ -221,6 +221,8 @@ impl LuaNameExpr {
221221
}
222222
}
223223

224+
impl PathTrait for LuaNameExpr {}
225+
224226
impl From<LuaNameExpr> for LuaVarExpr {
225227
fn from(expr: LuaNameExpr) -> Self {
226228
LuaVarExpr::NameExpr(expr)
@@ -312,6 +314,8 @@ impl LuaIndexExpr {
312314
}
313315
}
314316

317+
impl PathTrait for LuaIndexExpr {}
318+
315319
impl From<LuaIndexExpr> for LuaVarExpr {
316320
fn from(expr: LuaIndexExpr) -> Self {
317321
LuaVarExpr::IndexExpr(expr)
@@ -370,6 +374,8 @@ impl LuaCallExpr {
370374
}
371375
}
372376

377+
impl PathTrait for LuaCallExpr {}
378+
373379
impl From<LuaCallExpr> for LuaExpr {
374380
fn from(expr: LuaCallExpr) -> Self {
375381
LuaExpr::CallExpr(expr)

crates/emmylua_parser/src/syntax/node/lua/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod expr;
22
mod stat;
3+
mod path_trait;
4+
mod test;
35

46
use crate::{
57
kind::{LuaSyntaxKind, LuaTokenKind},
@@ -9,6 +11,7 @@ use crate::{
911

1012
pub use expr::*;
1113
pub use stat::*;
14+
pub use path_trait::*;
1215

1316
use super::{LuaLiteralToken, LuaNameToken, LuaNumberToken, LuaStringToken};
1417

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::LuaAstNode;
2+
3+
use super::{LuaExpr, LuaIndexKey};
4+
5+
pub trait PathTrait : LuaAstNode {
6+
fn get_access_path(&self) -> Option<String> {
7+
let mut paths = Vec::new();
8+
let mut current_node = self.syntax().clone();
9+
loop {
10+
match LuaExpr::cast(current_node)? {
11+
LuaExpr::NameExpr(name_expr) => {
12+
let name = name_expr.get_name_text()?;
13+
if paths.is_empty() {
14+
return Some(name);
15+
} else {
16+
paths.push(name);
17+
paths.reverse();
18+
return Some(paths.join("."));
19+
}
20+
}
21+
LuaExpr::CallExpr(call_expr) => {
22+
let prefix_expr = call_expr.get_prefix_expr()?;
23+
current_node = prefix_expr.syntax().clone();
24+
}
25+
LuaExpr::IndexExpr(index_expr) => {
26+
match index_expr.get_index_key()? {
27+
LuaIndexKey::String(s) => {
28+
paths.push(s.get_value());
29+
}
30+
LuaIndexKey::Name(name) => {
31+
paths.push(name.get_name_text().to_string());
32+
}
33+
LuaIndexKey::Integer(i) => {
34+
paths.push(i.get_int_value().to_string());
35+
}
36+
LuaIndexKey::Expr(expr) => {
37+
let text = format!("[{}]", expr.syntax().text());
38+
paths.push(text);
39+
}
40+
}
41+
42+
current_node = index_expr.get_prefix_expr()?.syntax().clone();
43+
}
44+
_ => return None,
45+
}
46+
}
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::{LuaAstNode, LuaCallExpr, LuaIndexExpr, LuaNameExpr, LuaParser, LuaSyntaxTree, ParserConfig, PathTrait};
4+
5+
fn get_tree(code: &str) -> LuaSyntaxTree {
6+
let config = ParserConfig::default();
7+
let tree = LuaParser::parse(code, config);
8+
tree
9+
}
10+
11+
12+
#[test]
13+
fn test_call_access_path() {
14+
let code = "call.ddd()";
15+
let tree = get_tree(code);
16+
let root = tree.get_chunk_node();
17+
let call_expr = root.descendants::<LuaCallExpr>().next().unwrap();
18+
assert_eq!(call_expr.get_access_path().unwrap(), "call.ddd");
19+
}
20+
21+
#[test]
22+
fn test_call_access_path2() {
23+
let code = "call[1].aaa.bbb.ccc()";
24+
let tree = get_tree(code);
25+
let root = tree.get_chunk_node();
26+
let call_expr = root.descendants::<LuaCallExpr>().next().unwrap();
27+
assert_eq!(call_expr.get_access_path().unwrap(), "call.1.aaa.bbb.ccc");
28+
}
29+
30+
#[test]
31+
fn test_name_access_path() {
32+
let code = "local a = name";
33+
let tree = get_tree(code);
34+
let root = tree.get_chunk_node();
35+
let name_expr = root.descendants::<LuaNameExpr>().next().unwrap();
36+
assert_eq!(name_expr.get_access_path().unwrap(), "name");
37+
}
38+
39+
#[test]
40+
fn test_index_expr_access_path() {
41+
let code = "local a = name.bbb.ccc";
42+
let tree = get_tree(code);
43+
let root = tree.get_chunk_node();
44+
let index_expr = root.descendants::<LuaIndexExpr>().next().unwrap();
45+
assert_eq!(index_expr.get_access_path().unwrap(), "name.bbb.ccc");
46+
}
47+
48+
#[test]
49+
fn test_index_expr_access_path2() {
50+
let code = "local a = name[okok.yes]";
51+
let tree = get_tree(code);
52+
let root = tree.get_chunk_node();
53+
let index_expr = root.descendants::<LuaIndexExpr>().next().unwrap();
54+
assert_eq!(index_expr.get_access_path().unwrap(), "name.[okok.yes]");
55+
}
56+
}

crates/emmylua_parser/src/syntax/traits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub trait LuaAstNode {
5858
WalkEvent::Leave(node) => N::cast(node).map(WalkEvent::Leave),
5959
})
6060
}
61-
61+
6262
fn ancestors<N: LuaAstNode>(&self) -> impl Iterator<Item = N> {
6363
self.syntax().ancestors().filter_map(N::cast)
6464
}
@@ -89,8 +89,8 @@ pub trait LuaAstNode {
8989
LuaSyntaxId::from_node(self.syntax())
9090
}
9191

92-
fn dump(&self) {
93-
println!("{:#?}", self.syntax());
92+
fn dump(&self) -> String {
93+
format!("{:#?}", self.syntax())
9494
}
9595
}
9696

@@ -158,8 +158,8 @@ pub trait LuaAstToken {
158158
self.syntax().parent_ancestors().filter_map(N::cast)
159159
}
160160

161-
fn dump(&self) {
162-
println!("{:#?}", self.syntax());
161+
fn dump(&self) -> String {
162+
format!("{:#?}", self.syntax())
163163
}
164164
}
165165

0 commit comments

Comments
 (0)