1- use std :: { collections :: HashMap , fmt :: Debug , rc :: Rc } ;
1+ use super :: { stmt :: Statement , Parser , ParserResult } ;
22
3- use crate :: { cursor :: WithCursor , lexer :: tokens :: { LexerToken , LexerTokenKind , ShellCommand } } ;
3+ pub type ProgramTree = Vec < Statement > ;
44
5- use super :: ParserErrorKind ;
6-
7- #[ derive( lang_macro:: EnumVariants , Debug , Clone , PartialEq , Eq ) ]
8- pub enum Expression {
9- Literal ( Box < Literal > ) ,
10- Group ( Box < Expression > ) ,
11- Unary ( Box < ( WithCursor < UnaryOperator > , WithCursor < Expression > ) > ) ,
12- Arithmetic ( Box < ( WithCursor < Expression > , WithCursor < ArithmeticOperator > , WithCursor < Expression > ) > ) ,
13- Logical ( Box < ( WithCursor < Expression > , WithCursor < LogicalOperator > , WithCursor < Expression > ) > ) ,
14- Assignment ( Box < ( WithCursor < Expression > , WithCursor < AssignmentOperator > , WithCursor < Expression > ) > ) ,
15- Range ( Box < ( WithCursor < Expression > , WithCursor < Expression > , bool ) > ) ,
16- ShellCommand ( Box < ShellCommand > ) ,
17- Identifier ( Box < Identifier > ) ,
18- FunctionCall ( Box < ( Identifier , Vec < WithCursor < Expression > > ) > ) ,
19- If ( Box < ( WithCursor < Expression > , WithCursor < Block > , Option < Else > ) > ) ,
20- Match ( Box < ( WithCursor < Expression > , MatchCase ) > ) ,
21- Block ( Box < Block > ) ,
22- }
23-
24- pub type Else = WithCursor < Block > ;
25- pub type MatchCase = HashMap < WithCursor < Literal > , Rc < WithCursor < Expression > > > ;
26-
27-
28- #[ derive( lang_macro:: EnumVariants , Debug , Clone , PartialEq , Eq ) ]
29- pub enum Statement {
30- While ( Box < ( WithCursor < Expression > , WithCursor < Block > ) > ) ,
31- For ( Box < ( Variable , WithCursor < Expression > , WithCursor < Block > ) > ) ,
32- Return ( Box < Option < WithCursor < Expression > > > ) ,
33- If ( Box < WithCursor < Expression > > ) ,
34- Match ( Box < WithCursor < Expression > > ) ,
35- Expression ( Box < WithCursor < Expression > > ) ,
36- Continue ,
37- Break ,
38- Variable ( Box < Variable > ) ,
39- Constant ( Box < Variable > ) ,
40- Function ( Box < Function > ) ,
41- Include ( Box < String > ) ,
42- }
43-
44-
45-
46- pub type ProgramTree = Block ;
47- pub type Identifier = String ;
48- pub type Block = Vec < Statement > ;
49-
50- #[ derive( lang_macro:: EnumVariants , Debug , Clone , PartialEq , Eq , Hash ) ]
51- pub enum Literal {
52- Integer ( isize ) ,
53- Boolean ( bool ) ,
54- String ( Box < String > ) ,
55- }
56-
57- impl TryFrom < LexerToken > for Literal {
58- type Error = crate :: parser:: ParserErrorKind ;
59-
60- fn try_from ( value : LexerToken ) -> Result < Self , Self :: Error > {
61- Ok ( match value. kind {
62- LexerTokenKind :: Integer => Self :: Integer ( * value. as_integer ( ) ?) ,
63- LexerTokenKind :: Boolean => Self :: Boolean ( * value. as_boolean ( ) ?) ,
64- LexerTokenKind :: String => Self :: String ( Box :: from ( value. as_string ( ) ?. to_owned ( ) ) ) ,
65- _ => return Err ( ParserErrorKind :: ConvertError ( value. kind ) )
66- } )
67- }
68- }
69-
70-
71-
72- #[ derive( lang_macro:: EnumVariants , Debug , Clone , Copy , PartialEq , Eq ) ]
73- pub enum ArithmeticOperator {
74- Add ,
75- Subtract ,
76- Multiply ,
77- Divide ,
78- }
79-
80- impl TryFrom < LexerTokenKind > for ArithmeticOperator {
81- type Error = crate :: parser:: ParserErrorKind ;
82-
83- fn try_from ( value : LexerTokenKind ) -> Result < Self , Self :: Error > {
84- Ok ( match value {
85- LexerTokenKind :: Plus => Self :: Add ,
86- LexerTokenKind :: Minus => Self :: Subtract ,
87- LexerTokenKind :: Multiply => Self :: Multiply ,
88- LexerTokenKind :: Divide => Self :: Divide ,
89- _ => return Err ( ParserErrorKind :: ConvertError ( value) )
90- } )
91- }
92- }
93-
94-
95-
96- #[ derive( lang_macro:: EnumVariants , Debug , Clone , Copy , PartialEq , Eq ) ]
97- pub enum UnaryOperator {
98- Not ,
99- Negative ,
100- }
101-
102- impl TryFrom < LexerTokenKind > for UnaryOperator {
103- type Error = crate :: parser:: ParserErrorKind ;
104-
105- fn try_from ( value : LexerTokenKind ) -> Result < Self , Self :: Error > {
106- Ok ( match value {
107- LexerTokenKind :: Not => Self :: Not ,
108- LexerTokenKind :: Minus => Self :: Negative ,
109- _ => return Err ( ParserErrorKind :: ConvertError ( value) )
110- } )
111- }
112- }
113-
114-
115-
116- #[ derive( lang_macro:: EnumVariants , Debug , Clone , Copy , PartialEq , Eq ) ]
117- pub enum LogicalOperator {
118- Equal ,
119- NotEqual ,
120- LesserThan ,
121- LesserEqualThan ,
122- GreaterThan ,
123- GreaterEqualThan ,
124- And ,
125- Or
126- }
127-
128- impl TryFrom < LexerTokenKind > for LogicalOperator {
129- type Error = crate :: parser:: ParserErrorKind ;
130-
131- fn try_from ( value : LexerTokenKind ) -> Result < Self , Self :: Error > {
132- Ok ( match value {
133- LexerTokenKind :: EqualEqual => Self :: Equal ,
134- LexerTokenKind :: NotEqual => Self :: NotEqual ,
135- LexerTokenKind :: LesserThan => Self :: LesserThan ,
136- LexerTokenKind :: LesserEqualThan => Self :: LesserEqualThan ,
137- LexerTokenKind :: GreaterThan => Self :: GreaterThan ,
138- LexerTokenKind :: GreaterEqualThan => Self :: GreaterEqualThan ,
139- LexerTokenKind :: And => Self :: And ,
140- LexerTokenKind :: Or => Self :: Or ,
141- _ => return Err ( ParserErrorKind :: ConvertError ( value) )
142- } )
143- }
144- }
145-
146-
147- #[ derive( lang_macro:: EnumVariants , Debug , Clone , Copy , PartialEq , Eq ) ]
148- pub enum AssignmentOperator {
149- PlusAssign ,
150- MinusAssign ,
151- MultiplyAssign ,
152- DivideAssign ,
153- Assign
154- }
155-
156- impl TryFrom < LexerTokenKind > for AssignmentOperator {
157- type Error = crate :: parser:: ParserErrorKind ;
158-
159- fn try_from ( value : LexerTokenKind ) -> Result < Self , Self :: Error > {
160- Ok ( match value {
161- LexerTokenKind :: PlusEqual => Self :: PlusAssign ,
162- LexerTokenKind :: MinusEqual => Self :: MinusAssign ,
163- LexerTokenKind :: MultiplyEqual => Self :: MultiplyAssign ,
164- LexerTokenKind :: DivideEqual => Self :: DivideAssign ,
165- LexerTokenKind :: Equal => Self :: Assign ,
166- _ => return Err ( ParserErrorKind :: ConvertError ( value) )
167- } )
168- }
169- }
170-
171-
172-
173- #[ derive( Debug , Clone , PartialEq , Eq ) ]
174- pub struct Function {
175- pub name : String ,
176- pub parameters : Option < Vec < Variable > > ,
177- pub strict_type : Option < String > ,
178- pub body : WithCursor < Block > ,
179- }
180-
181- #[ derive( Debug , Clone , PartialEq , Eq ) ]
182- pub struct Variable {
183- pub name : String ,
184- pub strict_type : Option < String > ,
185- pub value : Option < WithCursor < Expression > > ,
186- }
5+ pub trait Parse < T > {
6+ fn parse ( parser : & mut Parser ) -> ParserResult < T > ;
7+ }
0 commit comments