Author: olivier
Date: 2007-06-04 02:44:30 -0400 (Mon, 04 Jun 2007)
New Revision: 78535

Modified:
   trunk/olive/class/Microsoft.JScript.Compiler/ChangeLog
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.csproj
   trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.suo
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Compiler.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/IdentifierTable.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/NumericLiteralToken.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
   
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
   trunk/olive/class/Microsoft.JScript.Compiler/test/ParserTest.cs
Log:
2007-06-04  Olivier Dufour  <[EMAIL PROTECTED]>

        * Add switch to parser
        * fix bug for null ref and not initialize ref for test
        * Add small test for Parser
        * use of custom token in tokenizer
        

Modified: trunk/olive/class/Microsoft.JScript.Compiler/ChangeLog
===================================================================
--- trunk/olive/class/Microsoft.JScript.Compiler/ChangeLog      2007-06-04 
06:40:27 UTC (rev 78534)
+++ trunk/olive/class/Microsoft.JScript.Compiler/ChangeLog      2007-06-04 
06:44:30 UTC (rev 78535)
@@ -1,3 +1,10 @@
+2007-06-04  Olivier Dufour  <[EMAIL PROTECTED]>
+
+       * Add switch to parser
+       * fix bug for null ref and not initialize ref for test
+       * Add small test for Parser
+       * use of custom token in tokenizer
+       
 2007-06-03  Olivier Dufour  <[EMAIL PROTECTED]>
 
        * Make it compilable on linux

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Compiler.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Compiler.cs
 2007-06-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Compiler.cs
 2007-06-04 06:44:30 UTC (rev 78535)
@@ -15,7 +15,7 @@
                {
                        return CompileExpression (Input.ToCharArray (), ref 
Diagnostics);
                }
-
+               [MonoTODO]
                public Microsoft.Scripting.Internal.Ast.CodeBlock 
CompileExpression (char[] Input, ref List<Diagnostic> Diagnostics)
                {
                        Parser parser = new Parser (Input, new IdentifierTable 
());
@@ -31,6 +31,7 @@
                        return CompileProgram (Input, ref Diagnostics, ref 
IncompleteInput, false);
                }
 
+               [MonoTODO]
                public Microsoft.Scripting.Internal.Ast.CodeBlock 
CompileProgram (char[] Input, ref List<Diagnostic> Diagnostics, ref bool 
IncompleteInput, bool PrintExpressions)
                {
                        Parser parser = new Parser (Input, new IdentifierTable 
());

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/IdentifierTable.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/IdentifierTable.cs
  2007-06-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/IdentifierTable.cs
  2007-06-04 06:44:30 UTC (rev 78535)
@@ -15,8 +15,8 @@
 
                public Identifier InsertIdentifier (string Spelling)
                {
-                       Identifier result = identifiers[Spelling];
-                       if (result == null) {
+                       Identifier result;
+                       if (!identifiers.TryGetValue(Spelling, out result)) {
                                result = new Identifier (Spelling, this);
                                identifiers.Add (Spelling, result);
                        }

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/NumericLiteralToken.cs
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/NumericLiteralToken.cs
      2007-06-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/NumericLiteralToken.cs
      2007-06-04 06:44:30 UTC (rev 78535)
@@ -4,7 +4,17 @@
 
 namespace Mono.JScript.Compiler
 {
-       public class NumericLiteralToken
+       public class NumericLiteralToken : Token
        {
+               public readonly string Spelling;
+
+               public NumericLiteralToken (string Spelling, int 
StartCharacterPosition, int StartLine, int StartColumn, bool FirstOnLine)
+                       :base(Token.Type.NumericLiteral, 
StartCharacterPosition, StartLine, StartColumn, FirstOnLine)
+               {
+                       this.Spelling = Spelling;
+               }
+
+               public override int Width { get { return Spelling.Length; } }
+
        }
 }

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-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
   2007-06-04 06:44:30 UTC (rev 78535)
@@ -14,6 +14,8 @@
                public Parser (char[] Input, IdentifierTable IDTable)
                {
                        lexer = new Tokenizer (Input, IDTable);
+                       diagnostics = new List<Diagnostic> ();
+                       Next (); // innit on first token 
                }
 
                #region private fields
@@ -91,8 +93,7 @@
                        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);
                }
@@ -128,7 +129,9 @@
                        foreach (Statement statement in statements)
                                children.Append (statement);
                        CheckSyntaxExpected (Token.Type.RightBrace);
-                       return new BlockStatement (children, new TextSpan 
(start, current));
+                       BlockStatement block = new BlockStatement (children, 
new TextSpan (start, current));
+                       children.Parent = block;
+                       return block;
                }
 
                private List<Statement> ParseListStatement ()
@@ -142,6 +145,7 @@
 
                private Statement ParseStatement ()
                {
+                       Token start = current;
                        switch (current.Kind) {
                                case Token.Type.LeftBrace:
                                        return ParseBlock ();
@@ -170,16 +174,12 @@
                                case Token.Type.Function:
                                        return ParseFunctionDeclaration ();
                                case Token.Type.Identifier:
-                                       IdentifierToken ident = current as 
IdentifierToken;
-                                       Next ();
                                        return ParseExpressionStatement ();
                                default:
                                        SyntaxError.Add("Statement start with a 
strange token :" + Enum.GetName(typeof(Token.Type), current.Kind));
                                        break;
                        }
-                       //TODO
-                       return new Statement(Statement.Operation.Block, new 
TextSpan(0, 0, 0, 0, 0, 0));
-                       
+                       return new Statement(Statement.Operation.Block, new 
TextSpan(start,current));
                }
 
                private ExpressionStatement ParseExpressionStatement ()
@@ -376,9 +376,79 @@
 
                private SwitchStatement ParseSwitch ()
                {
-                       throw new Exception ("The method or operation is not 
implemented.");
+                       Token start = current;
+                       Next();
+                       CheckSyntaxExpected(Token.Type.LeftParenthesis);
+                       Token leftParen = current;
+                       Expression Value = ParseExpression();
+                       CheckSyntaxExpected(Token.Type.RightParenthesis);
+                       Token rightParen = current;
+                       Next();
+                       CheckSyntaxExpected (Token.Type.LeftBrace);
+                       Token leftBrace = current;
+                       Next ();
+                       DList<CaseClause, SwitchStatement> cases = new 
DList<CaseClause, SwitchStatement> ();
+                       bool defaultFlag = false;
+                       while (current.Kind == Token.Type.Case || current.Kind 
== Token.Type.Default) {
+                               if (current.Kind == Token.Type.Case)
+                                       cases.Append (ParseValueCaseClause ());
+                               else {
+                                       if (defaultFlag)
+                                               diagnostics.Add (new Diagnostic 
(DiagnosticCode.SwitchHasMultipleDefaults, new TextSpan (start, current)));
+                                       cases.Append (ParseDefaultCaseClause 
());
+                                       defaultFlag = true;
+                               }
+                               Next ();                        
+                       }
+                       CheckSyntaxExpected (Token.Type.RightBrace);
+                       SwitchStatement switchStatement = new SwitchStatement 
(Value, cases, new TextSpan (start, current), new TextSpan (start, rightParen), 
new TextPoint (leftParen.StartPosition), new TextPoint 
(rightParen.StartPosition), new TextPoint (leftBrace.StartPosition));
+                       cases.Parent = switchStatement;
+                       return switchStatement;
                }
 
+               private DefaultCaseClause ParseDefaultCaseClause ()
+               {
+                       Token start = current;
+                       Next ();
+                       CheckSyntaxExpected (Token.Type.Colon);
+                       Token colon = current;
+                       Next ();
+                       DList<Statement, CaseClause> children = new 
DList<Statement,CaseClause>();
+                       do {
+                               if (current.Kind == Token.Type.Case
+                                       || current.Kind == Token.Type.Default
+                                       || current.Kind == Token.Type.RightBrace
+                                       || current.Kind == 
Token.Type.EndOfInput)
+                                       break;
+                               children.Append (ParseStatement ());
+                       } while (true);
+                       DefaultCaseClause result = new DefaultCaseClause 
(children, new TextSpan (start, current), new TextSpan (start, colon), new 
TextPoint (colon.StartPosition));
+                       children.Parent = result;
+                       return result;
+               }
+
+               private ValueCaseClause ParseValueCaseClause ()
+               {
+                       Token start = current;
+                       Next ();
+                       Expression expression = ParseExpression ();
+                       CheckSyntaxExpected (Token.Type.Colon);
+                       Token colon = current;
+                       Next ();
+                       DList<Statement, CaseClause> children = new 
DList<Statement, CaseClause> ();
+                       do {
+                               if (current.Kind == Token.Type.Case
+                                       || current.Kind == Token.Type.Default
+                                       || current.Kind == Token.Type.RightBrace
+                                       || current.Kind == 
Token.Type.EndOfInput)
+                                       break;
+                               children.Append (ParseStatement ());
+                       } while (true);
+                       ValueCaseClause result = new ValueCaseClause 
(expression, children, new TextSpan (start, current), new TextSpan (start, 
colon), new TextPoint (colon.StartPosition));
+                       children.Parent = result;
+                       return result;
+               }
+
                private ReturnOrThrowStatement ParseReturnOrThrow ()
                {
                        throw new Exception ("The method or operation is not 
implemented.");
@@ -422,6 +492,8 @@
                 */
                private Expression ParseExpression ()
                {
+                       Token start = current; //ident
+                       Next ();
                        switch (current.Kind) {
                                case Token.Type.Equal:
                                        ParseSetVar ();
@@ -462,15 +534,24 @@
                                case Token.Type.New:
                                        ParseNew ();
                                        break;
+                               case Token.Type.Colon:
+                               case Token.Type.SemiColon:
+                                       ParseIdentifier ();
+                                       break;
                                default:
                                        SyntaxError.Add("Statement start with a 
strange token :" + Enum.GetName(typeof(Token.Type), current.Kind));
                                        break;
                        }
-                       //TODO
-                       return new Expression(Expression.Operation.Bang, new 
TextSpan(0, 0, 0, 0, 0, 0));
                        
+                       return new Expression(Expression.Operation.Bang, new 
TextSpan(start,current));
+                       
                }
 
+               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.");
@@ -574,7 +655,6 @@
 
                public List<Diagnostic> Diagnostics { get {     return 
diagnostics;     } }
                /* TODO 
-                       SwitchHasMultipleDefaults,
                        TryHasNoHandlers,
                        BadDivideOrRegularExpressionLiteral,
                        EnclosingLabelShadowed,

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-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
        2007-06-04 06:44:30 UTC (rev 78535)
@@ -430,12 +430,11 @@
                        }
                        string result = builder.ToString();
                        Token.Type type;
-                       if (keywords.TryGetValue(result, out type)) {
-                               current = new Token (type, position, row, 
position - lineStartPosition, position == lineStartPosition);
-                               return current;
+                       if (!keywords.TryGetValue(result, out type)) {
+                               this.IDTable.InsertIdentifier (result);
                        }
-                       this.IDTable.InsertIdentifier (result);
-                       current = new Token (Token.Type.Identifier, position, 
row, position - lineStartPosition, position == lineStartPosition);
+                       Identifier id = new Identifier(result, IDTable);
+                       current = new IdentifierToken (id, result.Length, 
Token.Type.Identifier, position, row, position - lineStartPosition, position == 
lineStartPosition);
                        return current;
                }
 
@@ -475,7 +474,7 @@
                                break;
                        }
                        string result = builder.ToString();
-                       current = new Token (Token.Type.NumericLiteral, 
position, row, position - lineStartPosition, position == lineStartPosition);
+                       current = new NumericLiteralToken (result, position, 
row, position - lineStartPosition, position == lineStartPosition);
                        return current;
                }
 

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.csproj
===================================================================
--- 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.csproj  
    2007-06-04 06:40:27 UTC (rev 78534)
+++ 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.csproj  
    2007-06-04 06:44:30 UTC (rev 78535)
@@ -135,12 +135,10 @@
     <Compile Include="Microsoft.JScript.Compiler\Token.cs" />
   </ItemGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.Scripting, Version=1.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>C:\Program Files\Microsoft 
Silverlight\Microsoft.Scripting.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
   </ItemGroup>
-  <ItemGroup>
-    <ProjectReference 
Include="..\Microsoft.Scripting\Microsoft.Scripting.csproj">
-      <Project>{162ED7C8-3FA6-49CE-A7EF-C1A11FFFCCE5}</Project>
-      <Name>Microsoft.Scripting</Name>
-    </ProjectReference>
-  </ItemGroup>
 </Project>
\ No newline at end of file

Modified: 
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler.suo
===================================================================
(Binary files differ)

Modified: trunk/olive/class/Microsoft.JScript.Compiler/test/ParserTest.cs
===================================================================
--- trunk/olive/class/Microsoft.JScript.Compiler/test/ParserTest.cs     
2007-06-04 06:40:27 UTC (rev 78534)
+++ trunk/olive/class/Microsoft.JScript.Compiler/test/ParserTest.cs     
2007-06-04 06:44:30 UTC (rev 78535)
@@ -3,6 +3,7 @@
 using System.Text;
 using NUnit.Core;
 using NUnit.Framework;
+using Mono.JScript.Compiler;
 
 namespace Mono.JScript.Compiler.Tests
 {
@@ -10,14 +11,21 @@
        public class ParserTest
        {
                [Test]
-               public void GlobalTest()
+               public void SyntaxOKTest()
                {
-                       /*IParser p1 = new JScriptParser("var } function + i 
'Hello',");
-                       Assert.IsFalse(p1.Parse());
-                       Assert.AreEqual(1, p1.Errors.Count);
-                       Assert.AreEqual("MJS0300", p1.Errors[0].ErrorNumber); 
// MJS300 = Erreur de syntaxe
-                       Assert.AreEqual(1, p1.Errors[0].Line);
-                       Assert.AreEqual(5, p1.Errors[0].Column);*/
+                       Parser parser = new Parser("var } function + i 
'Hello',".ToCharArray());
+                       List<Comment> comments = new List<Comment> ();
+                       parser.ParseProgram (ref comments);
+                       Assert.IsFalse (parser.SyntaxOK());
                }
+
+               [Test]
+               public void VarTest ()
+               {
+                       Parser parser = new Parser ("var a = 10;".ToCharArray 
());
+                       List<Comment> comments = new List<Comment> ();
+                       parser.ParseProgram (ref comments);
+                       Assert.IsTrue (parser.SyntaxOK ());
+               }
        }
 }

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

Reply via email to