Skip to content

Commit e42a7df

Browse files
committed
Ast V1 revisited introducing repateable and epsilon and removing optional and optional repeatable
1 parent 891c6a6 commit e42a7df

File tree

9 files changed

+144
-91
lines changed

9 files changed

+144
-91
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ binding = ident '='
2828
occurrence = ("*" | "+" | "?")
2929
additional = "|" ? parser
3030
transform = "->" '{' rust_code '}'
31-
atom = alter? '(' parser ')' | CHAR | STRING | ident
31+
atom = alter? '(' parser? ')' | CHAR | STRING | ident
3232
alter = ("^" | "!" | "#" | "/")
3333
ident = [a..zA..Z][a..zA..Z0..9_] * - {"let"}
3434
```

lang/v0/ast/src/syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#[derive(Clone, Debug, Eq, PartialEq)]
1818
pub enum ASTParsec {
19+
PEpsilon(),
1920
PIdent(String),
2021
PAtom(char),
2122
PAtoms(Vec<char>),

lang/v0/parser/src/parser.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use celma_v0_core::stream::specs::Stream;
2929
use std::ops::Range;
3030

3131
use celma_v0_ast::syntax::ASTParsec::{
32-
PAtom, PAtoms, PBind, PCheck, PChoice, PCode, PIdent, PMap, PNot, POptional, PRepeat,
32+
PAtom, PAtoms, PBind, PCheck, PChoice, PCode, PEpsilon, PIdent, PMap, PNot, POptional, PRepeat,
3333
PSequence, PTry,
3434
};
3535
use celma_v0_ast::syntax::{ASTParsec, ASTParsecRule};
@@ -214,7 +214,8 @@ where
214214
{
215215
a_char('(')
216216
.and_left(skip())
217-
.and_right(lazy(|| parser(parsec())))
217+
.and_right(lazy(|| parser(parsec())).opt())
218+
.map(|p| p.unwrap_or_else(PEpsilon))
218219
.and_left(skip())
219220
.and_left(a_char(')'))
220221
.or(code().map(PCode))

lang/v0/parser/src/transpiler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
extern crate proc_macro;
1818

1919
use celma_v0_ast::syntax::ASTParsec::{
20-
PAtom, PAtoms, PBind, PCheck, PChoice, PCode, PIdent, PMap, PNot, POptional, PRepeat,
20+
PAtom, PAtoms, PBind, PCheck, PChoice, PCode, PEpsilon, PIdent, PMap, PNot, POptional, PRepeat,
2121
PSequence, PTry,
2222
};
2323
use celma_v0_ast::syntax::{ASTParsec, ASTParsecRule};
@@ -109,6 +109,7 @@ pub trait TranspileBody<E> {
109109
impl TranspileBody<(Option<String>, TokenStream)> for ASTParsec {
110110
fn transpile_body(&self) -> Result<(Option<String>, TokenStream), Error> {
111111
match self {
112+
PEpsilon() => Ok((None, quote! { returns(()) })),
112113
PBind(n, p) => Ok((Some(n.clone()), p.transpile_body()?.1)),
113114
PIdent(n) => {
114115
let n = syn::Ident::new(n, Span::call_site());

lang/v0/parser/tests/parser/celma_parsec_tests.rs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#[cfg(test)]
1818
mod tests_and {
1919
use celma_v0_ast::syntax::ASTParsec::{
20-
PBind, PChoice, PCode, PMap, POptional, PRepeat, PSequence,
20+
PBind, PChoice, PCode, PEpsilon, PMap, POptional, PRepeat, PSequence,
2121
};
2222
use celma_v0_core::parser::response::Response::Success;
2323
use celma_v0_core::parser::specs::Parse;
@@ -30,7 +30,7 @@ mod tests_and {
3030

3131
match response {
3232
Success(ast, _, _) => assert_eq!(ast, PCode(String::from("char(\'a\')"))),
33-
_ => assert_eq!(true, false),
33+
_ => panic!(),
3434
};
3535
}
3636

@@ -42,11 +42,11 @@ mod tests_and {
4242
Success(ast, _, _) => assert_eq!(
4343
ast,
4444
PSequence(
45-
Box::new(PCode(String::from("char(\'a\')"))),
46-
Box::new(PCode(String::from("char(\'b\')"))),
45+
PCode(String::from("char(\'a\')")).wrap(),
46+
PCode(String::from("char(\'b\')")).wrap(),
4747
)
4848
),
49-
_ => assert_eq!(true, false),
49+
_ => panic!(),
5050
};
5151
}
5252

@@ -58,11 +58,11 @@ mod tests_and {
5858
Success(ast, _, _) => assert_eq!(
5959
ast,
6060
PChoice(
61-
Box::new(PCode(String::from("char(\'a\')"))),
62-
Box::new(PCode(String::from("char(\'b\')"))),
61+
PCode(String::from("char(\'a\')")).wrap(),
62+
PCode(String::from("char(\'b\')")).wrap(),
6363
)
6464
),
65-
_ => assert_eq!(true, false),
65+
_ => panic!(),
6666
};
6767
}
6868

@@ -73,12 +73,9 @@ mod tests_and {
7373
match response {
7474
Success(ast, _, _) => assert_eq!(
7575
ast,
76-
PBind(
77-
String::from("c"),
78-
Box::new(PCode(String::from("char(\'a\')"))),
79-
)
76+
PBind(String::from("c"), PCode(String::from("char(\'a\')")).wrap(),)
8077
),
81-
_ => assert_eq!(true, false),
78+
_ => panic!(),
8279
};
8380
}
8481

@@ -91,10 +88,10 @@ mod tests_and {
9188
ast,
9289
PBind(
9390
String::from("c"),
94-
Box::new(POptional(Box::new(PCode(String::from("char(\'a\')"))))),
91+
POptional(PCode(String::from("char(\'a\')")).wrap()).wrap(),
9592
)
9693
),
97-
_ => assert_eq!(true, false),
94+
_ => panic!(),
9895
};
9996
}
10097

@@ -107,10 +104,10 @@ mod tests_and {
107104
ast,
108105
PBind(
109106
String::from("c"),
110-
Box::new(PRepeat(true, Box::new(PCode(String::from("char(\'a\')"))))),
107+
PRepeat(true, PCode(String::from("char(\'a\')")).wrap()).wrap(),
111108
)
112109
),
113-
_ => assert_eq!(true, false),
110+
_ => panic!(),
114111
};
115112
}
116113

@@ -123,10 +120,10 @@ mod tests_and {
123120
ast,
124121
PBind(
125122
String::from("c"),
126-
Box::new(PRepeat(false, Box::new(PCode(String::from("char(\'a\')"))))),
123+
PRepeat(false, PCode(String::from("char(\'a\')")).wrap()).wrap(),
127124
)
128125
),
129-
_ => assert_eq!(true, false),
126+
_ => panic!(),
130127
};
131128
}
132129

@@ -138,17 +135,19 @@ mod tests_and {
138135
Success(ast, _, _) => assert_eq!(
139136
ast,
140137
PSequence(
141-
Box::new(PBind(
138+
PBind(
142139
String::from("a"),
143-
Box::new(PRepeat(false, Box::new(PCode(String::from("char(\'a\')"))))),
144-
)),
145-
Box::new(PBind(
140+
PRepeat(false, PCode(String::from("char(\'a\')")).wrap()).wrap(),
141+
)
142+
.wrap(),
143+
PBind(
146144
String::from("b"),
147-
Box::new(PRepeat(false, Box::new(PCode(String::from("char(\'b\')"))))),
148-
)),
145+
PRepeat(false, PCode(String::from("char(\'b\')")).wrap()).wrap(),
146+
)
147+
.wrap(),
149148
)
150149
),
151-
_ => assert_eq!(true, false),
150+
_ => panic!(),
152151
};
153152
}
154153

@@ -160,17 +159,19 @@ mod tests_and {
160159
Success(ast, _, _) => assert_eq!(
161160
ast,
162161
PChoice(
163-
Box::new(PBind(
162+
PBind(
164163
String::from("a"),
165-
Box::new(PRepeat(false, Box::new(PCode(String::from("char(\'a\')"))))),
166-
)),
167-
Box::new(PBind(
164+
PRepeat(false, PCode(String::from("char(\'a\')")).wrap()).wrap(),
165+
)
166+
.wrap(),
167+
PBind(
168168
String::from("b"),
169-
Box::new(PRepeat(false, Box::new(PCode(String::from("char(\'b\')"))))),
170-
)),
169+
PRepeat(false, PCode(String::from("char(\'b\')")).wrap()).wrap(),
170+
)
171+
.wrap(),
171172
)
172173
),
173-
_ => assert_eq!(true, false),
174+
_ => panic!(),
174175
};
175176
}
176177

@@ -182,14 +183,11 @@ mod tests_and {
182183
Success(ast, _, _) => assert_eq!(
183184
ast,
184185
PMap(
185-
Box::new(PBind(
186-
String::from("a"),
187-
Box::new(PCode(String::from("char(\'a\')"))),
188-
)),
186+
PBind(String::from("a"), PCode(String::from("char(\'a\')")).wrap(),).wrap(),
189187
String::from(" Result(a) "),
190188
)
191189
),
192-
_ => assert_eq!(true, false),
190+
_ => panic!(),
193191
};
194192
}
195193

@@ -203,17 +201,39 @@ mod tests_and {
203201
Success(ast, _, _) => assert_eq!(
204202
ast,
205203
PMap(
206-
Box::new(PBind(
204+
PBind(
207205
String::from("a"),
208-
Box::new(PMap(
209-
Box::new(PCode(String::from("char(\'a\')"))),
206+
PMap(
207+
PCode(String::from("char(\'a\')")).wrap(),
210208
String::from(" 'a' ")
211-
)),
212-
)),
209+
)
210+
.wrap(),
211+
)
212+
.wrap(),
213213
String::from(" Result(a) "),
214214
)
215215
),
216-
_ => assert_eq!(true, false),
216+
_ => panic!(),
217+
};
218+
}
219+
220+
#[test]
221+
fn it_parse_epsilon() {
222+
let response = celma_parsec().parse(CharStream::new("()"));
223+
224+
match response {
225+
Success(ast, _, _) => assert_eq!(ast, PEpsilon(),),
226+
_ => panic!(),
227+
};
228+
}
229+
230+
#[test]
231+
fn it_parse_binded_epsilon() {
232+
let response = celma_parsec().parse(CharStream::new("a=()"));
233+
234+
match response {
235+
Success(ast, _, _) => assert_eq!(ast, PBind(String::from("a"), PEpsilon().wrap()),),
236+
_ => panic!(),
217237
};
218238
}
219239
}

lang/v1/ast/src/syntax.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum ASTType {
3333

3434
#[derive(Clone, Debug, Eq, PartialEq)]
3535
pub enum ASTParsec<I> {
36-
PEpsilon,
36+
PEpsilon(),
3737
PIdent(String),
3838
PAtom(I),
3939
PAtoms(Vec<I>),
@@ -45,8 +45,7 @@ pub enum ASTParsec<I> {
4545
PNot(Box<ASTParsec<I>>),
4646
PTry(Box<ASTParsec<I>>),
4747
PCheck(Box<ASTParsec<I>>),
48-
POptional(Box<ASTParsec<I>>),
49-
PRepeat(bool, Box<ASTParsec<I>>),
48+
PRepeat(Box<ASTParsec<I>>),
5049
}
5150

5251
impl<I> ASTParsec<I> {

lang/v1/normalizer/src/ast.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,37 @@
1414
* limitations under the License.
1515
*/
1616

17+
#[derive(Clone, Debug, Eq, PartialEq)]
1718
pub enum ASTGrammar<A> {
18-
Epsilon,
19-
Token(A),
19+
Epsilon(),
2020
Bottom(),
21+
Var(String),
22+
Token(A),
2123
Seq(Box<AST<A>>, Box<AST<A>>),
2224
Choice(Box<AST<A>>, Box<AST<A>>),
2325
Rec(String, Box<AST<A>>),
24-
Var(String)
2526
}
26-
/*
27-
PAtom(char), // Single char
28-
PAtoms(Vec<char>), // Char sequence
29-
PBind(String, Box<ASTParsec>), // Variable
30-
PCode(String), // Production
31-
PMap(Box<ASTParsec>, String), // Remove?
32-
PNot(Box<ASTParsec>), // ?
33-
PCheck(Box<ASTParsec>), // No capture
3427

35-
-- Pre-normalization
36-
37-
PN : ASTParsec -> (string -> ASTParsec) -> string list -> ASTGrammar
28+
/*
29+
PAtom(char), // Single char
30+
PAtoms(Vec<char>), // Char sequence
31+
PBind(String, Box<ASTParsec>), // Variable
32+
PCode(String), // Production
33+
PMap(Box<ASTParsec>, String), // Remove?
34+
PNot(Box<ASTParsec>), // ?
35+
PCheck(Box<ASTParsec>), // No capture
3836
39-
/ Var(n) if n in l
40-
PN[PIdent(n)]gl = {
41-
\ mu(n,PN[g(n)]g(n::l) otherwise
37+
-- Pre-normalization
4238
43-
PN[PSequence(T1,T2]]gl = Seq(PN[T1]gl,PN[T2]gl)
44-
PN[PChoice(T1,T2)]gl = Choice(PN[T1]gl,PN[T2]gl)
45-
PN[PRepeat(false, T)]gl = Choice(PN[T]gl,PN[PRepeat(true, T)]gl)
39+
PN : ASTParsec -> (string -> ASTParsec) -> string list -> ASTGrammar
4640
47-
PN[PRepeat(true, T)]gl = PN[PChoice(PRepeat(false, T),PEpsilon)]gl
48-
PN[POptional(T)]gl = PN[PChoice(T,PEpsilon)]gl
49-
PN[PTry(T)gl] = PN[T]gl
50-
*/
41+
/ Var(n) if n in l
42+
PN[PIdent(n)]gl = {
43+
\ mu(n,PN[g(n)]g(n::l) otherwise
5144
45+
PN[PEpsilon()]gl = Epsilon()
46+
PN[PSequence(T1,T2]]gl = Seq(PN[T1]gl,PN[T2]gl)
47+
PN[PChoice(T1,T2)]gl = Choice(PN[T1]gl,PN[T2]gl)
48+
PN[PRepeat(T)]gl = PN[T]gl | mu(n,Choice(Seq(PN[T]gl,Var(n)),Epsilon()))
49+
PN[PTry(T)gl] = PN[T]gl
50+
*/

0 commit comments

Comments
 (0)