Author: olivier
Date: 2007-06-09 17:58:41 -0400 (Sat, 09 Jun 2007)
New Revision: 79083

Modified:
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/StringLiteralToken.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler.Tests.csproj
Log:
2007-06-10  Olivier Dufour  <[EMAIL PROTECTED]>

        * StringLiteralToken implementation and create in Tokenizer
        * Parser go ahead in expression but still a lot of work
        

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
   2007-06-09 21:58:14 UTC (rev 79082)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
   2007-06-09 21:58:41 UTC (rev 79083)
@@ -1,3 +1,8 @@
+2007-06-10  Olivier Dufour  <[EMAIL PROTECTED]>
+
+       * StringLiteralToken implementation and create in Tokenizer
+       * Parser go ahead in expression but still a lot of work
+       
 2007-06-08  Marek Sieradzki  <[EMAIL PROTECTED]>
 
        * Parser.cs (SyntaxOK): No errors == fine.

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
   2007-06-09 21:58:14 UTC (rev 79082)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
   2007-06-09 21:58:41 UTC (rev 79083)
@@ -77,30 +77,7 @@
 
                private FunctionStatement ParseFunctionDeclaration ()
                {
-                       Token start = current;
-
-                       Next ();
-                       CheckSyntaxExpected (Token.Type.Identifier);
-                       Identifier id = ((IdentifierToken) current).Spelling;
-                       TextPoint NameLocation = new TextPoint 
(this.current.StartPosition);
-
-                       Next ();
-                       CheckSyntaxExpected(Token.Type.LeftParenthesis);
-                       TextPoint leftParenLocation = new 
TextPoint(this.current.StartPosition);
-                       
-                       Next ();
-                       List<Parameter> parametters = ParseListParametter ();
-                       CheckSyntaxExpected (Token.Type.RightParenthesis);
-                       TextPoint rightParenLocation = new TextPoint 
(this.current.StartPosition);
-                       Token headerEnd = this.current;
-                       
-                       Next ();
-                       BlockStatement body = ParseBlock ();
-                       TextSpan location = new TextSpan (start,current);
-                       TextSpan HeaderLocation = new TextSpan 
(start,headerEnd);
-                       
-                       FunctionDefinition func = new FunctionDefinition(id, 
parametters, body, location, HeaderLocation, NameLocation, leftParenLocation, 
rightParenLocation);
-                       return new FunctionStatement (func);
+                       return new FunctionStatement 
(ParseFunctionDefinition());
                }
                
                private List<Parameter> ParseListParametter ()
@@ -562,7 +539,123 @@
                #endregion
 
                #region expressions
+               //MemberExpression
+               /*
+               MemberExpression [ Expression ]
+               MemberExpression . Identifier*/
 
+               private Expression ParseMemberExpression ()
+               {
+                       Token start = current; //ident
+                       switch (current.Kind) {
+                               case Token.Type.This:
+                                       return new Expression 
(Expression.Operation.This, new TextSpan(current,current));
+                               case Token.Type.Identifier:
+                                       return new IdentifierExpression 
((current as IdentifierToken).Spelling, new TextSpan (current, current));
+                               case Token.Type.Null:
+                                       return new NullExpression (new 
TextSpan(current,current));
+                               case Token.Type.True:
+                                       return new Expression 
(Expression.Operation.True, new TextSpan (current, current));
+                               case Token.Type.False:
+                                       return new Expression 
(Expression.Operation.False, new TextSpan (current, current));
+                               case Token.Type.NumericLiteral:
+                                       return new NumericLiteralExpression 
((current as NumericLiteralToken).Spelling, new TextSpan(current,current));
+                               case Token.Type.StringLiteral:
+                                       return new StringLiteralExpression 
((current as StringLiteralToken).Value ,(current as 
StringLiteralToken).Spelling, new TextSpan (current, current));
+                               case Token.Type.LeftBracket:
+                                       return ParseArrayLiteral ();
+                               case Token.Type.LeftBrace:
+                                       return ParseObjectLiteral ();
+                               case Token.Type.Function:
+                                       return new FunctionExpression 
(ParseFunctionDefinition ()); ;
+                               case Token.Type.New:
+                                       return ParseNew ();
+                               default:
+                                       SyntaxError.Add ("Statement start with 
a strange token :" + Enum.GetName (typeof (Token.Type), current.Kind));
+                                       return new Expression 
(Expression.Operation.SyntaxError, new TextSpan (start, current));
+                               }
+               }
+
+               private FunctionDefinition ParseFunctionDefinition ()
+               {
+                       Token start = current;
+
+                       Next ();
+                       CheckSyntaxExpected (Token.Type.Identifier);
+                       Identifier id = ((IdentifierToken)current).Spelling;
+                       TextPoint NameLocation = new TextPoint 
(this.current.StartPosition);
+
+                       Next ();
+                       CheckSyntaxExpected (Token.Type.LeftParenthesis);
+                       TextPoint leftParenLocation = new TextPoint 
(this.current.StartPosition);
+
+                       Next ();
+                       List<Parameter> parametters = ParseListParametter ();
+                       CheckSyntaxExpected (Token.Type.RightParenthesis);
+                       TextPoint rightParenLocation = new TextPoint 
(this.current.StartPosition);
+                       Token headerEnd = this.current;
+
+                       Next ();
+                       BlockStatement body = ParseBlock ();
+                       TextSpan location = new TextSpan (start, current);
+                       TextSpan HeaderLocation = new TextSpan (start, 
headerEnd);
+
+                       return new FunctionDefinition (id, parametters, body, 
location, HeaderLocation, NameLocation, leftParenLocation, rightParenLocation);
+               }
+
+               private Expression ParseObjectLiteral ()
+               {
+                       Token start = current;
+                       Next ();
+                       List<ObjectLiteralElement> elements = new 
List<ObjectLiteralElement> ();
+                       if (current.Kind == Token.Type.RightBrace) {
+                               return new ObjectLiteralExpression (elements, 
new TextSpan (start, current));
+                       }
+                       
+                       ObjectLiteralElement element;
+                       TextPoint comma = new TextPoint ();
+                       TextPoint colon = new TextPoint ();
+                       do {
+                               if (current.Kind != Token.Type.Identifier
+                                       && current.Kind != 
Token.Type.StringLiteral
+                                       && current.Kind != 
Token.Type.NumericLiteral) {
+                                       //diagnostics.Add(new 
Diagnostic(DiagnosticCode.
+                                       //TODO here
+                               }
+                               Expression name = ParseExpression ();
+                               CheckSyntaxExpected (Token.Type.Colon);
+                               colon = new TextPoint (current.StartPosition);
+                               Next ();
+                               Expression val = ParseExpression ();
+                               element = new ObjectLiteralElement (name, val, 
colon, comma);
+                               elements.Add (element);
+                               comma = new TextPoint (current.StartPosition);
+                       } while (current.Kind == Token.Type.Comma);
+                       CheckSyntaxExpected (Token.Type.RightBrace);
+                       elements.Add (element);
+                       return new ObjectLiteralExpression (elements, new 
TextSpan (start, current));
+               }
+
+               private ArrayLiteralExpression ParseArrayLiteral ()
+               {
+                       Token start = current;
+                       Next();
+                       List<ExpressionListElement> elements = new 
List<ExpressionListElement> ();
+                       if (current.Kind == Token.Type.RightBracket) {
+                               return new ArrayLiteralExpression (elements, 
new TextSpan(start, current));
+                       }
+                       // TODO elision?
+                       TextPoint comma = new TextPoint();
+                       do {
+                               Expression exp = ParseExpression ();
+                               ExpressionListElement element = new 
ExpressionListElement (exp, comma);
+                               elements.Add (element);
+                               comma = new TextPoint(current.StartPosition);
+                       } while (current.Kind == Token.Type.Comma);
+                       CheckSyntaxExpected (Token.Type.RightBracket);
+                       return new ArrayLiteralExpression (elements, new 
TextSpan(start, current));
+               }
+               
                private Expression ParseExpression ()
                {
                        Token start = current; //ident
@@ -609,7 +702,7 @@
                                        break;
                                case Token.Type.Colon:
                                case Token.Type.SemiColon:
-                                       ParseIdentifier ();
+                                       ParseNew ();//TODO
                                        break;
                                default:
                                        SyntaxError.Add("Statement start with a 
strange token :" + Enum.GetName(typeof(Token.Type), current.Kind));
@@ -620,11 +713,6 @@
                        
                }
 
-               private void ParseIdentifier ()
-               {
-                       throw new Exception ("The method or operation is not 
implemented.");
-               }
-
                private Expression ParseGreaterGreater ()
                {
                        throw new Exception ("The method or operation is not 
implemented.");

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/StringLiteralToken.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/StringLiteralToken.cs
       2007-06-09 21:58:14 UTC (rev 79082)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/StringLiteralToken.cs
       2007-06-09 21:58:41 UTC (rev 79083)
@@ -4,7 +4,18 @@
 
 namespace Mono.JScript.Compiler
 {
-       public class StringLiteralToken
+       public class StringLiteralToken : Token
        {
+               public readonly string Spelling;
+               public readonly string Value;
+
+               public StringLiteralToken (string Spelling, string Value, int 
StartCharacterPosition, int StartLine, int StartColumn, bool FirstOnLine)
+                       : base(Token.Type.StringLiteral, 
StartCharacterPosition, StartLine, StartColumn, FirstOnLine)
+               {
+                       this.Spelling = Spelling;
+                       this.Value = Value;
+               }
+
+               public override int Width { get { return Spelling.Length; } }
        }
 }

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
        2007-06-09 21:58:14 UTC (rev 79082)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
        2007-06-09 21:58:41 UTC (rev 79083)
@@ -29,7 +29,7 @@
                        // move that to static ctor?
                        InitKeywords();
                }
-
+               //TODO : unicode \u
                public Token GetNext()
                {
                        //Must use CreateToken everytime we create a Token to 
avoid issue of location
@@ -209,6 +209,10 @@
                                        } else {
                                                return CreateToken 
(Token.Type.Divide);
                                        }
+                               case '"':
+                               case '\'':
+                                       return CreateStringLiteralToken (c);
+
                                case '0':
                                case '1':
                                case '2':
@@ -330,6 +334,103 @@
                        return current;
                }
                
+               private Token CreateStringLiteralToken (int q)
+               {
+                       int quote = q;// " or '
+                       int startPosition = position;
+                       int startLine = Line;
+                       int startColumn = Column;
+                       StringBuilder builder = new StringBuilder ();
+
+                       while (Advance ()) {
+                               int c = PeekChar ();
+                               if (c == quote)
+                                       break;
+                               if (c == -1 || c == '\n') {
+                                       return new BadToken ("string is never 
closed!", DiagnosticCode.SyntaxError, startPosition, startLine, startColumn, 
FirstOnLine);
+                               }
+                               if (c == '\\') {
+                                       ReadChar ();
+                                       c = PeekChar ();
+                                       switch (c) {
+                                               case '\'':
+                                                       c = '\''; 
+                                                       break;
+                                               case '"' :
+                                                       c = '"';//not sure
+                                                       break;
+                                               case '\\':
+                                                       c = '\\';
+                                                       break;
+                                               case 'b':
+                                                       c = '\b';
+                                                       break;
+                                               case 'f':
+                                                       c = '\f';
+                                                       break;
+                                               case 'n':
+                                                       c = '\n';
+                                                       break;
+                                               case 'r':
+                                                       c = '\r';
+                                                       break;
+                                               case 't':
+                                                       c = '\t';
+                                                       break;
+                                               case 'v':
+                                                       c = '\v';
+                                                       break;
+                                               case 'x'://2 hex digits
+                                                       ReadChar ();//next
+                                                       int r2 = 0;
+                                                       for (int i =0;i<2;i++) {
+                                                               ReadChar ();
+                                                               int d = 
PeekChar ();
+                                                               if (d >= '0' && 
d <= '9')
+                                                                       d -= 
'0';
+                                                               else if (d >= 
'A' && d <= 'F')
+                                                                       d = d - 
'A' + 10;
+                                                               else if (d >= 
'a' && d <= 'f')
+                                                                       d = d - 
'a' + 10;
+                                                               else
+                                                                       return 
new BadToken ("", DiagnosticCode.HexLiteralNoDigits, startPosition, startLine, 
startColumn, FirstOnLine);
+                                                               r2 = (r2 << 4) 
| d;
+                                                       }
+                                                       c = r2;
+                                                       break;
+                                               case 'u'://4 hex digits
+                                                       ReadChar ();//next
+                                                       int r4 = 0;
+                                                       for (int i = 0; i < 4; 
i++) {
+                                                               ReadChar ();
+                                                               int d = 
PeekChar ();
+                                                               if (d >= '0' && 
d <= '9')
+                                                                       d -= 
'0';
+                                                               else if (d >= 
'A' && d <= 'F')
+                                                                       d = d - 
'A' + 10;
+                                                               else if (d >= 
'a' && d <= 'f')
+                                                                       d = d - 
'a' + 10;
+                                                               else
+                                                                       return 
new BadToken ("", DiagnosticCode.HexLiteralNoDigits, startPosition, startLine, 
startColumn, FirstOnLine);
+                                                               r4 = (r4 << 4) 
| d;
+                                                       }
+                                                       c = r4;
+                                                       break;
+                                               default:
+                                                       //TODO 0 [lookahead 
?DecimalDigit] or NonEscapeCharacter
+                                                       return new BadToken 
("", DiagnosticCode.HexLiteralNoDigits, startPosition, startLine, startColumn, 
FirstOnLine);
+                                       }
+                               }
+                               builder.Append ((char)c);
+                               ReadChar ();
+                       }
+                       //TODO : understand diff between spelling and value 
here!
+                       string spelling = builder.ToString();
+                       string value = builder.ToString ();// same ???
+
+                       return new StringLiteralToken (spelling, value, 
startPosition, startLine, startColumn, FirstOnLine);
+               }
+
                private Token CreateIdentOrKeyword (int first)
                {
                        StringBuilder builder = new StringBuilder ();

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler.Tests.csproj
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler.Tests.csproj
   2007-06-09 21:58:14 UTC (rev 79082)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler.Tests.csproj
   2007-06-09 21:58:41 UTC (rev 79083)
@@ -28,10 +28,10 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
-    <Compile Include="Microsoft.JScript.Compiler\LexerTest.cs" />
     <Compile Include="Microsoft.JScript.Compiler\ParserTest.cs" />
     <Compile Include="Microsoft.JScript.Compiler\SplayTreeTest.cs" />
     <Compile Include="Microsoft.JScript.Compiler\TableTest.cs" />
+    <Compile Include="Microsoft.JScript.Compiler\TokenizerTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>

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

Reply via email to