Beispiel #1
0
        private static IList <Token> Parse0(string data)
        {
            var res    = new List <Token>();
            var buffer = data.EndsWith(Environment.NewLine)
                ? new CharBuffer(data)
                : new CharBuffer(data, Environment.NewLine);

            while (!buffer.EOF)
            {
                var p = buffer.Peek();
                if (CharBuffer.Blanks.Contains(p))
                {
                    var bl = buffer.PopBlanks();
                    res.Add(Token0.Blank(bl));
                    continue;
                }

                if (CharBuffer.EolSymbols.Contains(p))
                {
                    var bnl = buffer.PopEOL();
                    res.Add(Token0.NewLine());
                    continue;
                }

                if (p == '@')
                {
                    var(_, p2) = buffer.Peek2();
                    if (char.IsLetter(p2))
                    {
                        buffer.Pop();
                        var lv = buffer.PopLiteral();
                        res.Add(Token0.Variable("@" + lv));
                        continue;
                    }
                }

                if (char.IsLetterOrDigit(p) || p == '_')
                {
                    var l = buffer.PopLiteral();
                    res.Add(Token0.Literal(l));
                    continue;
                }

                var s = buffer.PopSymbol();
                res.Add(Token0.Symbol(s));
            }

            return(res);
        }
Beispiel #2
0
        private static Token ParseQuote(TokenBuffer buffer)
        {
            var res = new StringBuilder();
            var p   = buffer.PopStr();

            if (p != "\"")
            {
                throw new Exception("\" Expected");
            }
            res.Append(p);
            var tks = buffer.PopWhile(t => t.Value != "\"").Select(t => t.Value);

            res.Append(string.Concat(tks));
            if (buffer.PeekStr() == "\"")
            {
                res.Append(buffer.PopStr());
            }
            return(Token0.Quote(res.ToString()));
        }
Beispiel #3
0
        private static Token ParseExpression(TokenBuffer buffer)
        {
            var nivel = 1;
            var tks   = new List <Token>();

            tks.Add(buffer.Pop());
            while (!buffer.EOF && nivel > 0)
            {
                var p = buffer.PeekStr();
                if (p == "'")
                {
                    var p3 = buffer.Peek3Str();
                    if (p3 == "'('" || p3 == "')'")
                    {
                        tks.Add(Token0.Literal(buffer.Pop3Str()));
                    }
                    continue;
                }

                if (p == "\"")
                {
                    var v = ParseQuote(buffer);
                    tks.Add(v);
                    continue;
                }

                if (p == "(")
                {
                    nivel++;
                }
                if (p == ")")
                {
                    nivel--;
                }
                tks.Add(buffer.Pop());
            }

            return(Token1.Expression(tks));
        }
Beispiel #4
0
            private static string RenderIf(IList <Token> inner, IList <Token> tokens, out string error)
            {
                error = "";
                var not = false;

                if (inner.Count < 1)
                {
                    return(Error("Empty If", out error));
                }
                var pt = inner.First();

                tokens = tokens.SkipWhile(t => t != pt).ToList();
                if (pt.Value == "!")
                {
                    not = true;
                    inner.RemoveAt(0);
                    tokens.RemoveAt(0);
                    pt = inner.FirstOrDefault();
                    if (pt == null)
                    {
                        return(Error("Empty If !", out error));
                    }
                }

                if (pt is Token0.TokenVariable v)
                {
                    // FLAGS #if var
                    if (inner.Count == 1 && !v.Value.Contains('_'))
                    {
                        if (not)
                        {
                            return($"if (!(Flags[\"{v.RenderVariable()}\"])){{");
                        }
                        return($"if (Flags[\"{v.RenderVariable()}\"]){{");
                    }

                    if (inner.Count > 1)
                    {
                        // FLAGS #if var IN collection
                        var incol = inner[1];
                        if (string.Equals(incol.Value, "in", StringComparison.OrdinalIgnoreCase))
                        {
                            var rvalue = tokens.SkipWhile(t => t != incol).Skip(1).ToArray();
                            var liner  = ToStringRenderVar(rvalue);
                            if (not)
                            {
                                return($"if (!(new []{{{liner}}}).Contains({v.RenderVariable()})){{");
                            }
                            return($"if ((new []{{{liner}}}).Contains({v.RenderVariable()})){{");
                        }
                    }
                }

                if (pt.Value != "(")
                {
                    var p0 = Token0.Symbol("(");
                    var p1 = Token0.Symbol(")");
                    inner.Insert(0, p0);
                    inner.Add(p1);
                    tokens.Insert(0, p0);
                    tokens.Add(p1);
                }

                var line = ToStringRenderVar(tokens);

                if (not)
                {
                    return($"if (!{line}){{");
                }
                return($"if {line}{{");
            }