示例#1
0
文件: Node.cs 项目: paulens12/C--
        public static LinkedListNode<Lexeme> TryParse(LinkedListNode<Lexeme> lexemes, out Node resultNode)
        {
            resultNode = null;
            var nextLexeme = StructDecl.TryParse(lexemes, out StructDecl structDecl);
            if (structDecl != null)
            {
                resultNode = structDecl;
                return nextLexeme;
            }

            nextLexeme = FnDecl.TryParse(lexemes, out FnDecl fn);
            if (fn != null)
            {
                resultNode = fn;
                return nextLexeme;
            }

            nextLexeme = Stmt.TryParseStruct(lexemes, out Stmt stmt);
            if (stmt != null)
            {
                resultNode = stmt;
                return nextLexeme;
            }

            return lexemes;
        }
示例#2
0
        public static LinkedListNode <Lexeme> TryParse(LinkedListNode <Lexeme> lexemes, out StructDecl resultNode)
        {
            resultNode = null;
            if (lexemes.Value.Type != LT.KW_STRUCT)
            {
                return(lexemes);
            }

            var nextLexeme = lexemes.Next;

            if (nextLexeme.Value.Type != LT.IDENT)
            {
                throw new Exception($"Missing struct name at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }
            var nameLexeme = nextLexeme.Value;

            nextLexeme = nextLexeme.Next;
            if (nextLexeme.Value.Type != LT.OP_BRACE_O)
            {
                throw new Exception($"Missing opening bracket for struct at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            List <Stmt> stmts = new List <Stmt>();

            nextLexeme = nextLexeme.Next;

            while (true)
            {
                nextLexeme = Stmt.TryParseStruct(nextLexeme, out Stmt stmt);
                if (stmt != null)
                {
                    stmts.Add(stmt);
                }
                else
                {
                    break;
                }
            }
            if (stmts.Count == 0)
            {
                throw new Exception($"Missing struct body at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            if (nextLexeme.Value.Type != LT.OP_BRACE_C)
            {
                throw new Exception($"Missing closing bracket for struct at {nextLexeme.Value.File}:{nextLexeme.Value.Line}");
            }

            resultNode = new StructDecl {
                Body = new BlockBody(stmts), Token = nameLexeme
            };
            return(nextLexeme.Next);
        }