Exemplo n.º 1
0
        public CToken Read()
        {
            if (tokenPrev.Type != CTokenType.EOF)
            {
                var t = tokenPrev;
                tokenPrev = new CToken(CTokenType.EOF);
                return t;
            }

            var token = lexer.Read();
            string lastString = null;
            var state = State.Start;
            StringBuilder sb = null;
            for (; ; )
            {
                switch (state)
                {
                case State.Start:
                    if (token.Type == CTokenType.LineDirective)
                    {
                        Expect(CTokenType.NumericLiteral);
                        Expect(CTokenType.StringLiteral);
                        token = lexer.Read();
                    }
                    else if (token.Type == CTokenType.PragmaDirective)
                    {
                        token = ReadPragma((string) lexer.Read().Value);
                    }
                    else if (token.Type == CTokenType.__Pragma)
                    {
                        Expect(CTokenType.LParen);
                        token = ReadPragma((string) lexer.Read().Value);
                        token = lexer.Read();
                    }
                    else if (token.Type == CTokenType.StringLiteral)
                    {
                        state = State.Strings;
                        lastString = (string) token.Value;
                    }
                    else
                    {
                        return token;
                    }
                    break;
                case State.Strings:
                    if (token.Type == CTokenType.StringLiteral)
                    {
                        if (lastString != null)
                        {
                            sb = new StringBuilder(lastString);
                            lastString = null;
                        }
                        else
                        {
                            sb.Append(token.Value);
                        }
                        token = lexer.Read();
                    }
                    else
                    {
                        tokenPrev = token;
                        if (lastString != null)
                        {
                            var tok = new CToken(CTokenType.StringLiteral, lastString);
                            lastString = null;
                            return tok;
                        }
                        else
                        {
                            return new CToken(CTokenType.StringLiteral, sb.ToString());
                        }
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
 private object Expect(CTokenType expected, CToken actualToken)
 {
     if (actualToken.Type != expected)
         throw new FormatException(string.Format("Expected '{0}' but got '{1}'.", expected, actualToken.Type));
     return actualToken.Value;
 }
Exemplo n.º 3
0
 public CDirectiveLexer(ParserState state, CLexer lexer)
 {
     this.state = state;
     this.lexer = lexer;
     this.tokenPrev = new CToken(    CTokenType.EOF);
 }
Exemplo n.º 4
0
 public bool IsTypeName(CToken x)
 {
     if (x.Type != CTokenType.Id)
         return false;
     return ParserState.Typedefs.Contains((string)x.Value);
 }