Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

360 lines (329 sloc) 6.509 kb
%{
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 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 PARENT
%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.Minus, (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);
}
| PARENT opt_parent_level OPAREN expression CPAREN
{
$$ = new ParentExpression ((Expression) $4, (int) $2);
}
| OPAREN expression CPAREN
{
$$ = $2;
}
;
opt_parent_level
: /* empty */
{
$$ = 0;
}
| PLUS INT
{
if ((int) $2 < 1)
throw new yyParser.yyException ("expected positive integer");
$$ = (int) $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
{
lexer.ReadGenericArity = true;
}
INT
{
lexer.ReadGenericArity = false;
$$ = String.Format ("{0}`{1}", (string) $1, (int) $4);
}
;
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 (yyParser.yyException ex) {
throw new ExpressionParsingException (text, lexer.Position, ex.Message);
} catch (Exception ex) {
string message = String.Format ("caught unexpected exception: {0}", ex.Message);
throw new ExpressionParsingException (text, lexer.Position, message);
}
}
/* end end end */
}
Jump to Line
Something went wrong with that request. Please try again.