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