public ILGenerationContext()
 {
     Output = new ILOutputStream();
     ScopeStack = new ScopeStack();
     ILScope globalScope = new ILScope();
     ScopeStack.Push(globalScope);
 }
Example #2
0
        private bool ParseDollar(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            if (stream.Current != '$' || scope.Current == Scope.Block) return false;

            var next = stream.Peek();
            if (next == '$')
            {
                stream.Advance(2);
                return true;
            }

            if (char.IsLetter(next))
            {
                scope.Push(Scope.Statement);
                stream.Advance();
                return true;
            }

            if (next == '{')
            {
                scope.Push(Scope.Block);
                stream.Advance();
                return true;
            }

            return false;
        }
        public ScopeContainer()
        {
            Global = new ScopeStack<Scope>();

            Class = new ScopeStack<Scope>();

            CurrentScopeType = ScopeType.Global;
        }
Example #4
0
        public TokenList Tokenize(string template)
        {
            //var x = ClassificationRegistry.GetClassificationType(Constants.KeywordClassificationType);


            var stream = new TemplateStream(template);

            var tokens = new TokenList();
            var context = new ContextStack(tokens);
            var scope = new ScopeStack();
            var brace = new BraceStack();

            while (true)
            {
                if (ParseComment(stream, tokens, context))
                    continue;

                if (ParseDollar(stream, tokens, context, scope))
                    continue;

                if (ParseStatement(stream, tokens, context, scope))
                    continue;

                if (ParseFilter(stream, tokens, context, scope))
                    continue;

                if (ParseOther(stream, tokens, context, scope, brace))
                    continue;

                if (stream.Advance() == false)
                    break;
            }

            context.Clear(stream.Position);

            return tokens;
        }
 /// <summary>
 /// Stops the stopwatch, logs the result, pops nested scope stack.
 /// </summary>
 public void Dispose()
 {
     Stop();
     ScopeStack = ScopeStack.Pop();
 }
Example #6
0
        internal override void AnalyzeNode()
        {
            // figure out if our reference type is a function or a constructor
            if (Parent is CallNode)
            {
                m_refType = (
                    ((CallNode)Parent).IsConstructor
                  ? ReferenceType.Constructor
                  : ReferenceType.Function
                    );
            }

            ActivationObject scope = ScopeStack.Peek();

            VariableField = scope.FindReference(m_name);
            if (VariableField == null)
            {
                // this must be a global. if it isn't in the global space, throw an error
                // this name is not in the global space.
                // if it isn't generated, then we want to throw an error
                // we also don't want to report an undefined variable if it is the object
                // of a typeof operator
                if (!m_isGenerated && !(Parent is TypeOfNode))
                {
                    // report this undefined reference
                    Context.ReportUndefined(this);

                    // possibly undefined global (but definitely not local)
                    Context.HandleError(
                        (Parent is CallNode && ((CallNode)Parent).Function == this ? JSError.UndeclaredFunction : JSError.UndeclaredVariable),
                        null,
                        false
                        );
                }

                if (!(scope is GlobalScope))
                {
                    // add it to the scope so we know this scope references the global
                    scope.AddField(new JSGlobalField(
                                       m_name,
                                       Missing.Value,
                                       0
                                       ));
                }
            }
            else
            {
                // BUT if this field is a place-holder in the containing scope of a named
                // function expression, then we need to throw an ambiguous named function expression
                // error because this could cause problems.
                // OR if the field is already marked as ambiguous, throw the error
                if (VariableField.NamedFunctionExpression != null ||
                    VariableField.IsAmbiguous)
                {
                    // mark it as a field that's referenced ambiguously
                    VariableField.IsAmbiguous = true;
                    // throw as an error
                    Context.HandleError(JSError.AmbiguousNamedFunctionExpression, true);

                    // if we are preserving function names, then we need to mark this field
                    // as not crunchable
                    if (Parser.Settings.PreserveFunctionNames)
                    {
                        VariableField.CanCrunch = false;
                    }
                }

                // see if this scope already points to this name
                if (scope[m_name] == null)
                {
                    // create an inner reference so we don't keep walking up the scope chain for this name
                    VariableField = scope.CreateInnerField(VariableField);
                }

                // add the reference
                VariableField.AddReference(scope);

                if (VariableField is JSPredefinedField)
                {
                    // this is a predefined field. If it's Nan or Infinity, we should
                    // replace it with the numeric value in case we need to later combine
                    // some literal expressions.
                    if (string.CompareOrdinal(m_name, "NaN") == 0)
                    {
                        // don't analyze the new ConstantWrapper -- we don't want it to take part in the
                        // duplicate constant combination logic should it be turned on.
                        Parent.ReplaceChild(this, new ConstantWrapper(double.NaN, PrimitiveType.Number, Context, Parser));
                    }
                    else if (string.CompareOrdinal(m_name, "Infinity") == 0)
                    {
                        // don't analyze the new ConstantWrapper -- we don't want it to take part in the
                        // duplicate constant combination logic should it be turned on.
                        Parent.ReplaceChild(this, new ConstantWrapper(double.PositiveInfinity, PrimitiveType.Number, Context, Parser));
                    }
                }
            }
        }
        /// <summary>
        /// Get instance of the parent delta feed scope
        /// </summary>
        /// <returns>
        /// The parent delta feed scope
        /// Or null if there is no parent delta feed scope
        /// </returns>
        private DeltaFeedScope GetParentDeltaFeedScope()
        {
            ScopeStack scopeStack = new ScopeStack();
            Scope parentFeedScope = null;

            if (this.scopes.Count > 0)
            {
                // pop current scope and push into scope stack
                scopeStack.Push(this.scopes.Pop());
            }

            while (this.scopes.Count > 0)
            {
                Scope scope = this.scopes.Pop();
                scopeStack.Push(scope);

                if (scope is DeltaFeedScope)
                {
                    parentFeedScope = scope;
                    break;
                }
            }

            while (scopeStack.Count > 0)
            {
                Scope scope = scopeStack.Pop();
                this.scopes.Push(scope);
            }

            return parentFeedScope as DeltaFeedScope;
        }
Example #8
0
        private bool ParseStatement(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            if (scope.Current == Scope.Statement)
            {
                scope.Pop();
            }
            else if (scope.Current == Scope.Block)
            {
                var previous = stream.Peek(-1);
                if (previous == '$' || char.IsLetterOrDigit(previous)) return false;
            }
            else
            {
                return false;
            }

            var name = stream.PeekWord();
            var identifier = context.Current.GetIdentifier(name);

            if (identifier != null)
            {
                tokens.Add(new Token(stream.Position, name.Length, stream.Line, TokenType.Identifier, context.Current, identifier.QuickInfo));
                stream.Advance(name.Length);

                if (identifier.Type == IdentifierType.Indexed)
                {
                    if (stream.Current == '(') scope.Push(Scope.Filter);
                    if (stream.Current == '[')
                    {
                        scope.Push(Scope.Template);
                        context.Push(name, stream.Position);
                    }
                }
                else if (identifier.Type == IdentifierType.Boolean)
                {
                    if (stream.Current == '[') scope.Push(Scope.True);
                }

                return true;
            }

            return false;
        }
        void ScanAndAddComment(List <ColoredSegment> coloredSegments, int startOffset, ScopeStack commentScopeStack, ClassificationSpan classificationSpan)
        {
            int lastClassifiedOffset = classificationSpan.Span.Start;

            try {
                // Scan comments for tag highlighting
                var text = textView.TextSnapshot.GetText(classificationSpan.Span);
                int idx = 0, oldIdx = 0;

                while ((idx = FindNextCommentTagIndex(text, idx, out string commentTag)) >= 0)
                {
                    var headSpanLength = idx - oldIdx;
                    if (headSpanLength > 0)
                    {
                        var headSegment = new ColoredSegment(lastClassifiedOffset - startOffset, headSpanLength, commentScopeStack);
                        lastClassifiedOffset += headSpanLength;
                        coloredSegments.Add(headSegment);
                    }
                    var highlightSegment = new ColoredSegment(lastClassifiedOffset - startOffset, commentTag.Length, commentScopeStack.Push("markup.other"));
                    coloredSegments.Add(highlightSegment);
                    idx += commentTag.Length;
                    lastClassifiedOffset += commentTag.Length;
                    oldIdx = idx;
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while scanning comment tags.", e);
            }
            int tailSpanLength = classificationSpan.Span.End - lastClassifiedOffset;

            if (tailSpanLength > 0)
            {
                var tailSpan = new ColoredSegment(lastClassifiedOffset - startOffset, tailSpanLength, commentScopeStack);
                coloredSegments.Add(tailSpan);
            }
        }
 public InterpretorVisitor()
 {
     Environment = new Stack<MemorySpace>();
     MemorySpaces = new ScopeStack<MemorySpace>();
 }
Example #11
0
 public void SetScopeByID(string id)
 {
     _currentScopeID = id;
     _currentScope   = _data[_currentScopeID];
 }
Example #12
0
        public VariableDeclaration(Context context, JSParser parser, string identifier, Context idContext, AstNode initializer, FieldAttributes fieldAttributes, bool ignoreDuplicates)
            : base(context, parser)
        {
            // identifier cannot be null
            m_identifier = identifier;

            // initializer may be null
            m_initializer = initializer;
            if (m_initializer != null)
            {
                m_initializer.Parent = this;
            }

            // we'll need to do special stuff if the initializer if a function expression,
            // so try the conversion now
            FunctionObject functionValue = m_initializer as FunctionObject;
            string         name          = m_identifier.ToString();

            ActivationObject currentScope  = ScopeStack.Peek();
            ActivationObject definingScope = currentScope;

            if (definingScope is BlockScope)
            {
                // block scope -- the variable is ACTUALLY defined in the containing function/global scope,
                // so we need to check THERE for duplicate defines.
                do
                {
                    definingScope = definingScope.Parent;
                } while (definingScope is BlockScope);
            }

            JSVariableField field = definingScope[name];

            if (field != null &&
                (functionValue == null || functionValue != field.FieldValue))
            {
                // this is a declaration that already has a field declared.
                // if the field is a named function expression, we want to fire an
                // ambiguous named function expression error -- and we know it's an NFE
                // if the FieldValue is a function object OR if the field
                // has already been marked ambiguous
                if (field.IsAmbiguous || field.FieldValue is FunctionObject)
                {
                    if (idContext != null)
                    {
                        idContext.HandleError(
                            JSError.AmbiguousNamedFunctionExpression,
                            true
                            );
                    }
                    else if (context != null)
                    {
                        // not identifier context???? Try the whole statment context.
                        // if neither context is set, then we don't get an error!
                        context.HandleError(
                            JSError.AmbiguousNamedFunctionExpression,
                            true
                            );
                    }

                    // if we are preserving function names, then we need to mark this field
                    // as not crunchable
                    if (Parser.Settings.PreserveFunctionNames)
                    {
                        field.CanCrunch = false;
                    }
                }
                else if (!ignoreDuplicates)
                {
                    if (idContext != null)
                    {
                        // otherwise just a normal duplicate error
                        idContext.HandleError(
                            JSError.DuplicateName,
                            field.IsLiteral
                            );
                    }
                    else if (context != null)
                    {
                        // otherwise just a normal duplicate error
                        context.HandleError(
                            JSError.DuplicateName,
                            field.IsLiteral
                            );
                    }
                }
            }

            bool isLiteral = ((fieldAttributes & FieldAttributes.Literal) != 0);

            // normally the value will be null.
            // but if there is no initializer, we'll use Missing so we can tell the difference.
            // and if this is a literal, we'll set it to the actual literal astnode
            object val = null;

            if (m_initializer == null)
            {
                val = Missing.Value;
            }
            else if (isLiteral || (functionValue != null))
            {
                val = m_initializer;
            }

            m_field = currentScope.DeclareField(
                m_identifier,
                val,
                fieldAttributes
                );
            m_field.OriginalContext = idContext;

            // we are now declared by a var statement
            m_field.IsDeclared = true;

            // if we are declaring a variable inside a with statement, then we will be declaring
            // a local variable in the enclosing scope if the with object doesn't have a property
            // of that name. But if it does, we won't actually be creating a variable field -- we'll
            // just use the property. So if we use an initializer in this declaration, then we will
            // actually be referencing the value.
            // SO, if this is a with-scope and this variable declaration has an initializer, we're going
            // to go ahead and bump up the reference.
            if (currentScope is WithScope && m_initializer != null)
            {
                m_field.AddReference(currentScope);
            }

            // special case the ambiguous function expression test. If we are var-ing a variable
            // with the same name as the function expression, then it's okay. We won't have an ambiguous
            // reference and it will be okay to use the name to reference the function expression
            if (functionValue != null && string.CompareOrdinal(m_identifier, functionValue.Name) == 0)
            {
                // null out the link to the named function expression
                // and make the function object point to the PROPER variable: the local within its own scope
                // and the inner is not pointing to the outer.
                functionValue.DetachFromOuterField(false);
                m_field.IsFunction = false;
            }
        }
Example #13
0
 protected void Visit(VariableReferenceExpression variableRef)
 {
     ((ScopeDeclarationWithRef)ScopeStack.Peek()).VariableReferences.Add(variableRef);
 }
 internal DynamicOperatorTransformer(ScopeStack <TScope, TRootScope> scopeStack, ILanguageDefinition languageDefinition, DynamicTransformer <TScope, TRootScope> nodeTransformer)
 {
     LanguageDefinition = languageDefinition;
     NodeTransformer    = nodeTransformer;
     ScopeStack         = scopeStack;
 }
Example #15
0
 public static TextMateLanguage Create(ScopeStack scope) => new TextMateLanguage(scope);
 internal static bool IsCurrentLoggingScope(LoggingScope scope)
 => !ScopeStack.IsEmpty && ScopeStack.Peek().LoggingScope == scope;
Example #17
0
        Dictionary <string, ScopeStack> GetClassificationMap(string scope)
        {
            Dictionary <string, ScopeStack> result;
            var baseScopeStack = new ScopeStack(scope);

            defaultScopeStack = baseScopeStack.Push(EditorThemeColors.Foreground);
            if (classificationMapCache.TryGetValue(scope, out result))
            {
                return(result);
            }
            result = new Dictionary <string, ScopeStack> {
                [ClassificationTypeNames.Comment]             = MakeScope(baseScopeStack, "comment." + scope),
                [ClassificationTypeNames.ExcludedCode]        = MakeScope(baseScopeStack, "comment.excluded." + scope),
                [ClassificationTypeNames.Identifier]          = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.Keyword]             = MakeScope(baseScopeStack, "keyword." + scope),
                [ClassificationTypeNames.NumericLiteral]      = MakeScope(baseScopeStack, "constant.numeric." + scope),
                [ClassificationTypeNames.Operator]            = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.PreprocessorKeyword] = MakeScope(baseScopeStack, "meta.preprocessor." + scope),
                [ClassificationTypeNames.StringLiteral]       = MakeScope(baseScopeStack, "string." + scope),
                [ClassificationTypeNames.WhiteSpace]          = MakeScope(baseScopeStack, "text." + scope),
                [ClassificationTypeNames.Text] = MakeScope(baseScopeStack, "text." + scope),

                [ClassificationTypeNames.PreprocessorText]      = MakeScope(baseScopeStack, "meta.preprocessor.region.name." + scope),
                [ClassificationTypeNames.Punctuation]           = MakeScope(baseScopeStack, "punctuation." + scope),
                [ClassificationTypeNames.VerbatimStringLiteral] = MakeScope(baseScopeStack, "string.verbatim." + scope),

                [ClassificationTypeNames.ClassName]         = MakeScope(baseScopeStack, "entity.name.class." + scope),
                [ClassificationTypeNames.DelegateName]      = MakeScope(baseScopeStack, "entity.name.delegate." + scope),
                [ClassificationTypeNames.EnumName]          = MakeScope(baseScopeStack, "entity.name.enum." + scope),
                [ClassificationTypeNames.InterfaceName]     = MakeScope(baseScopeStack, "entity.name.interface." + scope),
                [ClassificationTypeNames.ModuleName]        = MakeScope(baseScopeStack, "entity.name.module." + scope),
                [ClassificationTypeNames.StructName]        = MakeScope(baseScopeStack, "entity.name.struct." + scope),
                [ClassificationTypeNames.TypeParameterName] = MakeScope(baseScopeStack, "entity.name.typeparameter." + scope),

                [ClassificationTypeNames.FieldName]           = MakeScope(baseScopeStack, "entity.name.field." + scope),
                [ClassificationTypeNames.EnumMemberName]      = MakeScope(baseScopeStack, "entity.name.enummember." + scope),
                [ClassificationTypeNames.ConstantName]        = MakeScope(baseScopeStack, "entity.name.constant." + scope),
                [ClassificationTypeNames.LocalName]           = MakeScope(baseScopeStack, "entity.name.local." + scope),
                [ClassificationTypeNames.ParameterName]       = MakeScope(baseScopeStack, "entity.name.parameter." + scope),
                [ClassificationTypeNames.ExtensionMethodName] = MakeScope(baseScopeStack, "entity.name.extensionmethod." + scope),
                [ClassificationTypeNames.MethodName]          = MakeScope(baseScopeStack, "entity.name.function." + scope),
                [ClassificationTypeNames.PropertyName]        = MakeScope(baseScopeStack, "entity.name.property." + scope),
                [ClassificationTypeNames.EventName]           = MakeScope(baseScopeStack, "entity.name.event." + scope),

                [ClassificationTypeNames.XmlDocCommentAttributeName]         = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentAttributeQuotes]       = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentAttributeValue]        = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentCDataSection]          = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentComment]               = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentDelimiter]             = MakeScope(baseScopeStack, "comment.line.documentation.delimiter." + scope),
                [ClassificationTypeNames.XmlDocCommentEntityReference]       = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentName]                  = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentProcessingInstruction] = MakeScope(baseScopeStack, "comment.line.documentation." + scope),
                [ClassificationTypeNames.XmlDocCommentText]                  = MakeScope(baseScopeStack, "comment.line.documentation." + scope),

                [ClassificationTypeNames.XmlLiteralAttributeName]      = MakeScope(baseScopeStack, "entity.other.attribute-name." + scope),
                [ClassificationTypeNames.XmlLiteralAttributeQuotes]    = MakeScope(baseScopeStack, "punctuation.definition.string." + scope),
                [ClassificationTypeNames.XmlLiteralAttributeValue]     = MakeScope(baseScopeStack, "string.quoted." + scope),
                [ClassificationTypeNames.XmlLiteralCDataSection]       = MakeScope(baseScopeStack, "text." + scope),
                [ClassificationTypeNames.XmlLiteralComment]            = MakeScope(baseScopeStack, "comment.block." + scope),
                [ClassificationTypeNames.XmlLiteralDelimiter]          = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.XmlLiteralEmbeddedExpression] = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.XmlLiteralEntityReference]    = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.XmlLiteralName] = MakeScope(baseScopeStack, "entity.name.tag.localname." + scope),
                [ClassificationTypeNames.XmlLiteralProcessingInstruction] = MakeScope(baseScopeStack, scope),
                [ClassificationTypeNames.XmlLiteralText] = MakeScope(baseScopeStack, "text." + scope),
            };
            classificationMapCache = classificationMapCache.SetItem(scope, result);

            return(result);
        }
 internal StringRenderTransformer(JinjaEnvironment environment, ScopeStack <DynamicContext, DynamicRootContext> scopes)
 {
     Scopes      = scopes;
     Environment = environment;
     ExpressionParserTransformer = Environment.Evaluation.CreateDynamicTransformer(Scopes);
 }
 static ScopeStack MakeScope(ScopeStack defaultScope, string scope)
 {
     return(defaultScope.Push(scope));
 }
        public void New_Stack_has_Depth_of_1( )
        {
            var stack = new ScopeStack <int>( );

            Assert.AreEqual(1, stack.Depth);
        }
Example #21
0
        private bool ParseFilter(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            //if (scope.Current != Scope.Filter) return false;

            //scope.Pop();

            //tokens.Add(new Token(stream.Position, 1, TokenType.OpenFunctionBrace, context.Current));

            //while (stream.Current != ')')
            //    if (stream.Advance() == false) return true;

            //tokens.Add(new Token(stream.Position, 1, TokenType.CloseFunctionBrace, context.Current));

            //stream.Advance();

            //if (stream.Current == '[') scope.Push(Scope.Template);

            //return true;
            return false;
        }
Example #22
0
        private bool ParseStatement(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            if (scope.Current == Scope.Statement)
            {
                scope.Pop();
            }
            else if (scope.Current == Scope.Block)
            {
                var previous = stream.Peek(-1);
                if (previous == '$' || char.IsLetterOrDigit(previous))
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            var name       = stream.PeekWord();
            var identifier = context.Current.GetIdentifier(name);

            if (identifier != null)
            {
                tokens.Add(new Token(stream.Position, name.Length, stream.Line, TokenType.Identifier, context.Current, identifier.QuickInfo));
                stream.Advance(name.Length);

                if (identifier.Type == IdentifierType.Indexed)
                {
                    if (stream.Current == '(')
                    {
                        scope.Push(Scope.Filter);
                    }
                    if (stream.Current == '[')
                    {
                        scope.Push(Scope.Template);
                        context.Push(name, stream.Position);
                    }
                }
                else if (identifier.Type == IdentifierType.Boolean)
                {
                    if (stream.Current == '[')
                    {
                        scope.Push(Scope.True);
                    }
                }

                return(true);
            }

            return(false);
        }
Example #23
0
        private bool ParseOther(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope, BraceStack brace)
        {
            switch (stream.Current)
            {
                case '[':
                    brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenBrace, context.Current)), scope.Changed);
                    stream.Advance();
                    return true;

                case ']':
                    var openBrace = brace.Pop(TokenType.OpenBrace);
                    var token = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseBrace, context.Current, null, openBrace.Token));
                    if (openBrace.Token != null)
                    {
                        openBrace.Token.MatchingToken = token;
                    }
                    stream.Advance();

                    if (openBrace.ScopeChanged)
                    {
                        var current = scope.Pop();
                        if (current == Scope.Template)
                        {
                            context.Pop(stream.Position);
                            if (stream.Current == '[') scope.Push(Scope.Separator);
                        }
                        else if (current == Scope.True)
                        {
                            context.Pop(stream.Position);
                            if (stream.Current == '[') scope.Push(Scope.False);
                        }
                    }
                    return true;

                case '{':
                    //tokens.Add(new Token(stream.Position - 1, 2, TokenType.OpenBlock, context.Current));
                    brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenCurlyBrace, context.Current)), scope.Changed);
                    stream.Advance();
                    return true;

                case '}':
                    var openCurlyBrace = brace.Pop(TokenType.OpenCurlyBrace);
                    token = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseCurlyBrace, context.Current, null, openCurlyBrace.Token));
                    if (openCurlyBrace.Token != null)
                    {
                        openCurlyBrace.Token.MatchingToken = token;
                    }
                    //tokens.Add(new Token(stream.Position, 1, TokenType.CloseBlock, context.Current));
                    stream.Advance();

                    if (openCurlyBrace.ScopeChanged)
                    {
                        scope.Pop();
                    }
                    return true;

                case '(':
                    brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenFunctionBrace, context.Current)), scope.Changed);
                    stream.Advance();
                    return true;

                case ')':
                    var openFunctionBrace = brace.Pop(TokenType.OpenFunctionBrace);
                    token = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseFunctionBrace, context.Current, null, openFunctionBrace.Token));
                    if (openFunctionBrace.Token != null)
                    {
                        openFunctionBrace.Token.MatchingToken = token;
                    }
                    stream.Advance();

                    if (openFunctionBrace.ScopeChanged)
                    {
                        scope.Pop();
                    }
                    return true;
            }

            if (scope.Current == Scope.Block) return false;

            var name = stream.PeekWord();

            if (name == null) return false;

            if (keywords.Contains(name))
            {
                tokens.Add(new Token(stream.Position, name.Length, stream.Line, TokenType.Keyword, context.Current));
            }

            stream.Advance(name.Length);
            return true;
        }
Example #24
0
        private bool ParseFilter(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            //if (scope.Current != Scope.Filter) return false;

            //scope.Pop();

            //tokens.Add(new Token(stream.Position, 1, TokenType.OpenFunctionBrace, context.Current));

            //while (stream.Current != ')')
            //    if (stream.Advance() == false) return true;

            //tokens.Add(new Token(stream.Position, 1, TokenType.CloseFunctionBrace, context.Current));

            //stream.Advance();

            //if (stream.Current == '[') scope.Push(Scope.Template);

            //return true;
            return(false);
        }
Example #25
0
        /// <summary>
        /// Get instance of the parent entry scope
        /// </summary>
        /// <returns>
        /// The parent entry scope
        /// Or null if there is no parent entry scope
        /// </returns>
        protected EntryScope GetParentEntryScope()
        {
            ScopeStack scopeStack = new ScopeStack();
            Scope parentEntryScope = null;

            if (this.scopes.Count > 0)
            {
                // pop current scope and push into scope stack
                scopeStack.Push(this.scopes.Pop());
            }

            while (this.scopes.Count > 0)
            {
                Scope scope = this.scopes.Pop();
                scopeStack.Push(scope);

                if (scope is EntryScope)
                {
                    parentEntryScope = scope;
                    break;
                }
            }

            while (scopeStack.Count > 0)
            {
                Scope scope = scopeStack.Pop();
                this.scopes.Push(scope);
            }

            return parentEntryScope as EntryScope;
        }
Example #26
0
        private bool ParseOther(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope, BraceStack brace)
        {
            switch (stream.Current)
            {
            case '[':
                brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenBrace, context.Current)), scope.Changed);
                stream.Advance();
                return(true);

            case ']':
                var openBrace = brace.Pop(TokenType.OpenBrace);
                var token     = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseBrace, context.Current, null, openBrace.Token));
                if (openBrace.Token != null)
                {
                    openBrace.Token.MatchingToken = token;
                }
                stream.Advance();

                if (openBrace.ScopeChanged)
                {
                    var current = scope.Pop();
                    if (current == Scope.Template)
                    {
                        context.Pop(stream.Position);
                        if (stream.Current == '[')
                        {
                            scope.Push(Scope.Separator);
                        }
                    }
                    else if (current == Scope.True)
                    {
                        context.Pop(stream.Position);
                        if (stream.Current == '[')
                        {
                            scope.Push(Scope.False);
                        }
                    }
                }
                return(true);

            case '{':
                //tokens.Add(new Token(stream.Position - 1, 2, TokenType.OpenBlock, context.Current));
                brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenCurlyBrace, context.Current)), scope.Changed);
                stream.Advance();
                return(true);

            case '}':
                var openCurlyBrace = brace.Pop(TokenType.OpenCurlyBrace);
                token = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseCurlyBrace, context.Current, null, openCurlyBrace.Token));
                if (openCurlyBrace.Token != null)
                {
                    openCurlyBrace.Token.MatchingToken = token;
                }
                //tokens.Add(new Token(stream.Position, 1, TokenType.CloseBlock, context.Current));
                stream.Advance();

                if (openCurlyBrace.ScopeChanged)
                {
                    scope.Pop();
                }
                return(true);

            case '(':
                brace.Push(tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.OpenFunctionBrace, context.Current)), scope.Changed);
                stream.Advance();
                return(true);

            case ')':
                var openFunctionBrace = brace.Pop(TokenType.OpenFunctionBrace);
                token = tokens.Add(new Token(stream.Position, 1, stream.Line, TokenType.CloseFunctionBrace, context.Current, null, openFunctionBrace.Token));
                if (openFunctionBrace.Token != null)
                {
                    openFunctionBrace.Token.MatchingToken = token;
                }
                stream.Advance();

                if (openFunctionBrace.ScopeChanged)
                {
                    scope.Pop();
                }
                return(true);
            }

            if (scope.Current == Scope.Block)
            {
                return(false);
            }

            var name = stream.PeekWord();

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

            if (keywords.Contains(name))
            {
                tokens.Add(new Token(stream.Position, name.Length, stream.Line, TokenType.Keyword, context.Current));
            }

            stream.Advance(name.Length);
            return(true);
        }
Example #27
0
        private bool ParseDollar(TemplateStream stream, TokenList tokens, ContextStack context, ScopeStack scope)
        {
            if (stream.Current != '$' || scope.Current == Scope.Block)
            {
                return(false);
            }

            var next = stream.Peek();

            if (next == '$')
            {
                stream.Advance(2);
                return(true);
            }

            if (char.IsLetter(next))
            {
                scope.Push(Scope.Statement);
                stream.Advance();
                return(true);
            }

            if (next == '{')
            {
                scope.Push(Scope.Block);
                stream.Advance();
                return(true);
            }

            return(false);
        }
Example #28
0
 public override Node Visit(VariableReferenceExpression variableRef)
 {
     ((ScopeDeclarationWithRef)ScopeStack.Peek()).VariableReferences.Add(variableRef);
     return(variableRef);
 }