Esempio n. 1
0
        public void ReparseModule(ProjectFile pf, bool doUfcsCache = true)
        {
            if (pf == null || !DLanguageBinding.IsDFile(pf.FilePath.FileName))
            {
                return;
            }

            try {
                var ddom = DParser.ParseFile(pf.FilePath.ToAbsolute(BaseDirectory));

                // Update relative module name
                ddom.ModuleName = DParserWrapper.BuildModuleName(pf);

                LocalFileCache.UfcsCache.RemoveModuleItems(LocalFileCache.GetModuleByFileName(pf.FilePath, BaseDirectory));
                LocalFileCache.AddOrUpdate(ddom);
                if (doUfcsCache)
                {
                    LocalFileCache.UfcsCache.CacheModuleMethods(ddom, new ResolverContextStack(ParseCache, new ResolverContext {
                        ScopedBlock = ddom
                    }));
                }
            } catch (Exception ex) {
                LoggingService.LogError("Error while parsing " + pf.FilePath.ToString(), ex);
            }
        }
Esempio n. 2
0
        public void UpdateModule(string filename, string srcText, bool verbose)
        {
            DModule ast;

            try
            {
                ast = DParser.ParseString(srcText, false);
            }
            catch (Exception ex)
            {
                ast = new DModule {
                    ParseErrors = new System.Collections.ObjectModel.ReadOnlyCollection <ParserError>(
                        new List <ParserError> {
                        new ParserError(false, ex.Message + "\n\n" + ex.StackTrace, DTokens.Invariant, CodeLocation.Empty)
                    })
                };                            //WTF
            }
            if (string.IsNullOrEmpty(ast.ModuleName))
            {
                ast.ModuleName = Path.GetFileNameWithoutExtension(filename);
            }
            ast.FileName = filename;

            //GlobalParseCache.RemoveModule(filename);
            GlobalParseCache.AddOrUpdateModule(ast);
            ConditionalCompilationFlags cflags = new ConditionalCompilationFlags(_editorData);

            //GlobalParseCache.UfcsCache.CacheModuleMethods(ast, ResolutionContext.Create(_parseCacheList, cflags, null, null));

            _modules[filename] = ast;
            _sources[filename] = srcText;
            //MessageBox.Show("UpdateModule(" + filename + ")");
            //throw new NotImplementedException();
        }
Esempio n. 3
0
        public void TypeRefFinding()
        {
            var modA = DParser.ParseString(@"module modA;
class A(T)
{
	int n;
	static int prop;
	static A!float statA;
}

void main()
{
	auto a = new A!int();
	a.n;
	A.prop = 3;
	int b = A.prop + 4;
	A!double.statA.statA = new A!double();
}
");

            var ed = new EditorData {
                SyntaxTree = modA,
                ParseCache = new D_Parser.Misc.ParseCacheView(new RootPackage[0])
            };

            var res = TypeReferenceFinder.Scan(ed, null, -1);

            Assert.That(res.Count, Is.GreaterThan(6));
        }
Esempio n. 4
0
        public void IsExpressionAlias()
        {
            var ctxt = ResolutionTests.CreateCtxt("A", @"module A;
static if(is(const(int)* U == const(U)*))
U var;

U derp;
");

            IExpression  x;
            AbstractType t;
            DSymbol      ds;

            x = DParser.ParseExpression("var");
            (x as IdentifierExpression).Location = new CodeLocation(2, 3);
            t  = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            ds = t as DSymbol;

            Assert.That(t, Is.TypeOf(typeof(MemberSymbol)));
            Assert.That(ds.Base, Is.TypeOf(typeof(TemplateParameterSymbol)));
            ds = ds.Base as DSymbol;
            Assert.That(ds.Base, Is.TypeOf(typeof(PrimitiveType)));
            Assert.That((ds.Base as PrimitiveType).Modifier, Is.EqualTo(0));

            ctxt.CurrentContext.DeducedTemplateParameters.Clear();

            var dv = ctxt.ParseCache[0]["A"]["derp"].First() as DVariable;

            t = TypeDeclarationResolver.HandleNodeMatch(dv, ctxt);
            Assert.That(t, Is.TypeOf(typeof(MemberSymbol)));
            Assert.That((t as MemberSymbol).Base, Is.Null);
        }
Esempio n. 5
0
        public void BasicResolution()
        {
            var          ctxt = ResolutionTests.CreateCtxt("modA", @"module modA;
void writeln(T...)(T t) {}
int[] foo(string a) {}
int foo(int a) {}

string globStr;
int globI;
");
            IExpression  x;
            AbstractType t;

            x = DParser.ParseExpression("globStr.foo()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(ArrayType)));

            x = DParser.ParseExpression("globI.foo()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(PrimitiveType)));

            x = DParser.ParseExpression("globStr.writeln()");
            t = ExpressionTypeEvaluation.EvaluateType(x, ctxt);
            Assert.That(t, Is.TypeOf(typeof(PrimitiveType)));
            Assert.That((t as PrimitiveType).TypeToken, Is.EqualTo(DTokens.Void));
        }
Esempio n. 6
0
                public override bool Matches(object actual)
                {
                    var code = actual as string;

                    if (code == null)
                    {
                        return(false);
                    }

                    code += "\n";

                    var m     = DParser.ParseString(code);
                    var cache = ResolutionTests.CreateCache();

                    (cache [0] as MutableRootPackage).AddModule(m);

                    var ed = new EditorData {
                        ModuleCode    = code,
                        CaretOffset   = code.Length - 1,
                        CaretLocation = DocumentHelper.OffsetToLocation(code, code.Length - 1),
                        SyntaxTree    = m,
                        ParseCache    = cache
                    };

                    var gen = new TestCompletionDataGen(null, null);
                    var res = CodeCompletion.GenerateCompletionData(ed, gen, 'a');

                    return(neg ? !res : res);
                }
Esempio n. 7
0
        public void ForStatement()
        {
            var code = @"module A; // 1
void main(){
	for(
		int
		i= // 5
		0;
		i<100;
		i++)
		
	{ // 10
	}"    ;
            var mod  = DParser.ParseString(code);
            var main = mod["main"].First() as DMethod;

            var          l   = new CodeLocation(1, 4);
            var          off = DocumentHelper.LocationToOffset(code, l);
            CodeLocation caretLoc;
            //var parsedBlock = AbstractCompletionProvider.FindCurrentCaretContext(code,main, off,l,out ptr, out caretLoc);

            /*
             * a) Test if completion popup would open in each code situation
             * b) Test in which case iteration variables can be found.
             */

            //TODO
        }
Esempio n. 8
0
        ISemantic E(MixinExpression x)
        {
            // 1) Evaluate the mixin expression
            var cnst = resolveConstOnly;

            resolveConstOnly = true;
            var v = E(((MixinExpression)x).AssignExpression) as ArrayValue;

            resolveConstOnly = cnst;

            if (v == null || !v.IsString)
            {
                EvalError(new InvalidStringException(x));
                return(null);
            }

            // 2) Parse it as an expression
            var ex = DParser.ParseAssignExpression(v.StringValue);

            if (ex == null)
            {
                EvalError(new EvaluationException(x, "Invalid expression code given"));
                return(null);
            }
            //TODO: Excessive caching
            // 3) Evaluate the expression's type/value
            return(E(ex));
        }
Esempio n. 9
0
        public void SyntaxError_Issue160()
        {
            var s   = @"
struct Bar5832(alias v) {}

template isBar5832a(T)
{
    static if (is(T _ : Bar5832!(v), alias v))
        enum isBar5832a = true;
    else
        enum isBar5832a = false;
}
template isBar5832b(T)
{
    static if (is(T _ : Bar5832!(v), alias int v))
        enum isBar5832b = true;
    else
        enum isBar5832b = false;
}
template isBar5832c(T)
{
    static if (is(T _ : Bar5832!(v), alias string v))
        enum isBar5832c = true;
    else
        enum isBar5832c = false;
}
static assert( isBar5832a!(Bar5832!1234));
static assert( isBar5832b!(Bar5832!1234));
static assert(!isBar5832c!(Bar5832!1234));";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 10
0
        public void UpdateModule(string filename, string srcText, bool verbose)
        {
            filename = normalizePath(filename);
            DModule ast;

            try
            {
                ast = DParser.ParseString(srcText, false, true, _taskTokens);
            }
            catch (Exception ex)
            {
                ast = new DModule {
                    ParseErrors = new System.Collections.ObjectModel.ReadOnlyCollection <ParserError>(
                        new List <ParserError> {
                        new ParserError(false, ex.Message + "\n\n" + ex.StackTrace, DTokens.Invariant, CodeLocation.Empty)
                    })
                };                            //WTF
            }
            if (string.IsNullOrEmpty(ast.ModuleName))
            {
                ast.ModuleName = Path.GetFileNameWithoutExtension(filename);
            }
            ast.FileName = filename;

            _modules [filename] = ast;
            GlobalParseCache.AddOrUpdateModule(ast);

            _editorData.ParseCache = null;

            _sources[filename] = srcText;
            //MessageBox.Show("UpdateModule(" + filename + ")");
            //throw new NotImplementedException();
            _activityCounter++;
        }
Esempio n. 11
0
        public void IncrementalParsing_LambdaParameters()
        {
            var code = @"module A;
int foo;
void main() {
	
}";

            var m    = DParser.ParseString(code);
            var main = m ["main"].First() as DMethod;
            var foo  = m ["foo"].First() as DVariable;

            var b = DResolver.SearchBlockAt(m, new CodeLocation(1, 4));

            Assert.That(b, Is.SameAs(main));

            code = @"module A;
int foo;
void main() {
int a;
(string ;
}";
            var  caret = new CodeLocation(9, 5);
            bool _u;
            var  newMod = IncrementalParsing.UpdateBlockPartly(main.Body, code, DocumentHelper.LocationToOffset(code, caret), caret, out _u) as DMethod;

            var lambda = newMod.Children [0] as DMethod;

            Assert.That(lambda, Is.Not.Null);
            Assert.That(lambda.Parameters.Count, Is.EqualTo(1));
            Assert.That(lambda.Parameters [0].Type, Is.Not.Null);
            Assert.That(lambda.Parameters [0].NameHash, Is.EqualTo(DTokens.IncompleteIdHash));
        }
Esempio n. 12
0
        public void SyntaxError_Issue159()
        {
            var s   = @"mixin typeof(b).Def!(int);";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 13
0
        public void SyntaxError_Issue162()
        {
            var s   = @"int foo19(alias int a)() { return a; }";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 14
0
        public void SyntaxError_Issue166()
        {
            var s   = "mixin .mix;";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 15
0
        public void SyntaxError_Issue178()
        {
            var s   = "static assert(!__traits(compiles, v1 * v2));";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 16
0
        public void SyntaxError_Issue158()
        {
            var s   = @"int array1[3] = [1:1,2,0:3];";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 17
0
        public void SyntaxError_Issue153()
        {
            var s   = @"alias fnRtlAllocateHeap = extern(Windows) void* function(void* HeapHandle, uint Flags, size_t Size) nothrow;";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(0, mod.ParseErrors.Count);
        }
Esempio n. 18
0
        /// <summary>
        /// Searches in current solution and in the global cache for the given file and returns it.
        /// If not found, file will be parsed.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static IAbstractSyntaxTree GetFileSyntaxTree(string file, out DProject OwnerProject)
        {
            OwnerProject = null;
            if (CoreManager.CurrentSolution != null)
            {
                foreach (var prj in CoreManager.CurrentSolution)
                {
                    var dprj = prj as DProject;

                    if (dprj != null && dprj.ContainsFile(file))
                    {
                        OwnerProject = dprj;
                        return(dprj.ParsedModules.GetModuleByFileName(file, dprj.BaseDirectory));
                    }
                }
            }

            var pcl = ParseCacheList.Create(DSettings.Instance.dmd1.ASTCache, DSettings.Instance.dmd2.ASTCache);

            IAbstractSyntaxTree ret = null;

            foreach (var pc in pcl)
            {
                foreach (var pdir in pc.ParsedDirectories)
                {
                    if (file.StartsWith(pdir) && (ret = pc.GetModuleByFileName(file, pdir)) != null)
                    {
                        return(ret);
                    }
                }
            }

            return(DParser.ParseFile(file));
        }
Esempio n. 19
0
        /// <summary>
        /// Since 2.064. new Server(args).run(); is allowed
        /// </summary>
        public void PostNewExpressions()
        {
            var x = DParser.ParseExpression("new Server(args).run()") as PostfixExpression;

            Assert.That(x, Is.TypeOf(typeof(PostfixExpression_MethodCall)));
            Assert.That((x.PostfixForeExpression as PostfixExpression_Access).PostfixForeExpression, Is.TypeOf(typeof(NewExpression)));
        }
Esempio n. 20
0
        public void Attributes1()
        {
            var n = DParser.ParseString("align(2) align int a;");

            var a = n["a"].First() as DVariable;

            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.Attributes.Count);

            var attr = a.Attributes[0] as Modifier;

            Assert.AreEqual(DTokens.Align, attr.Token);
            Assert.That(attr.LiteralContent == null || attr.LiteralContent as string == string.Empty);

            n = DParser.ParseString("private public int a;");
            a = n["a"].First() as DVariable;
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.Attributes.Count);

            n = DParser.ParseString(@"private:
public int a;
int b;");
            a = n["a"].First() as DVariable;
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.Attributes.Count);
            Assert.AreEqual(DTokens.Public, ((Modifier)a.Attributes[0]).Token);

            a = n["b"].First() as DVariable;
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.Attributes.Count);
            Assert.AreEqual(DTokens.Private, ((Modifier)a.Attributes[0]).Token);
        }
Esempio n. 21
0
        public void EponymousTemplates()
        {
            var m = DParser.ParseString(@"
enum mixinStuff = q{import C;};
enum isIntOrFloat(T) = is(T == int) || is(T == float);
alias isInt(T) = is(T == int);

enum
    allSatisfy(alias pred, TL...) =
        TL.length == 0 || (pred!(TL[0]) && allSatisfy!(pred, TL[1..$])),
    anySatisfy(alias pred, TL...) =
        TL.length != 0 && (pred!(TL[0]) || anySatisfy!(pred, TL[1..$])) || false;
");

            Assert.AreEqual(0, m.ParseErrors.Count);
            TemplateParameter tp;

            var dc = m.Children ["isIntOrFloat"].First() as EponymousTemplate;

            Assert.That(dc, Is.Not.Null);
            Assert.That(dc.TryGetTemplateParameter("T".GetHashCode(), out tp), Is.True);

            dc = m.Children ["isInt"].First() as EponymousTemplate;
            Assert.That(dc, Is.Not.Null);
            Assert.That(dc.TryGetTemplateParameter("T".GetHashCode(), out tp), Is.True);
        }
Esempio n. 22
0
        public void Attributes2()
        {
            var m = DParser.ParseString(@"
int foo() if(is(T==string)) {}
debug(1) private int a;
int b;

debug
	version = Cust;

debug
	int A;
else version(D)
	int B;
else int C;");
            var a = m["a"].First() as DVariable;
            var b = m["b"].First() as DVariable;

            Assert.AreEqual(2, a.Attributes.Count);
            Assert.That(a.ContainsAttribute(DTokens.Private));
            Assert.AreEqual(0, b.Attributes.Count);

            var A = m["A"].First() as DVariable;
            var B = m["B"].First() as DVariable;
            var C = m["C"].First() as DVariable;

            Assert.AreEqual(1, A.Attributes.Count);
            Assert.AreEqual(2, B.Attributes.Count);
            Assert.AreEqual(2, C.Attributes.Count);

            Assert.That((m["foo"].First() as DMethod).TemplateConstraint, Is.TypeOf(typeof(IsExpression)));
        }
Esempio n. 23
0
        public void LongNumberLiteral()
        {
            var mod = DParser.ParseString(@"
version(9223372036854775807){}
debug(9223372036854775807){}
");
        }
Esempio n. 24
0
        public void TestSyntaxError2()
        {
            var s   = "class Foo( if(is(T==float) {} class someThingElse {}";
            var mod = DParser.ParseString(s);

            Assert.GreaterOrEqual(mod.ParseErrors.Count, 1);
        }
Esempio n. 25
0
        public void Test2_066UCSnytax()
        {
            var s   = "auto b = creal(3+4i);";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }
Esempio n. 26
0
        /// <summary>
        /// Parse the base directory.
        /// </summary>
        public ParsePerformanceData UpdateFromBaseDirectory(bool ReturnOnException = false)
        {
            Clear();
            // wild card character ? seems to behave differently across platforms
            // msdn: -> Exactly zero or one character.
            // monodocs: -> Exactly one character.
            string[] dFiles  = Directory.GetFiles(BaseDirectory, "*.d", SearchOption.AllDirectories);
            string[] diFiles = Directory.GetFiles(BaseDirectory, "*.di", SearchOption.AllDirectories);
            string[] files   = new string[dFiles.Length + diFiles.Length];
            Array.Copy(dFiles, 0, files, 0, dFiles.Length);
            Array.Copy(diFiles, 0, files, dFiles.Length, diFiles.Length);

            var sw = new Stopwatch();

            var ppd = new ParsePerformanceData {
                BaseDirectory = BaseDirectory
            };

            foreach (string tf in files)
            {
                if (tf.EndsWith("phobos" + Path.DirectorySeparatorChar + "index.d") ||
                    tf.EndsWith("phobos" + Path.DirectorySeparatorChar + "phobos.d"))
                {
                    continue;                                                                                       // Skip index.d (D2) || phobos.d (D2|D1)
                }
                string tmodule = Path.ChangeExtension(tf, null).Remove(0, BaseDirectory.Length + 1).Replace(Path.DirectorySeparatorChar, '.');

                sw.Start();
                try
                {
                    var ast = DParser.ParseFile(tf, !ParseFunctionBodies);
                    sw.Stop();
                    ppd.AmountFiles++;
                    ppd.TotalDuration += sw.Elapsed.TotalSeconds;
                    sw.Reset();
                    ast.ModuleName = tmodule;
                    ast.FileName   = tf;
                    Add(ast);
                }
                catch (Exception)
                {
                    if (ReturnOnException)
                    {
                        return(ppd);
                    }

                    /*
                     * ErrorLogger.Log(ex);
                     * if (MessageBox.Show("Continue Parsing?", "Parsing exception", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                     *      return;*/
                }
            }

            return(ppd);

            /*
             * ErrorLogger.Log("Parsed "+files.Length+" files in "+BaseDirectory+" in "+Math.Round(duration,2).ToString()+"s (~"+Math.Round(duration/files.Length,3).ToString()+"s per file)",
             *      ErrorType.Information,ErrorOrigin.Parser);*/
        }
Esempio n. 27
0
        public void SpecialTokenSequences()
        {
            var m = DParser.ParseString(@"module A;
#line 1
#line 0x123 ""ohyeah/asd.d""");

            Assert.AreEqual(0, m.ParseErrors.Count);
        }
Esempio n. 28
0
        public void AccessExpression1()
        {
            var e1 = DParser.ParseExpression("a.b!c");
            var e2 = DParser.ParseExpression("a.b");

            Assert.That(e1, Is.TypeOf(typeof(PostfixExpression_Access)));
            Assert.That(e2, Is.TypeOf(typeof(PostfixExpression_Access)));
        }
Esempio n. 29
0
        public void SyntaxError_Issue155()
        {
            var s   = @"static assert(cast(bool)set.isSet(fd) == cast(bool)(() @trusted => FD_ISSET(fd, fdset))());
enum dg7761 = (int a) pure => 2 * a;";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(0, mod.ParseErrors.Count);
        }
Esempio n. 30
0
        public void SyntaxError_Issue156()
        {
            var s   = @"deprecated(""This module will be removed in future release."")
module imports.a12567;";
            var mod = DParser.ParseString(s);

            Assert.AreEqual(mod.ParseErrors.Count, 0);
        }