Browse Source

made EF parser more suitable for the ExpressionFinder

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/vbnet@6060 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 16 years ago
parent
commit
c76bf5efc2
  1. 80
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.atg
  2. 17
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.cs
  3. 2811
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Parser.cs
  4. 117
      src/Libraries/NRefactory/Test/Lexer/VBNet/LexerContextTests.cs

80
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.atg

@ -259,7 +259,7 @@ TOKENS
PRODUCTIONS PRODUCTIONS
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
ExpressionFinder = ExpressionFinder =
(. PushContext(Context.Global, t); .) (. PushContext(Context.Global, la, t); .)
{ OptionStatement } { OptionStatement }
{ ImportsStatement } { ImportsStatement }
{ EXPECTEDCONFLICT("<") AttributeBlock } /* Expected LL(1) conflict: we can't tell global attributes */ { EXPECTEDCONFLICT("<") AttributeBlock } /* Expected LL(1) conflict: we can't tell global attributes */
@ -276,11 +276,14 @@ OptionStatement =
. .
ImportsStatement = ImportsStatement =
"Imports" (. nextTokenIsStartOfImportsOrAccessExpression = true; .) { ANY } StatementTerminator "Imports"
(. nextTokenIsStartOfImportsOrAccessExpression = true;
if (la != null)
CurrentBlock.lastExpressionStart = la.Location; .) { ANY } StatementTerminator
. .
AttributeBlock = AttributeBlock =
"<" (. PushContext(Context.Attribute, t); .) { ANY } ">" (. PopContext(); .) [ EOL ] "<" (. PushContext(Context.Attribute, la, t); .) { ANY } ">" (. PopContext(); .) [ EOL ]
. .
NamespaceMemberDeclaration = NamespaceMemberDeclaration =
@ -302,11 +305,13 @@ TypeDeclaration =
. .
ClassOrModuleOrStructureTypeDeclaration = ClassOrModuleOrStructureTypeDeclaration =
( "Module" | "Class" | "Structure" ) (. PushContext(Context.IdentifierExpected, t); .) ANY (. PopContext(); .) ( "Module" | "Class" | "Structure" ) (.
if (la != null)
CurrentBlock.lastExpressionStart = la.Location; PushContext(Context.IdentifierExpected, la, t); .) ANY (. PopContext(); .)
[ "(" "Of" [ "Out" | "In" ] IdentifierExceptOut [ "As" GenericConstraintList ] { "," [ "Out" | "In" ] IdentifierExceptOut [ "As" GenericConstraintList ] } ")" ] { ANY } [ StatementTerminator ] [ "(" "Of" [ "Out" | "In" ] IdentifierExceptOut [ "As" GenericConstraintList ] { "," [ "Out" | "In" ] IdentifierExceptOut [ "As" GenericConstraintList ] } ")" ] { ANY } [ StatementTerminator ]
[ "Inherits" { ANY } StatementTerminator ] [ "Inherits" { ANY } StatementTerminator ]
[ "Implements" { ANY } StatementTerminator ] [ "Implements" { ANY } StatementTerminator ]
(. PushContext(Context.Type, t); .) (. PushContext(Context.Type, la, t); .)
{ MemberDeclaration } { MemberDeclaration }
"End" ( "Module" | "Class" | "Structure" ) StatementTerminator "End" ( "Module" | "Class" | "Structure" ) StatementTerminator
(. PopContext(); .) (. PopContext(); .)
@ -322,12 +327,13 @@ GenericConstraintList =
DelegateTypeDeclaration = DelegateTypeDeclaration =
"Delegate" ("Sub" | "Function") "Delegate" ("Sub" | "Function")
(. PushContext(Context.IdentifierExpected, t); .) ANY (. PopContext(); .) (. if (la != null)
CurrentBlock.lastExpressionStart = la.Location; PushContext(Context.IdentifierExpected, la, t); .) ANY (. PopContext(); .)
[ "(" [ ParameterList ] ")" ] [ "As" TypeName ] StatementTerminator [ "(" [ ParameterList ] ")" ] [ "As" TypeName ] StatementTerminator
. .
MemberDeclaration = MemberDeclaration =
(. PushContext(Context.Member, t); .) (. PushContext(Context.Member, la, t); .)
{ AttributeBlock } { MemberModifier } { AttributeBlock } { MemberModifier }
( (
MemberVariableOrConstantDeclaration | MemberVariableOrConstantDeclaration |
@ -342,19 +348,19 @@ MemberDeclaration =
SubOrFunctionDeclaration = SubOrFunctionDeclaration =
("Sub" | "Function") ("Sub" | "Function")
(. PushContext(Context.IdentifierExpected, t); .) ANY (. PopContext(); .) (. PushContext(Context.IdentifierExpected, la, t); .) ANY (. PopContext(); .)
[ "(" [ ParameterList ] ")" ] [ "As" TypeName ] [ "(" [ ParameterList ] ")" ] [ "As" TypeName ]
StatementTerminatorAndBlock StatementTerminatorAndBlock
"End" ("Sub" | "Function") StatementTerminator "End" ("Sub" | "Function") StatementTerminator
. .
ExternalMemberDeclaration = ExternalMemberDeclaration =
"Declare" [ "Ansi" | "Unicode" | "Auto" ] ( "Sub" | "Function" ) (. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) "Declare" [ "Ansi" | "Unicode" | "Auto" ] ( "Sub" | "Function" ) (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .)
"Lib" LiteralString [ "Alias" LiteralString ] [ "(" [ ParameterList ] ")" ] [ "As" TypeName ] StatementTerminator "Lib" LiteralString [ "Alias" LiteralString ] [ "(" [ ParameterList ] ")" ] [ "As" TypeName ] StatementTerminator
. .
EventMemberDeclaration = EventMemberDeclaration =
"Event" (. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) ( "As" TypeName | [ "(" [ ParameterList ] ")" ] ) "Event" (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .) ( "As" TypeName | [ "(" [ ParameterList ] ")" ] )
[ "Implements" TypeName /*"." IdentifierOrKeyword*/ { "," TypeName /*"." IdentifierOrKeyword*/ } ] [ "Implements" TypeName /*"." IdentifierOrKeyword*/ { "," TypeName /*"." IdentifierOrKeyword*/ } ]
/* the TypeName production already allows the "." IdentifierOrKeyword syntax, so to avoid an ambiguous grammer we just leave that out */ /* the TypeName production already allows the "." IdentifierOrKeyword syntax, so to avoid an ambiguous grammer we just leave that out */
StatementTerminator StatementTerminator
@ -371,13 +377,20 @@ CustomEventMemberDeclaration =
. .
OperatorDeclaration = OperatorDeclaration =
"Operator" (. PushContext(Context.IdentifierExpected, t); .) ANY (. PopContext(); .) "(" ParameterList ")" [ "As" { AttributeBlock } TypeName ] "Operator" (. PushContext(Context.IdentifierExpected, la, t); .) ANY (. PopContext(); .) "(" ParameterList ")" [ "As" { AttributeBlock } TypeName ]
StatementTerminatorAndBlock StatementTerminatorAndBlock
"End" "Operator" StatementTerminator "End" "Operator" StatementTerminator
. .
MemberVariableOrConstantDeclaration = MemberVariableOrConstantDeclaration =
[ "Const" ] (. PushContext(Context.IdentifierExpected, t); .) IdentifierForFieldDeclaration (. PopContext(); .) [ "As" TypeName ] [ "=" Expression ] StatementTerminator [ "Const" ]
(.
if (la != null)
CurrentBlock.lastExpressionStart = la.Location;
PushContext(Context.IdentifierExpected, la, t);
if (la != null)
CurrentBlock.lastExpressionStart = la.Location;
.) IdentifierForFieldDeclaration (. PopContext(); .) [ "As" TypeName ] [ "=" Expression ] StatementTerminator
. .
ParameterList = ParameterList =
@ -385,11 +398,11 @@ ParameterList =
. .
Parameter = Parameter =
{ AttributeBlock } { ParameterModifier } (. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] [ "=" Expression ] { AttributeBlock } { ParameterModifier } (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] [ "=" Expression ]
. .
StatementTerminatorAndBlock = StatementTerminatorAndBlock =
(. PushContext(Context.Body, t); .) (. PushContext(Context.Body, la, t); .)
StatementTerminator StatementTerminator
{ EXPECTEDCONFLICT("End") { EXPECTEDCONFLICT("End")
( (
@ -415,11 +428,11 @@ StatementTerminatorAndBlock =
Expression Expression
(.NamedState:startOfExpression.) (.NamedState:startOfExpression.)
= =
(. PushContext(Context.Expression, t); .) (. if (la != null)
(. CurrentBlock.lastExpressionStart = la.Location; .)
if (la != null) (. PushContext(Context.Expression, la, t); .)
CurrentBlock.lastExpressionStart = la.Location; (. if (la != null)
.) CurrentBlock.lastExpressionStart = la.Location; .)
SimpleExpressionWithSuffix { BinaryOperator SimpleExpressionWithSuffix } SimpleExpressionWithSuffix { BinaryOperator SimpleExpressionWithSuffix }
(. PopContext(); .) (. PopContext(); .)
. .
@ -442,10 +455,14 @@ SimpleExpressionWithSuffix =
SimpleExpression { ExpressionSuffix } SimpleExpression { ExpressionSuffix }
| "TypeOf" SimpleExpressionWithSuffix "Is" TypeName | "TypeOf" SimpleExpressionWithSuffix "Is" TypeName
| NewExpression | NewExpression
| CollectionInitializer
) )
. .
SimpleExpression = SimpleExpression
(. if (la != null)
CurrentBlock.lastExpressionStart = la.Location; .)
=
( Literal ( Literal
| ( "(" Expression ")" ) | ( "(" Expression ")" )
| IdentifierForExpressionStart | IdentifierForExpressionStart
@ -495,11 +512,11 @@ ObjectInitializer =
. .
CollectionInitializer = CollectionInitializer =
"{" ( Expression | CollectionInitializer ) { "," ( Expression | CollectionInitializer ) } "}" "{" ( Expression | GREEDY CollectionInitializer ) { "," ( Expression | GREEDY CollectionInitializer ) } "}"
. .
ExpressionSuffix = ExpressionSuffix =
"(" ( "Of" TypeName { "," TypeName } ")" | ArgumentList ")" ) "(" ( "Of" TypeName { "," TypeName } ")" | [ ArgumentList ] ")" )
| ( "." | "!" | ".@" | "..." ) (. nextTokenIsStartOfImportsOrAccessExpression = true; .) [ XmlOpenTag ] IdentifierOrKeyword [ XmlCloseTag ] | ( "." | "!" | ".@" | "..." ) (. nextTokenIsStartOfImportsOrAccessExpression = true; .) [ XmlOpenTag ] IdentifierOrKeyword [ XmlCloseTag ]
. .
@ -549,7 +566,7 @@ FunctionLambdaExpression =
. .
QueryExpression QueryExpression
(. PushContext(Context.Query, t); .) (. PushContext(Context.Query, la, t); .)
= =
( FromQueryOperator | AggregateQueryOperator ) ( FromQueryOperator | AggregateQueryOperator )
{ QueryOperator } { QueryOperator }
@ -630,7 +647,7 @@ ExpressionRangeVariable =
"Explicit", "Equals", "Distinct", "Descending", "Compare", "By", "Explicit", "Equals", "Distinct", "Descending", "Compare", "By",
"Binary", "Auto", "Assembly", "Ascending", "Ansi", "Aggregate", ident) "Binary", "Auto", "Assembly", "Ascending", "Ansi", "Aggregate", ident)
( (
(. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .)
( (
"As" TypeName "=" "As" TypeName "="
| "=" | "="
@ -649,14 +666,14 @@ ExpressionRangeVariable =
. .
CollectionRangeVariable = CollectionRangeVariable =
(. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] "In" Expression (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] "In" Expression
. .
/* semantic action will be inserted on all paths that possibly lead to XmlLiteral */ /* semantic action will be inserted on all paths that possibly lead to XmlLiteral */
XmlLiteral XmlLiteral
(.OnEachPossiblePath: nextTokenIsPotentialStartOfXmlMode = true; .) (.OnEachPossiblePath: nextTokenIsPotentialStartOfXmlMode = true; .)
= =
(. PushContext(Context.Xml, t); .) (. PushContext(Context.Xml, la, t); .)
{ ( XmlComment | XmlProcessingInstruction ) [ XmlContent ] } XmlElement { XmlComment [ XmlContent ] } { ( XmlComment | XmlProcessingInstruction ) [ XmlContent ] } XmlElement { XmlComment [ XmlContent ] }
(. PopContext(); .) (. PopContext(); .)
. .
@ -922,11 +939,13 @@ Statement
VariableDeclarationStatement = VariableDeclarationStatement =
( "Dim" | "Static" | "Const" ) ( "Dim" | "Static" | "Const" )
(. (.
PushContext(Context.IdentifierExpected, t); if (la != null)
CurrentBlock.lastExpressionStart = la.Location;
PushContext(Context.IdentifierExpected, la, t);
if (la != null) if (la != null)
CurrentBlock.lastExpressionStart = la.Location; CurrentBlock.lastExpressionStart = la.Location;
.) .)
Identifier (. PopContext(); .) [ "?" ] [ ( "(" { "," } ")" ) ] { "," (. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) [ "?" ] [ ( "(" { "," } ")" ) ] } [ "As" [ "New" ] TypeName ] [ "=" Expression ] Identifier (. PopContext(); .) [ "?" ] [ ( "(" { "," } ")" ) ] { "," (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .) [ "?" ] [ ( "(" { "," } ")" ) ] } [ "As" [ "New" ] TypeName ] [ "=" Expression ]
. .
WithOrLockStatement = WithOrLockStatement =
@ -1015,7 +1034,8 @@ ForEachLoopStatement =
. .
ForLoopVariable = ForLoopVariable =
(. PushContext(Context.IdentifierExpected, t); .) SimpleExpression (. PopContext(); .) [ "?" ] { ExpressionSuffix } [ "As" TypeName ] (. if (la != null)
CurrentBlock.lastExpressionStart = la.Location; .)(. PushContext(Context.IdentifierExpected, la, t); .) SimpleExpression (. PopContext(); .) [ "?" ] { ExpressionSuffix } [ "As" TypeName ]
. .
ErrorHandlingStatement = ErrorHandlingStatement =
@ -1032,7 +1052,7 @@ TryStatement =
StatementTerminatorAndBlock StatementTerminatorAndBlock
{ {
"Catch" "Catch"
[ (. PushContext(Context.IdentifierExpected, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] ] [ (. PushContext(Context.IdentifierExpected, la, t); .) Identifier (. PopContext(); .) [ "As" TypeName ] ]
[ "When" Expression ] [ "When" Expression ]
StatementTerminatorAndBlock StatementTerminatorAndBlock
} }

17
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.cs

@ -28,19 +28,22 @@ namespace ICSharpCode.NRefactory.Parser.VB
} }
} }
void PushContext(Context context, Token token) void PushContext(Context context, Token la, Token t)
{ {
string indent = new string('\t', stack.Count); string indent = new string('\t', stack.Count);
Location l = token == null ? Location.Empty : token.EndLocation; Location l = la == null ? Location.Empty : la.EndLocation;
if (la != null)
CurrentBlock.lastExpressionStart = la.Location;
stack.Push(new Block() { context = context, lastExpressionStart = l }); stack.Push(new Block() { context = context, lastExpressionStart = l });
Print(indent + "enter " + context); Print(indent + "enter " + context);
} }
void SetContext(Context context, Token token) void SetContext(Context context, Token la, Token t)
{ {
PopContext(); PopContext();
PushContext(context, token); PushContext(context, la, t);
} }
void ApplyToken(Token token) void ApplyToken(Token token)
@ -145,5 +148,11 @@ namespace ICSharpCode.NRefactory.Parser.VB
public Context context; public Context context;
public Location lastExpressionStart; public Location lastExpressionStart;
public override string ToString()
{
return string.Format("[Block Context={0}, LastExpressionStart={1}]", context, lastExpressionStart);
}
} }
} }

2811
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff

117
src/Libraries/NRefactory/Test/Lexer/VBNet/LexerContextTests.cs

@ -289,6 +289,123 @@ exit Global
); );
} }
[Test]
public void ClassTest()
{
RunTest(
@"Class MainClass ' a comment
Dim under_score_field As Integer
Sub SomeMethod()
simple += 1
For Each loopVarName In collection
Next
End Sub
End Class",
@"enter Global
enter IdentifierExpected
exit IdentifierExpected
enter Type
enter Member
enter IdentifierExpected
exit IdentifierExpected
exit Member
enter Member
enter IdentifierExpected
exit IdentifierExpected
enter Body
enter Expression
exit Expression
enter IdentifierExpected
exit IdentifierExpected
enter Expression
exit Expression
enter Body
exit Body
exit Body
exit Member
exit Type
exit Global
");
}
[Test]
public void CollectionInitializer()
{
RunTest(@"'
' Created by SharpDevelop.
' User: Siegfried
' Date: 22.06.2010
' Time: 21:29
'
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Option Infer On
Imports System.Linq
Imports System.Xml.Linq
Module Program
Sub Main()
Console.WriteLine(""Hello World!"")
Dim name = ""Test""
Dim content = { 4, 5, New XAttribute(""a"", 3) }
Dim xml = <<%= name %> <%= content %> />
Console.ReadKey()
End Sub
End Module",
@"enter Global
enter IdentifierExpected
exit IdentifierExpected
enter Type
enter Member
enter IdentifierExpected
exit IdentifierExpected
enter Body
enter Expression
enter Expression
exit Expression
exit Expression
enter IdentifierExpected
exit IdentifierExpected
enter Expression
exit Expression
enter IdentifierExpected
exit IdentifierExpected
enter Expression
enter Expression
exit Expression
enter Expression
exit Expression
enter Expression
enter Expression
exit Expression
enter Expression
exit Expression
exit Expression
exit Expression
enter IdentifierExpected
exit IdentifierExpected
enter Expression
enter Xml
enter Expression
exit Expression
enter Expression
exit Expression
exit Xml
exit Expression
enter Expression
exit Expression
exit Body
exit Member
exit Type
exit Global
");
}
void RunTest(string code, string expectedOutput) void RunTest(string code, string expectedOutput)
{ {
ExpressionFinder p = new ExpressionFinder(); ExpressionFinder p = new ExpressionFinder();

Loading…
Cancel
Save