Esempio n. 1
0
 private void TryAddItem(IAst ast)
 {
     if (_predicate(ast))
     {
         _collectedItems.Add(ast);
     }
 }
        private HashSet <Completion> CompleteWord(int pos, IAst astRoot, IParseResult parseResult, SourceSnapshot source, ITextSnapshot compiledSnapshot)
        {
            var completionList = new HashSet <Completion>();
            var nspan          = new NSpan(pos, pos);

            if (IsInsideComment(parseResult, nspan))
            {
                return(completionList);
            }

            var visitor = new FindNodeAstVisitor(nspan);

            visitor.Visit(astRoot);
            var stack = visitor.Stack
                        .Where(ast => !(ast is IEnumerable))        // Skip IAstList
                        .ToArray();

            if (ShouldComplete(stack, pos, compiledSnapshot))
            {
                GetCompletions(completionList, stack);
                AddKeywordCompletions(stack, completionList);
            }

            return(completionList);
        }
Esempio n. 3
0
        public void Parse(XmlNode e, out IAst el)
        {
            Target = e.Attributes["target"].Value;
            Method = e.Attributes["method"].Value;

            el = this;
        }
Esempio n. 4
0
        public void GetAst(IAst node, ParserState state)
        {
            var token = state.PeekToken();

            while (token != null && token.Type != TokenType.EOF)
            {
                if (token.Type == TokenType.BlockLeft)
                {
                    node?.Block.Add(ParseBlock(state));
                }
                else if (token.Type == TokenType.BlockRight)
                {
                    break;
                }
                else if (token.Type == TokenType.Identifier)
                {
                    node?.Block.Add(Parse(state));
                }
                else if (token.Type == TokenType.Comment)
                {
                    _ = state.GetToken();
                    // Comments are ignored, so just gobble it up.
                }
                else
                {
                    throw new SyntaxError(token, $"Invalid token type ({token.Type}), expected block or identifier.");
                }
                token = state.PeekToken();
            }
        }
Esempio n. 5
0
 protected static void DumpChild(IAst child, SourceWriter sw, int indentChange = 0)
 {
     if (child != null)
     {
         child.Dump(sw, indentChange);
     }
 }
Esempio n. 6
0
 public JsFunction(IEnvironment environment, IJsClass @class, string name, IList<string> arguments, IAst body)
     : base(environment, @class)
 {
     Name = name;
     Arguments = arguments;
     Body = body;
 }
Esempio n. 7
0
 public static void Initialize(ParseContext parseContext, IAst ast)
 {
     Ast.Walk(
         ref ast,
         (ref IAst o) => Prefix(parseContext, ref o),
         (ref IAst o) => Postfix(parseContext, ref o));
 }
Esempio n. 8
0
 public ForIn(IAst binding, IAst inspected, IAst block, bool declareBinding)
 {
     Binding = binding;
     Inspected = inspected;
     Block = block;
     DeclareBinding = declareBinding;
 }
Esempio n. 9
0
File: For.cs Progetto: jlarsson/Yes
 public For(IAst initial, IAst condition, IAst loop, IAst block)
 {
     Initial = initial;
     Condition = condition;
     Loop = loop;
     Block = block;
 }
Esempio n. 10
0
        private MixinSignature GetSignature(int pos, IAst astRoot, ITrackingSpan applicableToSpan)
        {
            var visitor = new FindNodeAstVisitor(new NSpan(pos, pos));

            visitor.Visit(astRoot);

            foreach (var ast in visitor.Stack)
            {
                if (ast is FunctionRef)
                {
                    var functionRef = (FunctionRef)ast;
                    if (functionRef.IsSymbolEvaluated && functionRef.Symbol.IsFunctionEvaluated)
                    {
                        var symbol     = functionRef.Symbol;
                        var function   = symbol.Function;
                        var parameters = function.Parameters.MakeCompletionList("")
                                         .OfType <FunctionParameterSymbol>()
                                         .OrderBy(p => p.Index)
                                         .Select(p => p.Name)
                                         .ToList();
                        var paramString = string.Join(", ", parameters);

                        return(CreateSignature(_buffer, function.Name + "(" + paramString + ")", "", applicableToSpan));
                    }

                    return(null);
                }
            }

            return(null);
        }
Esempio n. 11
0
        public static int ReadAsInt(this IAst ast)
        {
            if (ast is AstInteger)
            {
                long signed = ((AstInteger)ast).Value;
                if (signed > Int32.MaxValue || signed < Int32.MinValue)
                {
                    throw new ParserContextErrorException("Number to big for target integer field.", ast);
                }

                return((int)signed);
            }
            else if (ast is AstUnsigned)
            {
                uint unsigned = ((AstUnsigned)ast).Value;
                if ((long)unsigned > Int32.MaxValue)
                {
                    throw new ParserContextErrorException("Number to big for target integer field.", ast);
                }

                return((int)unsigned);
            }
            else
            {
                throw new ParserContextErrorException("Expected an integer", ast);
            }
        }
Esempio n. 12
0
        void EmitTemplateMethod(IAst ast, TypeBuilder tb, NLiquidDependentPropertyEvalContext context)
        {
            var compilationUnit = (CompilationUnit)ast;
            var methodBuilder   = CreatreMethodBuilder(ast, tb);

            _variablesMap = new Dictionary <LocalVariableSymbol, Expression>();
            _context      = context;
            //_variablesMap           = new Dictionary<LocalVariableSymbol, Expression>(context.PredefinedTypes.DotNetTypesMap);
            // the first parameter of tempalte method is TextWriter
            var methodParameters = new BlockCollector();

            methodParameters.AddVariable(_textWriterParameter);

            var commentParameter = EmitCommentParameter(compilationUnit);

            if (commentParameter != null)
            {
                methodParameters.AddVariable(commentParameter);
            }

            var blockCollector = new BlockCollector();

            foreach (var statement in compilationUnit.Statements)
            {
                EmitStatement(statement, blockCollector);
            }

            var templateMethod = Expression.Lambda(blockCollector.ToBlockExpression(), methodParameters.Variables);

            templateMethod.CompileToMethod(methodBuilder);
        }
Esempio n. 13
0
 private void ShowAstTreeBlock(IAst node, TreeNode tn)
 {
     foreach (Ast_Base n in node.Block)
     {
         ShowAstTree(n, tn);
     }
 }
Esempio n. 14
0
 public void Visit(IAst parseTree)
 {
     if (parseTree.Span.IntersectsWith(_span))
     {
         parseTree.Accept(this);
     }
 }
Esempio n. 15
0
        internal static void Rewrite(IAst query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            Ast.Walk(ref query, Prefix, Postfix);
        }
Esempio n. 16
0
        private static MethodBuilder CreatreMethodBuilder(IAst ast, TypeBuilder tb)
        {
            var file         = ast.Location.Source.File;
            var templateName = Path.GetFileNameWithoutExtension(file.Name).Replace(".", "_").Replace("-", "_");

            var methodBuilder = tb.DefineMethod(templateName, MethodAttributes.Public | MethodAttributes.Static, null, null);

            return(methodBuilder);
        }
Esempio n. 17
0
 private static Func<IEnvironment, IJsValue, IReference> CreateGetMemberReference(IAst name)
 {
     if (name is IAstWithName)
     {
         var literalName = (name as IAstWithName).Name;
         return (env, obj) => obj.GetReference(literalName);
     }
     return (env, obj) => obj.GetReference(name.Evaluate(env));
 }
Esempio n. 18
0
 private void Execute(XmlNode e, out IAst el)
 {
     if (e.Name == "call")
     {
         var ee = new CallStmt();
         ee.Parse(e, out el);
     }
     el = null;
 }
Esempio n. 19
0
        public static List <IAst> Astyfie(Token c)
        {
            var re = new List <IAst>();
            var x  = IAst.ParseToken(c);

            if (x != null)
            {
                re.AddRange(x);
            }
            return(re);
        }
Esempio n. 20
0
 public static string ReadAsText(this IAst ast)
 {
     if (ast is AstText)
     {
         return(((AstText)ast).Value);
     }
     else
     {
         throw new ParserContextErrorException($"Expected a string but was {ast.GetType()}", ast);
     }
 }
Esempio n. 21
0
 public static AstSeq ReadAsSeq(this IAst ast)
 {
     if (ast is AstSeq)
     {
         return((AstSeq)ast);
     }
     else
     {
         throw new ParserContextErrorException("Expected sequence of values", ast);
     }
 }
Esempio n. 22
0
        private static string RenderXamlForDeclaration(string name, IAst ast)
        {
            var declatation = ast as Declaration;
            var suffix      = declatation == null ? null : (": " + Utils.Escape(declatation.Name.Text));

            return(@"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
" + (string.IsNullOrWhiteSpace(name) ? null : ("<Span Foreground = 'blue'>" + Utils.Escape(name) + "</Span>: "))
                   + ast.ToXaml() + suffix + @"
</Span>");
        }
Esempio n. 23
0
        private void ShowAstTree(IAst node, TreeNode tvn)
        {
            string   text = node.ToString();
            TreeNode tn   = new TreeNode(text);

            AddNode(tn, tvn);
            if (node.Block.Count > 0 && !(node is Ast_Call))
            {
                ShowAstTreeBlock(node, tn);
            }
        }
Esempio n. 24
0
        public override IAst Parse(Token raw)
        {
            var re = new LoopStmt();

            if (raw.RawHeader.Split(' ').Length == 2)//counted
            {
                if (char.IsDigit(raw.RawHeader.Split(' ').Last()[0]))
                {
                    re.LoopType    = LoopType.CountedLoop;
                    re.CountedLoop = IAst.ParseToken(raw.RawHeader.Split(' ').Last())[0];
                }
            }
            else
            {
                if (raw.RawHeader.Trim() == "loop")//infinitloop
                {
                    re.LoopType = LoopType.InfinitLoop;
                }
                else
                {
                    if (raw.RawHeader.Trim().Split(' ')[2] == "to")//ForLoop
                    {
                        re.LoopType = LoopType.ForLoop;
                        var parts = raw.RawHeader.Trim().Split(' ');
                        re.VarName     = parts[1];
                        re.CountedLoop = IAst.ParseToken(parts[3])[0];
                        //todo: step
                    }
                    else
                    {
                        if (raw.RawHeader.Trim().Split(' ')[2] == "in")//ForLoop
                        {
                            re.LoopType = LoopType.Foreachloop;
                            var parts = raw.RawHeader.Trim().Split(' ');
                            re.VarName        = parts[1];
                            re.CollectionName = IAst.ParseToken(parts[3])[0];
                        }
                    }
                }
            }



            foreach (var i in raw.Body)
            {
                re.Body.AddRange(IAst.ParseToken(i));
            }

            return(re);
        }
Esempio n. 25
0
 public void EmitParameter(IAst Opcode, ref int count)
 {
     if (Opcode is ValueStmt)
     {
         var x = Opcode as ValueStmt;
         if (x.Value.StartsWith("\""))
         {
             count++;
             WriteByte(0x20);//loadstr opcode
             WriteString(x.Value.Trim().Trim('"'));
             return;
         }
     }
 }
Esempio n. 26
0
        private static bool Postfix(ref IAst ast)
        {
            var fromExpression = ast as QueryStartClause;

            if (fromExpression != null)
            {
                if (fromExpression.RangeVariable.HasExplicitType)
                {
                    fromExpression.Initializer = new InvokeExpression
                    {
                        Target = new MemberAccessExpression
                        {
                            Left          = fromExpression.Initializer,
                            Name          = "Cast",
                            TypeArguments =
                            {
                                fromExpression.RangeVariable.ElementType
                            }
                        },
                        ParentAst = fromExpression.Initializer.ParentAst
                    };
                }
                return(true);
            }

            var joinExpression = ast as JoinClause;

            if (joinExpression != null)
            {
                if (joinExpression.VariableName.HasExplicitType)
                {
                    joinExpression.Initializer = new InvokeExpression
                    {
                        Target = new MemberAccessExpression
                        {
                            Left          = joinExpression.Initializer,
                            Name          = "Cast",
                            TypeArguments =
                            {
                                joinExpression.VariableName.ElementType
                            }
                        },
                        ParentAst = joinExpression.Initializer.ParentAst
                    };
                }
            }

            return(true);
        }
Esempio n. 27
0
        public override IAst Parse(Token raw)
        {
            var re = new ExpressionStmt();

            if (raw.Raw.Contains("=="))
            {
                var x = Regex.Split(raw.Raw, "==");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "beq";
            }
            else if (raw.Raw.Contains("!="))
            {
                var x = Regex.Split(raw.Raw, "!=");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "Bne";
            }
            else if (raw.Raw.Contains("<="))
            {
                var x = Regex.Split(raw.Raw, "<=");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "Ble";
            }
            else if (raw.Raw.Contains(">="))
            {
                var x = Regex.Split(raw.Raw, ">=");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "Bge";
            }
            else if (raw.Raw.Contains(">"))
            {
                var x = Regex.Split(raw.Raw, ">");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "Bgt";
            }
            else if (raw.Raw.Contains("<"))
            {
                var x = Regex.Split(raw.Raw, "<");
                re.Left     = IAst.ParseToken(x[0].Trim())[0];
                re.Right    = IAst.ParseToken(x[1].Trim())[0];
                re.Operator = "Blt";
            }

            return(re);
        }
Esempio n. 28
0
 public static long ReadAsLong(this IAst ast)
 {
     if (ast is AstInteger)
     {
         return(((AstInteger)ast).Value);
     }
     else if (ast is AstUnsigned)
     {
         return(((AstUnsigned)ast).Value);
     }
     else
     {
         throw new ParserContextErrorException("Expected an integer", ast);
     }
 }
Esempio n. 29
0
        public override IAst Parse(Token raw)
        {
            var re = new RetStmt();
            var x  = IAst.ParseToken(new Token()
            {
                Raw = raw.Raw?.Trim().Split(' ')[1]
            });

            if (x != null)
            {
                re.RetValue = x[0];
            }

            return(re);
        }
Esempio n. 30
0
        public AstSeq Append(IAst newItem)
        {
            if (newItem == null)
            {
                throw new ArgumentNullException(nameof(newItem));
            }

            var itemList = newItem as AstSeq;

            if (itemList?.Items?.Count == 1)
            {
                newItem = itemList.Items[0];
            }

            return(new AstSeq(LexLocation, Items.Concat(new[] { newItem })));
        }
Esempio n. 31
0
        private static bool Prefix(ref IAst ast)
        {
            var intoExpression = ast as IntoClause;

            if (intoExpression != null)
            {
                ast = new QueryStartClause
                {
                    RangeVariable = intoExpression.RangeVariable,
                    Initializer   = intoExpression.Initializer,
                    Next          = intoExpression.Next
                };
            }

            return(true);
        }
Esempio n. 32
0
        public void Emit(IAst Opcode, ref int count)
        {
            if (Opcode is InvokeStmt)
            {
                var x = Opcode as InvokeStmt;
                foreach (var i in x.Perams)
                {
                    EmitParameter(i, ref count);
                }

                count++;

                WriteByte(0x30);//call opcode
                WriteString(ResolveCall(x.Path));
            }
        }
Esempio n. 33
0
        public override IAst Parse(Token raw)
        {
            var atr = raw.Raw.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 1; i < atr.Length; i++)
            {
                var att = atr[i];
                IAst.Buffer.AddRange(IAst.ParseToken(new Token {
                    Raw = "@" + att.Trim()
                }));
            }

            return(new AttributeStmt()
            {
                Atrribute = atr[0].Trim()
            });
        }
Esempio n. 34
0
        public override IAst Parse(Token raw)
        {
            var re = new TypeStmt();

            re.Name = raw.RawHeader.Trim().Split(' ')[1];

            foreach (var i in raw.Body)
            {
                re.Body.AddRange(IAst.ParseToken(i));
            }

            if (raw.RawHeader.Split(')').Last().Contains(":"))
            {
                re.BaseType = raw.RawHeader.Split(':').Last().Trim();
            }

            return(re);
        }
Esempio n. 35
0
 protected MemberBase(IAst instance, IAst member, Func<IEnvironment, IJsValue, IReference> getMemberReference)
 {
     Instance = instance;
     Member = member;
     _getMemberReference = getMemberReference;
 }
Esempio n. 36
0
File: Var.cs Progetto: jlarsson/Yes
 public Var(IAst name, IAst value)
 {
     Value = value;
     Name = ((IAstWithName) name).Name;
 }
Esempio n. 37
0
 public IfThenElse(IAst @if, IAst @then, IAst @else)
 {
     If = @if;
     Then = then;
     Else = @else;
 }
Esempio n. 38
0
 public IJsFunction Construct(IEnvironment environment, string name, IList<string> argumentNames, IAst body)
 {
     return new JsFunction(environment, null, name, argumentNames, body);
 }
Esempio n. 39
0
 public UnaryOperation(IUnaryOperator @operator, IAst value)
 {
     Operator = @operator;
     Value = value;
 }
Esempio n. 40
0
 public static IJsFunction CreateFunction(this IEnvironment environment, string name, IList<string> argumentNames, IAst body)
 {
     return environment.Context.CreateFunction(name, argumentNames, body);
 }
Esempio n. 41
0
 public Delete(IAst member)
 {
     Member = member;
 }
Esempio n. 42
0
 public Throw(IAst expression)
 {
     Expression = expression;
 }
Esempio n. 43
0
 public Member(IAst instance, IAst member)
     : base(instance, member, CreateGetMemberReference(member))
 {
 }
Esempio n. 44
0
 public static IJsFunction CreateFunction(this IContext context, string name, IList<string> argumentNames, IAst body)
 {
     return context.FunctionConstructor.Construct(context.Environment, name, argumentNames, body);
 }
Esempio n. 45
0
 public PreAssign(IAst lhs, IAst rhs)
 {
     Lhs = lhs;
     Rhs = rhs;
 }
Esempio n. 46
0
 public Return(IAst value)
 {
     Value = value;
 }
Esempio n. 47
0
 public BinaryOperation(IBinaryOperator @operator, IAst lhs, IAst rhs)
 {
     Operator = @operator;
     Lhs = lhs;
     Rhs = rhs;
 }
Esempio n. 48
0
 public Function(IAst name, IList<IAst> arguments, IAst statements)
 {
     Statements = statements;
     Name = name == null ? null : name.ReferenceCast<IAstWithName>().Name;
     Arguments = arguments;
 }
Esempio n. 49
0
 public Conditional(IAst test, IAst trueValue, IAst falseValue)
 {
     Test = test;
     TrueValue = trueValue;
     FalseValue = falseValue;
 }
Esempio n. 50
0
    private List<CompletionData> CompleteWord(int pos, IAst astRoot)
    {
      NSpan replacementSpan;
      var parseResult = astRoot.Location.Source.File.ParseResult;
      var result = NitraUtils.CompleteWord(pos, parseResult, astRoot, out replacementSpan);
      var completionList = new List<CompletionData>();

      foreach (var elem in result)
      {
        var symbol = elem as DeclarationSymbol;
        if (symbol != null && symbol.IsNameValid)
        {
          var content = symbol.ToXaml();
          var description = content;
          // TODO: починить отображение неоднозначностей
          //var amb = symbol as IAmbiguousSymbol;
          //if (amb != null)
          //  description = Utils.WrapToXaml(string.Join(@"<LineBreak/>", amb.Ambiguous.Select(a => a.ToXaml())));
          if (symbol.Name.All(IsIdenrChar))
            completionList.Add(new CompletionData(replacementSpan, symbol.Name, content, description, priority: 1.0));
        }

        var literal = elem as string;
        if (literal != null)
        {
          var escaped = Utils.Escape(literal);
          var xaml = "<Span Foreground='blue'>" + escaped + "</Span>";
          completionList.Add(new CompletionData(replacementSpan, literal, xaml, "keyword " + xaml, priority: 2.0));
        }
      }

      return completionList;
    }
Esempio n. 51
0
 public Apply(IAst function, IList<IAst> arguments)
 {
     Function = function;
     Arguments = arguments;
 }
Esempio n. 52
0
 public void Visit(IAst parseTree)
 {
   if (parseTree.Span.IntersectsWith(_span))
     parseTree.Accept(this);
 }
Esempio n. 53
0
 public While(IAst condition, IAst statements)
 {
     Condition = condition;
     Statements = statements;
 }
    private static string RenderXamlForDeclaration(string name, IAst ast)
    {
      var declatation = ast as Declaration;
      var suffix = declatation == null ? null : (": " + Utils.Escape(declatation.Name.Text));
      return @"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
" + (string.IsNullOrWhiteSpace(name) ? null : ("<Span Foreground = 'blue'>" + Utils.Escape(name) + "</Span>: "))
             + ast.ToXaml() + suffix + @"
</Span>";
    }
Esempio n. 55
0
 public IndexedMember(IAst instance, IAst member)
     : base(instance, member, (env, obj) => obj.GetReference(member.Evaluate(env)))
 {
 }
Esempio n. 56
0
 public Construct(IAst constructor, IList<IAst> arguments)
 {
     Constructor = constructor;
     Arguments = arguments;
 }