Skip to content

Commit 4305459

Browse files
authored
Merge pull request #361 from Pivot-Studio/griffin/add-ebnf
docs: support ebnf grammar for types
2 parents 499ef36 + 4b6cff0 commit 4305459

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/nomparser/function.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ use super::*;
1616
/// ```ebnf
1717
/// function_def = "fn" identifier "(" (typed_identifier (","typed_identifier)*)? ")" type_name (statement_block | newline) ;
1818
/// ```
19+
#[test_parser(
20+
"
21+
/// this is a comment
22+
gen pub fn f( x: int, y : int ) int {
23+
x = x+1;
24+
return 0;
25+
}
26+
"
27+
)]
1928
#[test_parser(
2029
"gen pub fn f( x: int, y : int ) int {
2130
x = x+1;

src/nomparser/grammar.ebnf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
newline = "\r\n" | "\n" | "\r" ; (*CRLF for windows, LF for unix and macos X, CR for Mac up through version 9*)
3+
letter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N"
4+
| "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
5+
| "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n"
6+
| "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
7+
8+
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
9+
10+
characters = "!" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "(" | ")" | "-" | "_" | "="
11+
| "+" | "[" | "]" | "{" | "}" | ";" | ":" | "'" | "\"" | "," | "." | "<" | ">"
12+
| "/" | "?" | "\\" | "|" | "`" | "~" | " ";
13+
14+
space = " " | "\t"
15+
16+
Comment = { ( "///" | "//" ) { letter | digit | characters } newline };
17+
18+
(*------------------ identifier.rs ----------------------*)
19+
Identifier = (letter | "$" | "_" ) { letter | digit | "_"};
20+
21+
ExternalIdentifier = Identifier { "::" Identifier };
22+
23+
(* todo: typed identifier *)
24+
25+
(*------------------ types.rs ----------------------*)
26+
27+
(*todo: BasicType EBNF is different from the implementation*)
28+
TypeName = { "*" } ( BasicType | ArrayType | ClosureType | TupleType ) ;
29+
BasicType = ExternalIdentifier GenericParamDef?;
30+
ArrayType = "[" TypeName "]";
31+
GenericTypeDef = "<" { "|" Identifier ( ":" MultiTrait )? } ">";
32+
GenericParamDef = "<" TypeName { "|" TypeName } ">";
33+
(*todo: support FunctionDef in the future*)
34+
TraitDef = "pub"? "trait" Identifier GenericTypeDef? ( ":" TypeAdd )? "{" {FunctionDef} "}";
35+
TypeAdd = TypeName { "+" TypeName };
36+
MultiTrait = TypeAdd;
37+
TupleType = "(" (TypeName { "," TypeName })? ")";
38+
ClosureType = "|" TypeName {"," TypeName} "|=>" TypeName;

src/nomparser/types.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ fn array_type(input: Span) -> IResult<Span, Box<TypeNodeEnum>> {
8181
)(input)
8282
}
8383

84-
/// ```enbf
85-
/// generic_type_def = "<" identifier ("|" identifier)* ">" ;
86-
/// ```
87-
/// 形参
8884
pub fn generic_type_def(input: Span) -> IResult<Span, Box<GenericDefNode>> {
8985
map_res(
9086
tuple((
@@ -103,10 +99,6 @@ pub fn generic_type_def(input: Span) -> IResult<Span, Box<GenericDefNode>> {
10399
)(input)
104100
}
105101

106-
/// ```enbf
107-
/// generic_param_def = "<" (extern_id|"_") ("|"(extern_id|"_"))* ">" ;
108-
/// ```
109-
/// 实参
110102
#[test_parser("<a|b|B::c>")]
111103
#[test_parser("<a>")]
112104
pub fn generic_param_def(input: Span) -> IResult<Span, Box<GenericParamNode>> {
@@ -132,9 +124,6 @@ pub fn generic_param_def(input: Span) -> IResult<Span, Box<GenericParamNode>> {
132124
)(input)
133125
}
134126

135-
/// ```enbf
136-
/// trait_def = "trait" identifier generic_type_def? "{" function_def* "}" ;
137-
/// ```
138127
#[test_parser(
139128
"trait mytrait {
140129
fn a() A;
@@ -188,9 +177,6 @@ pub fn trait_def(input: Span) -> IResult<Span, Box<TraitDefNode>> {
188177
)(input)
189178
}
190179

191-
/// ```enbf
192-
/// type_add = type_name ("+" type_name)* ;
193-
/// ```
194180
#[test_parser("a+b+c")]
195181
#[test_parser("a")]
196182
#[test_parser(

0 commit comments

Comments
 (0)