Merge pull request #537 from madewokherd/ccwmarshal
[mono.git] / mcs / class / monodoc / Monodoc.Ecma / EcmaUrlParser.jay
1 %{
2 using System.Text;
3 using System.IO;
4 using System;
5 using System.Linq;
6 using System.Collections.Generic;
7
8 namespace Monodoc.Ecma
9 {
10         public class EcmaUrlParser
11         {
12         int yacc_verbose_flag = 0;
13
14         public void IsValid (string input)
15         {
16                         var lexer = new EcmaUrlTokenizer (input);
17                         this.yyparse (lexer);
18         }
19
20         public EcmaDesc Parse (string input)
21         {
22                         var lexer = new EcmaUrlTokenizer (input);
23                         return (EcmaDesc)this.yyparse (lexer);
24         }
25
26         public bool TryParse (string input, out EcmaDesc desc)
27         {
28             desc = null;
29             try {
30                 desc = Parse (input);
31             } catch {
32                 return false;
33             }
34             return true;
35         }
36
37         EcmaDesc SetEcmaDescType (object result, EcmaDesc.Kind kind)
38         {
39             var desc = result as EcmaDesc;
40             desc.DescKind = kind;
41             return desc;
42         }
43
44         List<T> SafeReverse<T> (List<T> input)
45         {
46             if (input == null)
47                return null;
48             input.Reverse ();
49             return input;
50         }
51 %}
52
53 %token ERROR
54 %token IDENTIFIER
55 %token DIGIT
56 %token DOT
57 %token COMMA
58 %token COLON
59 %token INNER_TYPE_SEPARATOR
60 %token OP_GENERICS_LT
61 %token OP_GENERICS_GT
62 %token OP_GENERICS_BACKTICK
63 %token OP_OPEN_PAREN
64 %token OP_CLOSE_PAREN
65 %token OP_ARRAY_OPEN
66 %token OP_ARRAY_CLOSE
67 %token SLASH_SEPARATOR
68 %token STAR
69 %token REF_ARG
70 %token OUT_ARG
71 %token EXPLICIT_IMPL_SEP
72
73 %start expression
74
75 %%
76
77 expression
78         : 'T' COLON type_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Type); }
79         | 'N' COLON namespace_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Namespace); }
80         | 'M' COLON method_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Method); }
81         | 'F' COLON simple_member_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Field); }
82         | 'C' COLON constructor_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Constructor); }
83         | 'P' COLON property_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Property); }
84         | 'E' COLON simple_member_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Event); }
85         | 'O' COLON operator_expression { $$ = SetEcmaDescType ($3, EcmaDesc.Kind.Operator); }
86
87 /* i.e. id.id.id or id */
88 dot_expression
89         : IDENTIFIER { $$ = new List<string> { (string)$1 }; }
90         | IDENTIFIER DOT dot_expression { ((ICollection<string>)$3).Add ((string)$1); $$ = $3; }
91
92 namespace_expression
93         : dot_expression { $$ = new EcmaDesc { Namespace = string.Join (".", ((IEnumerable<string>)$1).Reverse ()) }; }
94
95 type_expression
96         : dot_expression type_expression_suffix {
97                          var dotExpr = ((List<string>)$1);
98                          dotExpr.Reverse ();
99                          var desc = $2 as EcmaDesc;
100                          desc.DescKind = EcmaDesc.Kind.Type;
101                          desc.Namespace = string.Join (".", dotExpr.Take (dotExpr.Count - 1));
102                          desc.TypeName = dotExpr.Last ();
103                          $$ = desc;
104                      }
105
106 /* To be used in types with no namespaces attached to them like an inner type*/
107 reduced_type_expression
108         : IDENTIFIER type_expression_suffix {
109                          var desc = $2 as EcmaDesc;
110                          desc.DescKind = EcmaDesc.Kind.Type;
111                          desc.TypeName = $1 as string;
112                          $$ = desc;
113                      }
114
115 type_expression_suffix
116         : opt_generic_type_suffix opt_inner_type_description opt_array_definition opt_etc {
117                          bool nestedDescHasEtc = $2 != null && ((EcmaDesc)$2).IsEtc;
118                          EcmaDesc nestedType = (EcmaDesc)$2;
119                          $$ = new EcmaDesc {
120                             GenericTypeArguments = $1 as List<EcmaDesc>,
121                             NestedType = nestedType,
122                             ArrayDimensions = SafeReverse ($3 as List<int>),
123                             Etc = $4 != null ? ((Tuple<char, string>)$4).Item1 : nestedDescHasEtc ? nestedType.Etc : (char)0,
124                             EtcFilter = $4 != null ? ((Tuple<char, string>)$4).Item2 : nestedDescHasEtc ? nestedType.EtcFilter : null
125                          };
126                          if (nestedDescHasEtc) {
127                             nestedType.Etc = (char)0;
128                             nestedType.EtcFilter = null;
129                          }
130                      }
131
132 opt_inner_type_description
133         : /* empty */ { $$ = null; }
134         | INNER_TYPE_SEPARATOR reduced_type_expression { $$ = $2; }
135
136 opt_generic_type_suffix
137         : /* empty */ { $$ = null; }
138         | OP_GENERICS_BACKTICK DIGIT { $$ = Enumerable.Repeat<EcmaDesc> (null, (int)$2).ToList (); }
139         | OP_GENERICS_LT generic_type_arg_list OP_GENERICS_GT { $$ = $2; }
140
141 generic_type_arg_list
142         : type_expression { $$ = new List<EcmaDesc> () { (EcmaDesc)$1 }; }
143         | generic_type_arg_list COMMA type_expression { ((List<EcmaDesc>)$1).Add ((EcmaDesc)$3); $$ = $1; }
144
145 opt_array_definition
146         : /* empty */ { $$ = null; }
147         | OP_ARRAY_OPEN opt_array_definition_list OP_ARRAY_CLOSE opt_array_definition {
148                       var dims = ((IList<int>)$4) ?? new List<int> (2);
149                       dims.Add ((int)$2);
150                       $$ = dims;
151                 }
152
153 opt_array_definition_list
154         : /* empty */ { $$ = 1; }
155         | COMMA opt_array_definition_list { $$ = ((int)$2) + 1; }
156
157 opt_etc
158         : /* empty */ { $$ = null; }
159         | SLASH_SEPARATOR etc_identifier { $$ = Tuple.Create<char, string> (((string)$2)[0], null); }
160         | SLASH_SEPARATOR etc_identifier SLASH_SEPARATOR reduced_member_expression { $$ = Tuple.Create<char, string> (((string)$2)[0], (string)$4); }
161 /*        | SLASH_SEPARATOR etc_identifier SLASH_SEPARATOR IDENTIFIER opt_generic_type_suffix { $$ = Tuple.Create<char, string> (((string)$2)[0], (string)$4 + ($5 == null ? string.Empty : "<" + string.Join (",", ((IEnumerable<EcmaDesc>)$5).Select (t => t.ToCompleteTypeName ())) + ">")); } */
162
163 etc_identifier
164         : STAR { $$ = "*"; }
165         | IDENTIFIER { $$ = $1; }
166
167 method_expression
168         : type_expression DOT IDENTIFIER opt_generic_type_suffix opt_arg_list_suffix {
169                       var desc = $1 as EcmaDesc;
170                       desc.MemberName = $3 as string;
171                       desc.GenericMemberArguments = $4 as List<EcmaDesc>;
172                       desc.MemberArguments = SafeReverse ($5 as List<EcmaDesc>);
173                       $$ = desc;
174                 }
175         | dot_expression opt_generic_type_suffix opt_arg_list_suffix {
176                       var dotExpr = ((List<string>)$1);
177                       $$ = new EcmaDesc {
178                            Namespace = string.Join (".", dotExpr.Skip (2).DefaultIfEmpty (string.Empty).Reverse ()),
179                            TypeName = dotExpr.Skip (1).First (),
180                            MemberName = dotExpr.First (),
181                            GenericMemberArguments = $2 as List<EcmaDesc>,
182                            MemberArguments = SafeReverse ($3 as List<EcmaDesc>)
183                       };
184                 }
185         | type_expression EXPLICIT_IMPL_SEP method_expression {
186                       var desc = $1 as EcmaDesc;
187                       desc.ExplicitImplMember = $3 as EcmaDesc;
188                       $$ = desc;
189                 }
190
191 /* To be used with members that may have no type/namespace attached */
192 reduced_member_expression
193         : IDENTIFIER opt_generic_type_suffix { $$ = (string)$1 + ($2 == null ? string.Empty : "<" + string.Join (",", ((IEnumerable<EcmaDesc>)$2).Select (t => t.ToCompleteTypeName ())) + ">"); }
194         | IDENTIFIER opt_generic_type_suffix DOT reduced_member_expression {
195                       var existing = $4 as string;
196                       var expr = (string)$1 + ($2 == null ? string.Empty : "<" + string.Join (",", ((IEnumerable<EcmaDesc>)$2).Select (t => t.ToCompleteTypeName ())) + ">");
197                       $$ = expr + "." + existing;
198                 }
199
200 arg_type_expression
201         : type_expression opt_arg_type_suffix { var desc = (EcmaDesc)$1; desc.DescModifier = (EcmaDesc.Mod)$2; $$ = desc; }
202
203 opt_arg_type_suffix
204         : /* empty */ { $$ = EcmaDesc.Mod.Normal; }
205         | STAR { $$ = EcmaDesc.Mod.Pointer; }
206         | REF_ARG { $$ = EcmaDesc.Mod.Ref; }
207         | OUT_ARG { $$ = EcmaDesc.Mod.Out; }
208
209 type_expression_list
210         : /* empty */ { $$ = null; }
211         | arg_type_expression { $$ = new List<EcmaDesc> () { (EcmaDesc)$1 }; }
212         | arg_type_expression COMMA type_expression_list { ((List<EcmaDesc>)$3).Add ((EcmaDesc)$1); $$ = $3; }
213
214 simple_member_expression
215         : dot_expression {
216                  var dotExpr = ((List<string>)$1);
217                  dotExpr.Reverse ();
218
219                  $$ = new EcmaDesc {
220                       Namespace = dotExpr.Count > 2 ? string.Join (".", dotExpr.Take (dotExpr.Count - 2)) : string.Empty,
221                       TypeName = dotExpr.Count > 1 ?  dotExpr[dotExpr.Count - 2] : string.Empty,
222                       MemberName = dotExpr[dotExpr.Count - 1]
223                  };
224              }
225         | type_expression DOT IDENTIFIER {
226                  var desc = $1 as EcmaDesc;
227                  desc.MemberName = $3 as string;
228                  $$ = desc;
229              }
230         | type_expression EXPLICIT_IMPL_SEP simple_member_expression {
231                  var desc = $1 as EcmaDesc;
232                  desc.ExplicitImplMember = $3 as EcmaDesc;
233                  $$ = desc;
234              }
235
236 constructor_expression
237         : method_expression { $$ = $1; }
238
239 operator_expression
240         : method_expression { $$ = $1; }
241
242 property_expression
243         : simple_member_expression opt_property_indexer {
244                  var desc = $1 as EcmaDesc;
245                  (desc.ExplicitImplMember ?? desc).MemberArguments = SafeReverse ($2 as List<EcmaDesc>);
246                  $$ = desc;
247              }
248
249 opt_property_indexer
250         : opt_arg_list_suffix { $$ = $1; }
251
252 /*simple_member_expression opt_arg_list_suffix { $$ = CopyFromEcmaDesc (new EcmaDesc {
253                            MemberArguments = SafeReverse ($2 as List<EcmaDesc>)
254                       }, (EcmaDesc)$1);
255                 }*/
256
257 opt_arg_list_suffix
258         : /* empty */ { $$ = null; }
259         | OP_OPEN_PAREN type_expression_list OP_CLOSE_PAREN { $$ = $2; }
260
261 %%
262
263 }