Author: toshok
Date: 2005-03-13 18:16:25 -0500 (Sun, 13 Mar 2005)
New Revision: 41773

Added:
   trunk/debugger/frontend/CSharpTokenizer.lex
Modified:
   trunk/debugger/ChangeLog
   trunk/debugger/Makefile.am
   trunk/debugger/configure.in
   trunk/debugger/frontend/CSharpExpressionParser.jay
   trunk/debugger/frontend/Expression.cs
   trunk/debugger/frontend/Makefile.am
Log:
2005-03-13  Chris Toshok  <[EMAIL PROTECTED]>

        * frontend/Makefile.am (MAINTAINERCLEANFILES): add
        CSharpTokenizer_lex.cs
        (CSharpTokenizer_lex.cs): add cslex rule to build this file.
        (EXTRA_DIST): add CSharpTokenizer_lex.cs/CSharpTokenizer.lex

        * frontend/ (svn:ignore): ignore CSharpTokenizer_lex.cs
        
        * frontend/CSharpTokenizer.lex: new file, lexer specification that
        implements the proper ifaces/properties that
        CSharpExpressionParser.jay expects.

        * frontend/CSharpExpressionParser.jay: clean up a little bit and
        make it work with the cslex generated lexer.

        * frontend/Expression.cs (NumberExpression..ctor): add a
        constructor for floats.  might not be the right place to put it,
        but it works.
        (BoolExpression): add a new type to represent boolean
        expressions. at the moment it can only deal with constant bool
        expressions like "true" and "false".
        (ConditionalExpression): add a new type to represent "a ? b : c".

        * Makefile.am (SUBDIRS): add cslex

        * configure.in (AC_OUTPUT): add cslex/Makefile




Modified: trunk/debugger/ChangeLog
===================================================================
--- trunk/debugger/ChangeLog    2005-03-13 22:53:56 UTC (rev 41772)
+++ trunk/debugger/ChangeLog    2005-03-13 23:16:25 UTC (rev 41773)
@@ -1,3 +1,31 @@
+2005-03-13  Chris Toshok  <[EMAIL PROTECTED]>
+
+       * frontend/Makefile.am (MAINTAINERCLEANFILES): add
+       CSharpTokenizer_lex.cs
+       (CSharpTokenizer_lex.cs): add cslex rule to build this file.
+       (EXTRA_DIST): add CSharpTokenizer_lex.cs/CSharpTokenizer.lex
+
+       * frontend/ (svn:ignore): ignore CSharpTokenizer_lex.cs
+       
+       * frontend/CSharpTokenizer.lex: new file, lexer specification that
+       implements the proper ifaces/properties that
+       CSharpExpressionParser.jay expects.
+
+       * frontend/CSharpExpressionParser.jay: clean up a little bit and
+       make it work with the cslex generated lexer.
+
+       * frontend/Expression.cs (NumberExpression..ctor): add a
+       constructor for floats.  might not be the right place to put it,
+       but it works.
+       (BoolExpression): add a new type to represent boolean
+       expressions. at the moment it can only deal with constant bool
+       expressions like "true" and "false".
+       (ConditionalExpression): add a new type to represent "a ? b : c".
+
+       * Makefile.am (SUBDIRS): add cslex
+
+       * configure.in (AC_OUTPUT): add cslex/Makefile
+
 2005-03-09  Chris Toshok  <[EMAIL PROTECTED]>
 
        * acinclude.m4 (READLINE_TRYLINK): supply a function body calling

Modified: trunk/debugger/Makefile.am
===================================================================
--- trunk/debugger/Makefile.am  2005-03-13 22:53:56 UTC (rev 41772)
+++ trunk/debugger/Makefile.am  2005-03-13 23:16:25 UTC (rev 41773)
@@ -1,4 +1,4 @@
-SUBDIRS = jay interfaces classes glue arch backends frontend wrapper test
+SUBDIRS = cslex jay interfaces classes glue arch backends frontend wrapper test
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = mono-debugger.pc

Modified: trunk/debugger/configure.in
===================================================================
--- trunk/debugger/configure.in 2005-03-13 22:53:56 UTC (rev 41772)
+++ trunk/debugger/configure.in 2005-03-13 23:16:25 UTC (rev 41773)
@@ -196,6 +196,7 @@
 AC_OUTPUT([
 Makefile
 mono-debugger.pc
+cslex/Makefile
 jay/Makefile
 interfaces/Makefile
 classes/Makefile

Modified: trunk/debugger/frontend/CSharpExpressionParser.jay
===================================================================
--- trunk/debugger/frontend/CSharpExpressionParser.jay  2005-03-13 22:53:56 UTC 
(rev 41772)
+++ trunk/debugger/frontend/CSharpExpressionParser.jay  2005-03-13 23:16:25 UTC 
(rev 41773)
@@ -34,6 +34,7 @@
 
 %token IDENTIFIER
 %token INTEGER
+%token FLOAT
 %token NUMBER
 %token STRING
 %token HASH
@@ -42,21 +43,29 @@
 %token DOLLAR
 %token DOT
 %token DOTDOT
-%token BANG
+%token NOT
 %token COMMA
-%token ASSIGN
+%token EQUAL
+%token EQUALEQUAL
+%token NOTEQUAL
 %token STAR
 %token PLUS
 %token MINUS
 %token DIV
-%token OPEN_PARENS
-%token CLOSE_PARENS
-%token OPEN_BRACKET
-%token CLOSE_BRACKET
-%token OP_LT
-%token OP_GT
+%token OPAREN
+%token CPAREN
+%token OBRACKET
+%token CBRACKET
+%token LT
+%token GT
+%token LE
+%token GE
+%token AND
+%token OR
 %token COLON
+%token QUESTION
 %token AMPERSAND
+%token ARROW
 
 %token LENGTH
 %token LOWER
@@ -66,6 +75,9 @@
 %token THIS
 %token BASE
 %token CATCH
+%token TRUE
+%token FALSE
+%token NULL
 
 %start parse_expression
 %%
@@ -79,25 +91,40 @@
 
 primary_expression
        : expression
-       | expression ASSIGN expression
+       | expression EQUAL expression
          {
                $$ = new AssignmentExpression ((Expression) $1, (Expression) 
$3);
          }
        ;       
 
-expression
-       : NUMBER
+constant
+       : TRUE
          {
+               $$ = new BoolExpression (true);
+         }
+       | FALSE
+         {
+               $$ = new BoolExpression (false);
+         }
+       | NUMBER
+         {
                $$ = new NumberExpression ((long) $1);
          }
        | INTEGER
          {
                $$ = new NumberExpression ((int) $1);
          }
+       | FLOAT
+         {
+               $$ = new NumberExpression ((float) $1);
+         }
        | STRING
          {
                $$ = new StringExpression ((string) $1);
          }
+
+expression
+       : constant
        | THIS
          {
                $$ = new ThisExpression ();
@@ -127,24 +154,28 @@
          {
                $$ = new AddressOfExpression ((Expression) $2);
          }
-       | expression OPEN_BRACKET expression CLOSE_BRACKET
+       | expression OBRACKET expression CBRACKET
          {
                $$ = new ArrayAccessExpression ((Expression) $1, (Expression) 
$3);
          }
-       | expression OPEN_PARENS argument_list CLOSE_PARENS
+       | expression OPAREN argument_list CPAREN
          {
                $$ = new InvocationExpression ((Expression) $1, ((Expression 
[]) $3));
          }
-       | NEW variable_or_type_name OPEN_PARENS argument_list CLOSE_PARENS
+       | NEW variable_or_type_name OPAREN argument_list CPAREN
          {
                $$ = new NewExpression ((Expression) $2, ((Expression []) $4));
          }
-       | OPEN_PARENS variable_or_type_name CLOSE_PARENS expression
+       | OPAREN variable_or_type_name CPAREN expression
          {
                $$ = new CastExpression ((Expression) $2, (Expression) $4);
          }
-       | OPEN_PARENS expression CLOSE_PARENS
+       | expression QUESTION expression COLON expression
          {
+               $$ = new ConditionalExpression ((Expression)$1, (Expression)$3, 
(Expression)$5);
+         }
+       | OPAREN expression CPAREN
+         {
                $$ = $2;
          }
        ;
@@ -193,10 +224,10 @@
          { 
                $$ = new MemberAccessExpression ((Expression) $1, "." + 
(string) $3);
          }
-       | expression MINUS OP_GT IDENTIFIER
+       | expression ARROW IDENTIFIER
          {
                Expression expr = new PointerDereferenceExpression 
((Expression) $1, true);
-               $$ = new MemberAccessExpression (expr, (string) $4);
+               $$ = new MemberAccessExpression (expr, (string) $3);
          }
        ;
 
@@ -207,7 +238,8 @@
        this.reader = new MyTextReader ();
        this.current_context = context;
 
-       lexer = new Tokenizer (context, reader, name);
+       lexer = new Tokenizer (reader);
+       lexer.Name = name;
 }
 
 public Expression Parse (string text)
@@ -223,7 +255,7 @@
                // Please do not remove this, it is used during debugging
                // of the grammar
                //
-               current_context.Error (lexer.location + "  : Parsing error ");
+               current_context.Error (lexer.Location + "  : Parsing error ");
                current_context.Error (e.ToString ());
                return null;
        }

Added: trunk/debugger/frontend/CSharpTokenizer.lex
===================================================================
--- trunk/debugger/frontend/CSharpTokenizer.lex 2005-03-13 22:53:56 UTC (rev 
41772)
+++ trunk/debugger/frontend/CSharpTokenizer.lex 2005-03-13 23:16:25 UTC (rev 
41773)
@@ -0,0 +1,202 @@
+using System;
+using System.Text;
+using System.IO;
+using System.Reflection;
+using System.Collections;
+using System.Globalization;
+using Mono.Debugger;
+
+public class Yytoken  {
+       public readonly int Index;
+       public readonly string Text;
+       public readonly object Value;
+       public readonly int Line;
+       public readonly int CharBegin;
+       public readonly int CharEnd;
+
+       internal Yytoken (int index)
+       {
+               Index = index;
+       }
+
+       internal Yytoken (int index, string text, int line, int charBegin, int 
charEnd)
+       {
+               Index = index;
+               Text = text;
+               Line = line;
+               CharBegin = charBegin;
+               CharEnd = charEnd;
+       }
+
+       internal Yytoken (int index, string text, object value, int line, int 
charBegin, int charEnd)
+       {
+               Index = index;
+               Text = text;
+               Value = value;
+               Line = line;
+               CharBegin = charBegin;
+               CharEnd = charEnd;
+       }
+
+       public override String ToString()
+       {
+               return "Token #"+ Index + ": " + Text  + " (line "+ Line + ")";
+       }
+}
+
+%%
+
+%public
+%namespace Mono.Debugger.Frontend.CSharp
+%class Tokenizer
+%implements yyParser.yyInput
+
+%eofval{
+       return new Yytoken (Token.EOF);
+%eofval}
+
+%{
+       string name;
+
+       public string Name {
+         set { name = value; }
+         get { return name; }
+       }
+
+       Yytoken current_token;
+
+       public bool advance () {
+               current_token = yylex();
+               return current_token.Index != Token.EOF;
+       }
+
+       public int token () {
+               return current_token.Index;
+       }
+
+       public Object value () {
+               return current_token.Value;
+       }
+
+       public void restart () {
+               yyline = 0;
+               yychar = 1;
+       }
+
+               static Hashtable tokenValues;
+               
+               private static Hashtable TokenValueName
+               {
+                       get {
+                               if (tokenValues == null)
+                                       tokenValues = GetTokenValueNameHash ();
+
+                               return tokenValues;
+                       }
+               }
+
+               private static Hashtable GetTokenValueNameHash ()
+               {
+                       Type t = typeof (Token);
+                       FieldInfo [] fields = t.GetFields ();
+                       Hashtable hash = new Hashtable ();
+                       foreach (FieldInfo field in fields) {
+                               if (field.IsLiteral && field.IsStatic && 
field.FieldType == typeof (int))
+                                       hash.Add (field.GetValue (null), 
field.Name);
+                       }
+                       return hash;
+               }
+
+               //
+               // Returns a verbose representation of the current location
+               //
+               public string Location {
+                       get {
+                               string det;
+
+#if false
+                               if (current_token == Token.ERROR)
+                                       det = "detail: " + error_details;
+                               else
+#endif
+                                       det = "";
+                               
+                               // return "Line:     "+line+" Col: "+col + "\n" 
+
+                               //       "VirtLine: "+ref_line +
+                               //       " Token: "+current_token + " " + det;
+                               string current_token_name = TokenValueName 
[current_token.Index] as string;
+                               if (current_token_name == null)
+                                       current_token_name = 
current_token.ToString ();
+
+                               return String.Format ("{0}, Token: {1} {2}", 
name,
+                                                     current_token_name, det);
+                       }
+               }
+
+
+%}
+
+%line
+%char
+
+DIGIT=[0-9]
+ALPHA=[A-Za-z]
+HEXDIGIT=[A-Fa-f0-9]
+IDENTIFIER=[A-Za-z_][A-Za-z0-9_]*
+NEWLINE=((\r\n)|\n)
+NONNEWLINE_WHITE_SPACE_CHAR=[\ \t\b\012]
+WHITE_SPACE_CHAR=[{NEWLINE}\ \t\b\012]
+STRING_TEXT=(\\\"|[^{NEWLINE}\"]|\\{WHITE_SPACE_CHAR}+\\)*
+
+%%
+
+<YYINITIAL> {DIGIT}+ { return new Yytoken (Token.INTEGER, yytext(), (int) 
UInt32.Parse (yytext()), yyline, yychar, yychar+1); }
+<YYINITIAL> {DIGIT}*\.{DIGIT}+ { return new Yytoken (Token.FLOAT, yytext(), 
Single.Parse (yytext()), yyline, yychar, yychar+1); }
+<YYINITIAL> 0x{HEXDIGIT}+ { return new Yytoken (Token.NUMBER, yytext(), (long) 
UInt64.Parse (yytext(), NumberStyles.HexNumber), yyline,yychar, 
yychar+yytext().Length);}
+
+<YYINITIAL> "."  { return new Yytoken (Token.DOT, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> ".." { return new Yytoken (Token.DOTDOT, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "#"  { return new Yytoken (Token.HASH, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "@"  { return new Yytoken (Token.AT, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "%"  { return new Yytoken (Token.PERCENT, yytext(), yyline, 
yychar, yychar+1); }
+<YYINITIAL> "$"  { return new Yytoken (Token.DOLLAR, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "&"  { return new Yytoken (Token.AMPERSAND, yytext(), yyline, 
yychar, yychar+1); }
+<YYINITIAL> "?"  { return new Yytoken (Token.QUESTION, yytext(), yyline, 
yychar, yychar+1); }
+<YYINITIAL> ":"  { return new Yytoken (Token.COLON, yytext(), yyline, yychar, 
yychar+1); }
+
+<YYINITIAL> "!" { return new Yytoken (Token.NOT, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "=" { return new Yytoken (Token.EQUAL, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "*" { return new Yytoken (Token.STAR, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "+" { return new Yytoken (Token.PLUS, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "-" { return new Yytoken (Token.MINUS, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "/" { return new Yytoken (Token.DIV, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "(" { return new Yytoken (Token.OPAREN, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> ")" { return new Yytoken (Token.CPAREN, yytext(), yyline, yychar, 
yychar+1); }
+<YYINITIAL> "[" { return new Yytoken (Token.OBRACKET, yytext(), yyline, 
yychar, yychar+1); }
+<YYINITIAL> "]" { return new Yytoken (Token.CBRACKET, yytext(), yyline, 
yychar, yychar+1); }
+
+<YYINITIAL> "->" { return new Yytoken (Token.ARROW, yytext(), yyline, yychar, 
yychar + 2); }
+
+<YYINITIAL> "==" { return new Yytoken (Token.EQUALEQUAL, yytext(), yyline, 
yychar, yychar + 2); }
+<YYINITIAL> "!=" { return new Yytoken (Token.NOTEQUAL, yytext(), yyline, 
yychar, yychar + 2); }
+<YYINITIAL> "<"  { return new Yytoken (Token.LT, yytext(), yyline, yychar, 
yychar + 1); }
+<YYINITIAL> ">"  { return new Yytoken (Token.GT, yytext(), yyline, yychar, 
yychar + 1); }
+<YYINITIAL> "<=" { return new Yytoken (Token.LE, yytext(), yyline, yychar, 
yychar + 2); }
+<YYINITIAL> ">=" { return new Yytoken (Token.GE, yytext(), yyline, yychar, 
yychar + 2); }
+<YYINITIAL> "||" { return new Yytoken (Token.OR, yytext(), yyline, yychar, 
yychar + 2); }
+<YYINITIAL> "&&" { return new Yytoken (Token.AND, yytext(), yyline, yychar, 
yychar + 2); }
+
+<YYINITIAL> "new"   { return new Yytoken (Token.NEW, yytext(), yyline, yychar, 
yychar+yytext().Length); }
+<YYINITIAL> "this"  { return new Yytoken (Token.THIS, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+<YYINITIAL> "base"  { return new Yytoken (Token.BASE, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+<YYINITIAL> "catch" { return new Yytoken (Token.CATCH, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+<YYINITIAL> "true"  { return new Yytoken (Token.TRUE, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+<YYINITIAL> "false" { return new Yytoken (Token.FALSE, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+<YYINITIAL> "null"  { return new Yytoken (Token.NULL, yytext(), yyline, 
yychar, yychar+yytext().Length); }
+
+<YYINITIAL> \"{STRING_TEXT}\" { return new Yytoken (Token.STRING, yytext(), 
yytext().Substring(1, yytext().Length - 1), yyline, yychar, 
yychar+yytext().Length); }
+<YYINITIAL> {IDENTIFIER}      { return new Yytoken (Token.IDENTIFIER, 
yytext(), yytext(), yyline, yychar, yychar+yytext().Length);}
+
+<YYINITIAL> {WHITE_SPACE_CHAR}+ { return null; }
+
+<YYINITIAL> . { Console.WriteLine ("illegal character: <{0}>", yytext()); 
return null; }

Modified: trunk/debugger/frontend/Expression.cs
===================================================================
--- trunk/debugger/frontend/Expression.cs       2005-03-13 22:53:56 UTC (rev 
41772)
+++ trunk/debugger/frontend/Expression.cs       2005-03-13 23:16:25 UTC (rev 
41773)
@@ -219,6 +219,11 @@
                        this.val = val;
                }
 
+               public NumberExpression (float val)
+               {
+                       this.val = val;
+               }
+
                public long Value {
                        get {
                                if (val is int)
@@ -300,6 +305,41 @@
                }
        }
 
+       public class BoolExpression : Expression
+       {
+               bool val;
+
+               public BoolExpression (bool val)
+               {
+                       this.val = val;
+               }
+
+               public override string Name {
+                       get { return val.ToString(); }
+               }
+
+               protected override Expression DoResolve (ScriptingContext 
context)
+               {
+                       resolved = true;
+                       return this;
+               }
+
+               protected override object DoEvaluate (ScriptingContext context)
+               {
+                       return val;
+               }
+
+               protected override ITargetObject DoEvaluateVariable 
(ScriptingContext context)
+               {
+                       StackFrame frame = context.CurrentFrame.Frame;
+                       if ((frame.Language == null) ||
+                           !frame.Language.CanCreateInstance (typeof (bool)))
+                               return null;
+
+                       return frame.Language.CreateInstance (frame, val);
+               }
+       }
+
        public class ThisExpression : Expression
        {
                public override string Name {
@@ -1599,6 +1639,74 @@
                }
        }
 
+       public class ConditionalExpression : Expression
+       {
+               Expression test;
+               Expression true_expr;
+               Expression false_expr;
+
+               public override string Name {
+                       get { return "conditional"; }
+               }
+
+               public ConditionalExpression (Expression test, Expression 
true_expr, Expression false_expr)
+               {
+                 this.test = test;
+                 this.true_expr = true_expr;
+                 this.false_expr = false_expr;
+               }
+
+               protected override Expression DoResolve (ScriptingContext 
context)
+               {
+                 this.test = this.test.Resolve (context);
+                 if (this.test == null)
+                   return null;
+
+                 this.true_expr = this.true_expr.Resolve (context);
+                 if (this.true_expr == null)
+                   return null;
+
+                 this.false_expr = this.false_expr.Resolve (context);
+                 if (this.false_expr == null)
+                   return null;
+
+                       resolved = true;
+                       return this;
+               }
+
+               protected override object DoEvaluate (ScriptingContext context)
+               {
+                 bool cond;
+
+                 try {
+                       cond = (bool) this.test.Evaluate (context);
+                 }
+                 catch (Exception e) {
+                   throw new ScriptingException (
+                          "Cannot convert {0} to a boolean for conditional: 
{1}",
+                          this.test, e);
+                 }
+
+                 return cond ? true_expr.Evaluate (context) : 
false_expr.Evaluate (context);
+               }
+
+               protected override ITargetObject DoEvaluateVariable 
(ScriptingContext context)
+               {
+                 bool cond;
+
+                 try {
+                   cond = (bool) this.test.Evaluate (context);
+                 }
+                 catch (Exception e) {
+                   throw new ScriptingException (
+                          "Cannot convert {0} to a boolean for conditional: 
{1}",
+                          this.test, e);
+                 }
+
+                 return cond ? true_expr.EvaluateVariable (context) : 
false_expr.EvaluateVariable (context);
+               }
+       }
+
        public class InvocationExpression : Expression
        {
                Expression method_expr;

Modified: trunk/debugger/frontend/Makefile.am
===================================================================
--- trunk/debugger/frontend/Makefile.am 2005-03-13 22:53:56 UTC (rev 41772)
+++ trunk/debugger/frontend/Makefile.am 2005-03-13 23:16:25 UTC (rev 41773)
@@ -1,12 +1,16 @@
-noinst_DATA = CSharpExpressionParser.cs
+noinst_DATA = CSharpExpressionParser.cs CSharpTokenizer_lex.cs
 
 JAY = $(top_builddir)/jay/jay
+CSLEX = $(top_builddir)/cslex/cslex.exe
 
 CSharpExpressionParser.cs: CSharpExpressionParser.jay
-       $(JAY) -ctv < skeleton CSharpExpressionParser.jay > tmp-p && mv -f 
tmp-p CSharpExpressionParser.cs
+       $(JAY) -ctv < skeleton $< > tmp-p && mv -f tmp-p 
CSharpExpressionParser.cs
 
-MAINTAINERCLEANFILES = CSharpExpressionParser.cs y.output
+CSharpTokenizer_lex.cs: CSharpTokenizer.lex
+       mono $(CSLEX) $<
 
+MAINTAINERCLEANFILES = CSharpExpressionParser.cs CSharpTokenizer_lex.cs 
y.output
+
 EXTRA_DIST = \
        CL.cs                           \
        Command.cs                      \
@@ -14,6 +18,8 @@
        Expression.cs                   \
        CSharpExpressionParser.cs       \
        CSharpExpressionParser.jay      \
+       CSharpTokenizer_lex.cs          \
+       CSharpTokenizer.lex             \
        GnuReadLine.cs                  \
        IExpressionParser.cs            \
        Interpreter.cs                  \
@@ -22,5 +28,4 @@
        ScriptingContext.cs             \
        Session.cs                      \
        Style.cs                        \
-       skeleton                        \
-       CSharpTokenizer.cs
+       skeleton

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to