Example #1
0
 protected override LNode MakeTriviaAttribute(Token t)
 {
     if (t.Type() == TokenType.PPregion)
     {
         return(LNode.Trivia(S.TriviaRegion, t.Value.ToString(), t.Range(SourceFile)));
     }
     else if (t.Type() == TokenType.PPendregion)
     {
         return(LNode.Trivia(S.TriviaEndRegion, t.Value.ToString(), t.Range(SourceFile)));
     }
     return(base.MakeTriviaAttribute(t));
 }
        /// <summary>Called to transform a trivia token into a trivia attribute.</summary>
        /// <remarks>If a trivia token is not recognized, null is returned to ignore the trivia.</remarks>
        protected virtual LNode MakeTriviaAttribute(Token t)
        {
            if (t.TypeInt == NewlineTypeInt)
            {
                return(_trivia_newline);
            }
            else
            {
                Symbol  commentType = null;
                UString text;
                if (t.Value == null || t.Value == WhitespaceTag.Value)
                {
                    text = SourceFile.Text.Slice(t.StartIndex, t.Length);
                }
                else
                {
                    text = t.Value is UString ? ((UString)t.Value) : t.Value.ToString();
                }

                if (MLCommentPrefix != null && text.StartsWith(MLCommentPrefix))
                {
                    commentType = S.TriviaMLComment;
                    text        = text.Substring(MLCommentPrefix.Length);
                    if (text.EndsWith(MLCommentSuffix))
                    {
                        text = text.Left(text.Length - MLCommentSuffix.Length);
                    }
                }
                else if (SLCommentPrefix != null && text.StartsWith(SLCommentPrefix))
                {
                    commentType = S.TriviaSLComment;
                    text        = text.Substring(SLCommentPrefix.Length);
                    if (SLCommentSuffix != null && text.EndsWith(SLCommentSuffix))
                    {
                        text = text.Left(text.Length - SLCommentSuffix.Length);
                    }
                }
                if (commentType == null)
                {
                    return(null);
                }
                return(LNode.Trivia(commentType, text.ToString(), t.Range(SourceFile)));
            }
        }
Example #3
0
        /// <summary>Converts a <see cref="Token"/> to a <see cref="LNode"/>.</summary>
        /// <param name="file">This becomes the <see cref="LNode.Source"/> property.</param>
        /// <remarks>If you really need to store tokens as LNodes, use this. Only
        /// the <see cref="Kind"/>, not the TypeInt, is preserved. Identifiers
        /// (where Kind==TokenKind.Id and Value is Symbol) are translated as Id
        /// nodes; everything else is translated as a call, using the TokenKind as
        /// the <see cref="LNode.Name"/> and the value, if any, as parameters. For
        /// example, if it has been treeified with <see cref="TokensToTree"/>, the
        /// token list for <c>"Nodes".Substring(1, 3)</c> as parsed by LES might
        /// translate to the LNode sequence <c>String("Nodes"), Dot(@@.),
        /// Substring, LParam(Number(1), Separator(@@,), Number(3)), RParen()</c>.
        /// The <see cref="LNode.Range"/> will match the range of the token.
        /// </remarks>
        public LNode ToLNode(ISourceFile file)
        {
            var    kind = Kind;
            Symbol kSym = GSymbol.Empty;
            Symbol id;

            if (kind != TokenKind.Id)
            {
                int k = (int)kind >> TokenKindShift;
                kSym = _kindAttrTable.TryGet(k, null);
            }

            var r = new SourceRange(file, StartIndex, Length);
            var c = Children;

            if (c != null)
            {
                if (c.Count != 0)
                {
                    r = new SourceRange(file, StartIndex, System.Math.Max(EndIndex, c.Last.EndIndex) - StartIndex);
                }
                return(LNode.Call(kSym, c.ToLNodes(), r, Style));
            }
            else if (IsOpenerOrCloser(kind) || Value == WhitespaceTag.Value)
            {
                return(LNode.Call(kSym, VList <LNode> .Empty, r, Style));
            }
            else if (kind == TokenKind.Id && (id = this.Value as Symbol) != null)
            {
                return(LNode.Id(id, r, Style));
            }
            else
            {
                return(LNode.Trivia(kSym, this.Value, r, Style));
            }
        }
Example #4
0
 /// <summary>Creates a trivia node with the specified Value attached.</summary>
 /// <seealso cref="LNode.Trivia(Symbol, object, LNode)"/>
 public LNode Trivia(Symbol name, object value, int startIndex = -1, int endIndex = -1)
 {
     return(LNode.Trivia(name, value, new SourceRange(_file, startIndex, endIndex - startIndex)));
 }
Example #5
0
        /// <summary>Creates a trivia node named <c>"#trivia_" + suffix</c> with the
        /// specified Value attached.</summary>
        /// <remarks>This method only adds the prefix <c>#trivia_</c> if it is not
        /// already present in the 'suffix' argument.</remarks>
        public LNode Trivia(string suffix, object value)
        {
            string name = suffix.StartsWith("#trivia_") ? suffix : "#trivia_" + suffix;

            return(LNode.Trivia(GSymbol.Get(name), value, new SourceRange(_file)));
        }