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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
%{
using System.Text;
using System.IO;
using System.Collections;
using System;
namespace Mono.Debugger.Frontend.CSharp
{
internal class ExpressionParser
{
MyTextReader reader;
Tokenizer lexer;
protected bool yacc_verbose_flag = false;
public bool Verbose {
set {
yacc_verbose_flag = value;
}
get {
return yacc_verbose_flag;
}
}
%}
%token QUIT
%token EOF
%token NONE // This token is never returned by our lexer
%token ERROR // This is used not by the parser, but by the tokenizer.
// do not remove.
%token IDENTIFIER
%token INT
%token UINT
%token FLOAT
%token DOUBLE
%token DECIMAL
%token ULONG
%token LONG
%token STRING
%token HASH
%token AT
%token DOLLAR
%token DOT
%token DOTDOT
%token NOT
%token COMMA
%token ASSIGN
%token EQUAL
%token NOTEQUAL
%token STAR
%token PLUS
%token MINUS
%token DIV
%token PERCENT
%token STARASSIGN
%token PLUSASSIGN
%token MINUSASSIGN
%token DIVASSIGN
%token PERCENTASSIGN
%token OPAREN
%token CPAREN
%token OBRACKET
%token CBRACKET
%token RIGHTSHIFT
%token RIGHTSHIFTASSIGN
%token LEFTSHIFT
%token LEFTSHIFTASSIGN
%token LT
%token GT
%token LE
%token GE
%token AND
%token OR
%token OROR
%token ANDAND
%token NOT
%token COLON
%token QUESTION
%token AMPERSAND
%token ARROW
%token BACKTICK
%token LENGTH
%token LOWER
%token UPPER
%token NEW
%token THIS
%token BASE
%token CATCH
%token TRUE
%token FALSE
%token NULL
%start parse_expression
%%
parse_expression
: primary_expression
{
return $1;
}
;
primary_expression
: expression
| expression ASSIGN expression
{
$$ = new AssignmentExpression ((Expression) $1, (Expression) $3);
}
| expression PLUS expression
{
$$ = new BinaryOperator (BinaryOperator.Kind.Plus, (Expression) $1, (Expression) $3);
}
| expression MINUS expression
{
$$ = new BinaryOperator (BinaryOperator.Kind.Plus, (Expression) $1, (Expression) $3);
}
| expression STAR expression
{
$$ = new BinaryOperator (BinaryOperator.Kind.Mult, (Expression) $1, (Expression) $3);
}
| expression DIV expression
{
$$ = new BinaryOperator (BinaryOperator.Kind.Div, (Expression) $1, (Expression) $3);
}
;
constant
: TRUE
{
$$ = new BoolExpression (true);
}
| FALSE
{
$$ = new BoolExpression (false);
}
| LONG
{
$$ = new NumberExpression ((long) $1);
}
| ULONG
{
$$ = new NumberExpression ((ulong) $1);
}
| INT
{
$$ = new NumberExpression ((int) $1);
}
| UINT
{
$$ = new NumberExpression ((uint) $1);
}
| FLOAT
{
$$ = new NumberExpression ((float) $1);
}
| DOUBLE
{
$$ = new NumberExpression ((double) $1);
}
| DECIMAL
{
$$ = new NumberExpression ((decimal) $1);
}
| STRING
{
$$ = new StringExpression ((string) $1);
}
| NULL
{
$$ = new NullExpression ();
}
;
expression
: constant
| THIS
{
$$ = new ThisExpression ();
}
| CATCH
{
$$ = new CatchExpression ();
}
| BASE DOTDOT IDENTIFIER
{
$$ = new MemberAccessExpression (new BaseExpression (), "." + ((string) $3));
}
| BASE DOT IDENTIFIER
{
$$ = new MemberAccessExpression (new BaseExpression (), (string) $3);
}
| variable_or_type_name
| PERCENT IDENTIFIER
{
$$ = new RegisterExpression ((string) $2);
}
| STAR expression
{
$$ = new PointerDereferenceExpression ((Expression) $2, false);
}
| AMPERSAND expression
{
$$ = new AddressOfExpression ((Expression) $2);
}
| expression OBRACKET expression_list CBRACKET
{
Expression[] exps = new Expression [((ArrayList) $3).Count];
((ArrayList) $3).CopyTo (exps, 0);
$$ = new ArrayAccessExpression ((Expression) $1, exps);
}
| expression OPAREN expression_list_0 CPAREN
{
$$ = new InvocationExpression ((Expression) $1, ((Expression []) $3));
}
| NEW variable_or_type_name OPAREN expression_list_0 CPAREN
{
$$ = new NewExpression ((Expression) $2, ((Expression []) $4));
}
| OPAREN variable_or_type_name CPAREN expression
{
$$ = new CastExpression ((Expression) $2, (Expression) $4);
}
| expression QUESTION expression COLON expression
{
$$ = new ConditionalExpression ((Expression)$1, (Expression)$3, (Expression)$5);
}
| OPAREN expression CPAREN
{
$$ = $2;
}
;
expression_list_0
: /* empty */
{
$$ = new Expression [0];
}
| expression_list
{
Expression[] exps = new Expression [((ArrayList) $1).Count];
((ArrayList) $1).CopyTo (exps, 0);
$$ = exps;
}
;
expression_list
: expression
{
ArrayList args = new ArrayList ();
args.Add ($1);
$$ = args;
}
| expression_list COMMA expression
{
ArrayList args = (ArrayList) $1;
args.Add ($3);
$$ = args;
}
;
variable_or_type_name
: variable_or_type_name_0
| variable_or_type_name STAR
{
$$ = new PointerTypeExpression ((Expression) $1);
}
;
member_name
: IDENTIFIER
{
$$ = (string) $1;
}
| IDENTIFIER BACKTICK INT
{
$$ = String.Format ("{0}`{1}", (string) $1, (int) $3);
}
;
variable_or_type_name_0
: member_name
{
$$ = new SimpleNameExpression ((string) $1);
}
| expression DOT member_name
{
$$ = new MemberAccessExpression ((Expression) $1, (string) $3);
}
| expression DOTDOT member_name
{
$$ = new MemberAccessExpression ((Expression) $1, "." + (string) $3);
}
| expression ARROW member_name
{
Expression expr = new PointerDereferenceExpression ((Expression) $1, true);
$$ = new MemberAccessExpression (expr, (string) $3);
}
;
%%
public ExpressionParser (string name)
{
this.reader = new MyTextReader ();
lexer = new Tokenizer (reader, name);
}
public Expression Parse (string text)
{
try {
reader.Text = text;
lexer.restart ();
if (yacc_verbose_flag)
return (Expression) yyparse (lexer, new yydebug.yyDebugSimple ());
else
return (Expression) yyparse (lexer);
} catch {
// Please do not remove this, it is used during debugging
// of the grammar
//
throw new ScriptingException (lexer.Location + " : Parsing error ");
}
}
/* end end end */
}
|