コード例 #1
0
        private void ParseSegments(AstContext context)
        {
            var exprParser = new Parser(context.Language, _templateSettings.ExpressionRoot);
            // As we go along the "value text" (that has all escapes done), we track the position in raw token text  in the variable exprPosInTokenText.
            // This position is position in original text in source code, including original escaping sequences and open/close quotes.
            // It will be passed to segment constructor, and maybe used later to compute the exact position of runtime error when it occurs.
            int currentPos = 0, exprPosInTokenText = 0;

            while (true)
            {
                var startTagPos = _template.IndexOf(_templateSettings.StartTag, currentPos);
                if (startTagPos < 0)
                {
                    startTagPos = _template.Length;
                }
                var text = _template.Substring(currentPos, startTagPos - currentPos);
                if (!string.IsNullOrEmpty(text))
                {
                    _segments.Add(new TemplateSegment(text, null, 0)); //for text segments position is not used
                }
                if (startTagPos >= _template.Length)
                {
                    break; //from while
                }
                //We have a real start tag, grab the expression
                currentPos = startTagPos + _templateSettings.StartTag.Length;
                var endTagPos = _template.IndexOf(_templateSettings.EndTag, currentPos);
                if (endTagPos < 0)
                {
                    //"No ending tag '{0}' found in embedded expression."
                    context.AddMessage(ErrorLevel.Error, this.Location, Resources.ErrNoEndTagInEmbExpr, _templateSettings.EndTag);
                    return;
                }
                var exprText = _template.Substring(currentPos, endTagPos - currentPos);
                if (!string.IsNullOrEmpty(exprText))
                {
                    //parse the expression
                    //_expressionParser.context.Reset();

                    var exprTree = exprParser.Parse(exprText);
                    if (exprTree.HasErrors())
                    {
                        //we use original search in token text instead of currentPos in template to avoid distortions caused by opening quote and escaped sequences
                        var baseLocation = this.Location + _tokenText.IndexOf(exprText);
                        CopyMessages(exprTree.ParserMessages, context.Messages, baseLocation, Resources.ErrInvalidEmbeddedPrefix);
                        return;
                    }
                    //add the expression segment
                    exprPosInTokenText = _tokenText.IndexOf(_templateSettings.StartTag, exprPosInTokenText) + _templateSettings.StartTag.Length;
                    var segmNode = exprTree.Root.AstNode as AstNode;
                    segmNode.Parent = this; //important to attach the segm node to current Module
                    _segments.Add(new TemplateSegment(null, segmNode, exprPosInTokenText));
                    //advance position beyond the expression
                    exprPosInTokenText += exprText.Length + _templateSettings.EndTag.Length;
                } //if
                currentPos = endTagPos + _templateSettings.EndTag.Length;
            }     //while
        }
コード例 #2
0
 public override void Init(AstContext context, ParseTreeNode treeNode)
 {
     base.Init(context, treeNode);
     if (!Exist1)
     {
         context.AddMessage(Irony.ErrorLevel.Error, Location, @"""Enter"" missing character list");
         return;
     }
 }