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

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