Skip to content

Commit c7647ab

Browse files
committed
Updated docs
1 parent d2fc1d0 commit c7647ab

File tree

8 files changed

+109
-111
lines changed

8 files changed

+109
-111
lines changed

Readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ The library API is a composition of code element constructors.
66
These build up a code AST to then serialize with a file builder.
77

88
This code base attempts follow the [handmade philosophy](https://handmade.network/manifesto),
9-
its not meant to be a black box metaprogramming utility, its meant for the user to extend for their project domain.
9+
its not meant to be a black box metaprogramming utility, it should be easy to intergrate into a user's their project domain.
1010

1111
## Notes
1212

1313
The project has reached an *alpha* state, all the current functionality works for the test cases but it will most likely break in many other cases.
14-
The [issues](https://github.com/Ed94/gencpp/issues) marked with v1.0 Feature indicate whats left before the library is considered feature complete.
1514

1615
A `natvis` and `natstepfilter` are provided in the scripts directory.
1716

@@ -25,7 +24,7 @@ A metaprogram is built to generate files before the main program is built. We'll
2524

2625
`gen.cpp` \`s `main()` is defined as `gen_main()` which the user will have to define once for their program. There they will dictate everything that should be generated.
2726

28-
In order to keep the locality of this code within the same files the following pattern may be used:
27+
In order to keep the locality of this code within the same files the following pattern may be used (although this pattern isn't required at all):
2928

3029
Within `program.cpp` :
3130

@@ -41,6 +40,8 @@ u32 gen_main()
4140
}
4241
#endif
4342

43+
// "Stage" agnostic code.
44+
4445
#ifndef GEN_TIME
4546
#include "program.gen.cpp"
4647

@@ -56,6 +57,7 @@ Example using each construction interface:
5657

5758
### Upfront
5859

60+
Validation and construction through a functional interface.
5961

6062
```cpp
6163
Code t_uw = def_type( name(uw) );
@@ -75,6 +77,8 @@ Code header;
7577

7678
### Parse
7779

80+
Validation through ast construction.
81+
7882
```cpp
7983
Code header = parse_struct( code(
8084
struct ArrayHeader
@@ -89,6 +93,8 @@ Code header = parse_struct( code(
8993

9094
### Untyped
9195

96+
No validation, just glorified text injection.
97+
9298
```cpp
9399
Code header = code_str(
94100
struct ArrayHeader

docs/Parsing.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# Parsing
22

33
The library features a naive parser tailored for only what the library needs to construct the supported syntax of C++ into its AST.
4-
This parser does not, and should not do the compiler's job. By only supporting this minimal set of features, the parser is kept under 5000 loc.
4+
This parser does not, and should not do the compiler's job. By only supporting this minimal set of features, the parser is kept (so far) under 5000 loc.
55

66
The parsing implementation supports the following for the user:
77

88
```cpp
9-
CodeClass parse_class ( StrC class_def );
10-
CodeEnum parse_enum ( StrC enum_def );
11-
CodeBody parse_export_body ( StrC export_def );
12-
CodeExtern parse_extern_link ( StrC exten_link_def);
13-
CodeFriend parse_friend ( StrC friend_def );
14-
CodeFn parse_function ( StrC fn_def );
15-
CodeBody parse_global_body ( StrC body_def );
16-
CodeNS parse_namespace ( StrC namespace_def );
17-
CodeOperator parse_operator ( StrC operator_def );
18-
CodeOpCast parse_operator_cast( StrC operator_def );
19-
CodeStruct parse_struct ( StrC struct_def );
20-
CodeTemplate parse_template ( StrC template_def );
21-
CodeType parse_type ( StrC type_def );
22-
CodeTypedef parse_typedef ( StrC typedef_def );
23-
CodeUnion parse_union ( StrC union_def );
24-
CodeUsing parse_using ( StrC using_def );
25-
CodeVar parse_variable ( StrC var_def );
9+
CodeClass parse_class ( StrC class_def );
10+
CodeConstructor parse_constructor ( StrC constructor_def );
11+
CodeDestructor parse_destructor ( StrC destructor_def );
12+
CodeEnum parse_enum ( StrC enum_def );
13+
CodeBody parse_export_body ( StrC export_def );
14+
CodeExtern parse_extern_link ( StrC exten_link_def );
15+
CodeFriend parse_friend ( StrC friend_def );
16+
CodeFn parse_function ( StrC fn_def );
17+
CodeBody parse_global_body ( StrC body_def );
18+
CodeNS parse_namespace ( StrC namespace_def );
19+
CodeOperator parse_operator ( StrC operator_def );
20+
CodeOpCast parse_operator_cast( StrC operator_def );
21+
CodeStruct parse_struct ( StrC struct_def );
22+
CodeTemplate parse_template ( StrC template_def );
23+
CodeType parse_type ( StrC type_def );
24+
CodeTypedef parse_typedef ( StrC typedef_def );
25+
CodeUnion parse_union ( StrC union_def );
26+
CodeUsing parse_using ( StrC using_def );
27+
CodeVar parse_variable ( StrC var_def );
2628
```
2729
2830
***Parsing will aggregate any tokens within a function body or expression statement to an untyped Code AST.***
2931
3032
Everything is done in one pass for both the preprocessor directives and the rest of the language.
31-
The parser performs no macro expansion as the scope of gencpp feature-set is to only support the preprocessor for the goal of having rudimentary awareness of preprocessor ***conditionals***, ***defines***, and ***includes***, and ***`pragmas`**.
33+
The parser performs no macro expansion as the scope of gencpp feature-set is to only support the preprocessor for the goal of having rudimentary awareness of preprocessor ***conditionals***, ***defines***, and ***includes***, and ***pragmas**.
3234
3335
The keywords supported for the preprocessor are:
3436
@@ -41,12 +43,30 @@ The keywords supported for the preprocessor are:
4143
* undef
4244
* pragma
4345
44-
Each directive `#` line is considered one preproecessor unit, and will be treated as one Preprocessor AST. *These ASTs will be considered members or entries of braced scope they reside within*.
45-
All keywords except *include* are suppported as members of a scope for a class/struct, global, or namespace body.
46+
Each directive `#` line is considered one preproecessor unit, and will be treated as one Preprocessor AST. *These ASTs will be considered members or entries of braced scope they reside within*.
47+
If a directive is used with an unsupported keyword its will be processed as an untyped AST.
4648
47-
Any preprocessor definition abuse that changes the syntax of the core language is unsupported and will fail to parse if not kept within an execution scope (function body, or expression assignment).
49+
The preprocessor lines are stored as members of their associated scope they are parsed within. ( Global, Namespace, Class/Struct )
4850
49-
Exceptions to the above rule (If its too hard to keep track of just follow the above notion):
51+
Any preprocessor definition abuse that changes the syntax of the core language is unsupported and will fail to parse if not kept within an execution scope (function body, or expression assignment).
52+
Exceptions:
5053
51-
* Typedefs allow of a macro exansion to be defined after the keyword; Ex: `typedef GEN_FILE_OPEN_PROC( file_open_proc );`
54+
* function signatures are allowed for a preprocessed macro: `neverinline MACRO() { ... }`
55+
* typedefs allow for a preprocessed macro: `typedef MACRO();`
5256
57+
*(See functions `parse_operator_function_or_variable` and `parse_typedef` )*
58+
59+
The lexing and parsing takes shortcuts from whats expected in the standard.
60+
61+
* Numeric literals are not checked for validity.
62+
* The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
63+
* *This includes the assignment of variables.*
64+
* Attributes ( `[[]]` (standard), `__declspec` (Microsoft), or `__attribute__` (GNU) )
65+
* Assumed to *come before specifiers* (`const`, `constexpr`, `extern`, `static`, etc) for a function
66+
* Or in the usual spot for class, structs, (*right after the declaration keyword*)
67+
* typedefs have attributes with the type (`parse_type`)
68+
* As a general rule; if its not available from the upfront constructors, its not available in the parsing constructors.
69+
* *Upfront constructors are not necessarily used in the parsing constructors, this is just a good metric to know what can be parsed.*
70+
* Parsing attributes can be extended to support user defined macros by defining `GEN_DEFINE_ATTRIBUTE_TOKENS` (see `gen.hpp` for the formatting)
71+
72+
Empty lines used throughout the file are preserved for formatting purposes for ast serialization.

docs/Readme.md

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ Otherwise the library is free of any templates.
4040

4141
### *WHAT IS NOT PROVIDED*
4242

43-
* Execution statement validation : Execution expressions are defined using the untyped AST.
44-
* Lambdas (This naturally means its unsupported)
45-
* Non-trivial template validation support.
46-
* RAII : This needs support for constructors/destructor parsing
47-
* Haven't gotten around to yet (its in the github issues)
48-
4943
Keywords kept from "Modern C++":
5044

5145
* constexpr : Great to store compile-time constants.
@@ -55,13 +49,9 @@ Keywords kept from "Modern C++":
5549
* import : ^^
5650
* module : ^^
5751

58-
When it comes to expressions:
59-
6052
**There is no support for validating expressions.**
6153
Its difficult to parse without enough benefits (At the metaprogramming level).
6254

63-
When it comes to templates:
64-
6555
**Only trivial template support is provided.**
6656
The intention is for only simple, non-recursive substitution.
6757
The parameters of the template are treated like regular parameter AST entries.
@@ -78,7 +68,7 @@ Use at your own mental peril.
7868

7969
### The Data & Interface
8070

81-
As mentioned in [Usage](#usage), the user is provided Code objects by calling the constructor's functions to generate them or find existing matches.
71+
As mentioned in root readme, the user is provided Code objects by calling the constructor's functions to generate them or find existing matches.
8272

8373
The AST is managed by the library and provided the user via its interface.
8474
However, the user may specifiy memory configuration.
@@ -89,39 +79,44 @@ Data layout of AST struct:
8979
union {
9080
struct
9181
{
92-
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
93-
AST* Specs; // Function, Operator, Type symbol, Variable
82+
AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable
83+
AST* Specs; // Function, Operator, Type symbol, Variable
9484
union {
95-
AST* ParentType; // Class, Struct
96-
AST* ReturnType; // Function, Operator
97-
AST* UnderlyingType; // Enum, Typedef
98-
AST* ValueType; // Parameter, Variable
85+
AST* InitializerList; // Constructor, Destructor
86+
AST* ParentType; // Class, Struct
87+
AST* ReturnType; // Function, Operator
88+
AST* UnderlyingType; // Enum, Typedef
89+
AST* ValueType; // Parameter, Variable
9990
};
100-
AST* Params; // Function, Operator, Template
10191
union {
102-
AST* ArrExpr; // Type Symbol
103-
AST* Body; // Class, Enum, Function, Namespace, Struct, Union
104-
AST* Declaration; // Friend, Template
105-
AST* Value; // Parameter, Variable
92+
AST* BitfieldSize; // Varaiable (Class/Struct Data Member)
93+
AST* Params; // Function, Operator, Template
94+
};
95+
union {
96+
AST* ArrExpr; // Type Symbol
97+
AST* Body; // Class, Constructr, Destructor, Enum, Function, Namespace, Struct, Union
98+
AST* Declaration; // Friend, Template
99+
AST* Value; // Parameter, Variable
106100
};
107101
};
108-
StringCached Content; // Attributes, Comment, Execution, Include
102+
StringCached Content; // Attributes, Comment, Execution, Include
109103
SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers
110104
};
111105
union {
112106
AST* Prev;
113-
AST* Front; // Used by CodeBody
114-
AST* Last; // Used by CodeParam
107+
AST* Front;
108+
AST* Last;
115109
};
116110
union {
117111
AST* Next;
118-
AST* Back; // Used by CodeBody
112+
AST* Back;
119113
};
120114
AST* Parent;
121115
StringCached Name;
122116
CodeT Type;
123117
ModuleFlag ModuleFlags;
124118
union {
119+
b32 IsFunction; // Used by typedef to not serialize the name field.
125120
OperatorT Op;
126121
AccessSpec ParentAccess;
127122
s32 NumEntries;
@@ -145,7 +140,7 @@ uw ArrSpecs_Cap =
145140
- sizeof(StringCached)
146141
- sizeof(CodeT)
147142
- sizeof(ModuleFlag)
148-
- sizeof(s32)
143+
- sizeof(u32)
149144
)
150145
/ sizeof(SpecifierT) -1; // -1 for 4 extra bytes (Odd num of AST*)
151146
```
@@ -155,7 +150,7 @@ uw ArrSpecs_Cap =
155150
Data Notes:
156151

157152
* The allocator definitions used are exposed to the user incase they want to dictate memory usage
158-
* You'll find the memory handling in `init`, `gen_string_allocator`, `get_cached_string`, `make_code`.
153+
* You'll find the memory handling in `init`, `deinit`, `reset`, `gen_string_allocator`, `get_cached_string`, `make_code`.
159154
* ASTs are wrapped for the user in a Code struct which is a wrapper for a AST* type.
160155
* Both AST and Code have member symbols but their data layout is enforced to be POD types.
161156
* This library treats memory failures as fatal.
@@ -168,15 +163,15 @@ Data Notes:
168163
* Linked lists used children nodes on bodies, and parameters.
169164
* Its intended to generate the AST in one go and serialize after. The constructors and serializer are designed to be a "one pass, front to back" setup.
170165
* Allocations can be tuned by defining the folloiwng macros:
171-
* `GEN_BUILDER_STR_BUFFER_RESERVE`
172-
* `GEN_CODEPOOL_NUM_BLOCKS` : Number of blocks per code pool in the code allocator
173166
* `GEN_GLOBAL_BUCKET_SIZE` : Size of each bucket area for the global allocator
174-
* `GEN_LEX_ALLOCATOR_SIZE`
167+
* `GEN_CODEPOOL_NUM_BLOCKS` : Number of blocks per code pool in the code allocator
168+
* `GEN_SIZE_PER_STRING_ARENA` : Size per arena used with string caching.
175169
* `GEN_MAX_COMMENT_LINE_LENGTH` : Longest length a comment can have per line.
176170
* `GEN_MAX_NAME_LENGTH` : Max length of any identifier.
177171
* `GEN_MAX_UNTYPED_STR_LENGTH` : Max content length for any untyped code.
178-
* `GEN_SIZE_PER_STRING_ARENA` : Size per arena used with string caching.
179172
* `GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE` : token_fmt_va uses local_persit memory of this size for the hashtable.
173+
* `GEN_LEX_ALLOCATOR_SIZE`
174+
* `GEN_BUILDER_STR_BUFFER_RESERVE`
180175

181176
The following CodeTypes are used which the user may optionally use strong typing with if they enable: `GEN_ENFORCE_STRONG_CODE_TYPES`
182177

@@ -187,7 +182,6 @@ The following CodeTypes are used which the user may optionally use strong typing
187182
* CodeConstructor
188183
* CodeDefine
189184
* CodeDestructor
190-
* CodePreprocessCond
191185
* CodeEnum
192186
* CodeExec
193187
* CodeExtern
@@ -199,6 +193,8 @@ The following CodeTypes are used which the user may optionally use strong typing
199193
* CodeOperator
200194
* CodeOpCast
201195
* CodeParam : Has support for `for-range` iterating across parameters.
196+
* CodePreprocessCond
197+
* CodePragma
202198
* CodeSpecifiers : Has support for `for-range` iterating across specifiers.
203199
* CodeStruct
204200
* CodeTemplate
@@ -221,7 +217,7 @@ Retrieving a raw version of the ast can be done using the `raw()` function defin
221217
### Upfront Construction
222218

223219
All component ASTs must be previously constructed, and provided on creation of the code AST.
224-
The construction will fail and return Code::Invalid otherwise.
220+
The construction will fail and return CodeInvalid otherwise.
225221

226222
Interface :``
227223

@@ -231,6 +227,7 @@ Interface :``
231227
* def_comment
232228
* def_class
233229
* def_constructor
230+
* def_define
234231
* def_destructor
235232
* def_enum
236233
* def_execution
@@ -245,6 +242,7 @@ Interface :``
245242
* def_operator_cast
246243
* def_param
247244
* def_params
245+
* def_preprocess_cond
248246
* def_specifier
249247
* def_specifiers
250248
* def_struct
@@ -321,19 +319,6 @@ Interface :
321319
* parse_using
322320
* parse_variable
323321
324-
The lexing and parsing takes shortcuts from whats expected in the standard.
325-
326-
* Numeric literals are not check for validity.
327-
* The parse API treats any execution scope definitions with no validation and are turned into untyped Code ASTs.
328-
* *This includes the assignment of variables.*
329-
* Attributes ( `[[]]` (standard), `__declspec` (Microsoft), or `__attribute__` (GNU) )
330-
* Assumed to *come before specifiers* (`const`, `constexpr`, `extern`, `static`, etc) for a function
331-
* Or in the usual spot for class, structs, (*right after the declaration keyword*)
332-
* typedefs have attributes with the type (`parse_type`)
333-
* As a general rule; if its not available from the upfront constructors, its not available in the parsing constructors.
334-
* *Upfront constructors are not necessarily used in the parsing constructors, this is just a good metric to know what can be parsed.*
335-
* Parsing attributes can be extended to support user defined macros by defining `GEN_DEFINE_ATTRIBUTE_TOKENS` (see `gen.hpp` for the formatting)
336-
337322
Usage:
338323
339324
```cpp
@@ -342,13 +327,6 @@ Code <name> = parse_<function name>( string with code );
342327
Code <name> = def_<function name>( ..., parse_<function name>(
343328
<string with code>
344329
));
345-
346-
Code <name> = make_<function name>( ... )
347-
{
348-
<name>->add( parse_<function name>(
349-
<string with code>
350-
));
351-
}
352330
```
353331

354332
### Untyped constructions
@@ -408,12 +386,15 @@ The following are provided predefined by the library as they are commonly used:
408386
* `access_public`
409387
* `access_protected`
410388
* `access_private`
389+
* `attrib_api_export`
390+
* `attrib_api_import`
411391
* `module_global_fragment`
412392
* `module_private_fragment`
393+
* `fmt_newline`
394+
* `pragma_once`
413395
* `param_varaidc` (Used for varadic definitions)
414396
* `preprocess_else`
415397
* `preprocess_endif`
416-
* `pragma_once`
417398
* `spec_const`
418399
* `spec_consteval`
419400
* `spec_constexpr`
@@ -425,19 +406,17 @@ The following are provided predefined by the library as they are commonly used:
425406
* `spec_internal_linkage` (internal macro)
426407
* `spec_local_persist` (local_persist macro)
427408
* `spec_mutable`
409+
* `spec_neverinline`
428410
* `spec_override`
429411
* `spec_ptr`
412+
* `spec_pure`
430413
* `spec_ref`
431414
* `spec_register`
432415
* `spec_rvalue`
433416
* `spec_static_member` (static)
434417
* `spec_thread_local`
435418
* `spec_virtual`
436419
* `spec_volatile`
437-
* `spec_type_signed`
438-
* `spec_type_unsigned`
439-
* `spec_type_short`
440-
* `spec_type_long`
441420
* `t_empty` (Used for varaidc macros)
442421
* `t_auto`
443422
* `t_void`

docs/Upfront.md

Whitespace-only changes.

0 commit comments

Comments
 (0)