Author: olivier
Date: 2007-06-23 13:28:10 -0400 (Sat, 23 Jun 2007)
New Revision: 80601
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/Tokenizer.cs
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ChangeLog
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ParserTest.cs
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/TokenizerTest.cs
Log:
2007-06-23 Olivier Dufour <[EMAIL PROTECTED]>
* ParserTest.cs : Add all statement test and temporary comment some
test to make test work. It seems that we enter in an unending loop
* TokenizerTest.cs : fix test of block comment
* Tokenizer.cs : fix block comment
* Parser.cs : add label and continue break test of syntax error
Modified:
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
===================================================================
---
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
2007-06-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/ChangeLog
2007-06-23 17:28:10 UTC (rev 80601)
@@ -1,3 +1,8 @@
+2007-06-23 Olivier Dufour <[EMAIL PROTECTED]>
+
+ * Tokenizer.cs : fix block comment
+ * Parser.cs : add label and continue break test of syntax error
+
2007-06-22 Olivier Dufour <[EMAIL PROTECTED]>
* Parser.cs : badToken, labelStatement and octalStatement management
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-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Parser.cs
2007-06-23 17:28:10 UTC (rev 80601)
@@ -25,6 +25,9 @@
private Token current;
private List<String> SyntaxError = new List<string>();
private bool syntaxIncomplete = false;
+ private int withinIteration;
+ private int withinSwitch;
+ private List<string> LabelSet = new List<string> ();
#endregion
@@ -32,8 +35,8 @@
public DList<Statement, BlockStatement> ParseProgram (ref
List<Comment> Comments)
{
+ Init ();
DList<Statement, BlockStatement> result = new
DList<Statement, BlockStatement> ();
-
while (current.Kind != Token.Type.EndOfInput) {
if (current.Kind == Token.Type.function)
result.Append (ParseFunctionDeclaration
());
@@ -47,6 +50,7 @@
public Expression ParseExpression (ref List<Comment> Comments)
{
+ Init ();
Expression ex = ParseExpression();
Comments = lexer.Comments;
return ex;
@@ -54,6 +58,7 @@
public Statement ParseStatement (ref List<Comment> Comments)
{
+ Init ();
Statement sta = ParseStatement ();
Comments = lexer.Comments;
return sta;
@@ -143,8 +148,9 @@
case [EMAIL PROTECTED]:
return ParseFor ();
case [EMAIL PROTECTED]:
+ return ParseContinue ();
case [EMAIL PROTECTED]:
- return ParseBreakOrContinue ();
+ return ParseBreak ();
case Token.Type.with:
return ParseWith ();
case [EMAIL PROTECTED]:
@@ -160,7 +166,7 @@
case Token.Type.Semicolon:
return new Statement
(Statement.Operation.Empty, new TextSpan (current, current));
case Token.Type.Identifier:
- return ParseLabelStatement();
+ return ParseLabelStatement(); //TODO if
2 not colon => expression statement
default:
if ( current.Kind !=
Token.Type.LeftBrace
&& current.Kind !=
Token.Type.Comma
@@ -181,7 +187,11 @@
CheckSyntaxExpected (Token.Type.Colon);
TextPoint colon = new TextPoint (current.StartPosition);
Next ();
+ if (LabelSet.Contains (label.Spelling))
+ Error (DiagnosticCode.EnclosingLabelShadowed,
new TextSpan (start, current));
+ LabelSet.Add (label.Spelling);
Statement labeled = ParseStatement();
+ LabelSet.Remove (label.Spelling);
return new LabelStatement (label, labeled, new TextSpan
(start, current), colon);
}
@@ -255,7 +265,9 @@
Expression condition = ParseExpression ();
CheckSyntaxExpected (Token.Type.RightParenthesis);
Token rightParen = current;
+ withinIteration++;
Statement body = ParseStatement ();
+ withinIteration--;
return new WhileStatement (condition, body, new
TextSpan (start, current), new TextSpan (start, rightParen), new TextPoint
(leftParen.StartPosition), new TextPoint (rightParen.StartPosition));
}
@@ -263,7 +275,9 @@
{
Token start = current;
Next ();
+ withinIteration++;
Statement body = ParseStatement ();
+ withinIteration--;
Next ();
CheckSyntaxExpected ([EMAIL PROTECTED]);
Token whileToken = current;
@@ -305,7 +319,9 @@
CheckSyntaxExpected
(Token.Type.RightParenthesis);
rightParen = current;
Next ();
+ withinIteration++;
body = ParseStatement ();
+ withinIteration--;
VariableDeclaration item;
if (varDecl.Declarations.Count == 1) {
item =
varDecl.Declarations[0].Declaration;
@@ -329,7 +345,9 @@
CheckSyntaxExpected
(Token.Type.RightParenthesis);
rightParen = current;
Next ();
+ withinIteration++;
body = ParseStatement ();
+ withinIteration--;
return new DeclarationForStatement
(varDecl.Declarations, condition, increment, body,
new TextSpan (start, current),
new TextSpan (start, rightParen),
new TextPoint
(firstSemiColon.StartPosition),
@@ -346,7 +364,9 @@
CheckSyntaxExpected
(Token.Type.RightParenthesis);
rightParen = current;
Next ();
+ withinIteration++;
body = ParseStatement ();
+ withinIteration--;
return new ExpressionForInStatement
(initial, collection, body, new TextSpan (start, current),
new TextSpan (start,
rightParen), new TextPoint (inToken.StartPosition),
new TextPoint
(leftParen.StartPosition), new TextPoint (rightParen.StartPosition));
@@ -362,7 +382,9 @@
CheckSyntaxExpected
(Token.Type.RightParenthesis);
rightParen = current;
Next ();
+ withinIteration++;
body = ParseStatement ();
+ withinIteration--;
return new ExpressionForStatement (initial,
condition, increment, body,
new TextSpan (start, current), new
TextSpan (start, rightParen),
new TextPoint
(firstSemiColon.StartPosition), new TextPoint (secondSemiColon.StartPosition),
@@ -370,20 +392,40 @@
}
}
- private BreakOrContinueStatement ParseBreakOrContinue ()
+ private BreakOrContinueStatement ParseContinue ()
{
Token start = current;
- Statement.Operation opcode =
Statement.Operation.Continue;
- if (current.Kind == [EMAIL PROTECTED])
- opcode = Statement.Operation.Break;
+ Next ();
+ Identifier label = null;
+ if (current.Kind != Token.Type.Semicolon) {
+ if (CheckSyntaxExpected (Token.Type.Identifier))
+ label = (current as
IdentifierToken).Spelling;
+ if (!LabelSet.Contains (label.Spelling))
+ Error
(DiagnosticCode.ContinueLabelInvalid, new TextSpan (start, current));
+ } else if (withinIteration == 0)
+ Error (DiagnosticCode.ContinueContextInvalid,
new TextSpan (start, current));
+
+ InsertSemicolon ();
+ return new BreakOrContinueStatement
(Statement.Operation.Continue, label, new TextSpan (start, current), new
TextPoint (current.StartPosition));
+ }
+
+ private BreakOrContinueStatement ParseBreak ()
+ {
+ //todo test within vars
+ Token start = current;
Next();
Identifier label = null;
if (current.Kind != Token.Type.Semicolon) {
if (CheckSyntaxExpected (Token.Type.Identifier))
label = (current as
IdentifierToken).Spelling;
- }
+ if ( !LabelSet.Contains(label.Spelling))
+ Error (DiagnosticCode.NoEnclosingLabel,
new TextSpan (start, current));
+ } else if (withinIteration == 0
+ && withinSwitch == 0)
+ Error (DiagnosticCode.BreakContextInvalid, new
TextSpan (start, current));
+
InsertSemicolon ();
- return new BreakOrContinueStatement (opcode, label, new
TextSpan (start, current), new TextPoint (current.StartPosition));
+ return new BreakOrContinueStatement
(Statement.Operation.Break, label, new TextSpan (start, current), new TextPoint
(current.StartPosition));
}
private WithStatement ParseWith ()
@@ -414,6 +456,7 @@
CheckSyntaxExpected (Token.Type.LeftBrace);
Token leftBrace = current;
Next ();
+ withinSwitch++;
DList<CaseClause, SwitchStatement> cases = new
DList<CaseClause, SwitchStatement> ();
bool defaultFlag = false;
while (current.Kind == [EMAIL PROTECTED] ||
current.Kind == [EMAIL PROTECTED]) {
@@ -427,6 +470,7 @@
}
Next ();
}
+ withinSwitch--;
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;
@@ -1111,6 +1155,15 @@
#region helpers
+ private void Init ()
+ {
+ withinIteration = 0;
+ withinSwitch = 0;
+ LabelSet.Clear();
+ SyntaxError.Clear();
+ syntaxIncomplete = false;
+ }
+
private void Next ()
{
current = lexer.GetNext ();
@@ -1149,6 +1202,10 @@
code = DiagnosticCode.SemicolonExpected;
break;
}
+
+ if (current.Kind == Token.Type.EndOfInput)
+ syntaxIncomplete = true;
+
Error (code, new TextSpan (current,current));
return false;
}
@@ -1169,16 +1226,11 @@
code = ((BadToken)current).Diagnostic;
diagnostics.Add (new Diagnostic (code, loc));
}
- //TODO label management (break, continue,...)
+
//TODO finished bad token and syntax error management
- //TODO
+ //TODO when no line terminator test is line have change...
/* TODO
BadDivideOrRegularExpressionLiteral,
- EnclosingLabelShadowed,
- NoEnclosingLabel,
- BreakContextInvalid,
- ContinueContextInvalid,
- ContinueLabelInvalid,
MalformedEscapeSequence,
NumericLiteralThenIdentifier,
UnterminatedStringLiteral,
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-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Microsoft.JScript.Compiler/Tokenizer.cs
2007-06-23 17:28:10 UTC (rev 80601)
@@ -705,7 +705,7 @@
int startPosition = position;
int startLine = Line;
int startColumn = Column;
- StringBuilder sb = new StringBuilder ();
+ StringBuilder sb = new StringBuilder ("/*");
int c = ReadChar ();
@@ -713,7 +713,7 @@
sb.Append ((char) c);
c = ReadChar ();
}
-
+ sb.Append ("*/");
if (PeekChar () == '/')
ReadChar ();
@@ -722,7 +722,7 @@
private void CreateLineComment ()
{
- StringBuilder sb = new StringBuilder ();
+ StringBuilder sb = new StringBuilder ("//");
int startPosition = position;
int c = PeekChar ();
Modified:
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ChangeLog
===================================================================
---
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ChangeLog
2007-06-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ChangeLog
2007-06-23 17:28:10 UTC (rev 80601)
@@ -1,3 +1,9 @@
+2007-06-23 Olivier Dufour <[EMAIL PROTECTED]>
+
+ * ParserTest.cs : Add all statement test and temporary comment
+ some test to make test work. It seems
that we enter in an unending loop
+ * TokenizerTest.cs : fix test of block comment
+
2007-06-22 Olivier Dufour <[EMAIL PROTECTED]>
* ParserTest.cs : Add a test and fix typo
Modified:
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ParserTest.cs
===================================================================
---
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ParserTest.cs
2007-06-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/ParserTest.cs
2007-06-23 17:28:10 UTC (rev 80601)
@@ -11,19 +11,24 @@
[TestFixture]
public class ParserTest
{
+ Parser parser;
+
[Test]
public void SyntaxOKTest ()
{
- Parser parser = new Parser ("var } function + i
'Hello',".ToCharArray());
+ /*parser = new Parser ("var } function + i
'Hello',".ToCharArray ());
List<Comment> comments = new List<Comment> ();
parser.ParseProgram (ref comments);
- Assert.IsFalse (parser.SyntaxOK ());
+ Assert.IsFalse (parser.SyntaxOK ());*/
+ Assert.Fail ();
}
+ # region statements
+
[Test]
public void VarTest ()
{
- Parser parser = new Parser ("var a = 10;".ToCharArray
());
+ parser = new Parser ("var a = 10;".ToCharArray ());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement > list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement,BlockStatement>.Iterator(list);
@@ -34,7 +39,7 @@
[Test]
public void FunctionTest ()
{
- Parser parser = new Parser ("function foo()
{}".ToCharArray ());
+ parser = new Parser ("function foo() {}".ToCharArray
());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
@@ -47,7 +52,7 @@
[Test]
public void BlockTest ()
{
- Parser parser = new Parser ("{}".ToCharArray ());
+ parser = new Parser ("{}".ToCharArray ());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
@@ -58,7 +63,7 @@
[Test]
public void IfTest ()
{
- Parser parser = new Parser ("if (true) { } else
;".ToCharArray ());
+ parser = new Parser ("if (true) { } else
;".ToCharArray ());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
@@ -71,7 +76,7 @@
[Test]
public void WhileTest ()
{
- Parser parser = new Parser ("while (true) {
}".ToCharArray ());
+ parser = new Parser ("while (true) { }".ToCharArray ());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
@@ -84,7 +89,7 @@
[Test]
public void DoWhileTest ()
{
- Parser parser = new Parser ("do { }while
(true);".ToCharArray ());
+ parser = new Parser ("do { }while (true);".ToCharArray
());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
@@ -106,16 +111,104 @@
[Test]
public void ForTest ()
{
- Parser parser = new Parser ("for (var i = 0 ; i < 5 ;
i++ ) { }".ToCharArray ());
+ /*parser = new Parser ("for (var i = 0 ; i < 5 ; i++ )
{ break; }".ToCharArray ());
List<Comment> comments = new List<Comment> ();
DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
Assert.IsInstanceOfType (typeof
(DeclarationForStatement), it.Element, "#7.1");
DeclarationForStatement forst =
((DeclarationForStatement)it.Element);
+ Assert.IsInstanceOfType (typeof (BlockStatement),
forst.Body, "#7.2");
+ it = new DList<Statement, BlockStatement>.Iterator
(((BlockStatement)forst.Body).Children);
+ Assert.IsInstanceOfType (typeof
(BreakOrContinueStatement), it.Element, "#7.3");
+ Assert.IsTrue (parser.SyntaxOK ());*/
+ Assert.Fail ();
+ }
+
+ [Test]
+ public void WithTest ()
+ {
+ //todo fix syntax error
+ parser = new Parser ("with (test) {}".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof (WithStatement),
it.Element, "#8.1");
Assert.IsTrue (parser.SyntaxOK ());
}
[Test]
+ public void switchTest ()
+ {
+ //todo fix syntax error
+ parser = new Parser ("switch (test) { case a: break;
default: break;}".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof (SwitchStatement),
it.Element, "#9.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ [Test]
+ public void TryTest ()
+ {
+ //todo fix syntax error
+ parser = new Parser ("try {} finally {}".ToCharArray
());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof (TryStatement),
it.Element, "#10.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ [Test]
+ public void ThrowTest ()
+ {
+ parser = new Parser ("throw a;".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof
(ReturnOrThrowStatement), it.Element, "#11.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ [Test]
+ public void ReturnTest ()
+ {
+ parser = new Parser ("return a;".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof
(ReturnOrThrowStatement), it.Element, "#12.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ [Test]
+ public void EmptyTest ()
+ {
+ parser = new Parser (";".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.AreEqual (Statement.Operation.Empty,
it.Element.Opcode, "#12.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ [Test]
+ public void labelTest ()
+ {
+ parser = new Parser ("a : {}".ToCharArray ());
+ List<Comment> comments = new List<Comment> ();
+ DList<Statement, BlockStatement> list =
parser.ParseProgram (ref comments);
+ DList<Statement, BlockStatement>.Iterator it = new
DList<Statement, BlockStatement>.Iterator (list);
+ Assert.IsInstanceOfType (typeof (LabelStatement),
it.Element, "#12.1");
+ Assert.IsTrue (parser.SyntaxOK ());
+ }
+
+ #endregion
+
+ #region Expressions
+
+ [Test]
public void AddTest ()
{
Parser parser = new Parser ("var i = 5 +
1;".ToCharArray ());
@@ -131,5 +224,8 @@
Assert.AreEqual (Expression.Operation.Plus,
ini.Initializer.Opcode, "#8.5");
Assert.IsTrue (parser.SyntaxOK ());
}
+
+ #endregion
+
}
}
Modified:
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/TokenizerTest.cs
===================================================================
---
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/TokenizerTest.cs
2007-06-23 16:44:46 UTC (rev 80600)
+++
trunk/olive/class/Microsoft.JScript.Compiler/Test/Microsoft.JScript.Compiler/TokenizerTest.cs
2007-06-23 17:28:10 UTC (rev 80601)
@@ -58,7 +58,7 @@
{
Tokenizer t = new Tokenizer ("// \n ;".ToCharArray (),
new IdentifierTable ());
Assert.AreEqual (Token.Type.Semicolon, t.GetNext
().Kind, "#5.1");
- Assert.AreEqual (" ",t.Comments[0].Spelling, "#5.1");
+ Assert.AreEqual ("// ",t.Comments[0].Spelling, "#5.1");
}
[Test]
@@ -66,7 +66,7 @@
{
Tokenizer t = new Tokenizer ("/*abcdef*/ ;".ToCharArray
(), new IdentifierTable ());
Assert.AreEqual (Token.Type.Semicolon, t.GetNext
().Kind, "#6.1");
- Assert.AreEqual ("abcdef", t.Comments[0].Spelling,
"#6.2");
+ Assert.AreEqual ("/*abcdef*/", t.Comments[0].Spelling,
"#6.2");
}
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches