Esempio n. 1
0
 public static IAction LoadFromString(string location, string source)
 {
     using (var lexer = new Lexer(location, source))
         return lexer.Scan();
 }
Esempio n. 2
0
        public IAction Scan()
        {
            if (_source == null)
                using (var sr = new StreamReader(FileSystem.GetFile(_location).Name))
                    _source = sr.ReadToEnd();

            var aggregate = new Aggregate();

            while (_offset < _source.Length)
            {
                // stops for the end of file or {
                ForwardTag();

                if (_offset >= _source.Length)
                    return aggregate;

                _offset++;

                var sequence = new Sequence();

                LexTag();

                if (_token.type == TokenType.Identifier)
                    sequence.Params.Add(new Constant(_token.value));
                else if (_token.type == TokenType.String)
                    sequence.Params.Add(new Constant(_token.value));
                else
                {
                    ErrorAction("Expecting a value (variable location, string, or number)");
                    return null;
                }

                LexTag();

                if (_token.type != TokenType.Separator)
                {
                    ErrorAction("Expecting a method name (this should be something like .Assign())");
                    return null;
                }

                LexTag();

                if (_token.type != TokenType.Identifier)
                {
                    ErrorAction("An operation must be defined (this should be something like .Assign())");
                    return null;
                }

                var functionName = _token.value;

                LexTag();

                if (_token.type != TokenType.ArgumentOpen)
                {
                    ErrorAction("Expecting \"(\"");
                    return null;
                }

                LexTag();

                if (_token.type != TokenType.ArgumentClose)
                {
                    Console.WriteLine("Expecting \")\"");
                    Console.WriteLine(PrintToken('!'));
                    return null;
                }

                LexTag();

                if (_token.type == TokenType.EOF)
                {
                    ErrorAction("Reached the end of file too soon, expecting more content");
                    return null;
                }

                if (_token.type == TokenType.Error)
                {
                    ErrorAction(_token.value);
                    return null;
                }

                //Newline found within tag (skipping tag)
                if (_token.type == TokenType.Newline)
                    continue;

                // _token.type should always equal TokenType.TagClose at this point
                if (_token.type != TokenType.TagClose)
                    throw new Exception("Unknown State\n" + PrintToken('!'));

                if (functionName == "Import")
                {
                    if (!File.Exists(_location))
                    {
                        ErrorAction("Import statement used and current location does not exist: " + _location);
                        return null;
                    }

                    if (!(sequence.Params[0] is Constant))
                    {
                        ErrorAction("Import statement requires a constant parameter");
                        return null;
                    }

                    // strip the filename off our path
                    var currentFile = new FileInfo(_location);
                    var pathToImport = ((Constant)sequence.Params[0]).Value.ToString();
                    var newPath = Path.Combine(currentFile.Directory.FullName, pathToImport);
                    // lex it
                    var lexer = new Lexer(newPath);
                    var result = lexer.Scan();
                    aggregate.Params.AddRange(result.Params);

                    continue;
                }

                var inner = ScanInner();
                if (inner == null)
                    return null;

                LexTag();

                if (_token.type != TokenType.Identifier || _token.value != functionName)
                {
                    ErrorAction("Expecting {/" + functionName + "}");
                    return null;
                }

                var func = ActionHandlers
                    .Where(p => p.Key.Type == ActionType.External
                        && p.Key.Identifier == functionName
                        && p.Key.HasParameter == false)
                    .Select(p => p.Value)
                    .FirstOrNull();

                if (func == null)
                {
                    ErrorAction("Unknown initial function");
                    return null;
                }

                var action = (IBlock)func();
                action.Content = inner;
                sequence.Params.Add(action);

                LexTag();

                if (_token.type != TokenType.TagClose)
                {
                    ErrorAction("Expecting \"}\"");
                    return null;
                }

                aggregate.Params.Add(sequence);
            }

            return aggregate;
        }
Esempio n. 3
0
 public static IAction LoadFromFile(string location)
 {
     using (var lexer = new Lexer(location))
         return lexer.Scan();
 }