Пример #1
0
        // TODO: Thoroughly test using stringMode = true
        void AssertParseResult(string code, Expression expected, bool stringMode = false)
        {
            var src  = new Uno.Compiler.SourceFile("test.cs", code);
            var expr = Parser.Parse(src, code, stringMode);

            AssertExtensions.AreEqualValues(expected, expr);
        }
Пример #2
0
        static Expression ParseStringExpression(Uno.Compiler.SourceFile file, string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return(null);
            }

            var parts = new List <Expression>();
            int p     = 0;

            for (int i = 0; i < code.Length; i++)
            {
                if (code[i] == '{' && (i == 0 || code[i - 1] != '\\'))
                {
                    if (i != p)
                    {
                        parts.Add(new StringLiteral(code.Substring(p, i - p)));
                    }

                    // Skip to end of binding expression
                    int start = i;
                    i++;
                    for (int scopes = 1; scopes > 0 && i < code.Length; i++)
                    {
                        if (code[i] == '}' && code[i - 1] != '\\')
                        {
                            scopes--;
                        }
                        if (code[i] == '{' && code[i - 1] != '\\')
                        {
                            scopes++;
                        }
                    }

                    var binding = code.Substring(start, i - start);
                    var be      = Parse(new Uno.Compiler.SourceFile(file.FullPath, binding), binding, false);
                    if (be == null)
                    {
                        throw new Exception("Expected binding expression after '{'");
                    }
                    parts.Add(be);
                    p = i;
                }
            }
            if (p < code.Length)
            {
                parts.Add(new StringLiteral(code.Substring(p, code.Length - p)));
            }

            return(parts.Count > 1
                ? AddExpression.Create(parts.ToArray())
                : parts[0]);
        }
Пример #3
0
 public static Expression Parse(Uno.Compiler.SourceFile file, string code, bool stringMode)
 {
     if (stringMode)
     {
         if (string.IsNullOrEmpty(code))
         {
             return(new StringLiteral(""));
         }
         return(ParseStringExpression(file, code));
     }
     else
     {
         if (string.IsNullOrEmpty(code))
         {
             throw new Exception("Expression cannot be empty");
         }
         return(new Parser(file, code).ParseCompleteSourceAsExpression());
     }
 }
Пример #4
0
        void AssertParseThrows <T>(string code, bool stringMode = false) where T : Exception
        {
            var src = new Uno.Compiler.SourceFile("test.cs", code);

            Assert.Throws <T>(() => Parser.Parse(src, code, stringMode));
        }
Пример #5
0
 Parser(Uno.Compiler.SourceFile file, string code)
 {
     _tokens = Lexer.Lexer.Tokenize(file, code);
 }