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