Skip to content

Commit ab3d28a

Browse files
author
Andrin Meier
committed
implement annotations using the split parser/lexer (#1116)
1 parent c35179e commit ab3d28a

27 files changed

+4380
-3234
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Rubberduck.Parsing.Grammar;
2+
3+
namespace Rubberduck.Parsing.Annotations
4+
{
5+
public abstract class AnnotationBase : IAnnotation
6+
{
7+
private readonly VBAParser.AnnotationContext _context;
8+
private readonly AnnotationType _annotationType;
9+
private readonly AnnotationTargetType _targetType;
10+
11+
public const string ANNOTATION_MARKER = "'@";
12+
13+
public AnnotationBase(VBAParser.AnnotationContext context, AnnotationType annotationType, AnnotationTargetType targetType)
14+
{
15+
_context = context;
16+
_annotationType = annotationType;
17+
_targetType = targetType;
18+
}
19+
20+
public VBAParser.AnnotationContext Context
21+
{
22+
get
23+
{
24+
return _context;
25+
}
26+
}
27+
28+
public AnnotationType AnnotationType
29+
{
30+
get
31+
{
32+
return _annotationType;
33+
}
34+
}
35+
36+
public AnnotationTargetType TargetType
37+
{
38+
get
39+
{
40+
return _targetType;
41+
}
42+
}
43+
44+
public override string ToString()
45+
{
46+
return string.Format("Annotation Type: {0}. TargetType: {1}.", _annotationType, _targetType);
47+
}
48+
}
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Antlr4.Runtime.Misc;
2+
using Rubberduck.Parsing.Grammar;
3+
using System.Collections.Generic;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public sealed class AnnotationListener : VBAParserBaseListener
8+
{
9+
private readonly List<IAnnotation> _annotations;
10+
private readonly IAnnotationFactory _factory;
11+
12+
public AnnotationListener(IAnnotationFactory factory)
13+
{
14+
_annotations = new List<IAnnotation>();
15+
_factory = factory;
16+
}
17+
18+
public IEnumerable<IAnnotation> Annotations
19+
{
20+
get
21+
{
22+
return _annotations;
23+
}
24+
}
25+
26+
public override void ExitAnnotation([NotNull] VBAParser.AnnotationContext context)
27+
{
28+
var newAnnotation = _factory.Create(context, AnnotationTargetType.Unknown);
29+
_annotations.Add(newAnnotation);
30+
}
31+
}
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Rubberduck.Parsing.Annotations
2+
{
3+
public enum AnnotationTargetType
4+
{
5+
Unknown,
6+
Module,
7+
Method,
8+
VariableStatement
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Rubberduck.Parsing.Annotations
2+
{
3+
public enum AnnotationType
4+
{
5+
TestModule,
6+
ModuleInitialize,
7+
ModuleCleanup,
8+
TestMethod,
9+
TestInitialize,
10+
TestCleanup,
11+
Ignore,
12+
Folder
13+
}
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public sealed class FolderAnnotation : AnnotationBase
8+
{
9+
private readonly string _folderName;
10+
11+
public FolderAnnotation(VBAParser.AnnotationContext context, AnnotationTargetType targetType, IEnumerable<string> parameters)
12+
: base(context, AnnotationType.Folder, targetType)
13+
{
14+
if (parameters.Count() != 1)
15+
{
16+
throw new InvalidAnnotationArgumentException(string.Format("{0} expects exactly one argument, the folder, but none or more than one were passed.", this.GetType().Name));
17+
}
18+
_folderName = parameters.First();
19+
}
20+
21+
public string FolderName
22+
{
23+
get
24+
{
25+
return _folderName;
26+
}
27+
}
28+
29+
public override string ToString()
30+
{
31+
return string.Format("Folder: {0}.", _folderName);
32+
}
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Rubberduck.Parsing.Grammar;
2+
3+
namespace Rubberduck.Parsing.Annotations
4+
{
5+
public interface IAnnotation
6+
{
7+
AnnotationType AnnotationType { get; }
8+
VBAParser.AnnotationContext Context { get; }
9+
AnnotationTargetType TargetType { get; }
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Antlr4.Runtime;
2+
using Rubberduck.Parsing.Grammar;
3+
using Rubberduck.Parsing.Symbols;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public interface IAnnotationFactory
8+
{
9+
IAnnotation Create(VBAParser.AnnotationContext annotationContext, AnnotationTargetType targetType);
10+
}
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Rubberduck.Parsing.Annotations
6+
{
7+
public sealed class IgnoreAnnotation : AnnotationBase
8+
{
9+
private readonly IEnumerable<string> _inspectionNames;
10+
11+
public IgnoreAnnotation(VBAParser.AnnotationContext context, AnnotationTargetType targetType, IEnumerable<string> parameters)
12+
: base(context, AnnotationType.Ignore, targetType)
13+
{
14+
if (!parameters.Any())
15+
{
16+
throw new InvalidAnnotationArgumentException(string.Format("{0} expects at least one argument but none were given.", this.GetType().Name));
17+
}
18+
_inspectionNames = parameters.ToList();
19+
}
20+
21+
public IEnumerable<string> InspectionNames
22+
{
23+
get
24+
{
25+
return _inspectionNames;
26+
}
27+
}
28+
29+
public bool IsIgnored(string inspectionName)
30+
{
31+
return _inspectionNames.Contains(inspectionName);
32+
}
33+
34+
public override string ToString()
35+
{
36+
return string.Format("Ignored inspections: {0}.", string.Join(", ", _inspectionNames));
37+
}
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Rubberduck.Parsing.Annotations
4+
{
5+
[Serializable]
6+
public class InvalidAnnotationArgumentException : Exception
7+
{
8+
public InvalidAnnotationArgumentException(string message)
9+
: base(message)
10+
{
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Rubberduck.Parsing.Grammar;
2+
using System.Collections.Generic;
3+
4+
namespace Rubberduck.Parsing.Annotations
5+
{
6+
public sealed class ModuleCleanupAnnotation : AnnotationBase
7+
{
8+
public ModuleCleanupAnnotation(VBAParser.AnnotationContext context, AnnotationTargetType targetType, IEnumerable<string> parameters)
9+
: base(context, AnnotationType.TestMethod, targetType)
10+
{
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)