nac3parser: add comment support #68

Merged
pca006132 merged 6 commits from with_nac3comment into master 2021-11-05 20:46:42 +08:00
11 changed files with 928 additions and 9 deletions
Showing only changes of commit 032e1d84cf - Show all commits

View File

@ -164,4 +164,67 @@ class Foo(A, B):
let parse_ast = parse_expression(&source).unwrap();
insta::assert_debug_snapshot!(parse_ast);
}
#[test]
fn test_more_comment() {
let source = "\
a: int # nac3: sf1
# nac3: sdf4
for i in (1, '12'): # nac3: sf2
a: int
# nac3: 3
# nac3: 5
while i < 2: # nac3: 4
# nac3: real pass
pass
# nac3: expr1
# nac3: expr3
1 + 2 # nac3: expr2
# nac3: if3
# nac3: if1
if 1: # nac3: if2
3";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_sample_comment() {
let source = "\
# nac3: while1
# nac3: while2
# normal comment
while test: # nac3: while3
# nac3: simple assign0
a = 3 # nac3: simple assign1
";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_comment_ambiguity() {
let source = "\
if a: d; # nac3: for d
if b: c # nac3: for c
if d: # nac3: for if d
b; b + 3; # nac3: for b + 3
a = 3; a + 3; b = a; # nac3: notif
# nac3: smallsingle1
# nac3: smallsingle3
aa = 3 # nac3: smallsingle2
if a: # nac3: small2
a
for i in a: # nac3: for1
pass
";
insta::assert_debug_snapshot!(parse_program(&source).unwrap());
}
#[test]
fn test_comment_should_fail() {
let source = "\
if a: # nac3: something
a = 3
";
assert!(parse_program(&source).is_err());
}
}

View File

@ -0,0 +1,448 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 1,
column: 4,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 1,
column: 7,
},
custom: (),
node: Name {
id: "d",
ctx: Load,
},
},
config_comment: [
"for d",
],
},
},
],
orelse: [],
config_comment: [],
},
},
Located {
location: Location {
row: 2,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 2,
column: 4,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 2,
column: 7,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 2,
column: 7,
},
custom: (),
node: Name {
id: "c",
ctx: Load,
},
},
config_comment: [
"for c",
],
},
},
],
orelse: [],
config_comment: [],
},
},
Located {
location: Location {
row: 3,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 3,
column: 4,
},
custom: (),
node: Name {
id: "d",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
config_comment: [],
},
},
Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 4,
column: 10,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
op: Add,
right: Located {
location: Location {
row: 4,
column: 12,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
},
},
config_comment: [
"for b + 3",
],
},
},
],
orelse: [],
config_comment: [
"for if d",
],
},
},
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 5,
column: 5,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [],
},
},
Located {
location: Location {
row: 5,
column: 8,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 5,
column: 10,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 5,
column: 8,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
op: Add,
right: Located {
location: Location {
row: 5,
column: 12,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
},
},
config_comment: [],
},
},
Located {
location: Location {
row: 5,
column: 15,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 5,
column: 15,
},
custom: (),
node: Name {
id: "b",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 5,
column: 19,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
type_comment: None,
config_comment: [
"notif",
],
},
},
Located {
location: Location {
row: 8,
column: 1,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 8,
column: 1,
},
custom: (),
node: Name {
id: "aa",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 8,
column: 6,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [
"smallsingle1",
"smallsingle3",
"smallsingle2",
],
},
},
Located {
location: Location {
row: 9,
column: 1,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 9,
column: 4,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 10,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 10,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
config_comment: [],
},
},
],
orelse: [],
config_comment: [
"small2",
],
},
},
Located {
location: Location {
row: 11,
column: 1,
},
custom: (),
node: For {
target: Located {
location: Location {
row: 11,
column: 5,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 11,
column: 10,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Pass {
config_comment: [],
},
},
],
orelse: [],
type_comment: None,
config_comment: [
"for1",
],
},
},
]

View File

@ -0,0 +1,313 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: AnnAssign {
target: Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
annotation: Located {
location: Location {
row: 1,
column: 4,
},
custom: (),
node: Name {
id: "int",
ctx: Load,
},
},
value: None,
simple: true,
config_comment: [
"sf1",
],
},
},
Located {
location: Location {
row: 2,
column: 1,
},
custom: (),
node: For {
target: Located {
location: Location {
row: 3,
column: 5,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
iter: Located {
location: Location {
row: 3,
column: 11,
},
custom: (),
node: Tuple {
elts: [
Located {
location: Location {
row: 3,
column: 11,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
Located {
location: Location {
row: 3,
column: 15,
},
custom: (),
node: Constant {
value: Str(
"12",
),
kind: None,
},
},
],
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: AnnAssign {
target: Located {
location: Location {
row: 4,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
annotation: Located {
location: Location {
row: 4,
column: 8,
},
custom: (),
node: Name {
id: "int",
ctx: Load,
},
},
value: None,
simple: true,
config_comment: [],
},
},
],
orelse: [],
type_comment: None,
config_comment: [
"sdf4",
"sf2",
],
},
},
Located {
location: Location {
row: 5,
column: 1,
},
custom: (),
node: While {
test: Located {
location: Location {
row: 7,
column: 9,
},
custom: (),
node: Compare {
left: Located {
location: Location {
row: 7,
column: 7,
},
custom: (),
node: Name {
id: "i",
ctx: Load,
},
},
ops: [
Lt,
],
comparators: [
Located {
location: Location {
row: 7,
column: 11,
},
custom: (),
node: Constant {
value: Int(
2,
),
kind: None,
},
},
],
},
},
body: [
Located {
location: Location {
row: 9,
column: 5,
},
custom: (),
node: Pass {
config_comment: [
"real pass",
],
},
},
Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 12,
column: 7,
},
custom: (),
node: BinOp {
left: Located {
location: Location {
row: 12,
column: 5,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
op: Add,
right: Located {
location: Location {
row: 12,
column: 9,
},
custom: (),
node: Constant {
value: Int(
2,
),
kind: None,
},
},
},
},
config_comment: [
"expr1",
"expr3",
"expr2",
],
},
},
Located {
location: Location {
row: 13,
column: 5,
},
custom: (),
node: If {
test: Located {
location: Location {
row: 15,
column: 8,
},
custom: (),
node: Constant {
value: Int(
1,
),
kind: None,
},
},
body: [
Located {
location: Location {
row: 16,
column: 9,
},
custom: (),
node: Expr {
value: Located {
location: Location {
row: 16,
column: 9,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
config_comment: [],
},
},
],
orelse: [],
config_comment: [
"if3",
"if1",
"if2",
],
},
},
],
orelse: [],
config_comment: [
"3",
"5",
"4",
],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
@ -74,12 +75,15 @@ expression: parse_program(&source).unwrap()
column: 3,
},
custom: (),
node: Pass,
node: Pass {
config_comment: [],
},
},
],
decorator_list: [],
returns: None,
type_comment: None,
config_comment: [],
},
},
Located {
@ -145,16 +149,20 @@ expression: parse_program(&source).unwrap()
column: 3,
},
custom: (),
node: Pass,
node: Pass {
config_comment: [],
},
},
],
decorator_list: [],
returns: None,
type_comment: None,
config_comment: [],
},
},
],
decorator_list: [],
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -45,6 +45,7 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
@ -90,6 +91,7 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
@ -114,12 +116,15 @@ expression: parse_ast
kind: None,
},
},
config_comment: [],
},
},
],
config_comment: [],
},
},
],
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -73,6 +73,7 @@ expression: parse_ast
],
},
},
config_comment: [],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
[
Located {
@ -85,6 +86,7 @@ expression: parse_ast
},
},
},
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
@ -60,6 +60,7 @@ expression: parse_ast
keywords: [],
},
},
config_comment: [],
},
},
]

View File

@ -1,6 +1,7 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_ast
---
[
Located {
@ -46,6 +47,7 @@ expression: parse_ast
keywords: [],
},
},
config_comment: [],
},
},
]

View File

@ -1,5 +1,5 @@
---
source: parser/src/parser.rs
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
@ -86,6 +86,7 @@ expression: parse_program(&source).unwrap()
},
},
type_comment: None,
config_comment: [],
},
},
]

View File

@ -0,0 +1,75 @@
---
source: nac3parser/src/parser.rs
expression: parse_program(&source).unwrap()
---
[
Located {
location: Location {
row: 1,
column: 1,
},
custom: (),
node: While {
test: Located {
location: Location {
row: 4,
column: 7,
},
custom: (),
node: Name {
id: "test",
ctx: Load,
},
},
body: [
Located {
location: Location {
row: 6,
column: 5,
},
custom: (),
node: Assign {
targets: [
Located {
location: Location {
row: 6,
column: 5,
},
custom: (),
node: Name {
id: "a",
ctx: Load,
},
},
],
value: Located {
location: Location {
row: 6,
column: 9,
},
custom: (),
node: Constant {
value: Int(
3,
),
kind: None,
},
},
type_comment: None,
config_comment: [
"simple assign0",
"simple assign1",
],
},
},
],
orelse: [],
config_comment: [
"while1",
"while2",
"while3",
],
},
},
]