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

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