|
Porytiles
|
Parser for C preprocessor constructs. More...
#include <parser.hpp>
Public Member Functions | |
| Parser (gsl::not_null< const TextFormatter * > format, std::vector< Token > tokens) | |
| Constructs a parser for the given token stream. | |
| Parser (gsl::not_null< const TextFormatter * > format, std::vector< Token > tokens, const CParserContext *context) | |
| Constructs a parser with a context for rich error formatting. | |
| ChainableResult< std::vector< DefineStatement > > | parse_defines () |
| Parses all #define statements from the token stream. | |
| ChainableResult< std::vector< EnumDeclaration > > | parse_enums () |
| Parses all enum declarations from the token stream. | |
| ChainableResult< std::vector< ArrayDeclaration > > | parse_pointer_arrays () |
| Parses all pointer array declarations from the token stream. | |
| ChainableResult< std::vector< FunctionDefinition > > | parse_functions () |
| Parses function definitions from the token stream. | |
| ChainableResult< std::vector< StructVariableDeclaration > > | parse_struct_variables () |
| Parses struct variable declarations from the token stream. | |
| ChainableResult< std::vector< StructInitializerDeclaration > > | parse_struct_initializers () |
| Parses struct variable declarations with their designated initializer fields. | |
| ChainableResult< std::vector< IncbinDeclaration > > | parse_incbin_arrays () |
| Parses INCBIN array declarations from the token stream. | |
Parser for C preprocessor constructs.
Parser analyzes a token stream and extracts structured information. The initial implementation focuses on parsing #define statements, extracting macro names and evaluating constant expressions.
The parser uses the Shunting Yard algorithm to evaluate arithmetic expressions in #define values, supporting operators: +, -, *, /, %, &, |, ^, ~, <<, >>
Example usage:
Future extensions will add methods like:
Definition at line 54 of file parser.hpp.
|
inline |
Constructs a parser for the given token stream.
This constructor creates a parser without a CParserContext. Errors will be formatted as simple "line:col: message" strings without source context highlighting.
| format | The text formatter for styled output |
| tokens | The tokens to parse (typically from Lexer::lex()) |
Definition at line 66 of file parser.hpp.
|
inline |
Constructs a parser with a context for rich error formatting.
This constructor creates a parser with a CParserContext that enables rich error formatting with source context highlighting via FileHighlightPrinter. The context must outlive the parser.
| format | The text formatter for styled output |
| tokens | The tokens to parse (typically from Lexer::lex()) |
| context | The parser context for rich error formatting (non-owning) |
Definition at line 82 of file parser.hpp.
| ChainableResult< std::vector< DefineStatement > > porytiles::Parser::parse_defines | ( | ) |
Parses all #define statements from the token stream.
Scans through the token stream looking for #define directives. For each one found, parses the macro name and evaluates the value expression if present. Non-define preprocessor directives and other code constructs are skipped.
Supports:
When constructed with a CParserContext, errors include source code context with highlighted error locations. When constructed without a context, errors are simple "line:col: message" format.
Definition at line 135 of file parser.cpp.
| ChainableResult< std::vector< EnumDeclaration > > porytiles::Parser::parse_enums | ( | ) |
Parses all enum declarations from the token stream.
Scans through the token stream looking for enum declarations. For each one found, parses the optional enum name and all members with their values. Supports both implicit counter-based values and explicit value assignments.
Supports:
When constructed with a CParserContext, errors include source code context with highlighted error locations. When constructed without a context, errors are simple "line:col: message" format.
Definition at line 166 of file parser.cpp.
| ChainableResult< std::vector< FunctionDefinition > > porytiles::Parser::parse_functions | ( | ) |
Parses function definitions from the token stream.
Scans through the token stream looking for function definitions matching the pattern:
This is used by AnimCodeParser to extract queue and driver functions like:
The parser captures the function name and all tokens within the body braces for later pattern matching.
Definition at line 863 of file parser.cpp.
| ChainableResult< std::vector< IncbinDeclaration > > porytiles::Parser::parse_incbin_arrays | ( | ) |
Parses INCBIN array declarations from the token stream.
Scans through the token stream looking for array declarations using INCBIN macros:
Examples from pokeemerald:
The parser extracts the variable name, INCBIN macro name, and path(s) from each declaration.
Definition at line 1171 of file parser.cpp.
| ChainableResult< std::vector< ArrayDeclaration > > porytiles::Parser::parse_pointer_arrays | ( | ) |
Parses all pointer array declarations from the token stream.
Scans through the token stream looking for pointer array declarations matching the pattern:
This is used by AnimCodeParser to extract animation frame arrays like:
The parser extracts the array name and all identifier elements from the initializer list.
Definition at line 753 of file parser.cpp.
| ChainableResult< std::vector< StructInitializerDeclaration > > porytiles::Parser::parse_struct_initializers | ( | ) |
Parses struct variable declarations with their designated initializer fields.
Scans through the token stream looking for struct variable declarations matching the pattern:
This is used by ProjectTilesetMetadataProvider to extract tileset metadata from headers.h files:
Unlike parse_struct_variables(), this method also parses the designated initializer fields, enabling extraction of field values like isSecondary and variable references.
Definition at line 1019 of file parser.cpp.
| ChainableResult< std::vector< StructVariableDeclaration > > porytiles::Parser::parse_struct_variables | ( | ) |
Parses struct variable declarations from the token stream.
Scans through the token stream looking for struct variable declarations matching the pattern:
This is used by ProjectTilesetMetadataProvider to extract tileset names from headers.h files like:
The parser captures the struct type and variable name; the initializer body is skipped.
Definition at line 939 of file parser.cpp.