public override int CompareTo(Ust other)
        {
            var baseCompareResult = base.CompareTo(other);

            if (baseCompareResult != 0)
            {
                return(baseCompareResult);
            }

            var binaryOperatorResult = BinaryOperator - ((BinaryOperatorLiteral)other).BinaryOperator;

            if (binaryOperatorResult != 0)
            {
                return(binaryOperatorResult);
            }

            return(0);
        }
Beispiel #2
0
        public override int CompareTo(Ust other)
        {
            var baseCompareResult = base.CompareTo(other);

            if (baseCompareResult != 0)
            {
                return(baseCompareResult);
            }

            var result = Value ^ ((BooleanLiteral)other).Value;

            if (result != false)
            {
                return(Value ? 1 : -1);
            }

            return(0);
        }
Beispiel #3
0
        public void Parse_NewLine_CorrectLineColumn()
        {
            string fileText = File.ReadAllText(Path.Combine(TestUtility.TestsDataPath, "newLine -r-n.php"));
            var    lineEnds = new [] { "\r", "\n", "\r\n" };

            foreach (var lineEnd in lineEnds)
            {
                var    phpParser  = new PhpAntlrParser();
                string code       = fileText.Replace("\r\n", lineEnd);
                var    sourceFile = new TextFile(code)
                {
                    Name = "newLine.php",
                };
                var tokens = Language.Php.CreateAntlrLexer().GetTokens(sourceFile, out TimeSpan _);
                phpParser.SourceFile = sourceFile;
                var     parseTree = (PhpAntlrParseTree)phpParser.Parse(tokens, out TimeSpan _);
                var     converter = new PhpAntlrParseTreeConverter();
                RootUst ust       = converter.Convert(parseTree);

                Ust intNode = ust.WhereDescendantsOrSelf(
                    node => node is IntLiteral intLiteral && intLiteral.Value == 42).First();

                LineColumnTextSpan intNodeSpan = intNode.LineColumnTextSpan;
                Assert.AreEqual(1, intNodeSpan.BeginLine);
                Assert.AreEqual(12, intNodeSpan.BeginColumn);
                Assert.AreEqual(14, intNodeSpan.EndColumn);

                Ust heredocNode = ust.WhereDescendantsOrSelf(
                    node => node is StringLiteral stringLiteral &&
                    stringLiteral.Text.StartsWith("Heredoc text")).First();

                LineColumnTextSpan heredocNodeSpan = heredocNode.LineColumnTextSpan;
                Assert.AreEqual(3, heredocNodeSpan.BeginLine);
                Assert.AreEqual(6, heredocNodeSpan.EndLine);

                Ust serverAddressNode = ust.WhereDescendantsOrSelf(
                    node => node is StringLiteral stringLiteral &&
                    stringLiteral.Text.Contains("http://127.0.0.1")).First();

                LineColumnTextSpan serverAddressNodeSpan = serverAddressNode.LineColumnTextSpan;
                Assert.AreEqual(8, serverAddressNodeSpan.BeginLine);
                Assert.AreEqual(15, serverAddressNodeSpan.BeginColumn);
            }
        }
Beispiel #4
0
 public void AddResultEntity(Ust ust, bool convert)
 {
     if (IsIncludeIntermediateResult || (convert && stageExt.IsConvert) || (!convert && stageExt.IsPreprocess))
     {
         int ustIndex = usts.FindIndex(tree => tree.FileName == ust.FileName);
         lock (usts)
         {
             if (ustIndex == -1)
             {
                 usts.Add(ust);
             }
             else
             {
                 usts.RemoveAt(ustIndex);
                 usts.Insert(ustIndex, ust);
             }
         }
     }
 }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            if (ust is StringLiteral stringLiteral)
            {
                return(String.Equals(stringLiteral.Text) ? context.AddMatch(stringLiteral) : context.Fail());
            }

            if (context.UstConstantFolder != null &&
                context.UstConstantFolder.TryGetOrFold(ust, out FoldResult foldingResult))
            {
                context.MatchedWithFolded = true;
                if (foldingResult.Value is string stringValue)
                {
                    return(String.Equals(stringValue) ? context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
            }

            return(context.Fail());
        }
Beispiel #6
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var invocation = ust as InvocationExpression;

            if (invocation == null)
            {
                return(context.Fail());
            }

            context = Target.MatchUst(invocation.Target, context);
            if (!context.Success)
            {
                return(context);
            }

            context = Arguments.MatchUst(invocation.Arguments, context);

            return(context.AddUstIfSuccess(invocation));
        }
Beispiel #7
0
        public override int CompareTo(Ust other)
        {
            if (other == null)
            {
                return(1);
            }

            if (!other.IsTerminal)
            {
                return(-1);
            }

            var nodeTypeResult = KindId - other.KindId;

            if (nodeTypeResult != 0)
            {
                return(nodeTypeResult);
            }

            Token otherLiteral = (Token)other;

            if (Expression != null && otherLiteral.Expression == null)
            {
                return(1);
            }

            if (Expression == null && otherLiteral.Expression != null)
            {
                return(-1);
            }

            if (Expression != null && otherLiteral.Expression != null)
            {
                var expressionCompareResult = Expression.CompareTo(otherLiteral.Expression);
                if (expressionCompareResult != 0)
                {
                    return(expressionCompareResult);
                }
            }

            return(0);
        }
Beispiel #8
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var typeDeclaration = ust as TypeDeclaration;

            if (typeDeclaration == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Modifiers.MatchSubset(typeDeclaration.Modifiers, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Name != null)
            {
                newContext = Name.MatchUst(typeDeclaration.Name, newContext);
                if (!newContext.Success)
                {
                    return(newContext);
                }
            }

            newContext = BaseTypes.MatchSubset(typeDeclaration.BaseTypes, newContext);

            if (!newContext.Success)
            {
                return(newContext);
            }

            if (Body != null)
            {
                if (!typeDeclaration.TypeMembers.Any(m => Body.MatchUst(m, newContext).Success))
                {
                    return(newContext.Fail());
                }
            }

            return(newContext.AddUstIfSuccess(typeDeclaration));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var unaryOperatorExpression = ust as UnaryOperatorExpression;

            if (unaryOperatorExpression == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Expression.MatchUst(unaryOperatorExpression.Expression, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Operator.MatchUst(unaryOperatorExpression.Operator, newContext);

            return(newContext.AddUstIfSuccess(ust));
        }
Beispiel #10
0
        public Ust Convert(ParseTree langParseTree)
        {
            Ust result = null;

            var aspxParseTree = (AspxParseTree)langParseTree;

            try
            {
                var converter = new AspxToCsConverter(aspxParseTree.FileName, aspxParseTree.FileData);
                result = new MostCommonUst((FileNode)aspxParseTree.Root.Accept(converter), ConvertedLanguages);
            }
            catch (Exception ex)
            {
                Logger.LogError(new ConversionException(aspxParseTree.FileName, ex));
                result = new MostCommonUst();
            }

            result.FileName = langParseTree.FileName;
            return(result);
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var objectCreateExpression = ust as ObjectCreateExpression;

            if (objectCreateExpression == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Type.MatchUst(objectCreateExpression.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Arguments.MatchUst(objectCreateExpression.Arguments, newContext);

            return(newContext.AddUstIfSuccess(objectCreateExpression));
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var parameterDeclaration = ust as ParameterDeclaration;

            if (parameterDeclaration == null)
            {
                return(context.Fail());
            }

            MatchContext newContext = Type.MatchUst(parameterDeclaration.Type, context);

            if (!newContext.Success)
            {
                return(newContext);
            }

            newContext = Name.MatchUst(parameterDeclaration.Name, newContext);

            return(newContext.AddUstIfSuccess(parameterDeclaration));
        }
 public void Init(Ust ust, Atlas atlas)
 {
     Atlas  = atlas;
     Ust    = ust;
     Parser = new Parser(Atlas, Ust);
     Settings.MinLengthDefault = 110 * (int)Ust.Tempo / 120;
     Settings.MinLength        = Settings.MinLengthDefault;
     textBoxMinLength.Text     = Settings.MinLength.ToString();
     ImportDict();
     comboBoxLanguage.Items.Clear();
     comboBoxLanguage.Items.AddRange(Lang.Languages);
     comboBoxLanguage.SelectedItem = Lang.Current;
     SetLyric();
     buttonSetText.Enabled = false;
     SetTextWindow         = new SetTextWindow(Atlas, Ust);
     if (Singer.Current.IsLoaded)
     {
         checkBoxLengthByOto.Checked = true;
     }
 }
Beispiel #14
0
        public override Ust VisitInitializerExpression(InitializerExpressionSyntax node)
        {
            Ust result   = null;
            var children = node.Expressions.Select(e => (Expression)VisitAndReturnNullIfError(e))
                           .ToArray();
            var textSpan = node.GetTextSpan();

            if (node.Kind() == SyntaxKind.ArrayInitializerExpression)
            {
                var sizes = new List <Expression> {
                    new IntLiteral(children.Length)
                };
                result = new ArrayCreationExpression(new TypeToken("", textSpan), sizes, children.ToList(), textSpan);
            }
            else
            {
                result = new MultichildExpression(children, textSpan);
            }

            return(result);
        }
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            switch (ust)
            {
            case IntLiteral intLiteral:
                return(Value == intLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());

            case LongLiteral longLiteral:
                return(Value == longLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());

            case BigIntLiteral bigIntLiteral:
                return(Value == bigIntLiteral.Value
                    ? context.AddMatch(ust)
                    : context.Fail());
            }

            if (context.UstConstantFolder != null &&
                context.UstConstantFolder.TryGetOrFold(ust, out FoldResult foldingResult))
            {
                context.MatchedWithFolded = true;
                if (foldingResult.Value is int intValue)
                {
                    return(Value == intValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
                if (foldingResult.Value is long longValue)
                {
                    return(Value == longValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
                if (foldingResult.Value is BigInteger bigIntValue)
                {
                    return(Value == bigIntValue?context.AddMatches(foldingResult.TextSpans) : context.Fail());
                }
            }

            return(context.Fail());
        }
Beispiel #16
0
        public Ust Convert(ParseTree langParseTree)
        {
            var antlrParseTree            = (AntlrParseTree)langParseTree;
            ParserRuleContext tree        = antlrParseTree.SyntaxTree;
            ICharStream       inputStream = tree.start.InputStream ?? tree.stop?.InputStream;
            string            filePath    = inputStream != null ? inputStream.SourceName : "";
            Ust      result   = null;
            FileNode fileNode = null;

            if (tree != null && inputStream != null)
            {
                try
                {
                    fileNode = CreateVisitorAndVisit(antlrParseTree.Tokens, tree, filePath, langParseTree.FileData, Logger);
                    result   = new MostCommonUst(fileNode, ConvertedLanguages);
                }
                catch (Exception ex)
                {
                    Logger.LogError(new ConversionException(filePath, ex));

                    if (result == null)
                    {
                        result          = new MostCommonUst();
                        result.Comments = ArrayUtils <CommentLiteral> .EmptyArray;
                    }
                }
            }
            else
            {
                fileNode = new FileNode(filePath, langParseTree.FileData);
                result   = new MostCommonUst()
                {
                    Root = fileNode
                };
                result.Comments = ArrayUtils <CommentLiteral> .EmptyArray;
            }
            result.FileName = langParseTree.FileName;
            result.Comments = antlrParseTree.Comments.Select(c => new CommentLiteral(c.Text, c.GetTextSpan(), fileNode)).ToArray();
            return(result);
        }
        public bool TryGetOrFold(Ust ust, out FoldResult result)
        {
            if (ust == null || !foldingTypes.Contains(ust.GetType()))
            {
                result = null;
                return(false);
            }

            lock (ust)
            {
                if (foldedResults.TryGetValue(ust.TextSpan, out result))
                {
                    return(result != null);
                }

                result = TryGetOrFold(ust);

                NormalizeAndAdd(ust, ref result);
            }

            return(result != null);
        }
Beispiel #18
0
        public string Render(Ust ust)
        {
            edgesString    = new StringBuilder();
            vertexesString = new StringBuilder();
            currentIndex   = 0;

            RenderNode(ust);

            var graphString = new StringBuilder();

            graphString.AppendLine("digraph ust {");
            if (RenderDirection != GraphvizDirection.TopBottom)
            {
                string rankdir;
                if (RenderDirection == GraphvizDirection.BottomTop)
                {
                    rankdir = "BT";
                }
                else if (RenderDirection == GraphvizDirection.LeftRight)
                {
                    rankdir = "LR";
                }
                else if (RenderDirection == GraphvizDirection.RightLeft)
                {
                    rankdir = "RL";
                }
                else
                {
                    rankdir = "TB";
                }
                graphString.AppendLine($"rankdir={rankdir};");
            }
            graphString.Append(vertexesString.ToString());
            graphString.Append(edgesString.ToString());
            graphString.AppendLine("}");

            return(graphString.ToString());
        }
Beispiel #19
0
        public static List <Ust> WhereDescendants(this Ust ust, Func <Ust, bool> predicate = null)
        {
            var result = new List <Ust>();

            GetDescendants(ust);

            void GetDescendants(Ust localResult)
            {
                if (localResult != null)
                {
                    if (predicate == null || predicate(localResult))
                    {
                        result.Add(localResult);
                    }
                    foreach (Ust child in localResult.Children)
                    {
                        GetDescendants(child);
                    }
                }
            }

            return(result);
        }
Beispiel #20
0
        private PropertyDeclaration VisitClassProperty(ClassProperty property)
        {
            Ust     body = VisitPropertyValue(property.Value);
            IdToken name = null;

            if (property.Key is Identifier identifier)
            {
                name = VisitIdentifier(identifier);
            }
            else
            {
                if (property.Key is Literal literal)
                {
                    name = new IdToken(literal.StringValue ?? literal.Raw, GetTextSpan(literal));
                }
                else
                {
                    Logger.LogDebug($"Not implemented type of property key {property.Key.GetType()}"); // TODO
                }
            }

            return(new PropertyDeclaration(null, name, body, GetTextSpan(property)));
        }
        public override int CompareTo(Ust other)
        {
            if (other == null)
            {
                return((int)KindId);
            }

            var nodeTypeCompareResult = KindId - other.KindId;

            if (nodeTypeCompareResult != 0)
            {
                return(nodeTypeCompareResult);
            }

            var otherArrayCreation = (ArrayCreationExpression)other;
            int compareSizesResult = Sizes.CompareTo(otherArrayCreation.Sizes);

            if (compareSizesResult != 0)
            {
                return(compareSizesResult);
            }

            return(Initializers.CompareTo(otherArrayCreation.Initializers));
        }
Beispiel #22
0
 public abstract MatchContext MatchUst(Ust ust, MatchContext context);
 protected override MatchContext Match(Ust ust, MatchContext context)
 {
     return(ust is ThisReferenceToken thisReferenceToken
         ? context.AddMatch(thisReferenceToken)
         : context.Fail());
 }
Beispiel #24
0
 public PropertyDeclaration(TypeToken type, IdToken name, Ust body, TextSpan textSpan)
     : base(name, textSpan)
 {
     Type = type;
     Body = body;
 }
 public WrapperExpression(Ust node)
     : base(node.TextSpan)
 {
     Node = node;
 }
 public WrapperExpression(Ust node, TextSpan textSpan)
     : base(textSpan)
 {
     Node = node;
 }
 protected override MatchContext Match(Ust ust, MatchContext context)
 {
     return(ust is Expression expression
         ? context.AddMatch(expression)
         : context.Fail());
 }
 protected override MatchContext Match(Ust ust, MatchContext context)
 {
     return(ust is NullLiteral nullLiteral
         ? context.AddMatch(nullLiteral)
         : context.Fail());
 }
 protected virtual void ExtraProcess(Ust ust, JObject ustJObject, JsonSerializer serializer)
 {
 }
Beispiel #30
0
 public NamespaceDeclaration(StringLiteral name, Ust member,
                             TextSpan textSpan)
     : this(name, new Ust[] { member }, textSpan)
 {
 }