├── .gitignore ├── .travis.yml ├── DParser2.Unittest.sln ├── DParser2 ├── Completion │ ├── CodeCompletion.cs │ ├── ICompletionDataGenerator.cs │ ├── IEditorData.cs │ ├── MemberCompletionEnumeration.cs │ ├── ParamInsightVisitor.cs │ ├── ParameterInsightResolution.cs │ ├── Providers │ │ ├── AbstractCompletionProvider.cs │ │ ├── AttributeCompletionProvider.cs │ │ ├── CompletionProviderVisitor.cs │ │ ├── CtrlSpaceCompletionProvider.cs │ │ ├── ImportStatementCompletionProvider.cs │ │ ├── InlineAsmCompletionProvider.cs │ │ ├── MemberCompletionProvider.cs │ │ ├── MethodOverrideCompletionProvider.cs │ │ ├── ModuleStatementCompletionProvider.cs │ │ ├── SelectiveImportCompletionProvider.cs │ │ ├── StructInitializerCompletion.cs │ │ └── VariableNameSuggestionCompletionProvider.cs │ └── ToolTips │ │ ├── NodeTooltipRepresentationGen.Headers.cs │ │ └── NodeTooltipRepresentationGen.cs ├── DParser2.csproj ├── DParser2.sln ├── Dom │ ├── AbstractTypeDeclaration.cs │ ├── Attribute.cs │ ├── CodeLocation.cs │ ├── DDeclarations.cs │ ├── Expressions │ │ ├── AbstractVariableInitializer.cs │ │ ├── AddExpression.cs │ │ ├── AndAndExpression.cs │ │ ├── AndExpression.cs │ │ ├── AnonymousClassExpression.cs │ │ ├── ArrayInitializer.cs │ │ ├── ArrayLiteralExpression.cs │ │ ├── AsmRegisterExpression.cs │ │ ├── AssertExpression.cs │ │ ├── AssignExpression.cs │ │ ├── AssocArrayExpression.cs │ │ ├── CastExpression.cs │ │ ├── CatExpression.cs │ │ ├── ConditionalExpression.cs │ │ ├── ContainerExpression.cs │ │ ├── DeleteExpression.cs │ │ ├── EqualExpression.cs │ │ ├── Expression.cs │ │ ├── FunctionLiteral.cs │ │ ├── IExpression.cs │ │ ├── IVariableInitializer.cs │ │ ├── IdentifierExpression.cs │ │ ├── IdentityExpression.cs │ │ ├── ImportExpression.cs │ │ ├── InExpression.cs │ │ ├── IsExpression.cs │ │ ├── MixinExpression.cs │ │ ├── MulExpression.cs │ │ ├── NewExpression.cs │ │ ├── OperatorBasedExpression.cs │ │ ├── OrExpression.cs │ │ ├── OrOrExpression.cs │ │ ├── PostfixExpression.cs │ │ ├── PostfixExpression_Access.cs │ │ ├── PostfixExpression_ArrayAccess.cs │ │ ├── PostfixExpression_Decrement.cs │ │ ├── PostfixExpression_Increment.cs │ │ ├── PostfixExpression_MethodCall.cs │ │ ├── PowExpression.cs │ │ ├── PrimaryExpression.cs │ │ ├── RelExpression.cs │ │ ├── ScalarConstantExpression.cs │ │ ├── ShiftExpression.cs │ │ ├── SimpleUnaryExpression.cs │ │ ├── StringLiteralExpression.cs │ │ ├── StructInitializer.cs │ │ ├── StructMemberInitializer.cs │ │ ├── SurroundingParenthesesExpression.cs │ │ ├── TemplateInstanceExpression.cs │ │ ├── TokenExpression.cs │ │ ├── TraitsArgument.cs │ │ ├── TraitsExpression.cs │ │ ├── TypeDeclarationExpression.cs │ │ ├── TypeidExpression.cs │ │ ├── UnaryExpression.cs │ │ ├── UnaryExpression_Add.cs │ │ ├── UnaryExpression_And.cs │ │ ├── UnaryExpression_Cat.cs │ │ ├── UnaryExpression_Decrement.cs │ │ ├── UnaryExpression_Increment.cs │ │ ├── UnaryExpression_Mul.cs │ │ ├── UnaryExpression_Not.cs │ │ ├── UnaryExpression_SegmentBase.cs │ │ ├── UnaryExpression_Sub.cs │ │ ├── UnaryExpression_Type.cs │ │ ├── VoidInitializer.cs │ │ └── XorExpression.cs │ ├── IDeclarationCondition.cs │ ├── ISyntaxRegion.cs │ ├── ImportStatement.cs │ ├── ModulePackage.cs │ ├── Nodes │ │ ├── AbstractNode.cs │ │ ├── AttributeBlock.cs │ │ ├── DBlockNode.cs │ │ ├── DModule.cs │ │ ├── DNode.cs │ │ ├── NodeDictionary.cs │ │ ├── NodeInterfaces.cs │ │ └── OtherNodeDefinitions.cs │ ├── ParserError.cs │ ├── Statements │ │ ├── AbstractStatement.cs │ │ ├── AsmAlignStatement.cs │ │ ├── AsmInstructionStatement.cs │ │ ├── AsmRawDataStatement.cs │ │ ├── AsmStatement.cs │ │ ├── BlockStatement.cs │ │ ├── BreakStatement.cs │ │ ├── ContinueStatement.cs │ │ ├── ContractStatement.cs │ │ ├── DebugSpecificiation.cs │ │ ├── DeclarationStatement.cs │ │ ├── ExpressionStatement.cs │ │ ├── ForStatement.cs │ │ ├── ForeachStatement.cs │ │ ├── GotoStatement.cs │ │ ├── IDeclarationContainingStatement.cs │ │ ├── IExpressionContainingStatement.cs │ │ ├── IStatement.cs │ │ ├── IfStatement.cs │ │ ├── LabeledStatement.cs │ │ ├── MixinStatement.cs │ │ ├── PragmaStatement.cs │ │ ├── ReturnStatement.cs │ │ ├── ScopeGuardStatement.cs │ │ ├── StatementCondition.cs │ │ ├── StatementContainingStatement.cs │ │ ├── StaticAssertStatement.cs │ │ ├── StaticForeachStatement.cs │ │ ├── SwitchStatement.cs │ │ ├── SynchronizedStatement.cs │ │ ├── TemplateMixin.cs │ │ ├── ThrowStatement.cs │ │ ├── TryStatement.cs │ │ ├── VersionSpecification.cs │ │ ├── VolatileStatement.cs │ │ ├── WhileStatement.cs │ │ └── WithStatement.cs │ ├── StaticStatement.cs │ ├── TemplateParameters.cs │ └── Visitors │ │ ├── AstElementHashingVisitor.cs │ │ ├── DefaultDepthFirstVisitor.cs │ │ ├── IVisitable.cs │ │ └── Visitors.cs ├── Formatting │ ├── DFormattingOptions.cs │ ├── DFormattingVisitor.Impl.cs │ ├── DFormattingVisitor.cs │ ├── DocumentAdapter.cs │ ├── Formatter.cs │ ├── FormattingIndentStack.cs │ ├── ITextEditorOptions.cs │ └── Indent │ │ ├── DIndentEngine.cs │ │ ├── IDocumentIndentEngine.cs │ │ ├── IStateMachineIndentEngine.cs │ │ ├── IndentEngine.cs │ │ ├── IndentEngineWrapper.cs │ │ ├── IndentStack.cs │ │ └── IndentStates.cs ├── Misc │ ├── CompletionOptions.cs │ ├── DocumentHelper.cs │ ├── GlobalParseCache.cs │ ├── HashNumbers.cs │ ├── Logger.cs │ ├── Mangling │ │ ├── Demangler.cs │ │ └── Mangler.cs │ ├── ModuleNameHelper.cs │ ├── ParseCacheView.cs │ ├── ParseLog.cs │ ├── StringCache.cs │ ├── StringExtension.cs │ ├── StringView.cs │ └── VersionIdEvaluation.cs ├── Parser │ ├── DDocParser.cs │ ├── Implementations │ │ ├── DAsmStatementParser.cs │ │ ├── DAttributesParser.cs │ │ ├── DBodiedSymbolsParser.cs │ │ ├── DDeclarationParser.cs │ │ ├── DExpressionsParser.cs │ │ ├── DModulesParser.cs │ │ ├── DParserImplementationPart.cs │ │ ├── DParserParts.cs │ │ ├── DParserStateContext.cs │ │ ├── DStatementParser.cs │ │ └── DTemplatesParser.cs │ ├── IncrementalParsing.cs │ ├── Lexer.cs │ ├── Parser.cs │ └── Tokens │ │ ├── Comment.cs │ │ ├── DToken.cs │ │ ├── DTokens.cs │ │ ├── DTokensSemanticHelpers.cs │ │ ├── LiteralFormat.cs │ │ └── LiteralSubformat.cs ├── Refactoring │ ├── ClassInterfaceDerivativeFinder.cs │ ├── ITextDocument.cs │ ├── ImportStmtCreation.cs │ ├── ReferencesFinder.cs │ ├── SortImportsRefactoring.cs │ └── TypeReferenceFinder.cs ├── Resolver │ ├── ASTScanner │ │ ├── AbstractResolutionVisitor.cs │ │ ├── AbstractVisitor.StatementHandler.cs │ │ ├── AbstractVisitor.cs │ │ ├── ConditionsFrame.cs │ │ ├── ItemCheckParameters.cs │ │ ├── ItemEnumeration.cs │ │ ├── MemberFilter.cs │ │ ├── NameScan.cs │ │ ├── SingleNodeNameScan.cs │ │ └── Util │ │ │ ├── IndexKeyExtendingEnumerable.cs │ │ │ ├── IotaEnumerable.cs │ │ │ └── StringCharValuesEnumerable.cs │ ├── Caching │ │ ├── ExpressionCache.cs │ │ └── ResolutionCache.cs │ ├── ConditionalCompilation.cs │ ├── ConditionalCompilationFlags.cs │ ├── DTypeToCodeVisitor.cs │ ├── DTypeToTypeDeclVisitor.cs │ ├── ExpressionSemantics │ │ ├── CTFE │ │ │ └── FunctionEvaluation.cs │ │ ├── Evaluation.Identifiers.cs │ │ ├── Evaluation.IsExpression.cs │ │ ├── Evaluation.OperandExpressions.cs │ │ ├── Evaluation.PostfixExpressions.cs │ │ ├── Evaluation.PrimaryExpression.cs │ │ ├── Evaluation.Traits.cs │ │ ├── Evaluation.TypeidExpression.cs │ │ ├── Evaluation.UnaryExpressions.cs │ │ ├── Evaluation.cs │ │ ├── EvaluationException.cs │ │ ├── Exceptions │ │ │ ├── CtfeException.cs │ │ │ ├── EvaluationStackOverflowException.cs │ │ │ └── VariableNotInitializedException.cs │ │ ├── ExpressionTypeEvaluation.cs │ │ ├── ExpressionValues.cs │ │ ├── ISymbolValue.cs │ │ ├── ISymbolValueVisitor.cs │ │ ├── LeftValueSetterVisitor.cs │ │ ├── LeftValues.cs │ │ ├── MathOperationEvaluation.cs │ │ ├── MethodOverloadCandidateSearchVisitor.cs │ │ ├── MethodOverloadsByParameterTypeComparisonFilter.cs │ │ ├── StatefulEvaluationContext.cs │ │ ├── SymbolValueComparer.cs │ │ └── VariableValueEvaluation.cs │ ├── IResolvedTypeVisitor.cs │ ├── MixinAnalysis.cs │ ├── Model │ │ ├── ComplexValue.cs │ │ ├── ContextFrame.cs │ │ ├── DType.cs │ │ └── ISemantic.cs │ ├── ResolutionContext.cs │ ├── ResolutionError.cs │ ├── ResolutionHooks │ │ ├── HookAttribute.cs │ │ ├── HookRegistry.cs │ │ ├── Hooks │ │ │ ├── Tuple.cs │ │ │ ├── autoimplement.cs │ │ │ └── bitfields.cs │ │ └── IHook.cs │ ├── ResolutionOptions.cs │ ├── ResolvedTypeCloner.cs │ ├── ResultComparer.cs │ ├── StaticProperties.cs │ ├── StorageClassImplicitCastCheck.cs │ ├── Templates │ │ ├── DeducedTypeDictionary.cs │ │ ├── SpecializationOrdering.cs │ │ ├── TemplateInstanceHandler.cs │ │ ├── TemplateParameterDeduction.cs │ │ ├── TemplateParameterDeductionVisitor.cs │ │ └── TemplateTypeParameterTypeMatcher.cs │ └── TypeResolution │ │ ├── ASTSearchHelper.cs │ │ ├── ClassInterfaceResolver.cs │ │ ├── DSymbolBaseTypeResolver.cs │ │ ├── LooseResolution.cs │ │ ├── OpDispatchResolution.cs │ │ ├── Resolver.cs │ │ ├── ScopedObjectVisitor.cs │ │ ├── SingleResolverVisitor.cs │ │ ├── TypeDeclarationResolver.cs │ │ └── UFCSResolver.cs └── readme.txt ├── ExaustiveCompletionTester ├── CompletionFacilities.cs ├── Config.cs ├── ExaustiveCompletionTester.csproj ├── FileProcessingData.cs ├── Program.cs └── SpecializedDataGen.cs ├── IndependentTests ├── IndependentTests.csproj ├── IndependentTests.sln ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── TestTool ├── BotanProfil.cs ├── Program.cs └── TestTool.csproj ├── Tests ├── Completion │ ├── CompletionTests.cs │ ├── TestCompletionDataGen.cs │ ├── TestUtil.cs │ ├── TestsEditorData.cs │ └── TooltipGenerationTests.cs ├── ExpressionEvaluation │ ├── CtfeTests.cs │ ├── EvaluationTests.cs │ └── TraitsEvaluationTests.cs ├── FormatterTests.cs ├── IndentationTests.cs ├── Misc │ ├── DemanglerTests.cs │ └── GlobalParseCacheTests.cs ├── ParseTests.cs ├── Resolution │ ├── DeclDefScopingTests.cs │ ├── DeclarationConstraintsTests.cs │ ├── ImplicitConversionTests.cs │ ├── LooseResolutionTests.cs │ ├── MethodCallOverloadRelatedTests.cs │ ├── MixinResolutionTests.cs │ ├── OperatorOverloadingTests.cs │ ├── ReferenceFindingTests.cs │ ├── ResolutionTestHelper.cs │ ├── ResolutionTests.cs │ ├── StatementTests.cs │ ├── TemplateMixinResolutionTests.cs │ ├── TemplateParameterDeductionTests.cs │ └── UFCSTests.cs ├── Tests.csproj └── formatterTests.txt ├── license.txt └── readme.md /.travis.yml: -------------------------------------------------------------------------------- 1 | ## Travis CI Integration 2 | 3 | notifications: 4 | email: false 5 | irc: "chat.freenode.net#d.mono-d" 6 | 7 | language: csharp 8 | mono: none 9 | dotnet: 3.1.103 10 | solution: DParser2.Unittest.sln 11 | cache: bundler 12 | 13 | install: 14 | - dotnet restore 15 | 16 | script: 17 | - dotnet build /p:Configuration=Debug "/p:Platform=Any CPU" 18 | - dotnet test ./Tests/bin/Debug/netcoreapp3.0/Tests.dll -------------------------------------------------------------------------------- /DParser2/Completion/ICompletionDataGenerator.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Completion 4 | { 5 | public interface ICompletionDataGenerator 6 | { 7 | /// 8 | /// Adds a token entry 9 | /// 10 | void Add(byte token); 11 | 12 | /// 13 | /// Adds a property attribute 14 | /// 15 | void AddPropertyAttribute(string attributeText); 16 | 17 | void AddIconItem(string iconName, string text, string description); 18 | void AddTextItem(string text, string description); 19 | 20 | /// 21 | /// Adds a node to the completion data 22 | /// 23 | /// 24 | void Add(INode node); 25 | 26 | void AddModule(DModule module,string nameOverride = null); 27 | void AddPackage(string packageName); 28 | 29 | /// 30 | /// Used for method override completion 31 | /// 32 | void AddCodeGeneratingNodeItem(INode node, string codeToGenerate); 33 | 34 | /// 35 | /// Sets a prefix or a 'contains'-Pattern that indicates items that will be added later on. 36 | /// The first matching item may be pre-selected in the completion list then. 37 | /// 38 | void SetSuggestedItem(string item); 39 | 40 | /// 41 | /// Notifies completion high-levels that the completion has been stopped due to taking too much time. 42 | /// After this message has been sent, other completion items may still be added! 43 | /// 44 | void NotifyTimeout(); 45 | 46 | /// 47 | /// Used for calculating the text region that shall be replaced on a completion commit. 48 | /// Can be null. 49 | /// 50 | ISyntaxRegion TriggerSyntaxRegion { set; } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/AbstractCompletionProvider.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Completion.Providers 4 | { 5 | public abstract class AbstractCompletionProvider 6 | { 7 | protected readonly ICompletionDataGenerator CompletionDataGenerator; 8 | 9 | protected AbstractCompletionProvider(ICompletionDataGenerator completionDataGenerator) 10 | { 11 | CompletionDataGenerator = completionDataGenerator; 12 | } 13 | 14 | #region Helper Methods 15 | public static bool CanItemBeShownGenerally(INode dn) 16 | { 17 | if (dn == null || dn.NameHash == 0) 18 | return false; 19 | 20 | if (dn is DMethod) 21 | { 22 | var dm = dn as DMethod; 23 | 24 | if (dm.SpecialType == DMethod.MethodType.Unittest || 25 | dm.SpecialType == DMethod.MethodType.Destructor || 26 | dm.SpecialType == DMethod.MethodType.Constructor) 27 | return false; 28 | } 29 | 30 | return true; 31 | } 32 | #endregion 33 | 34 | protected abstract void BuildCompletionDataInternal(IEditorData editor, char enteredChar); 35 | 36 | public void BuildCompletionData(IEditorData editor, char enteredChar) => 37 | BuildCompletionDataInternal(editor, enteredChar); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/ImportStatementCompletionProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using D_Parser.Dom; 3 | namespace D_Parser.Completion.Providers 4 | { 5 | /// 6 | /// import io = |; 7 | /// import |; 8 | /// 9 | class ImportStatementCompletionProvider : AbstractCompletionProvider 10 | { 11 | readonly ImportStatement.Import imp; 12 | 13 | public ImportStatementCompletionProvider( 14 | ICompletionDataGenerator gen, 15 | ImportStatement.Import imp) 16 | : base(gen) 17 | { 18 | this.imp = imp; 19 | } 20 | 21 | protected override void BuildCompletionDataInternal(IEditorData Editor, char enteredChar) 22 | { 23 | if(Editor.ParseCache == null) 24 | return; 25 | 26 | string pack = null; 27 | 28 | if (imp.ModuleIdentifier != null && imp.ModuleIdentifier.InnerDeclaration != null) 29 | { 30 | pack = imp.ModuleIdentifier.InnerDeclaration.ToString(); 31 | 32 | // Will occur after an initial dot 33 | if (string.IsNullOrEmpty(pack)) 34 | return; 35 | } 36 | 37 | foreach (var p in Editor.ParseCache.LookupPackage(Editor.SyntaxTree, pack)) 38 | { 39 | if (p == null) 40 | continue; 41 | 42 | foreach (var kv_pack in p.Packages) 43 | CompletionDataGenerator.AddPackage(kv_pack.Value.Name); 44 | 45 | foreach (var kv_mod in p.Modules) 46 | CompletionDataGenerator.AddModule(kv_mod.Value); 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/ModuleStatementCompletionProvider.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace D_Parser.Completion.Providers 3 | { 4 | class ModuleStatementCompletionProvider : AbstractCompletionProvider 5 | { 6 | public ModuleStatementCompletionProvider(ICompletionDataGenerator dg) : base(dg){} 7 | 8 | protected override void BuildCompletionDataInternal(IEditorData Editor, char enteredChar) 9 | { 10 | CompletionDataGenerator.AddModule(Editor.SyntaxTree, Editor.SyntaxTree.ModuleName); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/SelectiveImportCompletionProvider.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using D_Parser.Resolver; 3 | using D_Parser.Resolver.ASTScanner; 4 | using System.Linq; 5 | namespace D_Parser.Completion.Providers 6 | { 7 | /// 8 | /// import std.stdio : |, foo = |; 9 | /// 10 | class SelectiveImportCompletionProvider : AbstractCompletionProvider 11 | { 12 | readonly ImportStatement.Import import; 13 | 14 | public SelectiveImportCompletionProvider(ICompletionDataGenerator gen, ImportStatement.Import imp) : base(gen) { 15 | import = imp; 16 | } 17 | 18 | protected override void BuildCompletionDataInternal(IEditorData Editor, char enteredChar) 19 | { 20 | if (Editor.ParseCache == null) 21 | return; 22 | 23 | var module = Editor.ParseCache.LookupModuleName(Editor.SyntaxTree, import.ModuleIdentifier.ToString(true)).FirstOrDefault(); 24 | 25 | if (module == null) 26 | return; 27 | 28 | var ctxt = ResolutionContext.Create(Editor, true); 29 | 30 | /* 31 | * Show all members of the imported module 32 | * + public imports 33 | * + items of anonymous enums 34 | */ 35 | 36 | MemberCompletionEnumeration.EnumChildren(CompletionDataGenerator, ctxt, module, true, MemberFilter.All, true); 37 | 38 | return; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/StructInitializerCompletion.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using D_Parser.Dom.Expressions; 3 | using D_Parser.Resolver; 4 | using D_Parser.Resolver.ASTScanner; 5 | using D_Parser.Resolver.TypeResolution; 6 | using System.Collections.Generic; 7 | 8 | namespace D_Parser.Completion.Providers 9 | { 10 | class StructInitializerCompletion : AbstractCompletionProvider 11 | { 12 | public readonly DVariable initedVar; 13 | public readonly StructInitializer init; 14 | 15 | public StructInitializerCompletion(ICompletionDataGenerator gen,DVariable initializedVariable, StructInitializer init) : base(gen) 16 | { 17 | this.initedVar = initializedVariable; 18 | this.init = init; 19 | } 20 | 21 | protected override void BuildCompletionDataInternal(IEditorData Editor, char enteredChar) 22 | { 23 | var ctxt = ResolutionContext.Create(Editor, true); 24 | var resolvedVariable = TypeDeclarationResolver.HandleNodeMatch(initedVar, ctxt) as DSymbol; 25 | 26 | if (resolvedVariable == null) 27 | return; 28 | 29 | while (resolvedVariable is TemplateParameterSymbol) 30 | resolvedVariable = resolvedVariable.Base as DSymbol; 31 | 32 | var structType = resolvedVariable.Base as TemplateIntermediateType; 33 | 34 | if (structType == null) 35 | return; 36 | 37 | var alreadyTakenNames = new List(); 38 | foreach (var m in init.MemberInitializers) 39 | alreadyTakenNames.Add(m.MemberNameHash); 40 | 41 | new StructVis(structType,alreadyTakenNames,CompletionDataGenerator,ctxt); 42 | } 43 | 44 | class StructVis : AbstractVisitor 45 | { 46 | readonly List alreadyTakenNames; 47 | readonly ICompletionDataGenerator gen; 48 | 49 | public StructVis(TemplateIntermediateType structType,List tkn,ICompletionDataGenerator gen,ResolutionContext ctxt) 50 | : base(ctxt) 51 | { 52 | this.alreadyTakenNames = tkn; 53 | this.gen = gen; 54 | 55 | if (ctxt.CompletionOptions.ShowStructMembersInStructInitOnly) 56 | this.DeepScanClass(structType, new ItemCheckParameters(MemberFilter.Variables), false); 57 | else 58 | IterateThroughScopeLayers(CodeLocation.Empty, MemberFilter.All); 59 | } 60 | 61 | protected override bool PreCheckItem (INode n) => !alreadyTakenNames.Contains (n.NameHash); 62 | 63 | protected override void HandleItem(INode n, AbstractType resolvedCurrentScope) => gen.Add(n); 64 | protected override void HandleItem(PackageSymbol pack) { } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /DParser2/Completion/Providers/VariableNameSuggestionCompletionProvider.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using D_Parser.Resolver; 3 | using D_Parser.Resolver.TypeResolution; 4 | 5 | namespace D_Parser.Completion.Providers 6 | { 7 | public class VariableNameSuggestionCompletionProvider : AbstractCompletionProvider 8 | { 9 | private readonly DNode _node; 10 | 11 | public VariableNameSuggestionCompletionProvider(ICompletionDataGenerator completionDataGenerator, DNode node) 12 | : base(completionDataGenerator) 13 | { 14 | _node = node; 15 | } 16 | 17 | protected override void BuildCompletionDataInternal(IEditorData editor, char enteredChar) 18 | { 19 | var ctxt = ResolutionContext.Create(editor, true); 20 | var type = TypeDeclarationResolver.ResolveSingle(_node.Type, ctxt); 21 | while (type is TemplateParameterSymbol tps) 22 | type = tps.Base; 23 | if (type is TemplateIntermediateType tit && !string.IsNullOrEmpty(tit.Definition.Name)) 24 | { 25 | var name = tit.Definition.Name; 26 | var camelCasedName = char.ToLowerInvariant(name[0]) + name.Substring(1); 27 | CompletionDataGenerator.SetSuggestedItem(camelCasedName); 28 | CompletionDataGenerator.AddTextItem(camelCasedName, string.Empty); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /DParser2/DParser2.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42} 7 | Library 8 | D_Parser 9 | default 10 | netstandard2.0 11 | prompt 12 | 4 13 | True 14 | D_Parser 15 | $(Platform) 16 | full 17 | 18 | 19 | 20 | False 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | 24 | 25 | True 26 | bin\Release\ 27 | 28 | 29 | 30 | 31 | 38 | -------------------------------------------------------------------------------- /DParser2/DParser2.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DParser2", "DParser2.csproj", "{0290A229-9AA1-41C3-B525-CAFB86D8BC42}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /DParser2/Dom/CodeLocation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom 4 | { 5 | /// 6 | /// A line/column position. 7 | /// NRefactory lines/columns are counting from one. 8 | /// 9 | public struct CodeLocation : IComparable, IEquatable 10 | { 11 | public static readonly CodeLocation Empty = new CodeLocation(-1, -1); 12 | 13 | public readonly int Column, Line; 14 | 15 | public CodeLocation(int column, int line) 16 | { 17 | Column = column; 18 | Line = line; 19 | } 20 | 21 | public bool IsEmpty 22 | { 23 | get 24 | { 25 | return Line + Column < 1; 26 | } 27 | } 28 | 29 | public override string ToString() 30 | { 31 | return string.Format("(Line {1}, Col {0})", Column, Line); 32 | } 33 | 34 | public override int GetHashCode() 35 | { 36 | return unchecked(87 * Column.GetHashCode() ^ Line.GetHashCode()); 37 | } 38 | 39 | public override bool Equals(object obj) 40 | { 41 | if (!(obj is CodeLocation)) return false; 42 | return (CodeLocation)obj == this; 43 | } 44 | 45 | public bool Equals(CodeLocation other) 46 | { 47 | return this == other; 48 | } 49 | 50 | public static bool operator ==(CodeLocation a, CodeLocation b) 51 | { 52 | return a.Column == b.Column && a.Line == b.Line; 53 | } 54 | 55 | public static bool operator !=(CodeLocation a, CodeLocation b) 56 | { 57 | return a.Column != b.Column || a.Line != b.Line; 58 | } 59 | 60 | public static bool operator <(CodeLocation a, CodeLocation b) 61 | { 62 | if (a.Line < b.Line) 63 | return true; 64 | else if (a.Line == b.Line) 65 | return a.Column < b.Column; 66 | else 67 | return false; 68 | } 69 | 70 | public static bool operator >(CodeLocation a, CodeLocation b) 71 | { 72 | if (a.Line > b.Line) 73 | return true; 74 | else if (a.Line == b.Line) 75 | return a.Column > b.Column; 76 | else 77 | return false; 78 | } 79 | 80 | public static bool operator <=(CodeLocation a, CodeLocation b) 81 | { 82 | return !(a > b); 83 | } 84 | 85 | public static bool operator >=(CodeLocation a, CodeLocation b) 86 | { 87 | return !(a < b); 88 | } 89 | 90 | public int CompareTo(CodeLocation other) 91 | { 92 | if (this == other) 93 | return 0; 94 | if (this < other) 95 | return -1; 96 | else 97 | return 1; 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AbstractVariableInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public abstract class AbstractVariableInitializer : IVariableInitializer,IExpression 6 | { 7 | public const int AbstractInitializerHash = 1234; 8 | 9 | public CodeLocation Location 10 | { 11 | get; 12 | set; 13 | } 14 | 15 | public CodeLocation EndLocation 16 | { 17 | get; 18 | set; 19 | } 20 | 21 | public abstract void Accept(ExpressionVisitor vis); 22 | 23 | public abstract R Accept(ExpressionVisitor vis); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AddExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a + b; a - b; 8 | /// 9 | public class AddExpression : OperatorBasedExpression 10 | { 11 | public AddExpression(bool isMinus) 12 | { 13 | OperatorToken = isMinus ? DTokens.Minus : DTokens.Plus; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AndAndExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a && b; 8 | /// 9 | public class AndAndExpression : OperatorBasedExpression 10 | { 11 | public AndAndExpression() 12 | { 13 | OperatorToken = DTokens.LogicalAnd; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AndExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a & b; 8 | /// 9 | public class AndExpression : OperatorBasedExpression 10 | { 11 | public AndExpression() 12 | { 13 | OperatorToken = DTokens.BitwiseAnd; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AnonymousClassExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// NewArguments ClassArguments BaseClasslist { DeclDefs } 8 | /// new ParenArgumentList_opt class ParenArgumentList_opt SuperClass_opt InterfaceClasses_opt ClassBody 9 | /// 10 | public class AnonymousClassExpression : UnaryExpression, ContainerExpression 11 | { 12 | public IExpression[] NewArguments { get; set; } 13 | 14 | public DClassLike AnonymousClass { get; set; } 15 | 16 | public IExpression[] ClassArguments { get; set; } 17 | 18 | public override string ToString() 19 | { 20 | var ret = "new"; 21 | 22 | if (NewArguments != null) 23 | { 24 | ret += "("; 25 | foreach (var e in NewArguments) 26 | ret += e.ToString() + ","; 27 | ret = ret.TrimEnd(',') + ")"; 28 | } 29 | 30 | ret += " class"; 31 | 32 | if (ClassArguments != null) 33 | { 34 | ret += '('; 35 | foreach (var e in ClassArguments) 36 | ret += e.ToString() + ","; 37 | 38 | ret = ret.TrimEnd(',') + ")"; 39 | } 40 | 41 | if (AnonymousClass != null && AnonymousClass.BaseClasses != null) 42 | { 43 | ret += ":"; 44 | 45 | foreach (var t in AnonymousClass.BaseClasses) 46 | ret += t.ToString() + ","; 47 | 48 | ret = ret.TrimEnd(','); 49 | } 50 | 51 | ret += " {...}"; 52 | 53 | return ret; 54 | } 55 | 56 | public CodeLocation Location 57 | { 58 | get; 59 | set; 60 | } 61 | 62 | public CodeLocation EndLocation 63 | { 64 | get; 65 | set; 66 | } 67 | 68 | public IEnumerable SubExpressions 69 | { 70 | get 71 | { 72 | if (NewArguments != null) 73 | foreach (var arg in NewArguments) 74 | yield return arg; 75 | 76 | if (ClassArguments != null) 77 | foreach (var arg in ClassArguments) 78 | yield return arg; 79 | 80 | //ISSUE: Add the Anonymous class object to the return list somehow? 81 | } 82 | } 83 | 84 | public void Accept(ExpressionVisitor vis) 85 | { 86 | vis.Visit(this); 87 | } 88 | 89 | public R Accept(ExpressionVisitor vis) 90 | { 91 | return vis.Visit(this); 92 | } 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ArrayInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class ArrayInitializer : AssocArrayExpression,IVariableInitializer 6 | { 7 | public override void Accept(ExpressionVisitor vis) 8 | { 9 | vis.Visit(this); 10 | } 11 | 12 | public override R Accept(ExpressionVisitor vis) 13 | { 14 | return vis.Visit(this); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ArrayLiteralExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | using System.Collections.Generic; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | /// 8 | /// auto arr= [1,2,3,4,5,6]; 9 | /// 10 | public class ArrayLiteralExpression : PrimaryExpression, ContainerExpression 11 | { 12 | public readonly IEnumerable Elements; 13 | 14 | public ArrayLiteralExpression(IEnumerable elements) 15 | { 16 | Elements = elements; 17 | } 18 | 19 | public override string ToString() 20 | { 21 | var s = "["; 22 | 23 | //HACK: To prevent exessive string building flood, limit element count to 100 24 | int i = 0; 25 | foreach(var ex in Elements) 26 | { 27 | s += ex.ToString() + ", "; 28 | if (++i == 100) 29 | { 30 | s += "..."; 31 | break; 32 | } 33 | } 34 | s = s.TrimEnd(' ', ',') + "]"; 35 | return s; 36 | } 37 | 38 | public CodeLocation Location 39 | { 40 | get; 41 | set; 42 | } 43 | 44 | public CodeLocation EndLocation 45 | { 46 | get; 47 | set; 48 | } 49 | 50 | public IEnumerable SubExpressions 51 | { 52 | get { return Elements; } 53 | } 54 | 55 | public void Accept(ExpressionVisitor vis) 56 | { 57 | vis.Visit(this); 58 | } 59 | 60 | public R Accept(ExpressionVisitor vis) 61 | { 62 | return vis.Visit(this); 63 | } 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AssertExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class AssertExpression : PrimaryExpression, ContainerExpression 7 | { 8 | public List AssignExpressions; 9 | 10 | public override string ToString() 11 | { 12 | var ret = "assert("; 13 | 14 | foreach (var e in AssignExpressions) 15 | ret += e.ToString() + ","; 16 | 17 | return ret.TrimEnd(',') + ")"; 18 | } 19 | 20 | public CodeLocation Location 21 | { 22 | get; 23 | set; 24 | } 25 | 26 | public CodeLocation EndLocation 27 | { 28 | get; 29 | set; 30 | } 31 | 32 | public IEnumerable SubExpressions 33 | { 34 | get { return AssignExpressions; } 35 | } 36 | 37 | public void Accept(ExpressionVisitor vis) 38 | { 39 | vis.Visit(this); 40 | } 41 | 42 | public R Accept(ExpressionVisitor vis) 43 | { 44 | return vis.Visit(this); 45 | } 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AssignExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// a = b; 7 | /// a += b; 8 | /// a *= b; etc. 9 | /// 10 | public class AssignExpression : OperatorBasedExpression 11 | { 12 | public AssignExpression(byte opToken) 13 | { 14 | OperatorToken = opToken; 15 | } 16 | 17 | public override void Accept(ExpressionVisitor vis) 18 | { 19 | vis.Visit(this); 20 | } 21 | 22 | public override R Accept(ExpressionVisitor vis) 23 | { 24 | return vis.Visit(this); 25 | } 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/AssocArrayExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// auto arr=['a':0xa, 'b':0xb, 'c':0xc, 'd':0xd, 'e':0xe, 'f':0xf]; 8 | /// 9 | public class AssocArrayExpression : PrimaryExpression, ContainerExpression 10 | { 11 | public IList> Elements = new List>(); 12 | 13 | public override string ToString() 14 | { 15 | var s = "["; 16 | foreach (var expr in Elements) 17 | s += expr.Key.ToString() + ":" + expr.Value.ToString() + ", "; 18 | s = s.TrimEnd(' ', ',') + "]"; 19 | return s; 20 | } 21 | 22 | public CodeLocation Location 23 | { 24 | get; 25 | set; 26 | } 27 | 28 | public CodeLocation EndLocation 29 | { 30 | get; 31 | set; 32 | } 33 | 34 | public IEnumerable SubExpressions 35 | { 36 | get 37 | { 38 | foreach (var kv in Elements) 39 | { 40 | if (kv.Key != null) 41 | yield return kv.Key; 42 | if (kv.Value != null) 43 | yield return kv.Value; 44 | } 45 | } 46 | } 47 | 48 | public virtual void Accept(ExpressionVisitor vis) 49 | { 50 | vis.Visit(this); 51 | } 52 | 53 | public virtual R Accept(ExpressionVisitor vis) 54 | { 55 | return vis.Visit(this); 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/CastExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using D_Parser.Parser; 5 | 6 | namespace D_Parser.Dom.Expressions 7 | { 8 | /// 9 | /// CastExpression: 10 | /// cast ( Type ) UnaryExpression 11 | /// cast ( CastParam ) UnaryExpression 12 | /// 13 | public class CastExpression : UnaryExpression, ContainerExpression 14 | { 15 | public bool IsTypeCast 16 | { 17 | get { return Type != null; } 18 | } 19 | 20 | public IExpression UnaryExpression; 21 | 22 | public ITypeDeclaration Type { get; set; } 23 | 24 | public byte[] CastParamTokens { get; set; } 25 | 26 | public override string ToString() 27 | { 28 | var ret = new StringBuilder("cast("); 29 | 30 | if (IsTypeCast) 31 | ret.Append(Type.ToString()); 32 | else if (CastParamTokens != null && CastParamTokens.Length != 0) 33 | { 34 | foreach (var tk in CastParamTokens) 35 | ret.Append(DTokens.GetTokenString(tk)).Append(' '); 36 | ret.Remove(ret.Length - 1, 1); 37 | } 38 | 39 | ret.Append(')'); 40 | 41 | if (UnaryExpression != null) 42 | ret.Append(' ').Append(UnaryExpression.ToString()); 43 | 44 | return ret.ToString(); 45 | } 46 | 47 | public CodeLocation Location 48 | { 49 | get; 50 | set; 51 | } 52 | 53 | public CodeLocation EndLocation 54 | { 55 | get; 56 | set; 57 | } 58 | 59 | public IEnumerable SubExpressions 60 | { 61 | get { yield return UnaryExpression; } 62 | } 63 | 64 | public void Accept(ExpressionVisitor vis) 65 | { 66 | vis.Visit(this); 67 | } 68 | 69 | public R Accept(ExpressionVisitor vis) 70 | { 71 | return vis.Visit(this); 72 | } 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/CatExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a ~ b 8 | /// 9 | public class CatExpression : OperatorBasedExpression 10 | { 11 | public CatExpression() 12 | { 13 | OperatorToken = DTokens.Tilde; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ConditionalExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a ? b : b; 8 | /// 9 | public class ConditionalExpression : IExpression, ContainerExpression 10 | { 11 | public IExpression OrOrExpression { get; set; } 12 | 13 | public IExpression TrueCaseExpression { get; set; } 14 | 15 | public IExpression FalseCaseExpression { get; set; } 16 | 17 | public override string ToString() 18 | { 19 | return (OrOrExpression != null ? OrOrExpression.ToString() : "") + "?" + 20 | (TrueCaseExpression != null ? TrueCaseExpression.ToString() : "") + ':' + 21 | (FalseCaseExpression != null ? FalseCaseExpression.ToString() : ""); 22 | } 23 | 24 | public CodeLocation Location 25 | { 26 | get { return OrOrExpression.Location; } 27 | } 28 | 29 | public CodeLocation EndLocation 30 | { 31 | get { return (FalseCaseExpression ?? TrueCaseExpression ?? OrOrExpression).EndLocation; } 32 | } 33 | 34 | public IEnumerable SubExpressions 35 | { 36 | get { 37 | if (OrOrExpression != null) 38 | yield return OrOrExpression; 39 | if (TrueCaseExpression != null) 40 | yield return TrueCaseExpression; 41 | if (FalseCaseExpression != null) 42 | yield return FalseCaseExpression; 43 | } 44 | } 45 | 46 | public void Accept(ExpressionVisitor vis) 47 | { 48 | vis.Visit(this); 49 | } 50 | 51 | public R Accept(ExpressionVisitor vis) 52 | { 53 | return vis.Visit(this); 54 | } 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ContainerExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// Expressions that contain other sub-expressions somewhere share this interface 8 | /// 9 | public interface ContainerExpression : IExpression 10 | { 11 | IEnumerable SubExpressions { get; } 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/DeleteExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class DeleteExpression : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Delete; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/EqualExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a == b; a != b; 8 | /// 9 | public class EqualExpression : OperatorBasedExpression 10 | { 11 | public EqualExpression(bool isUnEqual) 12 | { 13 | OperatorToken = isUnEqual ? DTokens.NotEqual : DTokens.Equal; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/Expression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class Expression : IExpression, IEnumerable, ContainerExpression 7 | { 8 | public readonly List Expressions = new List(); 9 | 10 | public void Add(IExpression ex) 11 | { 12 | Expressions.Add(ex); 13 | } 14 | 15 | public IEnumerator GetEnumerator() 16 | { 17 | return Expressions.GetEnumerator(); 18 | } 19 | 20 | public override string ToString() 21 | { 22 | var s = ""; 23 | if (Expressions != null) 24 | foreach (var ex in Expressions) 25 | s += (ex == null ? string.Empty : ex.ToString()) + ","; 26 | return s.TrimEnd(','); 27 | } 28 | 29 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 30 | { 31 | return Expressions.GetEnumerator(); 32 | } 33 | 34 | public CodeLocation Location 35 | { 36 | get { return Expressions.Count > 0 ? Expressions[0].Location : CodeLocation.Empty; } 37 | } 38 | 39 | public CodeLocation EndLocation 40 | { 41 | get { return Expressions.Count > 0 ? Expressions[Expressions.Count - 1].EndLocation : CodeLocation.Empty; } 42 | } 43 | 44 | public IEnumerable SubExpressions 45 | { 46 | get { return Expressions; } 47 | } 48 | 49 | public void Accept(ExpressionVisitor vis) 50 | { 51 | vis.Visit(this); 52 | } 53 | 54 | public R Accept(ExpressionVisitor vis) 55 | { 56 | return vis.Visit(this); 57 | } 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/FunctionLiteral.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using D_Parser.Parser; 4 | using D_Parser.Dom.Statements; 5 | 6 | namespace D_Parser.Dom.Expressions 7 | { 8 | public class FunctionLiteral : PrimaryExpression 9 | { 10 | public byte LiteralToken = DTokens.Delegate; 11 | public readonly bool IsLambda; 12 | public readonly DMethod AnonymousMethod = new DMethod(DMethod.MethodType.AnonymousDelegate); 13 | 14 | public FunctionLiteral(bool lambda = false) 15 | { 16 | IsLambda = lambda; 17 | if (lambda) 18 | AnonymousMethod.SpecialType |= DMethod.MethodType.Lambda; 19 | } 20 | 21 | public FunctionLiteral(byte InitialLiteral) 22 | { 23 | LiteralToken = InitialLiteral; 24 | } 25 | 26 | public override string ToString() 27 | { 28 | if (IsLambda) 29 | { 30 | var sb = new StringBuilder(); 31 | 32 | if (AnonymousMethod.Parameters.Count == 1 && AnonymousMethod.Parameters[0].Type == null) 33 | sb.Append(AnonymousMethod.Parameters[0].Name); 34 | else 35 | { 36 | sb.Append('('); 37 | foreach (INode param in AnonymousMethod.Parameters) 38 | { 39 | sb.Append(param).Append(','); 40 | } 41 | 42 | if (AnonymousMethod.Parameters.Count > 0) 43 | sb.Remove(sb.Length - 1, 1); 44 | sb.Append(')'); 45 | } 46 | 47 | sb.Append(" => "); 48 | 49 | if (AnonymousMethod.Body != null) 50 | { 51 | var en = AnonymousMethod.Body.SubStatements.GetEnumerator(); 52 | if (en.MoveNext() && en.Current is ReturnStatement) 53 | sb.Append((en.Current as ReturnStatement).ReturnExpression.ToString()); 54 | else 55 | sb.Append(AnonymousMethod.Body.ToCode()); 56 | en.Dispose(); 57 | } 58 | else 59 | sb.Append("{}"); 60 | 61 | return sb.ToString(); 62 | } 63 | 64 | return DTokens.GetTokenString(LiteralToken) + (AnonymousMethod.NameHash == 0 ? "" : " ") + AnonymousMethod.ToString(); 65 | } 66 | 67 | public CodeLocation Location 68 | { 69 | get; 70 | set; 71 | } 72 | 73 | public CodeLocation EndLocation 74 | { 75 | get; 76 | set; 77 | } 78 | 79 | public void Accept(ExpressionVisitor vis) 80 | { 81 | vis.Visit(this); 82 | } 83 | 84 | public R Accept(ExpressionVisitor vis) 85 | { 86 | return vis.Visit(this); 87 | } 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/IExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public interface IExpression : ISyntaxRegion, IVisitable 6 | { 7 | R Accept(ExpressionVisitor vis); 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/IVariableInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public interface IVariableInitializer 6 | { 7 | 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/IdentifierExpression.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace D_Parser.Dom.Expressions 3 | { 4 | public interface IntermediateIdType : ISyntaxRegion 5 | { 6 | bool ModuleScoped { get; set; } 7 | int IdHash{get;} 8 | } 9 | 10 | public class IdentifierExpression : PrimaryExpression, IntermediateIdType 11 | { 12 | public bool ModuleScoped { 13 | get; 14 | set; 15 | } 16 | 17 | public int IdHash { 18 | get; 19 | private set; 20 | } 21 | 22 | public string StringValue => Strings.TryGet(IdHash); 23 | 24 | public IdentifierExpression(string id) 25 | { 26 | Strings.Add(id); 27 | IdHash = id.GetHashCode(); 28 | } 29 | 30 | public override string ToString() 31 | { 32 | return (ModuleScoped ? "." : "") + StringValue; 33 | } 34 | 35 | public CodeLocation Location 36 | { 37 | get; 38 | set; 39 | } 40 | 41 | public CodeLocation EndLocation 42 | { 43 | get; 44 | set; 45 | } 46 | 47 | public void Accept(ExpressionVisitor vis) 48 | { 49 | vis.Visit(this); 50 | } 51 | 52 | public R Accept(ExpressionVisitor vis) 53 | { 54 | return vis.Visit(this); 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/IdentityExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a is b; a !is b; 8 | /// 9 | public class IdentityExpression : OperatorBasedExpression 10 | { 11 | public bool Not; 12 | public readonly int opColumn, opLine; 13 | 14 | public IdentityExpression(bool notIs, CodeLocation oploc) 15 | { 16 | Not = notIs; 17 | opColumn = oploc.Column; 18 | opLine = oploc.Line; 19 | OperatorToken = DTokens.Is; 20 | } 21 | 22 | public override string ToString() 23 | { 24 | return LeftOperand.ToString() + (Not ? " !" : " ") + "is " + RightOperand.ToString(); 25 | } 26 | 27 | public override void Accept(ExpressionVisitor vis) 28 | { 29 | vis.Visit(this); 30 | } 31 | 32 | public override R Accept(ExpressionVisitor vis) 33 | { 34 | return vis.Visit(this); 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ImportExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | public class ImportExpression : PrimaryExpression,ContainerExpression 8 | { 9 | public IExpression AssignExpression; 10 | 11 | public override string ToString() 12 | { 13 | return "import(" + AssignExpression.ToString() + ")"; 14 | } 15 | 16 | public CodeLocation Location 17 | { 18 | get; 19 | set; 20 | } 21 | 22 | public CodeLocation EndLocation 23 | { 24 | get; 25 | set; 26 | } 27 | 28 | public IEnumerable SubExpressions 29 | { 30 | get { yield return AssignExpression; } 31 | } 32 | 33 | public void Accept(ExpressionVisitor vis) 34 | { 35 | vis.Visit(this); 36 | } 37 | 38 | public R Accept(ExpressionVisitor vis) 39 | { 40 | return vis.Visit(this); 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/InExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a in b; a !in b 8 | /// 9 | public class InExpression : OperatorBasedExpression 10 | { 11 | public bool Not; 12 | public readonly int opColumn, opLine; 13 | 14 | public InExpression(bool notIn, CodeLocation oploc) 15 | { 16 | Not = notIn; 17 | opColumn = oploc.Column; 18 | opLine = oploc.Line; 19 | OperatorToken = DTokens.In; 20 | } 21 | 22 | public override string ToString() 23 | { 24 | return LeftOperand.ToString() + (Not ? " !" : " ") + "in " + RightOperand.ToString(); 25 | } 26 | 27 | public override void Accept(ExpressionVisitor vis) 28 | { 29 | vis.Visit(this); 30 | } 31 | 32 | public override R Accept(ExpressionVisitor vis) 33 | { 34 | return vis.Visit(this); 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/MixinExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | public class MixinExpression : PrimaryExpression, ContainerExpression 8 | { 9 | public IExpression AssignExpression; 10 | 11 | public override string ToString() 12 | { 13 | return "mixin(" + AssignExpression.ToString() + ")"; 14 | } 15 | 16 | public CodeLocation Location 17 | { 18 | get; 19 | set; 20 | } 21 | 22 | public CodeLocation EndLocation 23 | { 24 | get; 25 | set; 26 | } 27 | 28 | public IEnumerable SubExpressions 29 | { 30 | get { yield return AssignExpression; } 31 | } 32 | 33 | public void Accept(ExpressionVisitor vis) 34 | { 35 | vis.Visit(this); 36 | } 37 | 38 | public R Accept(ExpressionVisitor vis) 39 | { 40 | return vis.Visit(this); 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/MulExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// a * b; a / b; a % b; 7 | /// 8 | public class MulExpression : OperatorBasedExpression 9 | { 10 | public MulExpression(byte mulOperator) 11 | { 12 | OperatorToken = mulOperator; 13 | } 14 | 15 | public override void Accept(ExpressionVisitor vis) 16 | { 17 | vis.Visit(this); 18 | } 19 | 20 | public override R Accept(ExpressionVisitor vis) 21 | { 22 | return vis.Visit(this); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/NewExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// NewExpression: 8 | /// NewArguments Type [ AssignExpression ] 9 | /// NewArguments Type ( ArgumentList ) 10 | /// NewArguments Type 11 | /// 12 | public class NewExpression : UnaryExpression, ContainerExpression 13 | { 14 | public ITypeDeclaration Type { get; set; } 15 | 16 | public IExpression[] NewArguments { get; set; } 17 | 18 | public IExpression[] Arguments { get; set; } 19 | 20 | public override string ToString() 21 | { 22 | var ret = "new"; 23 | 24 | if (NewArguments != null) 25 | { 26 | ret += "("; 27 | foreach (var e in NewArguments) 28 | ret += e.ToString() + ","; 29 | ret = ret.TrimEnd(',') + ")"; 30 | } 31 | 32 | if (Type != null) 33 | ret += " " + Type.ToString(); 34 | 35 | if (!(Type is ArrayDecl)) 36 | { 37 | ret += '('; 38 | if (Arguments != null) 39 | foreach (var e in Arguments) 40 | ret += e.ToString() + ","; 41 | 42 | ret = ret.TrimEnd(',') + ')'; 43 | } 44 | 45 | return ret; 46 | } 47 | 48 | public CodeLocation Location 49 | { 50 | get; 51 | set; 52 | } 53 | 54 | public CodeLocation EndLocation 55 | { 56 | get; 57 | set; 58 | } 59 | 60 | public IEnumerable SubExpressions 61 | { 62 | get 63 | { 64 | // In case of a template instance 65 | if (Type is IExpression) 66 | yield return Type as IExpression; 67 | 68 | if (NewArguments != null) 69 | foreach (var arg in NewArguments) 70 | yield return arg; 71 | 72 | if (Arguments != null) 73 | foreach (var arg in Arguments) 74 | yield return arg; 75 | } 76 | } 77 | 78 | public void Accept(ExpressionVisitor vis) 79 | { 80 | vis.Visit(this); 81 | } 82 | 83 | public R Accept(ExpressionVisitor vis) 84 | { 85 | return vis.Visit(this); 86 | } 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/OperatorBasedExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public abstract class OperatorBasedExpression : ContainerExpression 7 | { 8 | public IExpression LeftOperand { get; set; } 9 | 10 | public IExpression RightOperand { get; set; } 11 | 12 | public byte OperatorToken { get; protected set; } 13 | 14 | public override string ToString() 15 | { 16 | return LeftOperand + DTokens.GetTokenString(OperatorToken) + (RightOperand != null ? RightOperand.ToString() : ""); 17 | } 18 | 19 | public CodeLocation Location => LeftOperand?.Location ?? CodeLocation.Empty; 20 | 21 | public CodeLocation EndLocation => RightOperand?.EndLocation ?? CodeLocation.Empty; 22 | 23 | public IEnumerable SubExpressions 24 | { 25 | get { 26 | if (LeftOperand != null) yield return LeftOperand; 27 | if (RightOperand != null) yield return RightOperand; 28 | } 29 | } 30 | 31 | public abstract void Accept(ExpressionVisitor v); 32 | 33 | public abstract R Accept(ExpressionVisitor v); 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/OrExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a | b; 8 | /// 9 | public class OrExpression : OperatorBasedExpression 10 | { 11 | public OrExpression() 12 | { 13 | OperatorToken = DTokens.BitwiseOr; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/OrOrExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a || b; 8 | /// 9 | public class OrOrExpression : OperatorBasedExpression 10 | { 11 | public OrOrExpression() 12 | { 13 | OperatorToken = DTokens.LogicalOr; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PostfixExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public abstract class PostfixExpression : IExpression, ContainerExpression 7 | { 8 | public IExpression PostfixForeExpression { get; set; } 9 | 10 | public CodeLocation Location 11 | { 12 | get { return PostfixForeExpression != null ? PostfixForeExpression.Location : CodeLocation.Empty; } 13 | } 14 | 15 | public abstract CodeLocation EndLocation { get; set; } 16 | 17 | public virtual IEnumerable SubExpressions 18 | { 19 | get { yield return PostfixForeExpression; } 20 | } 21 | 22 | public abstract void Accept(ExpressionVisitor vis); 23 | 24 | public abstract R Accept(ExpressionVisitor vis); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PostfixExpression_Access.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// PostfixExpression . Identifier 8 | /// PostfixExpression . TemplateInstance 9 | /// PostfixExpression . NewExpression 10 | /// 11 | public class PostfixExpression_Access : PostfixExpression 12 | { 13 | /// 14 | /// Can be either 15 | /// 1) An Identifier 16 | /// 2) A Template Instance 17 | /// 3) A NewExpression 18 | /// 19 | public IExpression AccessExpression; 20 | 21 | public override string ToString() 22 | { 23 | var r = PostfixForeExpression.ToString() + '.'; 24 | 25 | if (AccessExpression != null) 26 | r += AccessExpression.ToString(); 27 | 28 | return r; 29 | } 30 | 31 | public override CodeLocation EndLocation 32 | { 33 | get; 34 | set; 35 | } 36 | 37 | public override IEnumerable SubExpressions 38 | { 39 | get 40 | { 41 | yield return PostfixForeExpression; 42 | yield return AccessExpression; 43 | } 44 | } 45 | 46 | public override void Accept(ExpressionVisitor vis) 47 | { 48 | vis.Visit(this); 49 | } 50 | 51 | public override R Accept(ExpressionVisitor vis) 52 | { 53 | return vis.Visit(this); 54 | } 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PostfixExpression_Decrement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class PostfixExpression_Decrement : PostfixExpression 6 | { 7 | public override string ToString() 8 | { 9 | return PostfixForeExpression.ToString() + "--"; 10 | } 11 | 12 | public sealed override CodeLocation EndLocation 13 | { 14 | get; 15 | set; 16 | } 17 | 18 | public override void Accept(ExpressionVisitor vis) 19 | { 20 | vis.Visit(this); 21 | } 22 | 23 | public override R Accept(ExpressionVisitor vis) 24 | { 25 | return vis.Visit(this); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PostfixExpression_Increment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class PostfixExpression_Increment : PostfixExpression 6 | { 7 | public override string ToString() 8 | { 9 | return PostfixForeExpression.ToString() + "++"; 10 | } 11 | 12 | public sealed override CodeLocation EndLocation 13 | { 14 | get; 15 | set; 16 | } 17 | 18 | public override void Accept(ExpressionVisitor vis) 19 | { 20 | vis.Visit(this); 21 | } 22 | 23 | public override R Accept(ExpressionVisitor vis) 24 | { 25 | return vis.Visit(this); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PostfixExpression_MethodCall.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// PostfixExpression ( ) 8 | /// PostfixExpression ( ArgumentList ) 9 | /// 10 | public class PostfixExpression_MethodCall : PostfixExpression 11 | { 12 | public IExpression[] Arguments; 13 | 14 | public int ArgumentCount => Arguments?.Length ?? 0; 15 | 16 | public override string ToString() 17 | { 18 | var sb = new StringBuilder(PostfixForeExpression.ToString()); 19 | sb.Append('('); 20 | 21 | if (Arguments != null) 22 | foreach (var a in Arguments) 23 | if (a != null) 24 | sb.Append(a.ToString()).Append(','); 25 | 26 | if (sb[sb.Length - 1] == ',') 27 | sb.Remove(sb.Length - 1, 1); 28 | 29 | return sb.Append(')').ToString(); 30 | } 31 | 32 | public sealed override CodeLocation EndLocation 33 | { 34 | get; 35 | set; 36 | } 37 | 38 | public override IEnumerable SubExpressions 39 | { 40 | get 41 | { 42 | if (Arguments != null) 43 | foreach (var arg in Arguments) 44 | yield return arg; 45 | 46 | if (PostfixForeExpression != null) 47 | yield return PostfixForeExpression; 48 | } 49 | } 50 | 51 | public override void Accept(ExpressionVisitor vis) 52 | { 53 | vis.Visit(this); 54 | } 55 | 56 | public override R Accept(ExpressionVisitor vis) 57 | { 58 | return vis.VisitPostfixExpression_Methodcall(this); 59 | } 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PowExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class PowExpression : OperatorBasedExpression, UnaryExpression 7 | { 8 | public PowExpression() 9 | { 10 | OperatorToken = DTokens.Pow; 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/PrimaryExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public interface PrimaryExpression : IExpression 6 | { 7 | 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/RelExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// a <>= b etc. 7 | /// 8 | public class RelExpression : OperatorBasedExpression 9 | { 10 | public RelExpression(byte relationalOperator) 11 | { 12 | OperatorToken = relationalOperator; 13 | } 14 | 15 | public override void Accept(ExpressionVisitor vis) 16 | { 17 | vis.Visit(this); 18 | } 19 | 20 | public override R Accept(ExpressionVisitor vis) 21 | { 22 | return vis.Visit(this); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ScalarConstantExpression.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Parser; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class ScalarConstantExpression : PrimaryExpression 6 | { 7 | public readonly object Value; 8 | public readonly LiteralFormat Format; 9 | public readonly LiteralSubformat Subformat; 10 | public readonly int EscapeStringHash; 11 | 12 | public ScalarConstantExpression(object value, 13 | LiteralFormat literalFormat, 14 | LiteralSubformat subformat = LiteralSubformat.None, 15 | string escapeString = null) 16 | { 17 | Value = value; 18 | Format = literalFormat; 19 | Subformat = subformat; 20 | 21 | if (escapeString != null) 22 | { 23 | Strings.Add(escapeString); 24 | EscapeStringHash = escapeString.GetHashCode(); 25 | } 26 | } 27 | 28 | public override string ToString() 29 | { 30 | if (Format == LiteralFormat.CharLiteral) 31 | return "'" + (EscapeStringHash != 0 ? ("\\" + Strings.TryGet(EscapeStringHash)) : ((char)Value).ToString()) + "'"; 32 | 33 | if(Value is decimal) 34 | return ((decimal)Value).ToString(System.Globalization.CultureInfo.InvariantCulture); 35 | 36 | return Value != null ? Value.ToString() : ""; 37 | } 38 | 39 | public CodeLocation Location 40 | { 41 | get; 42 | set; 43 | } 44 | 45 | public CodeLocation EndLocation 46 | { 47 | get; 48 | set; 49 | } 50 | 51 | public void Accept(ExpressionVisitor vis) 52 | { 53 | vis.VisitScalarConstantExpression(this); 54 | } 55 | 56 | public R Accept(ExpressionVisitor vis) 57 | { 58 | return vis.VisitScalarConstantExpression(this); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/ShiftExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// a >> b; a << b; a >>> b; 7 | /// 8 | public class ShiftExpression : OperatorBasedExpression 9 | { 10 | public ShiftExpression(byte shiftOperator) 11 | { 12 | OperatorToken = shiftOperator; 13 | } 14 | 15 | public override void Accept(ExpressionVisitor vis) 16 | { 17 | vis.Visit(this); 18 | } 19 | 20 | public override R Accept(ExpressionVisitor vis) 21 | { 22 | return vis.Visit(this); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/SimpleUnaryExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public abstract class SimpleUnaryExpression : UnaryExpression, ContainerExpression 7 | { 8 | public abstract byte ForeToken { get; } 9 | 10 | public IExpression UnaryExpression { get; set; } 11 | 12 | public override string ToString() 13 | { 14 | return DTokens.GetTokenString(ForeToken) + UnaryExpression.ToString(); 15 | } 16 | 17 | public virtual CodeLocation Location 18 | { 19 | get; 20 | set; 21 | } 22 | 23 | public CodeLocation EndLocation 24 | { 25 | get { return UnaryExpression.EndLocation; } 26 | } 27 | 28 | public virtual IEnumerable SubExpressions 29 | { 30 | get { yield return UnaryExpression; } 31 | } 32 | 33 | public abstract void Accept(ExpressionVisitor vis); 34 | 35 | public abstract R Accept(ExpressionVisitor vis); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/StringLiteralExpression.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Parser; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class StringLiteralExpression : PrimaryExpression 6 | { 7 | public readonly int ValueStringHash; 8 | public readonly LiteralFormat Format; 9 | public readonly LiteralSubformat Subformat; 10 | 11 | public string Value { get { return Strings.TryGet(ValueStringHash); } } 12 | 13 | public StringLiteralExpression(string stringValue, bool isVerbatim = false, LiteralSubformat Subformat = 0) 14 | { 15 | Strings.Add(stringValue); 16 | ValueStringHash = stringValue.GetHashCode(); 17 | Format = isVerbatim ? LiteralFormat.VerbatimStringLiteral : LiteralFormat.StringLiteral; 18 | this.Subformat = Subformat; 19 | } 20 | 21 | public override string ToString() 22 | { 23 | switch (Format) 24 | { 25 | default: 26 | return "\"" + Value + "\""; 27 | case LiteralFormat.VerbatimStringLiteral: 28 | return "r\"" + Value + "\""; 29 | } 30 | } 31 | 32 | public CodeLocation Location 33 | { 34 | get; 35 | set; 36 | } 37 | 38 | public CodeLocation EndLocation 39 | { 40 | get; 41 | set; 42 | } 43 | 44 | public void Accept(ExpressionVisitor vis) 45 | { 46 | vis.VisitStringLiteralExpression(this); 47 | } 48 | 49 | public R Accept(ExpressionVisitor vis) 50 | { 51 | return vis.VisitStringLiteralExpression(this); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/StructInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | using System.Collections.Generic; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | public class StructInitializer : AbstractVariableInitializer, ContainerExpression 8 | { 9 | public StructMemberInitializer[] MemberInitializers; 10 | 11 | public sealed override string ToString() 12 | { 13 | var ret = "{"; 14 | 15 | if (MemberInitializers != null) 16 | foreach (var i in MemberInitializers) 17 | ret += i.ToString() + ","; 18 | 19 | return ret.TrimEnd(',') + "}"; 20 | } 21 | 22 | public IEnumerable SubExpressions 23 | { 24 | get 25 | { 26 | if (MemberInitializers != null) 27 | foreach (var mi in MemberInitializers) 28 | if (mi.Value != null) 29 | yield return mi.Value; 30 | } 31 | } 32 | 33 | public override void Accept(ExpressionVisitor vis) 34 | { 35 | vis.Visit(this); 36 | } 37 | 38 | public override R Accept(ExpressionVisitor vis) 39 | { 40 | return vis.Visit(this); 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/StructMemberInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class StructMemberInitializer : ISyntaxRegion, IVisitable 6 | { 7 | public string MemberName { 8 | get { return Strings.TryGet(MemberNameHash); } 9 | set { 10 | Strings.Add(value); 11 | MemberNameHash = value.GetHashCode(); 12 | } 13 | } 14 | public int MemberNameHash = 0; 15 | public IExpression Value; 16 | 17 | public sealed override string ToString() 18 | { 19 | return (MemberNameHash != 0 ? (MemberName + ": ") : "") + (Value != null ? Value.ToString() : ""); 20 | } 21 | 22 | public CodeLocation Location 23 | { 24 | get; 25 | set; 26 | } 27 | 28 | public CodeLocation EndLocation 29 | { 30 | get; 31 | set; 32 | } 33 | 34 | public void Accept(ExpressionVisitor vis) 35 | { 36 | vis.Visit(this); 37 | } 38 | 39 | public R Accept(ExpressionVisitor vis) 40 | { 41 | return vis.Visit(this); 42 | } 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/SurroundingParenthesesExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | /// 8 | /// ( Expression ) 9 | /// 10 | public class SurroundingParenthesesExpression : PrimaryExpression,ContainerExpression 11 | { 12 | public IExpression Expression; 13 | 14 | public override string ToString() 15 | { 16 | return "(" + (Expression != null ? Expression.ToString() : string.Empty) + ")"; 17 | } 18 | 19 | public CodeLocation Location 20 | { 21 | get; 22 | set; 23 | } 24 | 25 | public CodeLocation EndLocation 26 | { 27 | get; 28 | set; 29 | } 30 | 31 | public IEnumerable SubExpressions 32 | { 33 | get { yield return Expression; } 34 | } 35 | 36 | public void Accept(ExpressionVisitor vis) 37 | { 38 | vis.Visit(this); 39 | } 40 | 41 | public R Accept(ExpressionVisitor vis) 42 | { 43 | return vis.Visit(this); 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/TokenExpression.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Parser; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class TokenExpression : PrimaryExpression 6 | { 7 | public readonly byte Token; 8 | 9 | public TokenExpression(byte token) 10 | { 11 | Token = token; 12 | } 13 | 14 | public TokenExpression(byte token, CodeLocation startLocation, CodeLocation endLocation) : this(token) 15 | { 16 | Location = startLocation; 17 | EndLocation = endLocation; 18 | } 19 | 20 | public override string ToString() 21 | { 22 | return DTokens.GetTokenString(Token); 23 | } 24 | 25 | public CodeLocation Location 26 | { 27 | get; 28 | } 29 | 30 | public CodeLocation EndLocation 31 | { 32 | get; 33 | } 34 | 35 | public void Accept(ExpressionVisitor vis) 36 | { 37 | vis.Visit(this); 38 | } 39 | 40 | public R Accept(ExpressionVisitor vis) 41 | { 42 | return vis.Visit(this); 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/TraitsArgument.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public class TraitsArgument : ISyntaxRegion 6 | { 7 | public readonly ITypeDeclaration Type; 8 | public readonly IExpression AssignExpression; 9 | 10 | public TraitsArgument(ITypeDeclaration t) 11 | { 12 | this.Type = t; 13 | } 14 | 15 | public TraitsArgument(IExpression x) 16 | { 17 | this.AssignExpression = x; 18 | } 19 | 20 | public override string ToString() 21 | { 22 | return Type != null ? Type.ToString(true) : AssignExpression.ToString(); 23 | } 24 | 25 | public CodeLocation Location 26 | { 27 | get; 28 | set; 29 | } 30 | 31 | public CodeLocation EndLocation 32 | { 33 | get; 34 | set; 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/TraitsExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | public class TraitsExpression : PrimaryExpression, ContainerExpression 8 | { 9 | public string Keyword; 10 | public TraitsArgument[] Arguments; 11 | 12 | public override string ToString() 13 | { 14 | var ret = "__traits(" + Keyword; 15 | 16 | if (Arguments != null) 17 | foreach (var a in Arguments) 18 | ret += "," + a.ToString(); 19 | 20 | return ret + ")"; 21 | } 22 | 23 | public CodeLocation Location 24 | { 25 | get; 26 | set; 27 | } 28 | 29 | public CodeLocation EndLocation 30 | { 31 | get; 32 | set; 33 | } 34 | 35 | public void Accept(ExpressionVisitor vis) 36 | { 37 | vis.Visit(this); 38 | } 39 | 40 | public R Accept(ExpressionVisitor vis) 41 | { 42 | return vis.Visit(this); 43 | } 44 | 45 | public IEnumerable SubExpressions 46 | { 47 | get 48 | { 49 | if (Arguments != null) 50 | foreach (var arg in Arguments) 51 | if (arg.AssignExpression != null) 52 | yield return arg.AssignExpression; 53 | } 54 | } 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/TypeDeclarationExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// BasicType . Identifier 7 | /// 8 | public class TypeDeclarationExpression : PrimaryExpression 9 | { 10 | public readonly ITypeDeclaration Declaration; 11 | 12 | public static IExpression TryWrap(ISyntaxRegion expressionOrDeclaration) 13 | { 14 | return expressionOrDeclaration != null ? expressionOrDeclaration as IExpression ?? 15 | new TypeDeclarationExpression(expressionOrDeclaration as ITypeDeclaration) : null; 16 | } 17 | 18 | TypeDeclarationExpression(ITypeDeclaration td) 19 | { 20 | Declaration = td; 21 | } 22 | 23 | public override string ToString() 24 | { 25 | return Declaration.ToString(); 26 | } 27 | 28 | public CodeLocation Location 29 | { 30 | get { return Declaration.Location; } 31 | } 32 | 33 | public CodeLocation EndLocation 34 | { 35 | get { return Declaration.EndLocation; } 36 | } 37 | 38 | public void Accept(ExpressionVisitor vis) 39 | { 40 | vis.Visit(this); 41 | } 42 | 43 | public R Accept(ExpressionVisitor vis) 44 | { 45 | return vis.Visit(this); 46 | } 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/TypeidExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Dom.Expressions 6 | { 7 | public class TypeidExpression : PrimaryExpression,ContainerExpression 8 | { 9 | public ITypeDeclaration Type; 10 | public IExpression Expression; 11 | 12 | public override string ToString() 13 | { 14 | return "typeid(" + (Type != null ? Type.ToString() : Expression.ToString()) + ")"; 15 | } 16 | 17 | public CodeLocation Location 18 | { 19 | get; 20 | set; 21 | } 22 | 23 | public CodeLocation EndLocation 24 | { 25 | get; 26 | set; 27 | } 28 | 29 | public IEnumerable SubExpressions 30 | { 31 | get 32 | { 33 | if (Expression != null) 34 | yield return Expression; 35 | else if (Type != null) 36 | yield return TypeDeclarationExpression.TryWrap(Type); 37 | } 38 | } 39 | 40 | public void Accept(ExpressionVisitor vis) 41 | { 42 | vis.Visit(this); 43 | } 44 | 45 | public R Accept(ExpressionVisitor vis) 46 | { 47 | return vis.Visit(this); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | public interface UnaryExpression : IExpression 6 | { 7 | 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Add.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class UnaryExpression_Add : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Plus; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_And.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// Creates a pointer from the trailing type 8 | /// 9 | public class UnaryExpression_And : SimpleUnaryExpression 10 | { 11 | public override byte ForeToken 12 | { 13 | get { return DTokens.BitwiseAnd; } 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Cat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// Bitwise negation operation: 8 | /// 9 | /// int a=56; 10 | /// int b=~a; 11 | /// 12 | /// b will be -57; 13 | /// 14 | public class UnaryExpression_Cat : SimpleUnaryExpression 15 | { 16 | public override byte ForeToken 17 | { 18 | get { return DTokens.Tilde; } 19 | } 20 | 21 | public override void Accept(ExpressionVisitor vis) 22 | { 23 | vis.Visit(this); 24 | } 25 | 26 | public override R Accept(ExpressionVisitor vis) 27 | { 28 | return vis.Visit(this); 29 | } 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Decrement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class UnaryExpression_Decrement : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Decrement; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Increment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class UnaryExpression_Increment : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Increment; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Mul.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// Gets the pointer base type 8 | /// 9 | public class UnaryExpression_Mul : SimpleUnaryExpression 10 | { 11 | public override byte ForeToken 12 | { 13 | get { return DTokens.Times; } 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Not.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class UnaryExpression_Not : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Not; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_SegmentBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public sealed class UnaryExpression_SegmentBase : SimpleUnaryExpression 7 | { 8 | public IExpression RegisterExpression { get; set; } 9 | // This should never be called for this. 10 | public override byte ForeToken { get { throw new NotSupportedException(); } } 11 | 12 | public override string ToString() 13 | { 14 | return RegisterExpression.ToString() + ":" + UnaryExpression.ToString(); 15 | } 16 | 17 | public override CodeLocation Location 18 | { 19 | get { return RegisterExpression.Location; } 20 | set { throw new NotSupportedException(); } 21 | } 22 | 23 | public override IEnumerable SubExpressions 24 | { 25 | get 26 | { 27 | if (RegisterExpression != null) 28 | yield return RegisterExpression; 29 | if (UnaryExpression != null) 30 | yield return UnaryExpression; 31 | } 32 | } 33 | 34 | public override void Accept(ExpressionVisitor vis) { vis.Visit(this); } 35 | public override R Accept(ExpressionVisitor vis) { return vis.Visit(this); } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Sub.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class UnaryExpression_Sub : SimpleUnaryExpression 7 | { 8 | public override byte ForeToken 9 | { 10 | get { return DTokens.Minus; } 11 | } 12 | 13 | public override void Accept(ExpressionVisitor vis) 14 | { 15 | vis.Visit(this); 16 | } 17 | 18 | public override R Accept(ExpressionVisitor vis) 19 | { 20 | return vis.Visit(this); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/UnaryExpression_Type.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Expressions 4 | { 5 | /// 6 | /// (Type).Identifier 7 | /// 8 | public class UnaryExpression_Type : UnaryExpression 9 | { 10 | public ITypeDeclaration Type { get; set; } 11 | 12 | public int AccessIdentifierHash; 13 | 14 | public string AccessIdentifier 15 | { 16 | get { return Strings.TryGet(AccessIdentifierHash); } 17 | set 18 | { 19 | AccessIdentifierHash = value != null ? value.GetHashCode() : 0; 20 | Strings.Add(value); 21 | } 22 | } 23 | 24 | public override string ToString() 25 | { 26 | return "(" + Type.ToString() + ")." + AccessIdentifier; 27 | } 28 | 29 | public CodeLocation Location 30 | { 31 | get; 32 | set; 33 | } 34 | 35 | public CodeLocation EndLocation 36 | { 37 | get; 38 | set; 39 | } 40 | 41 | public void Accept(ExpressionVisitor vis) 42 | { 43 | vis.Visit(this); 44 | } 45 | 46 | public R Accept(ExpressionVisitor vis) 47 | { 48 | return vis.Visit(this); 49 | } 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/VoidInitializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | public class VoidInitializer : AbstractVariableInitializer 7 | { 8 | public VoidInitializer() 9 | { 10 | } 11 | 12 | public override void Accept(ExpressionVisitor vis) 13 | { 14 | vis.Visit(this); 15 | } 16 | 17 | public override R Accept(ExpressionVisitor vis) 18 | { 19 | return vis.Visit(this); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /DParser2/Dom/Expressions/XorExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | 4 | namespace D_Parser.Dom.Expressions 5 | { 6 | /// 7 | /// a ^ b; 8 | /// 9 | public class XorExpression : OperatorBasedExpression 10 | { 11 | public XorExpression() 12 | { 13 | OperatorToken = DTokens.Xor; 14 | } 15 | 16 | public override void Accept(ExpressionVisitor vis) 17 | { 18 | vis.Visit(this); 19 | } 20 | 21 | public override R Accept(ExpressionVisitor vis) 22 | { 23 | return vis.Visit(this); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DParser2/Dom/IDeclarationCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace D_Parser.Dom 3 | { 4 | public interface IDeclarationCondition : IEquatable 5 | { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /DParser2/Dom/ISyntaxRegion.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | using System.Collections.Generic; 4 | using System.Threading; 5 | namespace D_Parser.Dom 6 | { 7 | public interface ISyntaxRegion 8 | { 9 | CodeLocation Location { get; } 10 | CodeLocation EndLocation { get; } 11 | 12 | //int Kind { get; } 13 | } 14 | 15 | public static class SyntaxRegionHelper 16 | { 17 | public static ISyntaxRegion First(this ISyntaxRegion a, ISyntaxRegion b) 18 | { 19 | return a.Location <= b.Location ? a : b; 20 | } 21 | 22 | static int NextTypeKind = 0; 23 | static Dictionary Kinds = new Dictionary(); 24 | 25 | public static int Kind(this ISyntaxRegion sr) 26 | { 27 | int k; 28 | if (Kinds.TryGetValue(sr.GetType(), out k)) 29 | return k; 30 | 31 | k = Interlocked.Increment(ref NextTypeKind); 32 | Kinds[sr.GetType()] = k; 33 | return k; 34 | } 35 | 36 | public static int Kind() 37 | { 38 | int k; 39 | if (Kinds.TryGetValue(typeof(T), out k)) 40 | return k; 41 | 42 | k = Interlocked.Increment(ref NextTypeKind); 43 | Kinds[typeof(T)] = k; 44 | return k; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DParser2/Dom/Nodes/NodeInterfaces.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | namespace D_Parser.Dom 3 | { 4 | public interface IBlockNode: INode, IEnumerable 5 | { 6 | CodeLocation BlockStartLocation { get; set; } 7 | NodeDictionary Children { get; } 8 | 9 | void Add(INode Node); 10 | void AddRange(IEnumerable Nodes); 11 | int Count { get; } 12 | void Clear(); 13 | 14 | IEnumerable this[string Name] { get; } 15 | IEnumerable this[int NameHash] { get; } 16 | } 17 | 18 | public interface INode : ISyntaxRegion, IVisitable 19 | { 20 | string Name { get; set; } 21 | int NameHash { get; set; } 22 | CodeLocation NameLocation { get; set; } 23 | string Description { get; set; } 24 | ITypeDeclaration Type { get; set; } 25 | 26 | new CodeLocation Location { get; set; } 27 | new CodeLocation EndLocation { get; set; } 28 | 29 | /// 30 | /// Assigns a node's properties 31 | /// 32 | /// 33 | void AssignFrom(INode Other); 34 | 35 | INode Parent { get; set; } 36 | INode NodeRoot { get; } 37 | 38 | R Accept(NodeVisitor vis); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DParser2/Dom/ParserError.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace D_Parser.Dom 3 | { 4 | public class ParserError 5 | { 6 | public readonly bool IsSemantic; 7 | public readonly string Message; 8 | public readonly int Token; 9 | public readonly CodeLocation Location; 10 | 11 | public ParserError(bool IsSemanticError, string Message, int KeyToken, CodeLocation ErrorLocation) 12 | { 13 | IsSemantic = IsSemanticError; 14 | this.Message = Message; 15 | this.Token = KeyToken; 16 | this.Location = ErrorLocation; 17 | } 18 | 19 | public override string ToString() 20 | { 21 | return "[Parse error] " + Message; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/AbstractStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public abstract class AbstractStatement:IStatement 6 | { 7 | public virtual CodeLocation Location { get; set; } 8 | 9 | public virtual CodeLocation EndLocation { get; set; } 10 | 11 | readonly WeakReference parentStmt = new WeakReference(null); 12 | readonly WeakReference parentNode = new WeakReference(null); 13 | 14 | public IStatement Parent 15 | { 16 | get 17 | { 18 | return parentStmt.Target as IStatement; 19 | } 20 | set 21 | { 22 | parentStmt.Target = value; 23 | } 24 | } 25 | 26 | public INode ParentNode 27 | { 28 | get 29 | { 30 | var n = parentNode.Target as INode; 31 | if (n != null) 32 | return n; 33 | var t = parentStmt.Target as IStatement; 34 | if (t != null) 35 | return t.ParentNode; 36 | return null; 37 | } 38 | set 39 | { 40 | var ps = parentStmt.Target as IStatement; 41 | if (ps != null) 42 | ps.ParentNode = value; 43 | else 44 | parentNode.Target = value; 45 | } 46 | } 47 | 48 | public abstract string ToCode(); 49 | 50 | public override string ToString() 51 | { 52 | return ToCode(); 53 | } 54 | 55 | public abstract void Accept(IStatementVisitor vis); 56 | 57 | public abstract R Accept(StatementVisitor vis); 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/AsmAlignStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public sealed class AsmAlignStatement : AbstractStatement 7 | { 8 | public IExpression ValueExpression { get; set; } 9 | 10 | public override string ToCode() 11 | { 12 | if (ValueExpression == null) 13 | return "align "; 14 | var ie = ValueExpression as ScalarConstantExpression; 15 | if (ie != null && ie.Value.Equals(2m)) 16 | return "even"; 17 | else 18 | return "align " + ValueExpression.ToString(); 19 | } 20 | 21 | public override void Accept(IStatementVisitor vis) { vis.VisitAsmAlignStatement(this); } 22 | public override R Accept(StatementVisitor vis) { return vis.VisitAsmAlignStatement(this); } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/AsmRawDataStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using D_Parser.Dom.Expressions; 4 | 5 | namespace D_Parser.Dom.Statements 6 | { 7 | public sealed class AsmRawDataStatement : AbstractStatement 8 | { 9 | public DataType TypeOfData { get; set; } 10 | public IExpression[] Data { get; set; } 11 | 12 | public enum DataType 13 | { 14 | __UNKNOWN__, 15 | 16 | Byte, 17 | Word, 18 | DWord, 19 | QWord, 20 | Single, 21 | Double, 22 | Real, 23 | } 24 | 25 | public static bool TryParseDataType(string str, out DataType tp) 26 | { 27 | switch (str.ToLower()) 28 | { 29 | case "db": 30 | tp = DataType.Byte; 31 | return true; 32 | case "dw": 33 | case "ds": 34 | tp = DataType.Word; 35 | return true; 36 | case "di": 37 | tp = DataType.DWord; 38 | return true; 39 | case "dq": 40 | case "dl": 41 | tp = DataType.QWord; 42 | return true; 43 | case "df": 44 | tp = DataType.Single; 45 | return true; 46 | case "dd": 47 | tp = DataType.Double; 48 | return true; 49 | case "de": 50 | tp = DataType.Real; 51 | return true; 52 | default: 53 | tp = DataType.__UNKNOWN__; 54 | return false; 55 | } 56 | } 57 | 58 | public override string ToCode() 59 | { 60 | var sb = new StringBuilder(Data.Length * 4); 61 | switch (TypeOfData) 62 | { 63 | case DataType.Byte: 64 | sb.Append("db"); 65 | break; 66 | case DataType.Word: 67 | sb.Append("ds"); 68 | break; 69 | case DataType.DWord: 70 | sb.Append("di"); 71 | break; 72 | case DataType.QWord: 73 | sb.Append("dl"); 74 | break; 75 | case DataType.Single: 76 | sb.Append("df"); 77 | break; 78 | case DataType.Double: 79 | sb.Append("dd"); 80 | break; 81 | case DataType.Real: 82 | sb.Append("de"); 83 | break; 84 | case DataType.__UNKNOWN__: 85 | sb.Append(""); 86 | break; 87 | default: 88 | throw new NotSupportedException(); 89 | } 90 | 91 | for (int i = 0; i < Data.Length; i++) 92 | { 93 | if (i > 0) 94 | sb.Append(','); 95 | sb.Append(' '); 96 | sb.Append(Data[i].ToString()); 97 | } 98 | return sb.ToString(); 99 | } 100 | 101 | public override void Accept(IStatementVisitor vis) { vis.VisitAsmRawDataStatement(this); } 102 | public override R Accept(StatementVisitor vis) { return vis.VisitAsmRawDataStatement(this); } 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/AsmStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class AsmStatement : StatementContainingStatement 7 | { 8 | public bool Naked { get; set; } 9 | public AbstractStatement[] Instructions; 10 | 11 | public override string ToCode() 12 | { 13 | var ret = "asm {"; 14 | 15 | if (Instructions != null && Instructions.Length > 0) 16 | { 17 | foreach (var i in Instructions) 18 | ret += Environment.NewLine + i.ToCode() + ';'; 19 | ret += Environment.NewLine; 20 | } 21 | 22 | return ret + '}'; 23 | } 24 | 25 | public override IEnumerable SubStatements { get { return Instructions; } } 26 | 27 | public override void Accept(IStatementVisitor vis) { vis.VisitAsmStatement(this); } 28 | public override R Accept(StatementVisitor vis) { return vis.VisitAsmStatement(this); } 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/BlockStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class BlockStatement : StatementContainingStatement, IEnumerable 7 | { 8 | internal readonly List _Statements = new List(); 9 | 10 | public IEnumerator GetEnumerator() 11 | { 12 | return _Statements.GetEnumerator(); 13 | } 14 | 15 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 16 | { 17 | return _Statements.GetEnumerator(); 18 | } 19 | 20 | public override string ToCode() 21 | { 22 | var ret = "{" + Environment.NewLine; 23 | 24 | foreach (var s in _Statements) 25 | ret += s.ToCode() + Environment.NewLine; 26 | 27 | return ret + "}"; 28 | } 29 | 30 | public void Add(IStatement s) 31 | { 32 | if (s == null) 33 | return; 34 | s.Parent = this; 35 | _Statements.Add(s); 36 | } 37 | 38 | public override IEnumerable SubStatements 39 | { 40 | get 41 | { 42 | return _Statements; 43 | } 44 | } 45 | 46 | public override void Accept(IStatementVisitor vis) 47 | { 48 | vis.Visit(this); 49 | } 50 | 51 | public override R Accept(StatementVisitor vis) 52 | { 53 | return vis.Visit(this); 54 | } 55 | 56 | public override string ToString() 57 | { 58 | return " " + base.ToString(); 59 | } 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/BreakStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class BreakStatement : AbstractStatement,IExpressionContainingStatement 7 | { 8 | public int IdentifierHash; 9 | 10 | public string Identifier 11 | { 12 | get{ return Strings.TryGet(IdentifierHash); } 13 | set 14 | { 15 | Strings.Add(value); 16 | IdentifierHash = value != null ? value.GetHashCode() : 0; 17 | } 18 | } 19 | 20 | public override string ToCode() 21 | { 22 | return "break" + (string.IsNullOrEmpty(Identifier) ? "" : (' ' + Identifier)) + ';'; 23 | } 24 | 25 | public IExpression[] SubExpressions 26 | { 27 | get { return string.IsNullOrEmpty(Identifier) ? null : new[] { new IdentifierExpression(Identifier) }; } 28 | } 29 | 30 | public override void Accept(IStatementVisitor vis) 31 | { 32 | vis.Visit(this); 33 | } 34 | 35 | public override R Accept(StatementVisitor vis) 36 | { 37 | return vis.Visit(this); 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ContinueStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ContinueStatement : AbstractStatement, IExpressionContainingStatement 7 | { 8 | public int IdentifierHash; 9 | 10 | public string Identifier 11 | { 12 | get{ return Strings.TryGet(IdentifierHash); } 13 | set 14 | { 15 | Strings.Add(value); 16 | IdentifierHash = value != null ? value.GetHashCode() : 0; 17 | } 18 | } 19 | 20 | public override string ToCode() 21 | { 22 | return "continue" + (string.IsNullOrEmpty(Identifier) ? "" : (' ' + Identifier)) + ';'; 23 | } 24 | 25 | public IExpression[] SubExpressions 26 | { 27 | get { return string.IsNullOrEmpty(Identifier) ? null : new[] { new IdentifierExpression(Identifier) }; } 28 | } 29 | 30 | public override void Accept(IStatementVisitor vis) 31 | { 32 | vis.Visit(this); 33 | } 34 | 35 | public override R Accept(StatementVisitor vis) 36 | { 37 | return vis.Visit(this); 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ContractStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ContractStatement : StatementContainingStatement, IExpressionContainingStatement 7 | { 8 | public IExpression Condition; 9 | public IExpression Message; 10 | public bool isOut; // true for Out 11 | public IdentifierDeclaration OutResultVariable; 12 | 13 | public override string ToCode() 14 | { 15 | string s = isOut ? "out" : "in"; 16 | if (Condition != null) 17 | { 18 | s += "("; 19 | if (OutResultVariable != null) 20 | s += OutResultVariable.ToString(); 21 | if (isOut) 22 | s += ";"; 23 | s += Condition.ToString(); 24 | if (Message != null) 25 | s += "," + Message.ToString(); 26 | s += ")"; 27 | } 28 | else if (ScopedStatement != null) 29 | { 30 | if (OutResultVariable != null) 31 | s += "(" + OutResultVariable.ToString() + ")"; 32 | s += Environment.NewLine + ScopedStatement.ToString(); 33 | } 34 | return s; 35 | } 36 | 37 | public IExpression[] SubExpressions 38 | { 39 | get { 40 | if (Condition != null && Message != null) 41 | return new[] { Condition, Message }; 42 | if (Condition != null) 43 | return new[] { Condition }; 44 | return new Expression[0]; 45 | } 46 | } 47 | 48 | public override void Accept(IStatementVisitor vis) 49 | { 50 | vis.Visit(this); 51 | } 52 | 53 | public override R Accept(StatementVisitor vis) 54 | { 55 | return vis.Visit(this); 56 | } 57 | } 58 | } 59 | 60 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/DebugSpecificiation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public class DebugSpecification : AbstractStatement, StaticStatement 6 | { 7 | public string SpecifiedId; 8 | public ulong SpecifiedDebugLevel; 9 | 10 | public override string ToCode() 11 | { 12 | return "debug = " + (SpecifiedId ?? SpecifiedDebugLevel.ToString()) + ";"; 13 | } 14 | 15 | public override void Accept(IStatementVisitor vis) 16 | { 17 | vis.Visit(this); 18 | } 19 | 20 | public override R Accept(StatementVisitor vis) 21 | { 22 | return vis.Visit(this); 23 | } 24 | 25 | public DAttribute[] Attributes 26 | { 27 | get; 28 | set; 29 | } 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/DeclarationStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | using System.Collections.Generic; 4 | 5 | namespace D_Parser.Dom.Statements 6 | { 7 | public class DeclarationStatement : AbstractStatement,IDeclarationContainingStatement 8 | { 9 | /// 10 | /// Declarations done by this statement. Contains more than one item e.g. on int a,b,c; 11 | /// 12 | //public INode[] Declaration; 13 | 14 | public override string ToCode() 15 | { 16 | if (Declarations == null || Declarations.Length < 1) 17 | return ";"; 18 | 19 | var r = Declarations[0].ToString(); 20 | 21 | for (int i = 1; i < Declarations.Length; i++) 22 | { 23 | var d = Declarations[i]; 24 | r += ',' + d.Name; 25 | 26 | var dv = d as DVariable; 27 | if (dv != null && dv.Initializer != null) 28 | r += '=' + dv.Initializer.ToString(); 29 | } 30 | 31 | return r + ';'; 32 | } 33 | 34 | public INode[] Declarations 35 | { 36 | get; 37 | set; 38 | } 39 | 40 | public override void Accept(IStatementVisitor vis) 41 | { 42 | vis.Visit(this); 43 | } 44 | 45 | public override R Accept(StatementVisitor vis) 46 | { 47 | return vis.Visit(this); 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ExpressionStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ExpressionStatement : AbstractStatement,IExpressionContainingStatement 7 | { 8 | public IExpression Expression; 9 | 10 | public override string ToCode() 11 | { 12 | return Expression.ToString() + ';'; 13 | } 14 | 15 | public IExpression[] SubExpressions 16 | { 17 | get { return new[]{ Expression }; } 18 | } 19 | 20 | public override void Accept(IStatementVisitor vis) 21 | { 22 | vis.Visit(this); 23 | } 24 | 25 | public override R Accept(StatementVisitor vis) 26 | { 27 | return vis.Visit(this); 28 | } 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ForStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | using System.Collections.Generic; 4 | 5 | namespace D_Parser.Dom.Statements 6 | { 7 | public class ForStatement : StatementContainingStatement, IExpressionContainingStatement 8 | { 9 | public IStatement Initialize; 10 | public IExpression Test; 11 | public IExpression Increment; 12 | 13 | public IExpression[] SubExpressions 14 | { 15 | get { return new[] { Test, Increment }; } 16 | } 17 | 18 | public override IEnumerable SubStatements 19 | { 20 | get 21 | { 22 | if (Initialize != null) 23 | yield return Initialize; 24 | if (ScopedStatement != null) 25 | yield return ScopedStatement; 26 | } 27 | } 28 | 29 | public override string ToCode() 30 | { 31 | var ret = "for("; 32 | 33 | if (Initialize != null) 34 | ret += Initialize.ToCode(); 35 | 36 | ret += ';'; 37 | 38 | if (Test != null) 39 | ret += Test.ToString(); 40 | 41 | ret += ';'; 42 | 43 | if (Increment != null) 44 | ret += Increment.ToString(); 45 | 46 | ret += ')'; 47 | 48 | if (ScopedStatement != null) 49 | ret += ' ' + ScopedStatement.ToCode(); 50 | 51 | return ret; 52 | } 53 | 54 | public override void Accept(IStatementVisitor vis) 55 | { 56 | vis.Visit(this); 57 | } 58 | 59 | public override R Accept(StatementVisitor vis) 60 | { 61 | return vis.Visit(this); 62 | } 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ForeachStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ForeachStatement : StatementContainingStatement, IExpressionContainingStatement, IDeclarationContainingStatement 7 | { 8 | public bool IsRangeStatement 9 | { 10 | get { return UpperAggregate != null; } 11 | } 12 | 13 | public bool IsReverse = false; 14 | public DVariable[] ForeachTypeList; 15 | 16 | public INode[] Declarations 17 | { 18 | get { return ForeachTypeList; } 19 | } 20 | 21 | public IExpression Aggregate; 22 | /// 23 | /// Used in ForeachRangeStatements. The Aggregate field will be the lower expression then. 24 | /// 25 | public IExpression UpperAggregate; 26 | 27 | public IExpression[] SubExpressions 28 | { 29 | get 30 | { 31 | if (Aggregate != null) 32 | { 33 | if (UpperAggregate == null) 34 | return new[]{ Aggregate }; 35 | return new[]{ Aggregate, UpperAggregate }; 36 | } 37 | if (UpperAggregate != null) 38 | return new[]{ UpperAggregate }; 39 | return new IExpression[0]; 40 | } 41 | } 42 | 43 | public override string ToCode() 44 | { 45 | var ret = (IsReverse ? "foreach_reverse" : "foreach") + '('; 46 | 47 | if (ForeachTypeList != null) 48 | { 49 | foreach (var v in ForeachTypeList) 50 | ret += v.ToString() + ','; 51 | 52 | ret = ret.TrimEnd(',') + ';'; 53 | } 54 | 55 | if (Aggregate != null) 56 | ret += Aggregate.ToString(); 57 | 58 | if (UpperAggregate != null) 59 | ret += ".." + UpperAggregate.ToString(); 60 | 61 | ret += ')'; 62 | 63 | if (ScopedStatement != null) 64 | ret += ' ' + ScopedStatement.ToCode(); 65 | 66 | return ret; 67 | } 68 | 69 | public override void Accept(IStatementVisitor vis) 70 | { 71 | vis.Visit(this); 72 | } 73 | 74 | public override R Accept(StatementVisitor vis) 75 | { 76 | return vis.Visit(this); 77 | } 78 | } 79 | } 80 | 81 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/GotoStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Parser; 3 | using D_Parser.Dom.Expressions; 4 | 5 | namespace D_Parser.Dom.Statements 6 | { 7 | public class GotoStatement : AbstractStatement, IExpressionContainingStatement 8 | { 9 | public enum GotoStmtType : byte 10 | { 11 | Identifier = DTokens.Identifier, 12 | Case = DTokens.Case, 13 | Default = DTokens.Default 14 | } 15 | 16 | public int LabelIdentifierHash; 17 | 18 | public string LabelIdentifier 19 | { 20 | get{ return Strings.TryGet(LabelIdentifierHash); } 21 | set 22 | { 23 | Strings.Add(value); 24 | LabelIdentifierHash = value != null ? value.GetHashCode() : 0; 25 | } 26 | } 27 | 28 | public IExpression CaseExpression; 29 | public GotoStmtType StmtType = GotoStmtType.Identifier; 30 | 31 | public override string ToCode() 32 | { 33 | switch (StmtType) 34 | { 35 | case GotoStmtType.Identifier: 36 | return "goto " + LabelIdentifier + ';'; 37 | case GotoStmtType.Default: 38 | return "goto default;"; 39 | case GotoStmtType.Case: 40 | return "goto" + (CaseExpression == null ? "" : (' ' + CaseExpression.ToString())) + ';'; 41 | } 42 | 43 | return null; 44 | } 45 | 46 | public IExpression[] SubExpressions 47 | { 48 | get { return CaseExpression != null ? new[] { CaseExpression } : null; } 49 | } 50 | 51 | public override void Accept(IStatementVisitor vis) 52 | { 53 | vis.Visit(this); 54 | } 55 | 56 | public override R Accept(StatementVisitor vis) 57 | { 58 | return vis.Visit(this); 59 | } 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/IDeclarationContainingStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public interface IDeclarationContainingStatement : IStatement 6 | { 7 | INode[] Declarations { get; } 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/IExpressionContainingStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public interface IExpressionContainingStatement : IStatement 7 | { 8 | IExpression[] SubExpressions { get; } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/IStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public interface IStatement : ISyntaxRegion, IVisitable 6 | { 7 | new CodeLocation Location { get; set; } 8 | 9 | new CodeLocation EndLocation { get; set; } 10 | 11 | IStatement Parent { get; set; } 12 | 13 | INode ParentNode { get; set; } 14 | 15 | string ToCode(); 16 | 17 | R Accept(StatementVisitor vis); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/IfStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace D_Parser.Dom.Statements 7 | { 8 | public class IfStatement : StatementContainingStatement,IDeclarationContainingStatement,IExpressionContainingStatement 9 | { 10 | public IExpression IfCondition; 11 | public DVariable IfVariable; 12 | 13 | public IStatement ThenStatement 14 | { 15 | get { return ScopedStatement; } 16 | set { ScopedStatement = value; } 17 | } 18 | 19 | public IStatement ElseStatement; 20 | 21 | public override IEnumerable SubStatements 22 | { 23 | get 24 | { 25 | if (ThenStatement != null) 26 | yield return ThenStatement; 27 | if (ElseStatement != null) 28 | yield return ElseStatement; 29 | } 30 | } 31 | 32 | public override CodeLocation EndLocation 33 | { 34 | get 35 | { 36 | if (ScopedStatement == null) 37 | return base.EndLocation; 38 | return ElseStatement != null ? ElseStatement.EndLocation : ScopedStatement.EndLocation; 39 | } 40 | set 41 | { 42 | if (ScopedStatement == null) 43 | base.EndLocation = value; 44 | } 45 | } 46 | 47 | public override string ToCode() 48 | { 49 | var sb = new StringBuilder("if("); 50 | 51 | if (IfCondition != null) 52 | sb.Append(IfCondition.ToString()); 53 | else if (IfVariable != null) 54 | sb.Append(IfVariable.ToString(true, false, true)); 55 | 56 | sb.AppendLine(")"); 57 | 58 | if (ScopedStatement != null) 59 | sb.Append(ScopedStatement.ToCode()); 60 | 61 | if (ElseStatement != null) 62 | sb.AppendLine().Append("else ").Append(ElseStatement.ToCode()); 63 | 64 | return sb.ToString(); 65 | } 66 | 67 | public IExpression[] SubExpressions 68 | { 69 | get 70 | { 71 | return new[] { IfCondition }; 72 | } 73 | } 74 | 75 | public INode[] Declarations 76 | { 77 | get 78 | { 79 | return IfVariable == null ? null : new[]{ IfVariable }; 80 | } 81 | } 82 | 83 | public override void Accept(IStatementVisitor vis) 84 | { 85 | vis.Visit(this); 86 | } 87 | 88 | public override R Accept(StatementVisitor vis) 89 | { 90 | return vis.Visit(this); 91 | } 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/LabeledStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public class LabeledStatement : AbstractStatement 6 | { 7 | public int IdentifierHash; 8 | 9 | public string Identifier 10 | { 11 | get{ return Strings.TryGet(IdentifierHash); } 12 | set 13 | { 14 | Strings.Add(value); 15 | IdentifierHash = value != null ? value.GetHashCode() : 0; 16 | } 17 | } 18 | 19 | public override string ToCode() 20 | { 21 | return Identifier + ":"; 22 | } 23 | 24 | public override void Accept(IStatementVisitor vis) 25 | { 26 | vis.Visit(this); 27 | } 28 | 29 | public override R Accept(StatementVisitor vis) 30 | { 31 | return vis.Visit(this); 32 | } 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/MixinStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class MixinStatement : AbstractStatement,IExpressionContainingStatement,StaticStatement 7 | { 8 | public IExpression MixinExpression; 9 | 10 | public override string ToCode() 11 | { 12 | return "mixin(" + (MixinExpression == null ? "" : MixinExpression.ToString()) + ");"; 13 | } 14 | 15 | public IExpression[] SubExpressions 16 | { 17 | get { return new[]{ MixinExpression }; } 18 | } 19 | 20 | public override void Accept(IStatementVisitor vis) 21 | { 22 | vis.VisitMixinStatement(this); 23 | } 24 | 25 | public override R Accept(StatementVisitor vis) 26 | { 27 | return vis.VisitMixinStatement(this); 28 | } 29 | 30 | public DAttribute[] Attributes{ get; set; } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/PragmaStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class PragmaStatement : StatementContainingStatement 7 | { 8 | public PragmaAttribute Pragma; 9 | 10 | public override string ToCode() 11 | { 12 | var r = Pragma == null ? "" : Pragma.ToString(); 13 | 14 | r += ScopedStatement == null ? "" : (" " + ScopedStatement.ToCode()); 15 | 16 | return r; 17 | } 18 | 19 | public override void Accept(IStatementVisitor vis) 20 | { 21 | vis.Visit(this); 22 | } 23 | 24 | public override R Accept(StatementVisitor vis) 25 | { 26 | return vis.Visit(this); 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ReturnStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ReturnStatement : AbstractStatement,IExpressionContainingStatement 7 | { 8 | public IExpression ReturnExpression; 9 | 10 | public override string ToCode() 11 | { 12 | return "return" + (ReturnExpression == null ? "" : (' ' + ReturnExpression.ToString())) + ';'; 13 | } 14 | 15 | public IExpression[] SubExpressions 16 | { 17 | get { return new[]{ ReturnExpression }; } 18 | } 19 | 20 | public override void Accept(IStatementVisitor vis) 21 | { 22 | vis.Visit(this); 23 | } 24 | 25 | public override R Accept(StatementVisitor vis) 26 | { 27 | return vis.Visit(this); 28 | } 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ScopeGuardStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public class ScopeGuardStatement : StatementContainingStatement 6 | { 7 | public const string ExitScope = "exit"; 8 | public const string SuccessScope = "success"; 9 | public const string FailureScope = "failure"; 10 | public string GuardedScope = ExitScope; 11 | 12 | public override string ToCode() 13 | { 14 | return "scope(" + GuardedScope + ')' + (ScopedStatement == null ? "" : ScopedStatement.ToCode()); 15 | } 16 | 17 | public override void Accept(IStatementVisitor vis) 18 | { 19 | vis.Visit(this); 20 | } 21 | 22 | public override R Accept(StatementVisitor vis) 23 | { 24 | return vis.Visit(this); 25 | } 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/StatementCondition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Collections.Generic; 4 | using D_Parser.Dom.Expressions; 5 | 6 | namespace D_Parser.Dom.Statements 7 | { 8 | public class StatementCondition : StatementContainingStatement 9 | { 10 | public IStatement ElseStatement; 11 | public DeclarationCondition Condition; 12 | 13 | public override string ToCode() 14 | { 15 | var sb = new StringBuilder("if("); 16 | 17 | if (Condition != null) 18 | sb.Append(Condition.ToString()); 19 | sb.AppendLine(")"); 20 | 21 | if (ElseStatement != null) 22 | sb.Append(ElseStatement); 23 | 24 | return sb.ToString(); 25 | } 26 | 27 | public override IEnumerable SubStatements 28 | { 29 | get 30 | { 31 | if (ElseStatement != null) 32 | yield return ElseStatement; 33 | if (ScopedStatement != null) 34 | yield return ScopedStatement; 35 | } 36 | } 37 | 38 | public override void Accept(IStatementVisitor vis) 39 | { 40 | vis.Visit(this); 41 | } 42 | 43 | public override R Accept(StatementVisitor vis) 44 | { 45 | return vis.Visit(this); 46 | } 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/StatementContainingStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | /// 7 | /// Represents a statement that can contain other statements, which may become scoped. 8 | /// 9 | public abstract class StatementContainingStatement : AbstractStatement 10 | { 11 | public virtual IStatement ScopedStatement { get; set; } 12 | 13 | public virtual IEnumerable SubStatements { get { return ScopedStatement != null ? new[] { ScopedStatement } : new IStatement[0]; } } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/StaticAssertStatement.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom.Expressions; 2 | using System; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class StaticAssertStatement : AbstractStatement, IExpressionContainingStatement, StaticStatement 7 | { 8 | public IExpression AssertedExpression; 9 | public IExpression Message; 10 | 11 | public override string ToCode() 12 | { 13 | return "static assert(" + (AssertedExpression != null ? AssertedExpression.ToString() : "") + 14 | (Message == null ? "" : ("," + Message)) + ");"; 15 | } 16 | 17 | public IExpression[] SubExpressions 18 | { 19 | get { return new[] { AssertedExpression }; } 20 | } 21 | 22 | public DAttribute[] Attributes 23 | { 24 | get; 25 | set; 26 | } 27 | 28 | public override void Accept(IStatementVisitor vis) 29 | { 30 | vis.Visit(this); 31 | } 32 | 33 | public override R Accept(StatementVisitor vis) 34 | { 35 | return vis.Visit(this); 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/StaticForeachStatement.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom.Expressions; 2 | using System; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class StaticForeachStatement : ForeachStatement, StaticStatement 7 | { 8 | public DAttribute[] Attributes { get; set; } 9 | 10 | public bool inSemanticAnalysis = false; 11 | 12 | public override string ToCode() 13 | { 14 | return "static " + base.ToCode(); 15 | } 16 | 17 | public override void Accept(IStatementVisitor vis) 18 | { 19 | vis.Visit(this); 20 | } 21 | 22 | public override R Accept(StatementVisitor vis) 23 | { 24 | return vis.Visit(this); 25 | } 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/SynchronizedStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class SynchronizedStatement : StatementContainingStatement,IExpressionContainingStatement 7 | { 8 | public IExpression SyncExpression; 9 | 10 | public override string ToCode() 11 | { 12 | var ret = "synchronized"; 13 | 14 | if (SyncExpression != null) 15 | ret += '(' + SyncExpression.ToString() + ')'; 16 | 17 | if (ScopedStatement != null) 18 | ret += ' ' + ScopedStatement.ToCode(); 19 | 20 | return ret; 21 | } 22 | 23 | public IExpression[] SubExpressions 24 | { 25 | get { return new[]{ SyncExpression }; } 26 | } 27 | 28 | public override void Accept(IStatementVisitor vis) 29 | { 30 | vis.Visit(this); 31 | } 32 | 33 | public override R Accept(StatementVisitor vis) 34 | { 35 | return vis.Visit(this); 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/TemplateMixin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | using System.Collections.Generic; 4 | 5 | namespace D_Parser.Dom.Statements 6 | { 7 | public class TemplateMixin : AbstractStatement, StaticStatement 8 | { 9 | public ITypeDeclaration Qualifier; 10 | public string MixinId; 11 | public CodeLocation IdLocation; 12 | 13 | public override string ToCode() 14 | { 15 | var r = "mixin"; 16 | 17 | if (Qualifier != null) 18 | r += " " + Qualifier.ToString(); 19 | 20 | if (!string.IsNullOrEmpty(MixinId)) 21 | r += ' ' + MixinId; 22 | 23 | return r + ';'; 24 | } 25 | 26 | public override void Accept(IStatementVisitor vis) 27 | { 28 | vis.Visit(this); 29 | } 30 | 31 | public override R Accept(StatementVisitor vis) 32 | { 33 | return vis.Visit(this); 34 | } 35 | 36 | public DAttribute[] Attributes 37 | { 38 | get; 39 | set; 40 | } 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/ThrowStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class ThrowStatement : AbstractStatement,IExpressionContainingStatement 7 | { 8 | public IExpression ThrowExpression; 9 | 10 | public override string ToCode() 11 | { 12 | return "throw" + (ThrowExpression == null ? "" : (' ' + ThrowExpression.ToString())) + ';'; 13 | } 14 | 15 | public IExpression[] SubExpressions 16 | { 17 | get { return new[]{ ThrowExpression }; } 18 | } 19 | 20 | public override void Accept(IStatementVisitor vis) 21 | { 22 | vis.Visit(this); 23 | } 24 | 25 | public override R Accept(StatementVisitor vis) 26 | { 27 | return vis.Visit(this); 28 | } 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/VersionSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public class VersionSpecification : AbstractStatement, StaticStatement 6 | { 7 | public string SpecifiedId; 8 | public ulong SpecifiedNumber; 9 | 10 | public override string ToCode() 11 | { 12 | return "version = " + (SpecifiedId ?? SpecifiedNumber.ToString()) + ";"; 13 | } 14 | 15 | public override void Accept(IStatementVisitor vis) 16 | { 17 | vis.Visit(this); 18 | } 19 | 20 | public override R Accept(StatementVisitor vis) 21 | { 22 | return vis.Visit(this); 23 | } 24 | 25 | public DAttribute[] Attributes 26 | { 27 | get; 28 | set; 29 | } 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/VolatileStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Dom.Statements 4 | { 5 | public class VolatileStatement : StatementContainingStatement 6 | { 7 | public override string ToCode() 8 | { 9 | return "volatile " + (ScopedStatement == null ? "" : ScopedStatement.ToCode()); 10 | } 11 | 12 | public override void Accept(IStatementVisitor vis) 13 | { 14 | vis.Visit(this); 15 | } 16 | 17 | public override R Accept(StatementVisitor vis) 18 | { 19 | return vis.Visit(this); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/WhileStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class WhileStatement : StatementContainingStatement, IExpressionContainingStatement 7 | { 8 | public IExpression Condition; 9 | 10 | public override CodeLocation EndLocation 11 | { 12 | get 13 | { 14 | if (ScopedStatement == null) 15 | return base.EndLocation; 16 | return ScopedStatement.EndLocation; 17 | } 18 | set 19 | { 20 | if (ScopedStatement == null) 21 | base.EndLocation = value; 22 | } 23 | } 24 | 25 | public override string ToCode() 26 | { 27 | var ret = "while("; 28 | 29 | if (Condition != null) 30 | ret += Condition.ToString(); 31 | 32 | ret += ") " + Environment.NewLine; 33 | 34 | if (ScopedStatement != null) 35 | ret += ScopedStatement.ToCode(); 36 | 37 | return ret; 38 | } 39 | 40 | public IExpression[] SubExpressions 41 | { 42 | get { return new[]{ Condition }; } 43 | } 44 | 45 | public override void Accept(IStatementVisitor vis) 46 | { 47 | vis.Visit(this); 48 | } 49 | 50 | public override R Accept(StatementVisitor vis) 51 | { 52 | return vis.Visit(this); 53 | } 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /DParser2/Dom/Statements/WithStatement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | 4 | namespace D_Parser.Dom.Statements 5 | { 6 | public class WithStatement : StatementContainingStatement, IExpressionContainingStatement 7 | { 8 | public IExpression WithExpression; 9 | public ITypeDeclaration WithSymbol; 10 | 11 | public override string ToCode() 12 | { 13 | var ret = "with("; 14 | 15 | if (WithExpression != null) 16 | ret += WithExpression.ToString(); 17 | else if (WithSymbol != null) 18 | ret += WithSymbol.ToString(); 19 | 20 | ret += ')'; 21 | 22 | if (ScopedStatement != null) 23 | ret += ScopedStatement.ToCode(); 24 | 25 | return ret; 26 | } 27 | 28 | public IExpression[] SubExpressions 29 | { 30 | get 31 | { 32 | return new[] { WithExpression }; 33 | } 34 | } 35 | 36 | public override void Accept(IStatementVisitor vis) 37 | { 38 | vis.Visit(this); 39 | } 40 | 41 | public override R Accept(StatementVisitor vis) 42 | { 43 | return vis.Visit(this); 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /DParser2/Dom/StaticStatement.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom.Statements; 2 | 3 | namespace D_Parser.Dom 4 | { 5 | public interface StaticStatement : IStatement 6 | { 7 | DAttribute[] Attributes { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DParser2/Dom/Visitors/IVisitable.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Dom 2 | { 3 | public interface IVisitable where Visitor : IVisitor 4 | { 5 | void Accept(Visitor vis); 6 | //R Accept(RetVisitor vis); 7 | } 8 | /* 9 | public interface IVisitiable_Return where Visitor : IVisitor 10 | { 11 | R Accept(IVisitor vis); 12 | }*/ 13 | } 14 | -------------------------------------------------------------------------------- /DParser2/Formatting/Formatter.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using D_Parser.Dom; 3 | using D_Parser.Parser; 4 | 5 | namespace D_Parser.Formatting 6 | { 7 | public static class Formatter 8 | { 9 | public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) 10 | { 11 | options = options ?? DFormattingOptions.CreateDStandard(); 12 | textStyle = textStyle ?? TextEditorOptions.Default; 13 | ast = ast ?? DParser.ParseString(code) as DModule; 14 | 15 | var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument{ Text = code }, ast, textStyle); 16 | 17 | formattingVisitor.WalkThroughAst(); 18 | 19 | var sb = new StringBuilder(code); 20 | 21 | formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { 22 | sb.Remove(start,length); 23 | sb.Insert(start,insertedText); 24 | }); 25 | 26 | return sb.ToString(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DParser2/Formatting/ITextEditorOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Formatting 4 | { 5 | public interface ITextEditorOptions 6 | { 7 | string EolMarker {get;} 8 | bool TabsToSpaces {get;} 9 | int TabSize{get;} 10 | bool KeepAlignmentSpaces { get; } 11 | 12 | int IndentSize {get;} 13 | int ContinuationIndent {get;} 14 | int LabelIndent {get;} 15 | 16 | bool RemoveTrailingWhitespaces { get; } 17 | } 18 | 19 | public class TextEditorOptions : ITextEditorOptions 20 | { 21 | public string EolMarker {get;set;} 22 | public bool TabsToSpaces {get;set;} 23 | public int TabSize {get;set;} 24 | public int IndentSize {get;set;} 25 | public int ContinuationIndent {get;set;} 26 | public int LabelIndent {get;set;} 27 | public bool KeepAlignmentSpaces { get; set; } 28 | public bool RemoveTrailingWhitespaces { get; set; } 29 | 30 | public static TextEditorOptions Default = new TextEditorOptions{ 31 | EolMarker = Environment.NewLine, 32 | TabsToSpaces = false, 33 | TabSize = 4, 34 | IndentSize = 4, 35 | ContinuationIndent = 4, 36 | LabelIndent = 0, 37 | KeepAlignmentSpaces = true, 38 | RemoveTrailingWhitespaces = false}; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DParser2/Formatting/Indent/IStateMachineIndentEngine.cs: -------------------------------------------------------------------------------- 1 | // 2 | // IStateMachineIndentEngine.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2014 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System; 27 | 28 | namespace D_Parser.Formatting.Indent 29 | { 30 | interface IStateMachineIndentEngine : IDocumentIndentEngine 31 | { 32 | Inside Inside {get;} 33 | 34 | new IStateMachineIndentEngine Clone(); 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /DParser2/Misc/Logger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Misc 5 | { 6 | public class Logger 7 | { 8 | public static readonly List Loggers = new List(); 9 | 10 | static Logger() 11 | { 12 | Loggers.Add (new ConsoleLogger()); 13 | } 14 | 15 | public static void Log(LogLevel lvl, string msg, Exception ex = null) 16 | { 17 | foreach (var l in Loggers) 18 | l.Log (lvl, msg, ex); 19 | } 20 | 21 | public static void LogError(string msg, Exception ex = null) 22 | { 23 | Log (LogLevel.Error, msg, ex); 24 | } 25 | 26 | public static void LogWarn(string msg, Exception ex = null) 27 | { 28 | Log (LogLevel.Warn, msg, ex); 29 | } 30 | } 31 | 32 | class ConsoleLogger : ILogger 33 | { 34 | public void Log (LogLevel lvl, string msg, Exception ex) 35 | { 36 | switch (lvl) { 37 | case LogLevel.Debug: 38 | Console.Write ("Debug: "); 39 | break; 40 | case LogLevel.Error: 41 | Console.Write ("Error: "); 42 | break; 43 | case LogLevel.Fatal: 44 | Console.Write ("Fatal error: "); 45 | break; 46 | case LogLevel.Info: 47 | Console.Write ("Info: "); 48 | break; 49 | case LogLevel.Warn: 50 | Console.Write ("Warning: "); 51 | break; 52 | } 53 | 54 | if (msg != null) 55 | Console.Write (msg); 56 | 57 | if (ex != null) { 58 | Console.WriteLine (); 59 | Console.WriteLine (ex.Message); 60 | Console.Write (ex.StackTrace); 61 | } 62 | 63 | Console.WriteLine (); 64 | } 65 | 66 | public LogLevel EnabledLogLevel { 67 | get { 68 | return LogLevel.Info; 69 | } 70 | } 71 | 72 | public string Name { 73 | get { 74 | return "Console"; 75 | } 76 | } 77 | } 78 | 79 | public interface ILogger 80 | { 81 | LogLevel EnabledLogLevel { get;} 82 | string Name {get;} 83 | void Log(LogLevel lvl, string msg, Exception ex); 84 | } 85 | 86 | public enum LogLevel 87 | { 88 | Fatal = 1, 89 | Error, 90 | Warn = 4, 91 | Info = 8, 92 | Debug = 16 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /DParser2/Misc/Mangling/Mangler.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Mangler.cs - D mangling helper. For a spec, see http://dlang.org/abi.html 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System.Text; 27 | 28 | using D_Parser.Dom; 29 | namespace D_Parser.Misc.Mangling 30 | { 31 | public class Mangler 32 | { 33 | public Mangler () 34 | { 35 | } 36 | 37 | public static string Mangle(ITypeDeclaration td) 38 | { 39 | var sb = new StringBuilder (); 40 | Mangle (td, sb); 41 | return sb.ToString (); 42 | } 43 | 44 | static void Mangle(ITypeDeclaration td, StringBuilder sb) 45 | { 46 | 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /DParser2/Misc/ModuleNameHelper.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace D_Parser.Misc 3 | { 4 | public static class ModuleNameHelper 5 | { 6 | /// 7 | /// a.b.c.d => a.b.c 8 | /// 9 | public static string ExtractPackageName(string ModuleName) 10 | { 11 | if (string.IsNullOrEmpty(ModuleName)) 12 | return ""; 13 | 14 | var i = ModuleName.LastIndexOf('.'); 15 | 16 | return i == -1 ? "" : ModuleName.Substring(0, i); 17 | } 18 | 19 | /// 20 | /// a.b.c.d => d 21 | /// 22 | public static string ExtractModuleName(string ModuleName) 23 | { 24 | if (string.IsNullOrEmpty(ModuleName)) 25 | return ""; 26 | 27 | var i = ModuleName.LastIndexOf('.'); 28 | 29 | return i == -1 ? ModuleName : ModuleName.Substring(i + 1); 30 | } 31 | 32 | public static string[] SplitModuleName(string ModuleName) 33 | { 34 | return ModuleName.Split('.'); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DParser2/Misc/ParseLog.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.IO; 6 | using D_Parser.Dom; 7 | 8 | namespace D_Parser.Misc 9 | { 10 | #if IGNORE 11 | public class ParseLog 12 | { 13 | StreamWriter sw; 14 | bool hadErrors; 15 | 16 | public static void Write(ParseCache cache, string outputLog) 17 | { 18 | var ms = new MemoryStream(32000); 19 | 20 | var pl = new ParseLog(ms); 21 | 22 | pl.Write(cache); 23 | 24 | if (File.Exists(outputLog)) 25 | File.Delete(outputLog); 26 | 27 | File.WriteAllBytes(outputLog, ms.ToArray()); 28 | ms.Close(); 29 | } 30 | 31 | public ParseLog(Stream s) 32 | { 33 | sw = new StreamWriter(s, System.Text.Encoding.Unicode) { AutoFlush=false }; 34 | } 35 | 36 | ~ParseLog() 37 | { 38 | sw.Close(); 39 | } 40 | 41 | public void Write(ParseCache cache) 42 | { 43 | sw.WriteLine("Parser error log"); 44 | sw.WriteLine(); 45 | 46 | Write(cache.Root); 47 | 48 | if(!hadErrors) 49 | sw.WriteLine("No errors found."); 50 | 51 | sw.WriteLine(); 52 | sw.Flush(); 53 | } 54 | 55 | void Write(ModulePackage package) 56 | { 57 | foreach (var kv in package.Modules) 58 | { 59 | if (kv.Value.ParseErrors.Count < 1) 60 | continue; 61 | hadErrors = true; 62 | sw.WriteLine(kv.Value.ModuleName + "\t\t(" + kv.Value.FileName + ")"); 63 | foreach (var err in kv.Value.ParseErrors) 64 | sw.WriteLine(string.Format("\t\t{0}\t{1}\t{2}", err.Location.Line, err.Location.Column, err.Message)); 65 | 66 | sw.WriteLine(); 67 | } 68 | 69 | sw.Flush(); 70 | 71 | foreach (var kv in package.Packages) 72 | Write(kv.Value); 73 | } 74 | } 75 | #endif 76 | } 77 | -------------------------------------------------------------------------------- /DParser2/Misc/StringCache.cs: -------------------------------------------------------------------------------- 1 | // 2 | // StringCache.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System; 27 | using System.Collections.Generic; 28 | 29 | namespace D_Parser 30 | { 31 | public static class Strings 32 | { 33 | static readonly Dictionary Table = new Dictionary(); 34 | 35 | public static void Add(string s) 36 | { 37 | if (!string.IsNullOrEmpty (s)) { 38 | var hash = s.GetHashCode (); 39 | lock (Table) 40 | { 41 | Table [hash] = s; 42 | } 43 | } 44 | } 45 | 46 | public static string TryGet(int hash) 47 | { 48 | if (hash != 0) 49 | { 50 | string s; 51 | Table.TryGetValue(hash, out s); 52 | return s; 53 | } 54 | return string.Empty; 55 | } 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /DParser2/Misc/StringExtension.cs: -------------------------------------------------------------------------------- 1 | // 2 | // StringExtension.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System.Text; 27 | 28 | namespace D_Parser 29 | { 30 | public static class StringExtension 31 | { 32 | public static StringBuilder Trim(this StringBuilder sb) 33 | { 34 | int trimLen = 0; 35 | for (;trimLen < sb.Length && char.IsWhiteSpace (sb [trimLen]); trimLen++); 36 | if(trimLen != 0) 37 | sb.Remove (0, trimLen); 38 | 39 | if (sb.Length != 0) { 40 | for (trimLen = sb.Length - 1; trimLen >= 0 && char.IsWhiteSpace (sb [trimLen]); trimLen--); 41 | sb.Length = trimLen + 1; 42 | } 43 | return sb; 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /DParser2/Misc/StringView.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace D_Parser.Misc 4 | { 5 | /// 6 | /// Read substrings out of a string without copying it. 7 | /// 8 | public class StringView : TextReader 9 | { 10 | string s; 11 | int i; 12 | int end; 13 | 14 | public StringView(string code, int begin, int length) 15 | { 16 | if (code == null) 17 | throw new System.ArgumentNullException ("code", "String must not be null"); 18 | if (begin < 0) 19 | throw new System.ArgumentOutOfRangeException ("begin", "Start offset must not be negative"); 20 | if (length < 0) 21 | throw new System.ArgumentOutOfRangeException ("length", "Length must not be negative"); 22 | if (begin + length > code.Length) 23 | throw new System.ArgumentOutOfRangeException ("length", "End offset must not be larger than string's length"); 24 | 25 | s = code; 26 | i = begin; 27 | end = begin + length; 28 | } 29 | 30 | public override int Peek() 31 | { 32 | if (i >= end) 33 | return -1; 34 | return s[i]; 35 | } 36 | 37 | public override int Read() 38 | { 39 | if (i >= end) 40 | return -1; 41 | return s[i++]; 42 | } 43 | 44 | public override int ReadBlock(char[] buffer, int index, int count) 45 | { 46 | int copied = System.Math.Min(s.Length - i, count - index); //TODO: Is this correct? 47 | s.CopyTo(i, buffer, index, copied); 48 | return copied; 49 | } 50 | 51 | public override int Read(char[] buffer, int index, int count) 52 | { 53 | return this.ReadBlock(buffer, index, count); 54 | } 55 | 56 | public override string ReadLine() 57 | { 58 | int start = i; 59 | 60 | while (i <= end && s[i] != '\n') 61 | i++; 62 | 63 | if (i <= end) // There had to be a \n 64 | i++; 65 | 66 | return s.Substring(start, end - i); 67 | } 68 | 69 | public override string ReadToEnd() 70 | { 71 | var s_ = s.Substring(i); 72 | i = end; 73 | return s_; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /DParser2/Parser/Implementations/DParserParts.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Parser.Implementations 2 | { 3 | class DParserParts 4 | { 5 | public readonly DAttributesParser attributesParser; 6 | public readonly DBodiedSymbolsParser bodiedSymbolsParser; 7 | public readonly DDeclarationParser declarationParser; 8 | public readonly DExpressionsParser expressionsParser; 9 | public readonly DModulesParser modulesParser; 10 | public readonly DStatementParser statementParser; 11 | public readonly DTemplatesParser templatesParser; 12 | 13 | public DParserParts(DParserStateContext stateContext) 14 | { 15 | attributesParser = new DAttributesParser(stateContext, this); 16 | bodiedSymbolsParser = new DBodiedSymbolsParser(stateContext, this); 17 | declarationParser = new DDeclarationParser(stateContext, this); 18 | modulesParser = new DModulesParser(stateContext, this); 19 | statementParser = new DStatementParser(stateContext, this); 20 | expressionsParser = new DExpressionsParser(stateContext, this); 21 | templatesParser = new DTemplatesParser(stateContext, this); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DParser2/Parser/Implementations/DParserStateContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using D_Parser.Dom; 7 | 8 | namespace D_Parser.Parser.Implementations 9 | { 10 | internal class DParserStateContext : IDisposable 11 | { 12 | /// 13 | /// Modifiers for entire block 14 | /// 15 | public Stack BlockAttributes = new Stack(); 16 | /// 17 | /// Modifiers for current expression only 18 | /// 19 | public Stack DeclarationAttributes = new Stack(); 20 | 21 | public bool ParseStructureOnly = false; 22 | public Lexer Lexer; 23 | 24 | public readonly List Comments = new List(); 25 | public StringBuilder PreviousComment = new StringBuilder(); 26 | 27 | DToken _backupt = new DToken(); 28 | public DToken t 29 | { 30 | [System.Diagnostics.DebuggerStepThrough] 31 | get 32 | { 33 | return Lexer.CurrentToken ?? _backupt; 34 | } 35 | } 36 | 37 | /// 38 | /// Used if the parser is unsure if there's a type or an expression - then, instead of throwing exceptions, the Type()-Methods will simply return null; 39 | /// 40 | public bool AllowWeakTypeParsing = false; 41 | 42 | public List ParseErrors = new List(); 43 | public const int MaxParseErrorsBeforeFailure = 100; 44 | 45 | public DParserStateContext(Lexer lexer) 46 | { 47 | this.Lexer = lexer; 48 | Lexer.LexerErrors = ParseErrors; 49 | } 50 | 51 | public void Dispose() 52 | { 53 | BlockAttributes.Clear(); 54 | BlockAttributes = null; 55 | DeclarationAttributes.Clear(); 56 | DeclarationAttributes = null; 57 | Lexer.Dispose(); 58 | Lexer = null; 59 | ParseErrors = null; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /DParser2/Parser/Tokens/Comment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | using D_Parser.Dom; 4 | 5 | namespace D_Parser.Parser 6 | { 7 | public sealed class Comment 8 | { 9 | [Flags] 10 | public enum Type : byte 11 | { 12 | Block = 1, 13 | SingleLine = 2, 14 | Documentation = 4 15 | } 16 | 17 | public readonly Type CommentType; 18 | public readonly string CommentText; 19 | public readonly CodeLocation StartPosition; 20 | public readonly CodeLocation EndPosition; 21 | 22 | /// 23 | /// Is true, when the comment is at line start or only whitespaces 24 | /// between line and comment start. 25 | /// 26 | public readonly bool CommentStartsLine; 27 | 28 | public Comment(Type commentType, string comment, bool commentStartsLine, CodeLocation startPosition, CodeLocation endPosition) 29 | { 30 | this.CommentType = commentType; 31 | this.CommentText = comment; 32 | this.CommentStartsLine = commentStartsLine; 33 | this.StartPosition = startPosition; 34 | this.EndPosition = endPosition; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /DParser2/Parser/Tokens/DToken.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Parser 4 | { 5 | public sealed class DToken 6 | { 7 | public int Line; 8 | internal int Column; 9 | internal ushort EndLineDifference; // A token shouldn't be greater than 65536, right? 10 | internal int EndColumn; 11 | public CodeLocation Location 12 | { 13 | get { return new CodeLocation(Column, Line); } 14 | } 15 | public CodeLocation EndLocation 16 | { 17 | get { return new CodeLocation(EndColumn, unchecked(Line + EndLineDifference)); } 18 | } 19 | 20 | public byte Kind; 21 | public LiteralFormat LiteralFormat; 22 | /// 23 | /// Used for scalar, floating and string literals. 24 | /// Marks special formats such as explicit unsigned-ness, wide char or dchar-based strings etc. 25 | /// 26 | public LiteralSubformat Subformat; 27 | public object LiteralValue; 28 | //public readonly string Value; 29 | public string Value { get { return LiteralValue as string; } } 30 | internal string RawCodeRepresentation; 31 | internal DToken next; 32 | 33 | public DToken Next 34 | { 35 | get { return next; } 36 | } 37 | 38 | public override string ToString() 39 | { 40 | if (Kind == DTokens.Identifier || Kind == DTokens.Literal) 41 | return LiteralValue is string ? LiteralValue as string : LiteralValue.ToString(); 42 | return DTokens.GetTokenString(Kind); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /DParser2/Parser/Tokens/LiteralFormat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Parser 4 | { 5 | [Flags] 6 | public enum LiteralFormat : byte 7 | { 8 | None = 0, 9 | Scalar = 1, 10 | FloatingPoint = 2, 11 | StringLiteral = 4, 12 | VerbatimStringLiteral = 8, 13 | CharLiteral = 16, 14 | } 15 | } -------------------------------------------------------------------------------- /DParser2/Parser/Tokens/LiteralSubformat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Parser 4 | { 5 | [Flags] 6 | public enum LiteralSubformat : byte 7 | { 8 | None = 0, 9 | 10 | Integer = 1, 11 | Unsigned = 2, 12 | Long = 4, 13 | 14 | Double = 8, 15 | Float = 16, 16 | Real = 32, 17 | Imaginary = 64, 18 | 19 | Utf8 = 128, 20 | Utf16 = 129, 21 | Utf32 = 130, 22 | } 23 | } -------------------------------------------------------------------------------- /DParser2/Refactoring/ITextDocument.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace D_Parser.Refactoring 7 | { 8 | public interface ITextDocument 9 | { 10 | int LocationToOffset(int line, int col); 11 | int OffsetToLineNumber(int offset); 12 | string EolMarker { get; } 13 | string GetLineIndent(int line); 14 | 15 | int Length { get; } 16 | char GetCharAt(int offset); 17 | void Remove(int offset, int length); 18 | void Insert(int offset, string text); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/ItemCheckParameters.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Resolver.ASTScanner 2 | { 3 | public struct ItemCheckParameters 4 | { 5 | public ItemCheckParameters(ItemCheckParameters essentialThingsToCopy) 6 | { 7 | VisibleMembers = essentialThingsToCopy.VisibleMembers; 8 | resolvedCurScope = essentialThingsToCopy.resolvedCurScope; 9 | dontHandleTemplateParamsInNodeScan = essentialThingsToCopy.dontHandleTemplateParamsInNodeScan; 10 | 11 | publicImportsOnly = false; 12 | isBaseClass = false; 13 | isMixinAst = false; 14 | takeStaticChildrenOnly = false; 15 | scopeIsInInheritanceHierarchy = false; 16 | } 17 | 18 | public ItemCheckParameters(MemberFilter vis) { 19 | VisibleMembers = vis; 20 | publicImportsOnly = false; 21 | isBaseClass = false; 22 | isMixinAst = false; 23 | takeStaticChildrenOnly = false; 24 | scopeIsInInheritanceHierarchy = false; 25 | resolvedCurScope = null; 26 | dontHandleTemplateParamsInNodeScan = false; 27 | } 28 | 29 | public MemberFilter VisibleMembers; 30 | public bool publicImportsOnly; 31 | public bool isBaseClass; 32 | public bool isMixinAst; 33 | public bool takeStaticChildrenOnly; 34 | public bool scopeIsInInheritanceHierarchy; 35 | public DSymbol resolvedCurScope; 36 | 37 | /// 38 | /// Temporary flag that is used for telling scanChildren() not to handle template parameters. 39 | /// Used to prevent the insertion of a template mixin's parameter set into the completion list etc. 40 | /// 41 | public bool dontHandleTemplateParamsInNodeScan; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/ItemEnumeration.cs: -------------------------------------------------------------------------------- 1 | // 2 | // ItemEnumeration.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System.Collections.Generic; 27 | using D_Parser.Dom; 28 | 29 | namespace D_Parser.Resolver.ASTScanner 30 | { 31 | class ItemEnumeration : AbstractVisitor 32 | { 33 | protected ItemEnumeration (ResolutionContext ctxt) : base (ctxt) 34 | { 35 | } 36 | 37 | public static List EnumScopedBlockChildren (ResolutionContext ctxt,MemberFilter VisibleMembers) 38 | { 39 | var en = new ItemEnumeration (ctxt); 40 | 41 | en.ScanBlock(ctxt.ScopedBlock, ctxt.ScopedBlock.EndLocation, new ItemCheckParameters(VisibleMembers)); 42 | 43 | return en.Nodes; 44 | } 45 | 46 | public static List EnumChildren(UserDefinedType ds,ResolutionContext ctxt, MemberFilter VisibleMembers) 47 | { 48 | var en = new ItemEnumeration(ctxt); 49 | 50 | en.DeepScanClass(ds, new ItemCheckParameters(VisibleMembers)); 51 | 52 | return en.Nodes; 53 | } 54 | 55 | List Nodes = new List (); 56 | 57 | protected override void HandleItem (INode n, AbstractType resolvedCurrentScope) 58 | { 59 | Nodes.Add (n); 60 | } 61 | 62 | protected override void HandleItem(PackageSymbol pack) { } 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/MemberFilter.cs: -------------------------------------------------------------------------------- 1 | // 2 | // MemberFilter.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System; 27 | 28 | namespace D_Parser.Resolver.ASTScanner 29 | { 30 | /// 31 | /// A whitelisting filter for members to show in completion menus. 32 | /// 33 | [Flags] 34 | public enum MemberFilter 35 | { 36 | None = 0, 37 | Variables = 1, 38 | Methods = 1 << 2, 39 | Classes = 1 << 3, 40 | Interfaces = 1 << 4, 41 | Templates = 1 << 5, 42 | StructsAndUnions = 1 << 6, 43 | Enums = 1 << 7, 44 | // 1 << 8 -- see BlockKeywords 45 | TypeParameters = 1 << 9, 46 | Labels = 1 << 10, 47 | x86Registers = 1 << 11, 48 | x64Registers = 1 << 12, 49 | BuiltInPropertyAttributes = 1 << 13, 50 | 51 | BlockKeywords = 1 << 8, // class, uint, __gshared, [static] if, (body,in,out) 52 | StatementBlockKeywords = 1 << 14, // for, if, 53 | ExpressionKeywords = 1 << 15, // __LINE__, true, base, this 54 | 55 | 56 | Registers = x86Registers | x64Registers, 57 | Types = Classes | Interfaces | Templates | StructsAndUnions, 58 | All = Variables | Methods | Types | Enums | TypeParameters 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/SingleNodeNameScan.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using System.Collections.Generic; 3 | 4 | namespace D_Parser.Resolver.ASTScanner 5 | { 6 | sealed class SingleNodeNameScan : NameScan 7 | { 8 | SingleNodeNameScan(ResolutionContext ctxt, int filterHash, ISyntaxRegion idObject) : base(ctxt, filterHash, idObject) {} 9 | 10 | /// 11 | /// Scans a block node. Not working with DMethods. 12 | /// Automatically resolves node matches so base types etc. will be specified directly after the search operation. 13 | /// 14 | public static List SearchChildrenAndResolve(ResolutionContext ctxt, DSymbol t, int nameHash, ISyntaxRegion idObject = null) 15 | { 16 | var scan = new SingleNodeNameScan(ctxt, nameHash, idObject); 17 | var parms = new ItemCheckParameters(MemberFilter.All); 18 | 19 | if (t is TemplateIntermediateType) 20 | scan.DeepScanClass(t as UserDefinedType, parms); 21 | else if (t.Definition is IBlockNode) 22 | scan.ScanBlock(t.Definition as IBlockNode, CodeLocation.Empty, parms); 23 | 24 | return scan.GetMatches(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/Util/IndexKeyExtendingEnumerable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using D_Parser.Resolver.ExpressionSemantics; 4 | 5 | namespace D_Parser.Resolver.ASTScanner.Util 6 | { 7 | class IndexKeyExtendingEnumerable : IEnumerable> 8 | { 9 | readonly IEnumerable values; 10 | 11 | public IndexKeyExtendingEnumerable(IEnumerable values) 12 | { 13 | this.values = values; 14 | } 15 | 16 | class IndexKeyExtendingEnumerator : IEnumerator> 17 | { 18 | readonly IEnumerator values; 19 | int index; 20 | 21 | public IndexKeyExtendingEnumerator(IEnumerator values) 22 | { 23 | this.values = values; 24 | Reset(); 25 | } 26 | 27 | public KeyValuePair Current { get; private set; } 28 | object IEnumerator.Current => Current; 29 | 30 | public void Dispose() { values.Dispose(); } 31 | 32 | public bool MoveNext() 33 | { 34 | if (!values.MoveNext()) 35 | return false; 36 | 37 | Current = new KeyValuePair(new PrimitiveValue(index), values.Current); 38 | index++; 39 | return true; 40 | } 41 | 42 | public void Reset() 43 | { 44 | values.Reset(); 45 | Current = new KeyValuePair(); 46 | } 47 | } 48 | 49 | public IEnumerator> GetEnumerator() 50 | => new IndexKeyExtendingEnumerator(values.GetEnumerator()); 51 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/Util/IotaEnumerable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using D_Parser.Resolver.ExpressionSemantics; 4 | 5 | namespace D_Parser.Resolver.ASTScanner.Util 6 | { 7 | class IotaEnumerable : IEnumerable> 8 | { 9 | readonly PrimitiveType primitiveType; 10 | readonly decimal lower, upper; 11 | 12 | public IotaEnumerable(PrimitiveType primitiveType, decimal lower, decimal upper) 13 | { 14 | this.primitiveType = primitiveType; 15 | this.lower = lower; 16 | this.upper = upper; 17 | } 18 | 19 | class IotaEnumerator : IEnumerator> 20 | { 21 | readonly decimal lower, upper; 22 | readonly PrimitiveType primitiveType; 23 | 24 | decimal currentIteration; 25 | 26 | 27 | public IotaEnumerator(PrimitiveType primitiveType, decimal lower, decimal upper) 28 | { 29 | this.lower = lower; 30 | this.primitiveType = primitiveType; 31 | this.upper = upper; 32 | 33 | Reset(); 34 | } 35 | 36 | public KeyValuePair Current { get; private set; } 37 | object IEnumerator.Current => Current; 38 | 39 | public void Dispose() { } 40 | 41 | public bool MoveNext() 42 | { 43 | if (currentIteration > upper) 44 | return false; 45 | 46 | var v = new PrimitiveValue(currentIteration, primitiveType); 47 | Current = new KeyValuePair(v, v); 48 | currentIteration++; 49 | return true; 50 | } 51 | 52 | public void Reset() 53 | { 54 | currentIteration = lower; 55 | Current = new KeyValuePair(); 56 | } 57 | } 58 | 59 | public IEnumerator> GetEnumerator() => new IotaEnumerator(primitiveType, lower, upper); 60 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /DParser2/Resolver/ASTScanner/Util/StringCharValuesEnumerable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using D_Parser.Resolver.ExpressionSemantics; 5 | 6 | namespace D_Parser.Resolver.ASTScanner.Util 7 | { 8 | class StringCharValuesEnumerable : IEnumerable 9 | { 10 | private readonly PrimitiveType charType; 11 | private readonly String enumeratee; 12 | 13 | public StringCharValuesEnumerable(PrimitiveType charType, String enumeratee) 14 | { 15 | this.charType = charType; 16 | this.enumeratee = enumeratee; 17 | } 18 | 19 | class ValuesEnumerator : IEnumerator 20 | { 21 | private readonly PrimitiveType charType; 22 | private readonly String enumeratee; 23 | int index; 24 | 25 | public ValuesEnumerator(PrimitiveType charType, String enumeratee) 26 | { 27 | this.charType = charType; 28 | this.enumeratee = enumeratee; 29 | Reset(); 30 | } 31 | 32 | public ISymbolValue Current { get; private set; } 33 | 34 | object IEnumerator.Current => Current; 35 | 36 | public void Dispose() { } 37 | 38 | public bool MoveNext() 39 | { 40 | if (index > enumeratee.Length) 41 | return false; 42 | 43 | Current = new PrimitiveValue(enumeratee[index], charType); 44 | index++; 45 | return true; 46 | } 47 | 48 | public void Reset() 49 | { 50 | index = 0; 51 | Current = null; 52 | } 53 | } 54 | 55 | public IEnumerator GetEnumerator() 56 | { 57 | throw new NotImplementedException(); 58 | } 59 | 60 | IEnumerator IEnumerable.GetEnumerator() 61 | { 62 | throw new NotImplementedException(); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /DParser2/Resolver/Caching/ExpressionCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Dom.Expressions; 4 | using D_Parser.Resolver.ExpressionSemantics; 5 | 6 | namespace D_Parser.Resolver 7 | { 8 | class ExpressionCache 9 | { 10 | #region Properties 11 | readonly ResolutionCache cache; 12 | #endregion 13 | 14 | public ExpressionCache(ResolutionContext ctxt) 15 | { 16 | cache = new ResolutionCache (ctxt); 17 | } 18 | 19 | #region Querying 20 | 21 | public ISymbolValue TryGetValue (IExpression x, params ISymbolValue[] argumentValues) 22 | { 23 | if (x == null) 24 | return null; 25 | 26 | var hashVis = D_Parser.Dom.Visitors.AstElementHashingVisitor.Instance; 27 | 28 | long hash = 0; 29 | 30 | foreach (var arg in argumentValues) 31 | unchecked { 32 | hash += arg.Accept (hashVis); 33 | } 34 | 35 | return cache.TryGetType (x, hash); 36 | } 37 | 38 | public void Add (IExpression x, ISymbolValue v, params ISymbolValue[] argumentValues) 39 | { 40 | if (x == null || v == null) 41 | return; 42 | 43 | var hashVis = D_Parser.Dom.Visitors.AstElementHashingVisitor.Instance; 44 | 45 | long hash = 0; 46 | 47 | foreach (var arg in argumentValues) 48 | unchecked { 49 | hash += arg.Accept (hashVis); 50 | } 51 | 52 | cache.Add (v, x, hash); 53 | } 54 | 55 | #endregion 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /DParser2/Resolver/Caching/ResolutionCache.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using D_Parser.Dom; 6 | using D_Parser.Dom.Visitors; 7 | using D_Parser.Misc; 8 | using D_Parser.Resolver.TypeResolution; 9 | 10 | namespace D_Parser.Resolver 11 | { 12 | class ResolutionCache 13 | { 14 | class CacheEntryDict : Dictionary { 15 | public T TryGetValue(ResolutionContext ctxt, long hashBias) 16 | { 17 | T t; 18 | Int64 d = unchecked(GetTemplateParamHash(ctxt) + hashBias); 19 | TryGetValue(d, out t); 20 | return t; 21 | } 22 | 23 | public void Add(ResolutionContext ctxt, T t, long hashBias) 24 | { 25 | Int64 d = unchecked(GetTemplateParamHash(ctxt) + hashBias); 26 | this[d] = t; 27 | } 28 | 29 | public bool Remove(ResolutionContext ctxt, long hashBias) 30 | { 31 | Int64 d = unchecked(GetTemplateParamHash(ctxt) + hashBias); 32 | return Remove(d); 33 | } 34 | 35 | static long GetTemplateParamHash(ResolutionContext ctxt) 36 | { 37 | var tpm = new List(); 38 | var hashVis = AstElementHashingVisitor.Instance; 39 | var h = ctxt.ScopedBlock == null ? 1 : ASTSearchHelper.SearchBlockAt(ctxt.ScopedBlock, ctxt.CurrentContext.Caret).Accept(hashVis); 40 | foreach (var tps in ctxt.DeducedTypesInHierarchy) 41 | { 42 | if (tps == null || tpm.Contains(tps.Parameter)) 43 | continue; 44 | 45 | h += tps.Accept(hashVis); 46 | tpm.Add(tps.Parameter); 47 | } 48 | return h; 49 | } 50 | } 51 | 52 | readonly Dictionary cache = new Dictionary(); 53 | public readonly ResolutionContext ctxt; 54 | 55 | public ResolutionCache(ResolutionContext ctxt) { 56 | this.ctxt = ctxt; 57 | } 58 | 59 | public T TryGetType(ISyntaxRegion sr, long hashBias = 0) 60 | { 61 | CacheEntryDict ce; 62 | return sr != null && cache.TryGetValue(sr, out ce) ? ce.TryGetValue(ctxt, hashBias) : default(T); 63 | } 64 | 65 | public void Add(T t, ISyntaxRegion sr, long hashBias = 0) 66 | { 67 | if (t == null || sr == null || !ctxt.CompletionOptions.EnableResolutionCache) 68 | return; 69 | 70 | CacheEntryDict ce; 71 | if (!cache.TryGetValue(sr, out ce)) 72 | cache[sr] = ce = new CacheEntryDict(); 73 | 74 | ce.Add(ctxt, t, hashBias); 75 | } 76 | 77 | public bool Remove(ISyntaxRegion sr, long hashBias = 0) 78 | { 79 | CacheEntryDict ce; 80 | return cache.TryGetValue(sr, out ce) && ce.Remove(ctxt, hashBias); 81 | } 82 | 83 | public void Clear() 84 | { 85 | cache.Clear (); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/Evaluation.TypeidExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Dom.Expressions; 3 | using D_Parser.Dom; 4 | using D_Parser.Resolver; 5 | using D_Parser.Resolver.TypeResolution; 6 | namespace D_Parser.Resolver.ExpressionSemantics 7 | { 8 | public partial class Evaluation 9 | { 10 | public ISymbolValue Visit(TypeidExpression tid) 11 | { 12 | /* 13 | * Depending on what's given as argument, it's needed to find out what kind of TypeInfo_ class to return 14 | * AND to fill it with all required information. 15 | * 16 | * http://dlang.org/phobos/object.html#TypeInfo 17 | */ 18 | throw new NotImplementedException("TypeInfo creation not supported yet"); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/EvaluationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using D_Parser.Dom; 5 | using D_Parser.Dom.Expressions; 6 | 7 | namespace D_Parser.Resolver 8 | { 9 | public class DParserException : Exception { 10 | public readonly string Module; 11 | public readonly ISyntaxRegion Location; 12 | 13 | public DParserException(ISyntaxRegion x,string Msg) :base(Msg) { 14 | Location = x; 15 | } 16 | 17 | public override string ToString() 18 | { 19 | return (Location != null ? (Location + ":") : "") + base.ToString(); 20 | } 21 | } 22 | 23 | public class ResolutionException : DParserException 24 | { 25 | public ISemantic[] LastSubResults { get; protected set; } 26 | 27 | public ResolutionException(ISyntaxRegion ObjToResolve, string Message, IEnumerable LastSubresults) 28 | : base(ObjToResolve,Message) 29 | { 30 | this.LastSubResults = LastSubresults.ToArray(); 31 | } 32 | 33 | public ResolutionException(ISyntaxRegion ObjToResolve, string Message, params ISemantic[] LastSubresult) 34 | : base(ObjToResolve,Message) 35 | { 36 | this.LastSubResults = LastSubresult; 37 | } 38 | } 39 | 40 | public class EvaluationException : ResolutionException 41 | { 42 | public IExpression EvaluatedExpression 43 | { 44 | get { return Location as IExpression; } 45 | } 46 | 47 | public EvaluationException(IExpression x,string Message, params ISemantic[] LastSubresults) 48 | : base(x, Message, LastSubresults) 49 | { } 50 | 51 | public EvaluationException(string Message, IEnumerable LastSubresults) 52 | : base(null, Message, LastSubresults) { } 53 | 54 | public EvaluationException(string Message, params ISemantic[] LastSubresults) 55 | : base(null, Message, LastSubresults) 56 | { } 57 | } 58 | 59 | public class NoConstException : EvaluationException 60 | { 61 | public NoConstException(IExpression x) : base(x, "Expression must resolve to constant value") { } 62 | } 63 | 64 | public class InvalidStringException : EvaluationException 65 | { 66 | public InvalidStringException(IExpression x) : base(x, "Expression must be a valid string") { } 67 | } 68 | 69 | public class AssertException : EvaluationException 70 | { 71 | public AssertException(AssertExpression ae, string optAssertMessage="") : base(ae, "Assert returned false. " + optAssertMessage) { } 72 | } 73 | 74 | public class WrongEvaluationArgException : Exception 75 | { 76 | public WrongEvaluationArgException() : base("Wrong argument type for expression evaluation given") {} 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/Exceptions/CtfeException.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom.Expressions; 2 | 3 | namespace D_Parser.Resolver.ExpressionSemantics.CTFE 4 | { 5 | public class CtfeException : EvaluationException 6 | { 7 | public CtfeException(IExpression x, string Message, params ISemantic[] LastSubresults) 8 | : base(x, Message, LastSubresults) 9 | { 10 | } 11 | 12 | public CtfeException(string Message, params ISemantic[] LastSubresults) 13 | : base(Message, LastSubresults) 14 | { 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/Exceptions/EvaluationStackOverflowException.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Resolver.ExpressionSemantics.Exceptions 2 | { 3 | public class EvaluationStackOverflowException : EvaluationException 4 | { 5 | public EvaluationStackOverflowException(string Message, params ISemantic[] LastSubresults) 6 | : base(Message, LastSubresults) 7 | { 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/Exceptions/VariableNotInitializedException.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Resolver.ExpressionSemantics.Exceptions 2 | { 3 | public class VariableNotInitializedException : EvaluationException 4 | { 5 | public VariableNotInitializedException(string Message, params ISemantic[] LastSubresults) : base(Message, LastSubresults) 6 | { 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/ISymbolValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using D_Parser.Resolver; 3 | using D_Parser.Dom; 4 | 5 | namespace D_Parser.Resolver.ExpressionSemantics 6 | { 7 | public interface ISymbolValue : IEquatable, ISemantic, IVisitable 8 | { 9 | AbstractType RepresentedType { get; } 10 | R Accept(ISymbolValueVisitor v); 11 | } 12 | 13 | public abstract class ExpressionValue : ISymbolValue 14 | { 15 | protected ExpressionValue(AbstractType RepresentedType) 16 | { 17 | this.RepresentedType = RepresentedType; 18 | } 19 | 20 | public virtual AbstractType RepresentedType 21 | { 22 | get; 23 | private set; 24 | } 25 | 26 | public virtual bool Equals(ISymbolValue other) 27 | { 28 | return SymbolValueComparer.IsEqual(this, other); 29 | } 30 | 31 | public abstract string ToCode(); 32 | 33 | public override string ToString() 34 | { 35 | try 36 | { 37 | return ToCode(); 38 | } 39 | catch 40 | { 41 | return null; 42 | } 43 | } 44 | 45 | public abstract void Accept(ISymbolValueVisitor vis); 46 | public abstract R Accept(ISymbolValueVisitor vis); 47 | } 48 | 49 | public class ErrorValue : ISymbolValue 50 | { 51 | public readonly EvaluationException[] Errors; 52 | 53 | public ErrorValue(params EvaluationException[] errors) 54 | { 55 | this.Errors = errors; 56 | } 57 | 58 | public string ToCode () 59 | { 60 | return ToString(); 61 | } 62 | public bool Equals (ISymbolValue other) 63 | { 64 | return false; 65 | } 66 | public AbstractType RepresentedType { 67 | get{return null;} 68 | } 69 | 70 | public override string ToString () 71 | { 72 | var sb = new System.Text.StringBuilder (); 73 | 74 | sb.AppendLine ("Evaluation errors:"); 75 | 76 | foreach (var err in Errors) { 77 | sb.AppendLine(err.ToString()); 78 | } 79 | 80 | return sb.ToString().TrimEnd(); 81 | } 82 | 83 | public void Accept(ISymbolValueVisitor vis) 84 | { 85 | vis.VisitErrorValue(this); 86 | } 87 | 88 | public R Accept(ISymbolValueVisitor vis) 89 | { 90 | return vis.VisitErrorValue(this); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/ISymbolValueVisitor.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Resolver.ExpressionSemantics 4 | { 5 | public interface ISymbolValueVisitor : IVisitor 6 | { 7 | void VisitErrorValue(ErrorValue v); 8 | void VisitPrimitiveValue(PrimitiveValue v); 9 | void VisitVoidValue(VoidValue v); 10 | void VisitArrayValue(ArrayValue v); 11 | void VisitAssociativeArrayValue(AssociativeArrayValue v); 12 | void VisitDelegateValue(DelegateValue v); 13 | void VisitNullValue(NullValue v); 14 | void VisitTypeOverloadValue(InternalOverloadValue v); 15 | void VisitVariableValue(VariableValue v); 16 | void VisitTypeValue(TypeValue v); 17 | void VisitDTuple(DTuple tuple); 18 | } 19 | 20 | public interface ISymbolValueVisitor : IVisitor 21 | { 22 | R VisitErrorValue(ErrorValue v); 23 | R VisitPrimitiveValue(PrimitiveValue v); 24 | R VisitVoidValue(VoidValue v); 25 | R VisitArrayValue(ArrayValue v); 26 | R VisitAssociativeArrayValue(AssociativeArrayValue v); 27 | R VisitDelegateValue(DelegateValue v); 28 | R VisitNullValue(NullValue v); 29 | R VisitTypeOverloadValue(InternalOverloadValue v); 30 | R VisitVariableValue(VariableValue v); 31 | R VisitTypeValue(TypeValue v); 32 | R VisitDTuple(DTuple tuple); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/LeftValues.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Resolver.ExpressionSemantics 4 | { 5 | /// 6 | /// An expression value that is allowed to have a new value assigned to as in 'a = b;' 7 | /// 8 | public abstract class LValue : ReferenceValue 9 | { 10 | protected LValue(MemberSymbol nodeType) : base(nodeType) { } 11 | } 12 | 13 | /// 14 | /// Contains a reference to a DVariable node. 15 | /// To get the actual value of the variable, use the value provider. 16 | /// 17 | public class VariableValue : LValue 18 | { 19 | public DVariable Variable {get{ return ReferencedNode as DVariable; }} 20 | 21 | public VariableValue(MemberSymbol mr) : base(mr) 22 | { } 23 | 24 | public override string ToCode() 25 | { 26 | return ReferencedNode == null ? "null" : ReferencedNode.ToString(false); 27 | } 28 | 29 | public override void Accept(ISymbolValueVisitor vis) 30 | { 31 | vis.VisitVariableValue(this); 32 | } 33 | public override R Accept(ISymbolValueVisitor vis) 34 | { 35 | return vis.VisitVariableValue(this); 36 | } 37 | } 38 | 39 | /// 40 | /// Used for accessing entries from an array. 41 | /// 42 | public class ArrayPointer : VariableValue 43 | { 44 | /// 45 | /// Used when accessing normal arrays. 46 | /// If -1, a item passed to Set() will be added instead of replaced. 47 | /// 48 | public readonly int ItemNumber; 49 | 50 | public ArrayPointer(MemberSymbol arrayVariable, int accessedItem) 51 | : base(arrayVariable) 52 | { 53 | ItemNumber = accessedItem; 54 | } 55 | 56 | /// 57 | /// Array ctor. 58 | /// 59 | /// 0 - the array's length-1; -1 when adding the item is wished. 60 | public ArrayPointer(DVariable accessedArray, ArrayType arrayType, int accessedItem) 61 | : base(new MemberSymbol(accessedArray, arrayType, null)) 62 | { 63 | ItemNumber = accessedItem; 64 | } 65 | } 66 | 67 | public class AssocArrayPointer : VariableValue 68 | { 69 | /// 70 | /// Used to identify the accessed item. 71 | /// 72 | public readonly ISymbolValue Key; 73 | 74 | public AssocArrayPointer(MemberSymbol assocArrayVariable, ISymbolValue accessedItemKey) 75 | : base(assocArrayVariable) 76 | { 77 | Key = accessedItemKey; 78 | } 79 | 80 | public AssocArrayPointer(DVariable accessedArray, AssocArrayType arrayType, ISymbolValue accessedItemKey) 81 | : base(new MemberSymbol(accessedArray, arrayType,null)) 82 | { 83 | Key = accessedItemKey; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/MathOperationEvaluation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace D_Parser.Resolver.ExpressionSemantics 3 | { 4 | public static class MathOperationEvaluation 5 | { 6 | public enum MathOp 7 | { 8 | Add, 9 | Sub, 10 | Mul, 11 | Div, 12 | 13 | Xor, 14 | Or, 15 | And, 16 | 17 | AndAnd, 18 | OrOr 19 | } 20 | 21 | public static bool TryCalc(object a, object b, MathOp op, out object x) 22 | { 23 | x = null; 24 | 25 | try 26 | { 27 | if (a is int) 28 | { 29 | var i1 = Convert.ToInt32(a); 30 | var i2 = Convert.ToInt32(b); 31 | 32 | switch (op) 33 | { 34 | case MathOp.Add: 35 | x = i1 + i2; 36 | break; 37 | case MathOp.Sub: 38 | x = i1 - i2; 39 | break; 40 | case MathOp.Mul: 41 | x = i1 * i2; 42 | break; 43 | case MathOp.Div: 44 | x = i1 / i2; 45 | break; 46 | 47 | case MathOp.Xor: 48 | x = i1 ^ i2; 49 | break; 50 | case MathOp.Or: 51 | x = i1 | i2; 52 | break; 53 | case MathOp.And: 54 | x = i1 & i2; 55 | break; 56 | 57 | case MathOp.AndAnd: 58 | break; 59 | case MathOp.OrOr: 60 | break; 61 | } 62 | } 63 | 64 | } 65 | catch (InvalidCastException) 66 | { 67 | return false; 68 | } 69 | 70 | return true; 71 | } 72 | 73 | #region Helpers 74 | public static bool ToBool(object value) 75 | { 76 | bool b = false; 77 | 78 | try 79 | { 80 | b = Convert.ToBoolean(value); 81 | } 82 | catch { } 83 | 84 | return b; 85 | } 86 | 87 | public static double ToDouble(object value) 88 | { 89 | double d = 0; 90 | 91 | try 92 | { 93 | d = Convert.ToDouble(value); 94 | } 95 | catch { } 96 | 97 | return d; 98 | } 99 | 100 | public static long ToLong(object value) 101 | { 102 | long d = 0; 103 | 104 | try 105 | { 106 | d = Convert.ToInt64(value); 107 | } 108 | catch { } 109 | 110 | return d; 111 | } 112 | #endregion 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/StatefulEvaluationContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using D_Parser.Dom; 4 | using D_Parser.Resolver.ExpressionSemantics.Exceptions; 5 | 6 | namespace D_Parser.Resolver.ExpressionSemantics 7 | { 8 | public class StatefulEvaluationContext 9 | { 10 | private readonly Dictionary _locals = new Dictionary(); 11 | public readonly ResolutionContext ResolutionContext; 12 | 13 | public StatefulEvaluationContext(ResolutionContext ctxt) 14 | { 15 | ResolutionContext = ctxt; 16 | } 17 | 18 | public void LogError(ISyntaxRegion involvedSyntaxObject, string msg, params ISemantic[] temporaryResults) 19 | { 20 | ResolutionContext.LogError (new EvaluationError(involvedSyntaxObject, msg, temporaryResults)); 21 | } 22 | 23 | public ISymbolValue GetLocalValue(DVariable variable) 24 | { 25 | if (_locals.TryGetValue(variable, out var content) && content != null) 26 | return content; 27 | if (variable.IsConst) 28 | return Evaluation.EvaluateValue(variable.Initializer, ResolutionContext); 29 | throw new VariableNotInitializedException("Variable " + variable.Name + " not defined"); 30 | } 31 | 32 | public void SetLocalValue(DVariable variable, ISymbolValue value) => _locals[variable] = value; 33 | 34 | public ICollection> GetAllLocals() 35 | { 36 | return _locals; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/SymbolValueComparer.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom.Expressions; 2 | namespace D_Parser.Resolver.ExpressionSemantics 3 | { 4 | public static class SymbolValueComparer 5 | { 6 | public static bool IsEqual(IExpression ex, IExpression ex2, StatefulEvaluationContext vp) 7 | { 8 | var val_x1 = Evaluation.EvaluateValue(ex, vp); 9 | var val_x2 = Evaluation.EvaluateValue(ex2, vp); 10 | 11 | //TEMPORARILY: Remove the string comparison 12 | if (val_x1 == null && val_x2 == null) 13 | return ex.ToString() == ex2.ToString(); 14 | 15 | return IsEqual(val_x1, val_x2); 16 | } 17 | 18 | public static bool IsEqual(ISymbolValue l, ISymbolValue r) 19 | { 20 | // If they are integral values or pointers, equality is defined as the bit pattern of the type matches exactly 21 | if (l is PrimitiveValue && r is PrimitiveValue) 22 | { 23 | var pv_l = (PrimitiveValue)l; 24 | var pv_r = (PrimitiveValue)r; 25 | 26 | return pv_l.Value == pv_r.Value && pv_l.ImaginaryPart == pv_r.ImaginaryPart; 27 | } 28 | 29 | else if(l is AssociativeArrayValue && r is AssociativeArrayValue) 30 | { 31 | var aa_l = l as AssociativeArrayValue; 32 | var aa_r = r as AssociativeArrayValue; 33 | 34 | if(aa_l.Elements == null || aa_l.Elements.Count == 0) 35 | return aa_r.Elements == null || aa_r.Elements.Count == 0; 36 | else if(aa_r.Elements != null && aa_r.Elements.Count == aa_l.Elements.Count) 37 | { 38 | //TODO: Check if each key of aa_l can be found somewhere in aa_r. 39 | //TODO: If respective keys are equal, check if values are equal 40 | return true; 41 | } 42 | } 43 | 44 | else if(l is ArrayValue && r is ArrayValue) 45 | { 46 | var av_l = l as ArrayValue; 47 | var av_r = r as ArrayValue; 48 | 49 | if(av_l.IsString == av_r.IsString) 50 | { 51 | if(av_l.IsString) 52 | return av_l.StringValue == av_r.StringValue; 53 | else 54 | { 55 | if(av_l.Elements == null || av_l.Elements.Length == 0) 56 | return av_r.Elements == null || av_r.Elements.Length == 0; 57 | else if(av_r.Elements != null && av_r.Elements.Length == av_l.Elements.Length) 58 | { 59 | for(int i = av_l.Elements.Length-1; i != -1; i--) 60 | { 61 | if(!IsEqual(av_l.Elements[i], av_r.Elements[i])) 62 | return false; 63 | } 64 | return true; 65 | } 66 | } 67 | } 68 | } 69 | 70 | return false; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /DParser2/Resolver/ExpressionSemantics/VariableValueEvaluation.cs: -------------------------------------------------------------------------------- 1 | namespace D_Parser.Resolver.ExpressionSemantics 2 | { 3 | public class VariableValueEvaluation 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /DParser2/Resolver/IResolvedTypeVisitor.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Resolver 4 | { 5 | public interface IResolvedTypeVisitor : IVisitor 6 | { 7 | void VisitPrimitiveType(PrimitiveType t); 8 | void VisitPointerType(PointerType t); 9 | void VisitArrayType(ArrayType t); 10 | void VisitAssocArrayType(AssocArrayType t); 11 | void VisitDelegateCallSymbol(DelegateCallSymbol t); 12 | void VisitDelegateType(DelegateType t); 13 | void VisitAliasedType(AliasedType t); 14 | void VisitEnumType(EnumType t); 15 | void VisitStructType(StructType t); 16 | void VisitUnionType(UnionType t); 17 | void VisitClassType(ClassType t); 18 | void VisitInterfaceType(InterfaceType t); 19 | void VisitTemplateType(TemplateType t); 20 | void VisitMixinTemplateType(MixinTemplateType t); 21 | void VisitEponymousTemplateType(EponymousTemplateType t); 22 | void VisitStaticProperty(StaticProperty t); 23 | void VisitMemberSymbol(MemberSymbol t); 24 | void VisitTemplateParameterSymbol(TemplateParameterSymbol t); 25 | void VisitArrayAccessSymbol(ArrayAccessSymbol t); 26 | void VisitModuleSymbol(ModuleSymbol t); 27 | void VisitPackageSymbol(PackageSymbol t); 28 | void VisitDTuple(DTuple t); 29 | 30 | void VisitUnknownType(UnknownType t); 31 | void VisitAmbigousType(AmbiguousType t); 32 | } 33 | public interface IResolvedTypeVisitor : IVisitor 34 | { 35 | R VisitPrimitiveType(PrimitiveType t); 36 | R VisitPointerType(PointerType t); 37 | R VisitArrayType(ArrayType t); 38 | R VisitAssocArrayType(AssocArrayType t); 39 | R VisitDelegateCallSymbol(DelegateCallSymbol t); 40 | R VisitDelegateType(DelegateType t); 41 | R VisitAliasedType(AliasedType t); 42 | R VisitEnumType(EnumType t); 43 | R VisitStructType(StructType t); 44 | R VisitUnionType(UnionType t); 45 | R VisitClassType(ClassType t); 46 | R VisitInterfaceType(InterfaceType t); 47 | R VisitTemplateType(TemplateType t); 48 | R VisitMixinTemplateType(MixinTemplateType t); 49 | R VisitEponymousTemplateType(EponymousTemplateType t); 50 | R VisitStaticProperty(StaticProperty t); 51 | R VisitMemberSymbol(MemberSymbol t); 52 | R VisitTemplateParameterSymbol(TemplateParameterSymbol t); 53 | R VisitArrayAccessSymbol(ArrayAccessSymbol t); 54 | R VisitModuleSymbol(ModuleSymbol t); 55 | R VisitPackageSymbol(PackageSymbol t); 56 | R VisitDTuple(DTuple t); 57 | 58 | R VisitUnknownType(UnknownType t); 59 | R VisitAmbigousType(AmbiguousType t); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /DParser2/Resolver/Model/ComplexValue.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using D_Parser.Dom; 3 | using D_Parser.Resolver.ExpressionSemantics; 4 | 5 | namespace D_Parser.Resolver.Model 6 | { 7 | public class ComplexValue : ISymbolValue 8 | { 9 | public ComplexValue(AbstractType type) 10 | { 11 | RepresentedType = type; 12 | } 13 | 14 | private readonly Dictionary _properties = new Dictionary(); 15 | 16 | public ISymbolValue GetPropertyValue(DVariable field) 17 | { 18 | return _properties[field]; 19 | } 20 | 21 | public void SetPropertyValue(DVariable field, ISymbolValue value) 22 | { 23 | _properties[field] = value; 24 | } 25 | 26 | public AbstractType RepresentedType { get; } 27 | 28 | public bool Equals(ISymbolValue other) 29 | { 30 | throw new System.NotImplementedException(); 31 | } 32 | 33 | public string ToCode() 34 | { 35 | throw new System.NotImplementedException(); 36 | } 37 | 38 | public void Accept(ISymbolValueVisitor vis) 39 | { 40 | throw new System.NotImplementedException(); 41 | } 42 | 43 | public R Accept(ISymbolValueVisitor v) 44 | { 45 | throw new System.NotImplementedException(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /DParser2/Resolver/Model/ContextFrame.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using D_Parser.Dom; 3 | using D_Parser.Resolver.Templates; 4 | 5 | namespace D_Parser.Resolver 6 | { 7 | public class ContextFrame 8 | { 9 | #region Properties 10 | ResolutionContext ctxt; 11 | public DeducedTypeDictionary DeducedTemplateParameters = new DeducedTypeDictionary(); 12 | public ResolutionOptions ContextDependentOptions = 0; 13 | IBlockNode scopedBlock; 14 | public CodeLocation Caret { get; protected set; } 15 | ConditionalCompilation.ConditionSet declarationCondititons; 16 | 17 | public IBlockNode ScopedBlock{get{ return scopedBlock; }} 18 | #endregion 19 | 20 | public ContextFrame(ResolutionContext ctxt) 21 | { 22 | this.ctxt = ctxt; 23 | } 24 | 25 | public void IntroduceTemplateParameterTypes(DSymbol tir) 26 | { 27 | if (tir != null) 28 | DeducedTemplateParameters.Add (tir.DeducedTypes); 29 | } 30 | 31 | public void RemoveParamTypesFromPreferredLocals(DSymbol tir) 32 | { 33 | if (tir != null) 34 | DeducedTemplateParameters.Remove (tir.DeducedTypes); 35 | } 36 | 37 | public void Set(CodeLocation caret) 38 | { 39 | Caret = caret; 40 | declarationCondititons = null; 41 | } 42 | 43 | public void Set(IBlockNode b) 44 | { 45 | scopedBlock = b; 46 | Caret = CodeLocation.Empty; 47 | declarationCondititons = null; 48 | } 49 | 50 | public void Set(IBlockNode b, CodeLocation caret) 51 | { 52 | scopedBlock = b; 53 | Caret = caret; 54 | declarationCondititons = null; 55 | } 56 | 57 | public ConditionalCompilation.ConditionSet DeclarationConditions 58 | { 59 | get 60 | { 61 | if(declarationCondititons == null) 62 | { 63 | declarationCondititons = new ConditionalCompilation.ConditionSet(ctxt.CompilationEnvironment); 64 | ConditionalCompilation.EnumConditions(declarationCondititons, scopedBlock, ctxt, Caret); 65 | } 66 | return declarationCondititons; 67 | } 68 | } 69 | 70 | /// 71 | /// Returns true if a node fully matches its environment concerning static if() declaration constraints and version/debug() restrictions. 72 | /// 73 | public bool MatchesDeclarationEnvironment(IEnumerable conditions) 74 | { 75 | return DeclarationConditions.IsMatching(conditions,ctxt); 76 | } 77 | 78 | public bool MatchesDeclarationEnvironment(DeclarationCondition dc) 79 | { 80 | return DeclarationConditions.IsMatching(dc,ctxt); 81 | } 82 | 83 | public override string ToString() 84 | { 85 | return scopedBlock.ToString() + " // " + Caret.ToString(); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /DParser2/Resolver/Model/ISemantic.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace D_Parser.Resolver 3 | { 4 | public interface ISemantic 5 | { 6 | string ToCode(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionError.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using D_Parser.Dom; 4 | using D_Parser.Dom.Expressions; 5 | 6 | namespace D_Parser.Resolver 7 | { 8 | public class ResolutionError 9 | { 10 | public readonly object SyntacticalContext; 11 | public readonly string Message; 12 | 13 | public ResolutionError(object syntacticalObj, string message) 14 | { 15 | this.SyntacticalContext = syntacticalObj; 16 | this.Message = message; 17 | } 18 | 19 | public override string ToString() 20 | { 21 | return Message + (SyntacticalContext != null ? ("\nContext: " + SyntacticalContext) : ""); 22 | } 23 | } 24 | 25 | public class EvaluationError : ResolutionError 26 | { 27 | public readonly ISemantic[] temporaryResults; 28 | 29 | public EvaluationError(ISyntaxRegion sr, string msg, params ISemantic[] tempRes) : base(sr, msg) 30 | { 31 | this.temporaryResults = tempRes; 32 | } 33 | } 34 | 35 | public class AmbiguityError : ResolutionError 36 | { 37 | public readonly ISemantic[] DetectedOverloads; 38 | 39 | public AmbiguityError(ISyntaxRegion syntaxObj, IEnumerable results, string msg=null) 40 | : base(syntaxObj, msg ?? "Resolution returned too many results") 41 | { 42 | if (results is ISemantic[]) 43 | this.DetectedOverloads = (ISemantic[])results; 44 | else if(results!=null) 45 | this.DetectedOverloads = results.ToArray(); 46 | } 47 | } 48 | 49 | public class NothingFoundError : ResolutionError 50 | { 51 | public NothingFoundError(ISyntaxRegion syntaxObj, string msg =null) 52 | : base(syntaxObj, msg ?? ((syntaxObj is IExpression ? "Expression" : "Declaration") + " could not be resolved.")) 53 | { } 54 | } 55 | 56 | public class TemplateParameterDeductionError : ResolutionError 57 | { 58 | public TemplateParameter Parameter { get { return SyntacticalContext as TemplateParameter; } } 59 | public readonly ISemantic Argument; 60 | 61 | public TemplateParameterDeductionError(TemplateParameter parameter, ISemantic argument, string msg) 62 | : base(parameter, msg) 63 | { 64 | this.Argument = argument; 65 | } 66 | } 67 | 68 | public class AmbigousSpecializationError : ResolutionError 69 | { 70 | public readonly AbstractType[] ComparedOverloads; 71 | 72 | public AmbigousSpecializationError(AbstractType[] comparedOverloads) 73 | : base(comparedOverloads[comparedOverloads.Length - 1], "Could not distinguish a most specialized overload. Both overloads seem to be equal.") 74 | { 75 | this.ComparedOverloads = comparedOverloads; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionHooks/HookAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace D_Parser.Resolver.ResolutionHooks 7 | { 8 | class BypassAttribute 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionHooks/HookRegistry.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using System.Collections.Generic; 3 | using System.Runtime.CompilerServices; 4 | 5 | namespace D_Parser.Resolver.ResolutionHooks 6 | { 7 | public static class HookRegistry 8 | { 9 | static readonly Dictionary DeductionHooks = new Dictionary(); 10 | 11 | /// 12 | /// For persisting the overall weakly referenced DNodes, store the containing AbstractType - and as soon as this type is getting free'd, its DNode will be either! 13 | /// 14 | static ConditionalWeakTable resultStore = new ConditionalWeakTable(); 15 | 16 | public static void AddHook(IHook hook) 17 | { 18 | DeductionHooks[hook.HookedSymbol] = hook; 19 | } 20 | 21 | public static bool RemoveHook(string hookedSymbol) 22 | { 23 | return DeductionHooks.Remove(hookedSymbol); 24 | } 25 | 26 | static HookRegistry() 27 | { 28 | AddHook (new autoimplement ()); 29 | AddHook(new TupleHook()); 30 | AddHook(new bitfields()); 31 | } 32 | 33 | public static AbstractType TryDeduce(DSymbol t, IEnumerable templateArguments, out bool supersedeOtherOverloads) 34 | { 35 | var def = t.Definition; 36 | IHook hook; 37 | if (def != null && DeductionHooks.TryGetValue(AbstractNode.GetNodePath(def, true), out hook)) 38 | { 39 | supersedeOtherOverloads = hook.SupersedesMultipleOverloads; 40 | INode n = null; 41 | var res = hook.TryDeduce(t, templateArguments, ref n); 42 | if(n != null) 43 | resultStore.Add(res, n); 44 | return res; 45 | } 46 | 47 | supersedeOtherOverloads = true; 48 | return null; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionHooks/Hooks/autoimplement.cs: -------------------------------------------------------------------------------- 1 | // 2 | // autoimplement.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2014 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System; 27 | using System.Collections.Generic; 28 | using D_Parser.Dom; 29 | using D_Parser.Resolver.TypeResolution; 30 | 31 | namespace D_Parser.Resolver.ResolutionHooks 32 | { 33 | class autoimplement : IHook 34 | { 35 | public AbstractType TryDeduce (DSymbol ds, IEnumerable templateArguments, ref INode returnedNode) 36 | { 37 | var cls= ds as ClassType; 38 | if (cls == null || templateArguments == null) 39 | return null; 40 | 41 | var en = templateArguments.GetEnumerator (); 42 | if (!en.MoveNext ()) 43 | return null; 44 | 45 | returnedNode = cls.Definition; 46 | var current = AbstractType.Get(en.Current); 47 | current = DResolver.StripAliasedTypes(current); 48 | current = DResolver.StripMemberSymbols(current); 49 | var baseClass = current as TemplateIntermediateType; 50 | 51 | if (baseClass == null) 52 | return ds; 53 | 54 | return new ClassType(cls.Definition, baseClass as ClassType, baseClass is InterfaceType ? new[] {baseClass as InterfaceType} : null, ds.DeducedTypes); 55 | } 56 | 57 | public string HookedSymbol { 58 | get { 59 | return "std.typecons.AutoImplement"; 60 | } 61 | } 62 | 63 | public bool SupersedesMultipleOverloads { 64 | get { 65 | return true; 66 | } 67 | } 68 | } 69 | } 70 | 71 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionHooks/IHook.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using D_Parser.Resolver.ExpressionSemantics; 7 | 8 | namespace D_Parser.Resolver.ResolutionHooks 9 | { 10 | public interface IHook 11 | { 12 | string HookedSymbol { get; } 13 | bool SupersedesMultipleOverloads { get; } 14 | AbstractType TryDeduce(DSymbol ds, IEnumerable templateArguments, ref INode returnedNode); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DParser2/Resolver/ResolutionOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace D_Parser.Resolver 4 | { 5 | [Flags] 6 | public enum ResolutionOptions 7 | { 8 | DontResolveAliases=1, 9 | /// 10 | /// If passed, base classes will not be resolved in any way. 11 | /// 12 | DontResolveBaseClasses= 2, 13 | /// 14 | /// If passed, variable/method return types will not be evaluated. 15 | /// 16 | DontResolveBaseTypes = 4, 17 | 18 | /// 19 | /// If set, the resolver won't filter out members by template parameter deduction. 20 | /// 21 | NoTemplateParameterDeduction= 8, 22 | 23 | ReturnMethodReferencesOnly = 16, 24 | 25 | IgnoreAllProtectionAttributes = 32, 26 | IgnoreDeclarationConditions = 64, 27 | 28 | Default = 0 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DParser2/Resolver/Templates/DeducedTypeDictionary.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | using D_Parser.Dom; 4 | 5 | namespace D_Parser.Resolver.Templates 6 | { 7 | public class DeducedTypeDictionary : Dictionary, IReadOnlyCollection { 8 | 9 | public readonly TemplateParameter[] ExpectedParameters; 10 | 11 | public DeducedTypeDictionary() { } 12 | 13 | public DeducedTypeDictionary(TemplateParameter[] parameters) 14 | { 15 | ExpectedParameters = parameters; 16 | 17 | if(parameters != null) 18 | foreach (var tp in parameters) 19 | Add(tp, null); 20 | } 21 | 22 | public DeducedTypeDictionary(DNode owner) 23 | : this(owner.TemplateParameters) 24 | {} 25 | 26 | public DeducedTypeDictionary(DSymbol ms) : this(ms.ValidSymbol ? ms.Definition.TemplateParameters : null) 27 | { 28 | Add (ms.DeducedTypes); 29 | if (ms is TemplateParameterSymbol) 30 | Remove ((ms as TemplateParameterSymbol).Parameter); 31 | } 32 | 33 | public void Remove(IEnumerable tpss) 34 | { 35 | if (tpss == null) 36 | return; 37 | 38 | foreach (var tps in tpss) 39 | if (tps != null) 40 | Remove (tps.Parameter); 41 | } 42 | 43 | public void AddDefault(IEnumerable templateParameters) 44 | { 45 | if (templateParameters == null) 46 | return; 47 | 48 | foreach (var tp in templateParameters) 49 | if (this [tp] == null) 50 | this [tp] = new TemplateParameterSymbol (tp, null); 51 | } 52 | 53 | public void Add(IEnumerable tpss) 54 | { 55 | if (tpss == null) 56 | return; 57 | 58 | foreach (var tps in tpss) 59 | if(tps != null) 60 | this [tps.Parameter] = tps; 61 | } 62 | 63 | public bool AllParamatersSatisfied 64 | { 65 | get 66 | { 67 | foreach (var kv in this) 68 | if (kv.Value == null) 69 | return false; 70 | 71 | return true; 72 | } 73 | } 74 | 75 | public override string ToString () 76 | { 77 | var sb = new StringBuilder ("DeducedTypeDict: "); 78 | 79 | foreach (var kv in this) 80 | sb.Append (kv.Key.Name).Append (": ").Append(kv.Value != null ? kv.Value.ToString() : "-").Append(','); 81 | 82 | return sb.ToString (); 83 | } 84 | 85 | IEnumerator IEnumerable.GetEnumerator () 86 | { 87 | return Values.GetEnumerator(); 88 | } 89 | 90 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () 91 | { 92 | return Values.GetEnumerator (); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /DParser2/Resolver/Templates/TemplateParameterDeduction.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | 3 | namespace D_Parser.Resolver.Templates 4 | { 5 | public class TemplateParameterDeduction 6 | { 7 | readonly TemplateParameterDeductionVisitor deductionVisitor; 8 | 9 | /// 10 | /// If true and deducing a type parameter, 11 | /// the equality of the given and expected type is required instead of their simple convertibility. 12 | /// Used when evaluating IsExpressions. 13 | /// 14 | public bool EnforceTypeEqualityWhenDeducing 15 | { 16 | get { return deductionVisitor.EnforceTypeEqualityWhenDeducing; } 17 | set { deductionVisitor.EnforceTypeEqualityWhenDeducing = value; } 18 | } 19 | 20 | [System.Diagnostics.DebuggerStepThrough] 21 | public TemplateParameterDeduction(DeducedTypeDictionary DeducedParameters, ResolutionContext ctxt) 22 | { 23 | deductionVisitor = new TemplateParameterDeductionVisitor(ctxt, DeducedParameters); 24 | } 25 | 26 | public bool Handle(TemplateParameter parameter, ISemantic argumentToAnalyze) 27 | { 28 | // Packages aren't allowed at all 29 | if (argumentToAnalyze is PackageSymbol) 30 | return false; 31 | 32 | // Module symbols can be used as alias only 33 | if (argumentToAnalyze is ModuleSymbol && 34 | !(parameter is TemplateAliasParameter)) 35 | return false; 36 | 37 | //TODO: Handle __FILE__ and __LINE__ correctly - so don't evaluate them at the template declaration but at the point of instantiation 38 | var ctxt = deductionVisitor.ctxt; 39 | 40 | /* 41 | * Introduce previously deduced parameters into current resolution context 42 | * to allow value parameter to be of e.g. type T whereas T is already set somewhere before 43 | */ 44 | DeducedTypeDictionary _prefLocalsBackup = null; 45 | if (ctxt != null && ctxt.CurrentContext != null) 46 | { 47 | _prefLocalsBackup = ctxt.CurrentContext.DeducedTemplateParameters; 48 | 49 | var d = new DeducedTypeDictionary(); 50 | foreach (var kv in deductionVisitor.TargetDictionary) 51 | if (kv.Value != null) 52 | d[kv.Key] = kv.Value; 53 | ctxt.CurrentContext.DeducedTemplateParameters = d; 54 | } 55 | 56 | try 57 | { 58 | return parameter.Accept(deductionVisitor, argumentToAnalyze); 59 | } 60 | finally 61 | { 62 | if (ctxt != null && ctxt.CurrentContext != null) 63 | ctxt.CurrentContext.DeducedTemplateParameters = _prefLocalsBackup; 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /ExaustiveCompletionTester/CompletionFacilities.cs: -------------------------------------------------------------------------------- 1 | using D_Parser; 2 | using D_Parser.Completion; 3 | using D_Parser.Dom; 4 | using D_Parser.Misc; 5 | using D_Parser.Parser; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | 11 | namespace ExaustiveCompletionTester 12 | { 13 | public class CompletionFacilities 14 | { 15 | public static DModule objMod = DParser.ParseString(@"module object; 16 | alias immutable(char)[] string; 17 | alias immutable(wchar)[] wstring; 18 | alias immutable(dchar)[] dstring; 19 | class Object { string toString(); } 20 | alias int size_t;"); 21 | 22 | public static LegacyParseCacheView CreateCache(params string[] moduleCodes) 23 | { 24 | var r = new MutableRootPackage(objMod); 25 | 26 | foreach (var code in moduleCodes) 27 | r.AddModule(DParser.ParseString(code)); 28 | 29 | return new LegacyParseCacheView(new[] { r }); 30 | } 31 | 32 | public static EditorData GenEditorData(int caretLine, int caretPos, string focusedModuleCode, params string[] otherModuleCodes) 33 | { 34 | var cache = CreateCache(otherModuleCodes); 35 | var ed = new EditorData { ParseCache = cache }; 36 | 37 | UpdateEditorData(ed, caretLine, caretPos, focusedModuleCode); 38 | 39 | return ed; 40 | } 41 | 42 | public static void UpdateEditorData(EditorData ed, int caretLine, int caretPos, string focusedModuleCode) 43 | { 44 | var mod = DParser.ParseString(focusedModuleCode); 45 | var pack = (ed.ParseCache as LegacyParseCacheView).EnumRootPackagesSurroundingModule(null).First() as MutableRootPackage; 46 | 47 | pack.AddModule(mod); 48 | 49 | ed.ModuleCode = focusedModuleCode; 50 | ed.SyntaxTree = mod; 51 | ed.CaretLocation = new CodeLocation(caretPos, caretLine); 52 | ed.CaretOffset = DocumentHelper.LocationToOffset(focusedModuleCode, caretLine, caretPos); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ExaustiveCompletionTester/Config.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ExaustiveCompletionTester 4 | { 5 | public static class Config 6 | { 7 | public const string PhobosPath = @"B:\Programs\D\dmd2\src\phobos"; 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /ExaustiveCompletionTester/ExaustiveCompletionTester.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netcoreapp3.0 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ExaustiveCompletionTester/SpecializedDataGen.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Completion; 2 | using D_Parser.Dom; 3 | using System; 4 | 5 | namespace ExaustiveCompletionTester 6 | { 7 | public sealed class SpecializedDataGen : ICompletionDataGenerator 8 | { 9 | public SpecializedDataGen() { } 10 | public void AddCodeGeneratingNodeItem (INode node, string codeToGenerate) { } 11 | public void Add (byte token) { } 12 | public void AddPropertyAttribute (string attributeText) { } 13 | public void AddTextItem (string text, string description) { } 14 | public void AddIconItem (string iconName, string text, string description) { } 15 | public void Add (INode n) { } 16 | public void AddModule (DModule module, string nameOverride = null) { } 17 | public void AddPackage (string packageName) { } 18 | public void NotifyTimeout() 19 | { 20 | throw new OperationCanceledException(); 21 | } 22 | 23 | public ISyntaxRegion TriggerSyntaxRegion { get; set; } 24 | public void SetSuggestedItem(string item) { } 25 | } 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /IndependentTests/IndependentTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 10.0.0 7 | 2.0 8 | {CA82BB45-01D9-420E-B010-B1D0783D217C} 9 | Exe 10 | IndependentTests 11 | IndependentTests 12 | 13 | 14 | true 15 | full 16 | false 17 | bin\Debug 18 | DEBUG; 19 | prompt 20 | 4 21 | true 22 | 23 | 24 | full 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | 31 | 32 | false 33 | bin\Debug-Linux 34 | 4 35 | true 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | {0290A229-9AA1-41C3-B525-CAFB86D8BC42} 48 | DParser2 49 | 50 | 51 | -------------------------------------------------------------------------------- /IndependentTests/IndependentTests.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IndependentTests", "IndependentTests.csproj", "{CA82BB45-01D9-420E-B010-B1D0783D217C}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | Debug-Linux|Any CPU = Debug-Linux|Any CPU 11 | EndGlobalSection 12 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 13 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 14 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Debug|Any CPU.Build.0 = Debug|Any CPU 15 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Debug-Linux|Any CPU.ActiveCfg = Debug-Linux|Any CPU 16 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Debug-Linux|Any CPU.Build.0 = Debug-Linux|Any CPU 17 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Release|Any CPU.ActiveCfg = Release|Any CPU 18 | {CA82BB45-01D9-420E-B010-B1D0783D217C}.Release|Any CPU.Build.0 = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(MonoDevelopProperties) = preSolution 21 | StartupItem = IndependentTests.csproj 22 | EndGlobalSection 23 | EndGlobal 24 | -------------------------------------------------------------------------------- /IndependentTests/Program.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Program.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System; 27 | using D_Parser.Parser; 28 | using D_Parser.Dom; 29 | 30 | namespace IndependentTests 31 | { 32 | class MainClass 33 | { 34 | public static void Main (string[] args) 35 | { 36 | var code = @"const A!string()"; 37 | 38 | var mod = DParser.ParseExpression (code); 39 | 40 | Console.WriteLine (mod.ToString()); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /IndependentTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // 2 | // AssemblyInfo.cs 3 | // 4 | // Author: 5 | // Alexander Bothe 6 | // 7 | // Copyright (c) 2013 Alexander Bothe 8 | // 9 | // Permission is hereby granted, free of charge, to any person obtaining a copy 10 | // of this software and associated documentation files (the "Software"), to deal 11 | // in the Software without restriction, including without limitation the rights 12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | // copies of the Software, and to permit persons to whom the Software is 14 | // furnished to do so, subject to the following conditions: 15 | // 16 | // The above copyright notice and this permission notice shall be included in 17 | // all copies or substantial portions of the Software. 18 | // 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | // THE SOFTWARE. 26 | using System.Reflection; 27 | using System.Runtime.CompilerServices; 28 | 29 | // Information about this assembly is defined by the following attributes. 30 | // Change them to the values specific to your project. 31 | 32 | [assembly: AssemblyTitle("IndependentTests")] 33 | [assembly: AssemblyDescription("")] 34 | [assembly: AssemblyConfiguration("")] 35 | [assembly: AssemblyCompany("")] 36 | [assembly: AssemblyProduct("")] 37 | [assembly: AssemblyCopyright("Alexander Bothe")] 38 | [assembly: AssemblyTrademark("")] 39 | [assembly: AssemblyCulture("")] 40 | 41 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 42 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 43 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 44 | 45 | [assembly: AssemblyVersion("1.0.*")] 46 | 47 | // The following attributes are used to specify the signing key for the assembly, 48 | // if desired. See the Mono documentation for more information about signing. 49 | 50 | //[assembly: AssemblyDelaySign(false)] 51 | //[assembly: AssemblyKeyFile("")] 52 | 53 | -------------------------------------------------------------------------------- /TestTool/TestTool.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netcoreapp3.0 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Tests/Completion/TestUtil.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using D_Parser; 3 | using D_Parser.Dom; 4 | using D_Parser.Parser; 5 | using NUnit.Framework; 6 | 7 | namespace Tests.Completion 8 | { 9 | public static class TestUtil 10 | { 11 | /// 12 | /// Use § as caret indicator! 13 | /// 14 | public static TestsEditorData GenEditorData(string focusedModuleCode, params string[] otherModuleCodes) 15 | { 16 | int caretOffset = focusedModuleCode.IndexOf('§'); 17 | Assert.IsTrue(caretOffset != -1); 18 | focusedModuleCode = focusedModuleCode.Substring(0, caretOffset) + 19 | focusedModuleCode.Substring(caretOffset + 1); 20 | var caret = DocumentHelper.OffsetToLocation(focusedModuleCode, caretOffset); 21 | 22 | return GenEditorData(caret.Line, caret.Column, focusedModuleCode, otherModuleCodes); 23 | } 24 | 25 | public static TestsEditorData GenEditorData(int caretLine, int caretPos,string focusedModuleCode,params string[] otherModuleCodes) 26 | { 27 | var cache = ResolutionTestHelper.CreateCache (out _, otherModuleCodes); 28 | var ed = new TestsEditorData { ParseCache = cache }; 29 | ed.CancelToken = CancellationToken.None; 30 | 31 | UpdateEditorData (ed, caretLine, caretPos, focusedModuleCode); 32 | 33 | return ed; 34 | } 35 | 36 | public static void UpdateEditorData(TestsEditorData ed,int caretLine, int caretPos, string focusedModuleCode) 37 | { 38 | var mod = DParser.ParseString (focusedModuleCode); 39 | 40 | ed.MainPackage.AddModule (mod); 41 | 42 | ed.ModuleCode = focusedModuleCode; 43 | ed.SyntaxTree = mod; 44 | ed.CaretLocation = new CodeLocation (caretPos, caretLine); 45 | ed.CaretOffset = DocumentHelper.LocationToOffset (focusedModuleCode, caretLine, caretPos); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Tests/Completion/TestsEditorData.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Completion; 2 | using D_Parser.Dom; 3 | using D_Parser.Misc; 4 | 5 | namespace Tests.Completion 6 | { 7 | public class TestsEditorData : EditorData 8 | { 9 | public MutableRootPackage MainPackage => (ParseCache as LegacyParseCacheView).FirstPackage(); 10 | } 11 | } -------------------------------------------------------------------------------- /Tests/FormatterTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Reflection; 5 | using System.Text; 6 | using D_Parser.Formatting; 7 | using NUnit.Framework; 8 | 9 | namespace Tests 10 | { 11 | [TestFixture] 12 | public class FormatterTests 13 | { 14 | [Test] 15 | public void Formatting() 16 | { 17 | var o = DFormattingOptions.CreateDStandard(); 18 | 19 | bool isTargetCode = false; 20 | 21 | var rawCode = string.Empty; 22 | var sb = new StringBuilder(); 23 | var l = new List>(); 24 | 25 | using(var st = Assembly.GetExecutingAssembly().GetManifestResourceStream("Tests.formatterTests.txt")){ 26 | using(var r = new StreamReader(st)) 27 | { 28 | int n; 29 | while((n=r.Read()) != -1) 30 | { 31 | if((n == ':' && r.Peek() == ':') || (n == '#' && r.Peek() == '#')) 32 | { 33 | r.ReadLine(); 34 | 35 | if(n == '#') 36 | { 37 | if(isTargetCode) 38 | { 39 | l.Add(new Tuple(rawCode, sb.ToString().Trim())); 40 | sb.Clear(); 41 | } 42 | else 43 | { 44 | rawCode = sb.ToString().Trim(); 45 | sb.Clear(); 46 | } 47 | 48 | isTargetCode = !isTargetCode; 49 | } 50 | } 51 | else if(n == '\r' || n == '\n') 52 | { 53 | sb.Append((char)n); 54 | } 55 | else 56 | { 57 | sb.Append((char)n); 58 | sb.AppendLine(r.ReadLine()); 59 | } 60 | } 61 | } 62 | } 63 | 64 | foreach(var tup in l) 65 | { 66 | Fmt(tup.Item1, tup.Item2, o); 67 | } 68 | } 69 | 70 | public static void Fmt(string code, string targetCode, DFormattingOptions policy) 71 | { 72 | var formatOutput = Formatter.FormatCode(code, null, new TextDocument{Text = code},policy, TextEditorOptions.Default); 73 | 74 | Assert.AreEqual(targetCode, formatOutput.Trim()); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Tests/Misc/DemanglerTests.cs: -------------------------------------------------------------------------------- 1 | using D_Parser.Dom; 2 | using D_Parser.Misc.Mangling; 3 | using D_Parser.Resolver; 4 | using NUnit.Framework; 5 | using Tests.Resolution; 6 | 7 | namespace Tests.Misc 8 | { 9 | [TestFixture] 10 | public class DemanglerTests 11 | { 12 | [Test] 13 | public void Demangling_writeln() 14 | { 15 | ITypeDeclaration q; 16 | var ctxt = ResolutionTests.CreateCtxt ("std.stdio", @"module std.stdio; 17 | void writeln() {}"); 18 | bool isCFun; 19 | var t = Demangler.Demangle("_D3std5stdio35__T7writelnTC3std6stream4FileTAAyaZ7writelnFC3std6stream4FileAAyaZv", ctxt, out q, out isCFun); 20 | 21 | Assert.IsFalse (isCFun); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Tests/Resolution/UFCSTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using D_Parser.Parser; 3 | using D_Parser.Resolver; 4 | using D_Parser.Dom.Expressions; 5 | using D_Parser.Resolver.ExpressionSemantics; 6 | 7 | namespace Tests 8 | { 9 | [TestFixture] 10 | public class UFCSTests 11 | { 12 | [Test] 13 | public void BasicResolution() 14 | { 15 | var ctxt = ResolutionTestHelper.CreateCtxt("modA",@"module modA; 16 | void writeln(T...)(T t) {} 17 | int[] foo(string a) {} 18 | int foo(int a) {} 19 | 20 | string globStr; 21 | int globI; 22 | "); 23 | IExpression x; 24 | AbstractType t; 25 | 26 | x = DParser.ParseExpression ("globStr.foo()"); 27 | t = ExpressionTypeEvaluation.EvaluateType (x, ctxt); 28 | Assert.IsInstanceOf(t); 29 | 30 | x = DParser.ParseExpression ("globI.foo()"); 31 | t = ExpressionTypeEvaluation.EvaluateType (x, ctxt); 32 | Assert.IsInstanceOf(t); 33 | 34 | x = DParser.ParseExpression ("globStr.writeln()"); 35 | t = ExpressionTypeEvaluation.EvaluateType (x, ctxt); 36 | Assert.IsInstanceOf(t); 37 | Assert.AreEqual(DTokens.Void, (t as PrimitiveType).TypeToken); 38 | } 39 | 40 | [Test] 41 | public void AccessUFCS() 42 | { 43 | var ctxt = ResolutionTestHelper.CreateCtxt("A", @"module A; 44 | template to(T) 45 | { 46 | T to(A...)(A args) 47 | { 48 | return toImpl!T(args); 49 | } 50 | } 51 | int a; 52 | "); 53 | IExpression x; 54 | AbstractType t; 55 | 56 | x = DParser.ParseExpression("a.to!string()"); 57 | Assert.IsInstanceOf(x); 58 | t = ExpressionTypeEvaluation.EvaluateType(x, ctxt); 59 | Assert.IsInstanceOf(t); 60 | Assert.IsInstanceOf((t as TemplateParameterSymbol).Base); 61 | } 62 | 63 | [Test] 64 | public void NoParenthesesUfcsCall() 65 | { 66 | var ctxt = ResolutionTestHelper.CreateCtxt("A", @"module A; 67 | int foo() { return 123; } 68 | string foo(string s) { return s ~ ""gh""; } 69 | "); 70 | 71 | IExpression x; 72 | ISymbolValue v; 73 | ArrayValue av; 74 | 75 | x = DParser.ParseExpression("\"asdf\".foo"); 76 | v = Evaluation.EvaluateValue(x, ctxt); 77 | 78 | Assert.IsInstanceOf(v); 79 | av = v as ArrayValue; 80 | 81 | Assert.IsTrue(av.IsString); 82 | Assert.AreEqual("asdfgh", av.StringValue); 83 | } 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Tests/Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netcoreapp3.0 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Tests/formatterTests.txt: -------------------------------------------------------------------------------- 1 | :: Comments must start at line start 2 | 3 | 4 | void main() 5 | { 6 | } 7 | ## 8 | void main() { 9 | } 10 | ## 11 | 12 | 13 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 |  2 | Copyright 2012 Alexander Bothe 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/aBothe/D_Parser.svg?branch=master)](https://travis-ci.org/aBothe/D_Parser) 2 | 3 | D_Parser - a library made for analyzing D code in C# environments 4 | --------------------------------------------------------------------------------