6 using System.Collections.Generic;
10 public class EcmaUrlParser
12 int yacc_verbose_flag = 0;
14 public void IsValid (string input)
16 var lexer = new EcmaUrlTokenizer (input);
20 public EcmaDesc Parse (string input)
22 var lexer = new EcmaUrlTokenizer (input);
23 return (EcmaDesc)this.yyparse (lexer);
26 public bool TryParse (string input, out EcmaDesc desc)
37 EcmaDesc SetEcmaDescType (object result, EcmaDesc.Kind kind)
39 var desc = result as EcmaDesc;
44 List<T> SafeReverse<T> (List<T> input)
59 %token INNER_TYPE_SEPARATOR
62 %token OP_GENERICS_BACKTICK
67 %token SLASH_SEPARATOR
71 %token EXPLICIT_IMPL_SEP
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); }
87 /* i.e. id.id.id or id */
89 : IDENTIFIER { $$ = new List<string> { (string)$1 }; }
90 | IDENTIFIER DOT dot_expression { ((ICollection<string>)$3).Add ((string)$1); $$ = $3; }
93 : dot_expression { $$ = new EcmaDesc { Namespace = string.Join (".", ((IEnumerable<string>)$1).Reverse ()) }; }
96 : dot_expression type_expression_suffix {
97 var dotExpr = ((List<string>)$1);
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 ();
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;
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;
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
126 if (nestedDescHasEtc) {
127 nestedType.Etc = (char)0;
128 nestedType.EtcFilter = null;
132 opt_inner_type_description
133 : /* empty */ { $$ = null; }
134 | INNER_TYPE_SEPARATOR reduced_type_expression { $$ = $2; }
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; }
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; }
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);
153 opt_array_definition_list
154 : /* empty */ { $$ = 1; }
155 | COMMA opt_array_definition_list { $$ = ((int)$2) + 1; }
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 ())) + ">")); } */
165 | IDENTIFIER { $$ = $1; }
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>);
175 | dot_expression opt_generic_type_suffix opt_arg_list_suffix {
176 var dotExpr = ((List<string>)$1);
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>)
185 | type_expression EXPLICIT_IMPL_SEP method_expression {
186 var desc = $1 as EcmaDesc;
187 desc.ExplicitImplMember = $3 as EcmaDesc;
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;
201 : type_expression opt_arg_type_suffix { var desc = (EcmaDesc)$1; desc.DescModifier = (EcmaDesc.Mod)$2; $$ = desc; }
204 : /* empty */ { $$ = EcmaDesc.Mod.Normal; }
205 | STAR { $$ = EcmaDesc.Mod.Pointer; }
206 | REF_ARG { $$ = EcmaDesc.Mod.Ref; }
207 | OUT_ARG { $$ = EcmaDesc.Mod.Out; }
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; }
214 simple_member_expression
216 var dotExpr = ((List<string>)$1);
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]
225 | type_expression DOT IDENTIFIER {
226 var desc = $1 as EcmaDesc;
227 desc.MemberName = $3 as string;
230 | type_expression EXPLICIT_IMPL_SEP simple_member_expression {
231 var desc = $1 as EcmaDesc;
232 desc.ExplicitImplMember = $3 as EcmaDesc;
236 constructor_expression
237 : method_expression { $$ = $1; }
240 : method_expression { $$ = $1; }
243 : simple_member_expression opt_property_indexer {
244 var desc = $1 as EcmaDesc;
245 (desc.ExplicitImplMember ?? desc).MemberArguments = SafeReverse ($2 as List<EcmaDesc>);
250 : opt_arg_list_suffix { $$ = $1; }
252 /*simple_member_expression opt_arg_list_suffix { $$ = CopyFromEcmaDesc (new EcmaDesc {
253 MemberArguments = SafeReverse ($2 as List<EcmaDesc>)
258 : /* empty */ { $$ = null; }
259 | OP_OPEN_PAREN type_expression_list OP_CLOSE_PAREN { $$ = $2; }