6 using System.Collections.Generic;
8 namespace Monkeydoc.Ecma
10 public class EcmaUrlParser
12 int yacc_verbose_flag = 0;
14 public void IsValid (string input)
16 var reader = new StringReader (input);
17 var lexer = new EcmaUrlTokenizer (reader);
21 public EcmaDesc Parse (string input)
23 var reader = new StringReader (input);
24 var lexer = new EcmaUrlTokenizer (reader);
25 return (EcmaDesc)this.yyparse (lexer);
28 public bool TryParse (string input, out EcmaDesc desc)
39 EcmaDesc SetEcmaDescType (object result, EcmaDesc.Kind kind)
41 var desc = result as EcmaDesc;
46 List<T> SafeReverse<T> (List<T> input)
61 %token INNER_TYPE_SEPARATOR
64 %token OP_GENERICS_BACKTICK
69 %token SLASH_SEPARATOR
73 %token EXPLICIT_IMPL_SEP
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); }
89 /* i.e. id.id.id or id */
91 : IDENTIFIER { $$ = new List<string> { (string)$1 }; }
92 | IDENTIFIER DOT dot_expression { ((ICollection<string>)$3).Add ((string)$1); $$ = $3; }
95 : dot_expression { $$ = new EcmaDesc { Namespace = string.Join (".", ((IEnumerable<string>)$1).Reverse ()) }; }
98 : dot_expression type_expression_suffix {
99 var dotExpr = ((List<string>)$1);
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 ();
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;
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;
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
128 if (nestedDescHasEtc) {
129 nestedType.Etc = (char)0;
130 nestedType.EtcFilter = null;
134 opt_inner_type_description
135 : /* empty */ { $$ = null; }
136 | INNER_TYPE_SEPARATOR reduced_type_expression { $$ = $2; }
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; }
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; }
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);
155 opt_array_definition_list
156 : /* empty */ { $$ = 1; }
157 | COMMA opt_array_definition_list { $$ = ((int)$2) + 1; }
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 ())) + ">")); } */
167 | IDENTIFIER { $$ = $1; }
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>);
177 | dot_expression opt_generic_type_suffix opt_arg_list_suffix {
178 var dotExpr = ((List<string>)$1);
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>)
187 | type_expression EXPLICIT_IMPL_SEP method_expression {
188 var desc = $1 as EcmaDesc;
189 desc.ExplicitImplMember = $3 as EcmaDesc;
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;
203 : type_expression opt_arg_type_suffix { var desc = (EcmaDesc)$1; desc.DescModifier = (EcmaDesc.Mod)$2; $$ = desc; }
206 : /* empty */ { $$ = EcmaDesc.Mod.Normal; }
207 | STAR { $$ = EcmaDesc.Mod.Pointer; }
208 | REF_ARG { $$ = EcmaDesc.Mod.Ref; }
209 | OUT_ARG { $$ = EcmaDesc.Mod.Out; }
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; }
216 simple_member_expression
218 var dotExpr = ((List<string>)$1);
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]
227 | type_expression DOT IDENTIFIER {
228 var desc = $1 as EcmaDesc;
229 desc.MemberName = $3 as string;
232 | type_expression EXPLICIT_IMPL_SEP simple_member_expression {
233 var desc = $1 as EcmaDesc;
234 desc.ExplicitImplMember = $3 as EcmaDesc;
238 constructor_expression
239 : method_expression { $$ = $1; }
242 : method_expression { $$ = $1; }
245 : simple_member_expression opt_property_indexer {
246 var desc = $1 as EcmaDesc;
247 desc.MemberArguments = SafeReverse ($2 as List<EcmaDesc>);
252 : opt_arg_list_suffix { $$ = $1; }
254 /*simple_member_expression opt_arg_list_suffix { $$ = CopyFromEcmaDesc (new EcmaDesc {
255 MemberArguments = SafeReverse ($2 as List<EcmaDesc>)
260 : /* empty */ { $$ = null; }
261 | OP_OPEN_PAREN type_expression_list OP_CLOSE_PAREN { $$ = $2; }