Esempio n. 1
0
        public Expression<Func<Any>> Parse()
        {
            ahead = scanner.Scan();

            if (ahead.Type == TokenType.Eof)
            {
                return null;
            }

            token = null;

            var program = ProgramRule();

            if (program == null)
            {
                ExpectError(ahead.Type);
            }

            while (TryExpect(TokenType.Eol))
            {

            }

            Expect(TokenType.Eof);

            //var folder = new ConstantFolder(runtime);
            //result = folder.Modify(result);

            return (Expression<Func<Any>>) program;
        }
Esempio n. 2
0
 private void Get()
 {
     token = ahead;
     ahead = scanner.Scan();
 }
Esempio n. 3
0
 private void DoDedent(Token source)
 {
     levels.Pop();
     var ded = new Token();
     ded.Type = TokenType.Dedent;
     ded.Col = source.Col;
     ded.Line = source.Line;
     pending.Enqueue(ded);
 }
Esempio n. 4
0
        private void DoIndent(int level, Token source)
        {
            var s = source.Value;

            if (s.Distinct().Count() > 1)
            {
                Console.WriteLine("Indent not same!");
                throw new Exception();
            }

            if (it == 0)
            {
                it = s[0];
            }
            else if (it != s[0])
            {
                Console.WriteLine("Indent not same!");
                throw new Exception();
            }

            levels.Push(level);
            var ind = new Token();
            ind.Type = TokenType.Indent;
            ind.Col = source.Col;
            ind.Line = source.Line;
            pending.Enqueue(ind);
        }
Esempio n. 5
0
        public Token Scan()
        {
            while (pending.Count == 0)
            {
                var token = Next();

                if (token == error)
                {
                    return null;
                }

                if (token == null)
                {
                    token = Create(TokenType.Eof);
                }

                switch (token.Type)
                {
                    case TokenType.Comment:
                    case TokenType.Ws:
                        break;
                    case TokenType.Eol:
                        {
                            var blankLine = last.Type == TokenType.Ws && last.Col == 1;
                            var commentLine = last.Type == TokenType.Comment && last.Col == 1;
                            if (token.Col > 1 && !blankLine && !commentLine && join == 0)
                            {
                                //Console.Write("[queued {0}]", token.Type);
                                pending.Enqueue(token);
                            }
                        }
                        break;
                    case TokenType.Eof:
                        {
                            if (last.Type != TokenType.Dummy && last.Type != TokenType.Eol)
                            {
                                var eol = Create(TokenType.Eol);
                                pending.Enqueue(eol);
                            }

                            while (levels.Peek() > 0) DoDedent(token);   

                            pending.Enqueue(token);
                        }
                        break;
                    case TokenType.LParen:
                    case TokenType.LBrack:
                        ++join;
                        pending.Enqueue(token);
                        break;
                    case TokenType.RParen:
                    case TokenType.RBrack:
                        --join;
                        pending.Enqueue(token);
                        break;                        
                    default:
                        {
                            if (last.Type == TokenType.Eol) 
                            {
                                while (levels.Peek() > 0) DoDedent(token);
                            } 
                            else if (last.Type == TokenType.Ws)
                            {                       
                                if (last.Col == 1 && join == 0)
                                {
                                    var level = last.Length;
                                    var curLevel = levels.Peek();
                                    if (level > curLevel)
                                    {         
                                        DoIndent(level, last);
                                    } 
                                    else if (level < curLevel)
                                    {
                                        do
                                        {
                                            DoDedent(token);
                                        }
                                        while (level < levels.Peek());
                                    }
                                }
                            }

                            pending.Enqueue(token);
                        }
                        break;
                }

                last = token;
            }

            return pending.Dequeue();
        }
Esempio n. 6
0
        public Scanner(string data)
        {
            this.data = data;

            col = 1;
            line = 1;
            p = 0;
            pe = data.Length;
            eof = data.Length;
            it = (char)0;
            levels = new Stack<int>();
            pending = new Queue<Token>();
            finished = false;
            join = 0;

            last = Create(TokenType.Dummy);
            error = Create(TokenType.Dummy);

#line 472 "Scanner.cs"
	{
	cs = scan_start;
	ts = -1;
	te = -1;
	act = 0;
	}

#line 416 "Scanner.rl"
            
            levels.Push(0);
        }
Esempio n. 7
0
        private Token Create(TokenType type)
        {
            var token = new Token();
            token.Type = type;
            token.Col = col;
            token.Line = line;

            switch (type)
            {
                case TokenType.Dummy:
                case TokenType.Indent:
                case TokenType.Dedent:
                case TokenType.Eof:
                    token.Length = -1;
                    token.Size = -1;
                    break;
                case TokenType.Ws:
                    {
                        token.Value = GetTokenString(ts, te);
                        token.Length = token.Value.Length;
                        var width = 4;
                        foreach (var c in token.Value)
                        {
                            switch (c)
                            {
                            case ' ':
                                col++;
                                break;
                            case '\t':
                                col = ((col + width) & ~(width - 1)) + 1;
                                break;
                            }
                        }
                        break;
                    }
                case TokenType.Eol:
                    col = 1;
                    line++;
                    break;
                case TokenType.String:
                    token.Value = GetTokenString(ts, te);
                    token.Length = token.Value.Length;
                    for (var i = ts; i != te; i++)
                    {
                        switch (data[i])
                        {
                            case '\r':
                                col = 1;
                                line++;
                                if (data[i + 1] == '\n') i++;  
                                break;          
                            case '\n':
                                col = 1;
                                line++;
                                break;
                            default:
                                col++;  
                                break;  
                        }           
                    }
                    break;
                default:
                    token.Value = GetTokenString(ts, te);
                    token.Length = token.Value.Length;
                    col += token.Length;
                    break;
            }

            return token;
        }