Ed94 / gencpp

Staged metaprogramming in C++ for C/C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing Design Changes

Ed94 opened this issue · comments

commented

Extension of #35

The following will be introduced to the parsing API:

namespace parser {
	struct StackNode
	{
		StackNode* Prev;

		Token Start;
		Token Name;       // The name of the AST node (if parsed)
		StrC    FailedProc; // The name of the procedure that failed
	};
	// Stack nodes are allocated the error's allocator

	struct Error
	{
		String     message;
		StackNode* context_stack;
	};
}

struct ParseInfo

	Arena FileMem;
	Arena TokMem;
	Arena CodeMem;

	FileContents         FileContent;
	Array<parser::Token> Tokens;
	Array<parser::Error> Errors;
	// Errors are allocated to a dedicated general arena.
;

CodeBody parse_file( StrC path );

Every time a parse action is completed a parse info will be provided to the user implicitly with the resulting Code AST.
This means the root node AST will always be provided it just may be invalid.
The Code node will have itself distinguished with flag specified in its code flags with CodeFlag_HasParseInfo
The parse info will be a header struct to an allocation that happens initially when using one of the API's parsing functions.

To retrieve the ParseInfo header, a get_parse_info will provide an ease of use to receive it's pointer.=

Code parse_code( StrC content ) {
  ParseInfo* parse_info = rcast( ParseInfo*, alloc( GlobalAllocator, sizeof( ParseInfo )) + sizeof(AST*) );
  AST* ast = rcast( AST* , parse_info + 1 );
  ...
  return Code{ ast };
}

ParseResult* get_parse_info( Code code ) {
  return rcast( ParseInfo*, code.ast) - 1;
}

If any portion of the code content fails to parse, that section of the string will fail and get shelved into an untyped code string. The parser functions will cascade invalidate the relevant context stack with the untyped code string into a untyped AST node. The error message will be stored in the error list while also logging the failure. And, the context stack will be saved to retrieve for later inspection.

All memory arenas used for the file content, tokens, & code will be within the parse info incase the user wants to free related memory. A utility function will be provided for ease of use:

bool free_parse_allocations( ParseInfo* info );