source: applications/trunk/LFC/parser/Parser.rf @ 3802

Last change on this file since 3802 was 3802, checked in by orlov, 13 years ago
  • Small fixes, !GetRAST, samples.
  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1$use Access Apply StdIO Stream;
2$use Lexer;
3
4$func Main = e;
5Main =
6  <Lexer <File_Open "samples.lfc">> :: e.tokens,
7  //<WriteLn e.tokens>,
8  <WriteLn <GetRAST <Program e.tokens>>>;
9
10$func GetRAST e.ast = e.ast;
11GetRAST {
12  /*empty*/ = /*empty*/;
13  t.term e.ast = t.term : {
14    (IDENT t.name VAR) = (VAR t.name);
15    (IDENT t.name CALL e.args) = (CALL t.name <GetRAST e.args>);
16    (SENTENCE t.name e.exp (WHERE e.assigns)) = (SENTENCE t.name (LET (<GetRAST e.assigns>) <GetRAST e.exp>));
17    (PAREN e.exp (WHERE e.assigns)) = (PAREN (LET (<GetRAST e.assigns>) <GetRAST e.exp>));
18    (e.exp) = (<GetRAST e.exp>);
19    s.sym = s.sym;
20  } :: t.term,
21    t.term <GetRAST e.ast>;
22};
23
24$func Parse (e.tokens) e.expr = e.ast (e.tokens);
25Parse {
26  (e.tokens) /*empty*/ = (e.tokens);
27  (e.tokens) t.term e.expr = t.term : {
28    (Get s.func) =
29      <Apply s.func e.tokens> : e.ast (e.tokens_rest),
30      e.ast <Parse (e.tokens_rest) e.expr>;
31    (Skip s.tk) =
32      {
33        e.tokens : (s.tk t.pos) e.tokens_rest = <Parse (e.tokens_rest) e.expr>;
34        <Error (e.tokens) "expected " s.tk>;
35      };
36    (e.expr2) =
37      <Parse (e.tokens) e.expr2> :: e.ast (e.tokens),
38      (e.ast) <Parse (e.tokens) e.expr>;
39    s.symbol = s.symbol <Parse (e.tokens) e.expr>;
40  };
41};
42
43$func Error (e.tokens) e.message = (e.tokens);
44Error ((e.tk (e.pos)) e) e.message =
45  <Write e.pos> <Print ' --- 'e.message' while ('> <Write e.tk> <PrintLn ') have been got'>,
46  $error "Can't parse the program";
47
48$func Program e.tokens = e.ast (e.tokens);
49Program e.tokens = {
50  e.tokens : (DEC e) e = <Parse (e.tokens) (FUNC (Get &FunctionDec) (Get &VarDecAndFuncDef)) (Get &ProgramRest)>;
51  <Error (e.tokens) "program should consist of a number of function definitions">;
52};
53
54$func ProgramRest e.tokens = e.ast (e.tokens);
55ProgramRest e.tokens = {
56  e.tokens : (EOF e) = ();
57  e.tokens : (DEC e) e = <Program e.tokens>;
58  <Error (e.tokens) "expected function declaration">;
59};
60
61$func FunctionDec e.tokens = e.ast (e.tokens);
62FunctionDec (DEC e) e.tokens =
63  <Parse (e.tokens) (Get &Identifier) (Skip COLON) ((Get &FunctionType)) (Skip SC)>;
64
65$func VarDecAndFuncDef e.tokens = e.ast (e.tokens);
66VarDecAndFuncDef e.tokens = {
67  e.tokens : (VAR e) e = <Parse (e.tokens) ((Get &VariableDec)) (Get &Definition)>;
68  e.tokens : (DEF e) e = <Parse (e.tokens) () (Get &Definition)>;
69  <Error (e.tokens) "expected variable declarations or a function definition">;
70};
71
72$func VariableDec e.tokens = e.ast (e.tokens);
73VariableDec (VAR e) e.tokens =
74  <Parse (e.tokens) ((Get &VarsDecl)) (Get &VariableDecRest)>;
75
76$func VarsDecl e.tokens = e.ast (e.tokens);
77VarsDecl e.tokens = {
78  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (Get &VariableList) (Skip COLON) (TYPE (Get &Identifier)) (Skip SC)>;
79  <Error (e.tokens) "expected the name of a variable to be declared">;
80};
81
82$func VariableDecRest e.tokens = e.ast (e.tokens);
83VariableDecRest e.tokens = {
84  e.tokens : (DEF e) e = (e.tokens);
85  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) ((Get &VarsDecl)) (Get &VariableDecRest)>;
86  <Error (e.tokens) "expected the name of a variable to be declared or 'def' and a function definition">;
87};
88
89$func VariableList e.tokens = e.ast (e.tokens);
90VariableList e.tokens = {
91  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (VAR (Get &Identifier)) (Get &VariableListRest)>;
92  <Error (e.tokens) "expected the name of a variable to be declared">;
93};
94
95$func VariableListRest e.tokens = e.ast (e.tokens);
96VariableListRest e.tokens = {
97  e.tokens : (COLON e) e = (e.tokens);
98  e.tokens : (COMMA e) e.tokens_rest = <Parse (e.tokens_rest) (VAR (Get &Identifier)) (Get &VariableListRest)>;
99  <Error (e.tokens) "expected the name of a variable to be declared or ':' and a concept name">;
100};
101
102$func Definition e.tokens = e.ast (e.tokens);
103Definition e.tokens = {
104  e.tokens : (DEF e) e.tokens_rest = <Parse (e.tokens_rest) (Get &Equation) (Get &EquationRest)>;
105};
106
107$func Equation e.tokens = e.ast (e.tokens);
108Equation e.tokens = {
109  e.tokens : (IDENTIFIER e) e =
110    <Parse (e.tokens) (SENTENCE (Get &Identifier) (Skip LPAR) ((Get &PatternList)) (Skip RPAR) (Skip EQ) (Get &ExpWithWhere) (Skip SC))>;
111  <Error (e.tokens) "expected the name of a function">;
112};
113
114$func EquationRest e.tokens = e.ast (e.tokens);
115EquationRest e.tokens = {
116  e.tokens : \{ (EOF e) e; (DEC e) e; } = (e.tokens);
117  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (Get &Equation) (Get &EquationRest)>;
118  <Error (e.tokens) "expected another equation or new function declaration">;
119};
120
121$func PatternList e.tokens = e.ast (e.tokens);
122PatternList e.tokens = {
123  e.tokens : \{ (IDENTIFIER e) e; (STRING e) e; } = <Parse (e.tokens) ((Get &Pattern)) (Get &PatternListRest)>;
124  <Error (e.tokens) "expected a constant or a variable name">;
125};
126
127$func Pattern e.tokens = e.ast (e.tokens);
128Pattern e.tokens = {
129  e.tokens : (STRING e.str t.pos) e.tokens_rest = (STRING e.str) <Parse (e.tokens_rest) (Get &PatConcat)>;
130  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (VAR (Get &Identifier)) (Get &PatConcat)>;
131  <Error (e.tokens) "expected a constant or a variable name">;
132};
133
134$func PatConcat e.tokens = e.ast (e.tokens);
135PatConcat e.tokens = {
136  e.tokens : \{ (COMMA e) e; (RPAR e) e; } = (e.tokens);
137  e.tokens : (CONCAT e) e.tokens_rest = <Pattern e.tokens_rest>;
138  <Error (e.tokens) "expected '[]' or ',' or ')'">;
139};
140
141$func PatternListRest e.tokens = e.ast (e.tokens);
142PatternListRest e.tokens = {
143  e.tokens : (RPAR e) e = (e.tokens);
144  e.tokens : (COMMA e) e.tokens_rest =  <Parse (e.tokens_rest) ((Get &Pattern)) (Get &PatternListRest)>;
145  <Error (e.tokens) "expected ',' or ')'">;
146};
147
148$func FunctionType e.tokens = e.ast (e.tokens);
149FunctionType e.tokens = {
150  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (TYPE (Get &Identifier)) (Get &ConceptListRest) (Skip TO) (TYPE (Get &Identifier))>;
151  <Error (e.tokens) "expected a type of a function argument">;
152};
153
154$func ConceptListRest e.tokens = e.ast (e.tokens);
155ConceptListRest e.tokens = {
156  e.tokens : (TO e) e = (e.tokens);
157  e.tokens : (ASTERISK e) e.tokens_rest = <Parse (e.tokens_rest) (TYPE (Get &Identifier)) (Get &ConceptListRest)>;
158  <Error (e.tokens) "expected '*' or '->'">;
159};
160
161$func Exp e.tokens = e.ast (e.tokens);
162Exp e.tokens = {
163  e.tokens : (STRING e.str t.pos) e.tokens_rest = (STRING e.str) <ExpConcat e.tokens_rest>;
164  e.tokens : (UNDEFINED e) e.tokens_rest = (UNDEFINED) <ExpConcat e.tokens_rest>;
165  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (IDENT (Get &Identifier) (Get &ExpVarOrFunc)) (Get &ExpConcat)>;
166  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) (PAREN (Get &ExpWithWhere)) (Skip RPAR) (Get &ExpConcat)>;
167  e.tokens : (IF e) e.tokens_rest = <Parse (e.tokens_rest) (IF ((Get &Exp)) (Skip THEN) ((Get &Exp)) (Skip ELSE) ((Get &Exp)))>;
168  <Error (e.tokens) "expected a constant, or an identifier, or '(', or 'if', or 'undefined'">;
169};
170
171$func ExpWithWhere e.tokens = e.ast (e.tokens);
172ExpWithWhere e.tokens = {
173  e.tokens : (STRING e.str t.pos) e.tokens_rest = (STRING e.str) <ExpConcatOrWhere e.tokens_rest>;
174  e.tokens : (UNDEFINED e) e.tokens_rest = (UNDEFINED) <ExpConcatOrWhere e.tokens_rest>;
175  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) (IDENT (Get &Identifier) (Get &ExpVarOrFunc)) (Get &ExpConcatOrWhere)>;
176  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) (PAREN (Get &ExpWithWhere)) (Skip RPAR) (Get &ExpConcatOrWhere)>;
177  e.tokens : (IF e) e.tokens_rest =
178     <Parse (e.tokens_rest) (IF ((Get &ExpWithWhere)) (Skip THEN) ((Get &ExpWithWhere)) (Skip ELSE) ((Get &ExpWithWhere)))>;
179  <Error (e.tokens) "expected a constant, or an identifier, or '(', or 'if', or 'undefined'">;
180};
181
182$func ExpVarOrFunc e.tokens = e.ast (e.tokens);
183ExpVarOrFunc e.tokens = {
184  e.tokens : \{ (COMMA e) e; (CONCAT e) e; (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; (WHERE e) e; } = VAR (e.tokens);
185  e.tokens : (LPAR e) e.tokens_rest = <Parse (e.tokens_rest) CALL (Get &ArgList) (Skip RPAR)>;
186  <Error (e.tokens)>;
187};
188
189$func ArgList e.tokens = e.ast (e.tokens);
190ArgList e.tokens = {
191  e.tokens : (RPAR e) e = (e.tokens);
192  e.tokens : \{ (IDENTIFIER e) e;  (IF e) e; (LPAR e) e; (STRING e) e; (UNDEFINED e) e; } =
193    <Parse (e.tokens) ((Get &Exp)) (Get &ArgListRest)>;
194  <Error (e.tokens) "expected a function argument or ')'">;
195};
196
197$func ArgListRest e.tokens = e.ast (e.tokens);
198ArgListRest e.tokens = {
199  e.tokens : (RPAR e) e = (e.tokens);
200  e.tokens : (COMMA e) e.tokens_rest = <Parse (e.tokens_rest) ((Get &Exp)) (Get &ArgListRest)>;
201  <Error (e.tokens) "expected ',' or ')'">;
202};
203
204$func ExpConcat e.tokens = e.ast (e.tokens);
205ExpConcat e.tokens = {
206  e.tokens : \{ (COMMA e) e; (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
207  e.tokens : (CONCAT e) e.tokens_rest = <Exp e.tokens_rest>;
208  <Error (e.tokens)>;
209};
210
211$func ExpConcatOrWhere e.tokens = e.ast (e.tokens);
212ExpConcatOrWhere e.tokens = {
213  e.tokens : \{ (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
214  e.tokens : (CONCAT e) e.tokens_rest = <ExpWithWhere e.tokens_rest>;
215  e.tokens : (WHERE e) e.tokens_rest = <Parse (e.tokens_rest) (WHERE (Get &VarassignList))>;
216  <Error (e.tokens)>;
217};
218
219$func VarassignList e.tokens = e.ast (e.tokens);
220VarassignList e.tokens = {
221  e.tokens : (IDENTIFIER e) e = <Parse (e.tokens) ((VAR (Get &Identifier)) (Skip EQ) (Get &Exp)) (Get &VarassignRest)>;
222  <Error (e.tokens) "expected a variable name">; 
223};
224
225$func VarassignRest e.tokens = e.ast (e.tokens);
226VarassignRest e.tokens = {
227  e.tokens : \{ (ELSE e) e; (RPAR e) e; (SC e) e; (THEN e) e; } = (e.tokens);
228  e.tokens : (COMMA e) e.tokens_rest = <VarassignList e.tokens_rest>;
229  <Error (e.tokens)>;
230};
231
232$func Identifier e.tokens = e.ast (e.tokens);
233Identifier {
234  (IDENTIFIER e.ident t.pos) e.tokens = (e.ident) (e.tokens);
235  e.tokens = <Error (e.tokens) "expected an identifier">;
236};
Note: See TracBrowser for help on using the repository browser.