Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LispVariant"/> class.
 /// </summary>
 /// <param name="token">The token.</param>
 /// <param name="unQuoted">The unquoted modus.</param>
 internal LispVariant(LispToken token, LispUnQuoteModus unQuoted = LispUnQuoteModus.None)
     : this(TypeOf(token.Value), token.Value, unQuoted)
 {
     Token = token;
     if (token.Type == LispTokenType.Nil)
     {
         Type = LispType.Nil;
     }
     if (token.Type == LispTokenType.Symbol)
     {
         Type = LispType.Symbol;
     }
 }
Esempio n. 2
0
        public LispToken GetPreviousToken(LispToken token)
        {
            LispToken previous = null;

            if (Tokens != null)
            {
                foreach (var item in Tokens)
                {
                    if (item == token)
                    {
                        return(previous);
                    }
                    previous = item;
                }
            }
            return(null);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LispException" /> class.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="token">The token.</param>
 /// <param name="moduleName">Name of the module.</param>
 /// <param name="stackInfo">The stack information.</param>
 public LispException(string text, LispToken token, string moduleName, string stackInfo = "not available")
     : base(text)
 {
     this.AddModuleNameAndStackInfos(moduleName, stackInfo);
     this.AddTokenInfos(token);
 }
Esempio n. 4
0
 /// <summary>
 /// Add infos about given token to exception data.
 /// </summary>
 /// <param name="ex">The exception.</param>
 /// <param name="token">The token.</param>
 public static void AddTokenInfos(this Exception ex, LispToken token)
 {
     ex.Data[LineNo]   = token != null ? token.LineNo : -1;
     ex.Data[StartPos] = token != null ? token.StartPos : -1;
     ex.Data[StopPos]  = token != null ? token.StopPos : -1;
 }
Esempio n. 5
0
        private static int ParseTokens(string moduleName, IList <LispToken> tokens, int startIndex, ref object parseResult, bool isToplevel)
        {
            int           i;
            List <object> current   = null;
            var           listStack = new Stack <List <object> >();

            for (i = startIndex; i < tokens.Count; i++)
            {
                LispToken token = tokens[i];
                if (token.Type == LispTokenType.ListStart)
                {
                    current = new List <object>();
                    listStack.Push(current);
                }
                else if (token.Type == LispTokenType.ListEnd)
                {
                    var temp = current;
                    listStack.Pop();
                    if (listStack.Count > 0)
                    {
                        listStack.Peek().Add(temp);
                        current = listStack.Peek();
                    }
                    else
                    {
                        if (isToplevel && i + 1 < tokens.Count && !OnlyCommentTokensFrom(tokens, i + 1))
                        {
                            throw new LispException(BracketsOutOfBalanceOrUnexpectedScriptCode, token, moduleName);
                        }
                        parseResult = current;
                        return(i);
                    }
                }
                else if (token.Type == LispTokenType.Quote || token.Type == LispTokenType.QuasiQuote)
                {
                    var quote = new List <object>();
                    quote.Add(new LispVariant(LispType.Symbol, token.Type == LispTokenType.Quote ? LispEnvironment.Quote : LispEnvironment.Quasiquote));

                    object quotedList = null;
                    i = ParseTokens(moduleName, tokens, i + 1, ref quotedList, isToplevel: false);
                    quote.Add(quotedList);

                    if (current != null)
                    {
                        current.Add(quote);
                    }
                }
                else if (token.Type == LispTokenType.UnQuote || token.Type == LispTokenType.UnQuoteSplicing)
                {
                    var unquote = new List <object>();
                    LispUnQuoteModus unquotedModus = token.Type == LispTokenType.UnQuote ? LispUnQuoteModus.UnQuote : LispUnQuoteModus.UnQuoteSplicing;
                    unquote.Add(new LispVariant(LispType.Symbol, token.Type == LispTokenType.UnQuote ? LispEnvironment.UnQuote : LispEnvironment.UnQuoteSplicing));

                    object quotedList = null;
                    i = ParseTokens(moduleName, tokens, i + 1, ref quotedList, isToplevel: false);
                    unquote.Add(quotedList);

                    if (current != null)
                    {
                        current.Add(unquote);
                    }
                    else
                    {
                        parseResult = unquote;
                        return(i);
                    }
                }
                else if (token.Type == LispTokenType.Comment)
                {
                    // just ignore comment
                }
                else
                {
                    if (!isToplevel && current == null)
                    {
                        parseResult = new LispVariant(token);
                        return(i);
                    }
                    if (current == null)
                    {
                        throw new LispException(UnexpectedToken, token, moduleName);
                    }
                    current.Add(new LispVariant(token));
                }
            }

            if (isToplevel && tokens.Count > 0)
            {
                LispToken token = tokens.Last();
                throw new LispException(BracketsOutOfBalance, token, moduleName);
            }

            parseResult = current;
            return(i);
        }