Esempio 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;
                }
            }
        }
Esempio n. 2
0
        public static string?GetName(IEnumerable <DeclSpec> declspecs, Declarator declarator, ParserState state)
        {
            var ndte = new NameExtractor();

            if (declarator != null)
            {
                return(declarator.Accept(ndte));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 3
0
 public CDirectiveLexer(ParserState state, CLexer lexer)
 {
     this.state     = state;
     this.lexer     = lexer;
     this.tokenPrev = new CToken(CTokenType.EOF);
 }