Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My take on Refactor2 #16

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
1b86542
Rename and delete some files
ChrisHixon May 13, 2022
0b9176a
Edit comments in grammars
ChrisHixon May 13, 2022
4401e89
Refactor
ChrisHixon May 13, 2022
fbde67d
Remove esc_bytes() declaration
ChrisHixon May 13, 2022
f009fc5
Replace malloc and free with mem.h macros
ChrisHixon May 13, 2022
fabca5f
Add read_file function
ChrisHixon May 14, 2022
23f3853
Change file offsets and lengths to type size_t
ChrisHixon May 14, 2022
b6b8123
Add prefix to everything op-code related
ChrisHixon May 15, 2022
d5a16c8
Change INST to CHPEG_INST
ChrisHixon May 15, 2022
47ba501
Rename ByteCode (struct) to ChpegByteCode
ChrisHixon May 15, 2022
8a5e7ca
Prefix flags with CHPEG_
ChrisHixon May 15, 2022
d998e81
Prefix parse error codes with CHPEG_ERR_
ChrisHixon May 15, 2022
10c6343
Rename Node to ChpegNode
ChrisHixon May 15, 2022
dc5a6fc
Rename Parser ChpegParser
ChrisHixon May 15, 2022
7960bd1
Remove unnecessary declaration
ChrisHixon May 15, 2022
2cc41c2
Prefix util functions with chpeg_
ChrisHixon May 15, 2022
635320f
Add prefix Chpeg to ByteCode_* methods
ChrisHixon May 15, 2022
0c3f18e
Rename Node_ prefixed methods to ChpegNode_
ChrisHixon May 15, 2022
004a8a5
Rename Parser_ prefixed methods to ChpegParser_
ChrisHixon May 15, 2022
f181521
Rename GNode to ChpegGNode
ChrisHixon May 15, 2022
70201a8
Rename CompilationUnit to ChpegCompUnit
ChrisHixon May 15, 2022
d3fc236
Add prefixes in compiler; new API on chpeg_compile
ChrisHixon May 15, 2022
fb75a7f
Fix potential stack overflow
ChrisHixon May 15, 2022
c0dd526
Remove ByteCode member copies from Parser struct
ChrisHixon May 15, 2022
ee4501d
Make default and generated C ByteCode const
ChrisHixon May 15, 2022
bd1b925
Move headers to include/chpeg; create amalgamation
ChrisHixon May 16, 2022
9c9ed8a
Add default memory allocation wrappers...
ChrisHixon May 17, 2022
857b697
My take on reafactor branch version 2
mingodad May 17, 2022
316a887
Replace magic numbers and fixes to my example mk-chpeg-nocase.sh
mingodad May 17, 2022
5d89554
Without LiteralSQ/LiteralDQ I found a way to make case insensitive work
mingodad May 23, 2022
085c01e
Make "EndLine" optional at the end of "Comment" for cases where the l…
mingodad May 29, 2022
20cfd72
Replace malloc by calloc
mingodad May 29, 2022
9d180df
Replace malloc by calloc and add the missing extra escape code
mingodad May 29, 2022
34b82cd
Updated amalgamation
mingodad May 29, 2022
e8afe87
Update license as done on origin
mingodad May 30, 2022
114d528
Add the "trim" extension
mingodad May 30, 2022
b7248a3
Add optional definition counters to help improve/refactor grammars
mingodad May 30, 2022
d03a21f
Add the missing memmory dealoocation for CHPEG_DEFINITION_TRACE
mingodad May 30, 2022
463e705
Add percentage to CHPEG_DEFINITION_TRACEthe
mingodad May 30, 2022
3907cb0
Add total definition count to help compare performance in grammar cha…
mingodad May 30, 2022
c096250
Add % of success/fail to CHPEG_DEFINITION_TRACE
mingodad May 30, 2022
f693de1
Add VM loop count to help tuning a grammar
mingodad May 31, 2022
c53bcda
Add full definition on CHPEG_DEFINITION_TRACE
mingodad May 31, 2022
73f4fcd
Replace inline CHPEG_INST decoding by a macro
mingodad May 31, 2022
649215a
Add the missing CHPEG_OP_PREDA output
mingodad May 31, 2022
135ed32
Add Definition flags and fix nested choices
mingodad May 31, 2022
62120a7
Reset the vm_count every time the parser is ececuted
mingodad Jun 1, 2022
5ec5d0d
Fix missing initialization of "vm_count"
mingodad Jun 1, 2022
4530bd4
Add initial line/column calculation for error messages
mingodad Jun 1, 2022
09a7fa6
Add the latest changes for origin/profile
mingodad Jun 2, 2022
c6a4fb5
Add "instructions per input byte" to the profile output
mingodad Jun 4, 2022
6fb9d77
Initial incomplete back reference implementation
mingodad Jun 5, 2022
25dcbed
Add changes from remotes/origin/references
mingodad Jun 7, 2022
71433ba
Avoid emit unnecessary parenthesis
mingodad Jun 7, 2022
1213c19
Update with the latest origin/references
mingodad Jun 9, 2022
5f7cfe5
Fixes to build an initial working playground
mingodad Jun 11, 2022
feadc91
Set the grammar editor mode to YAML that seems to work better for peg
mingodad Jun 11, 2022
0bc9751
Update with code from origin/compiler
mingodad Jun 12, 2022
7ef2780
Add a conditional macro to exclude "main"
mingodad Jun 12, 2022
b3c00de
If the grammar is empty assume input is a grammar to be parsed by the…
mingodad Jun 12, 2022
121124d
Merge the latest changes from origin/compiler and add a wrapper aroun…
mingodad Jun 14, 2022
9e9c321
A not elegant hack to show errors/warnings on the playground
mingodad Jun 14, 2022
6e95365
Fix an invalid json field name
mingodad Jun 14, 2022
1c7e464
Remove redundant '<span>' from error/warning messages
mingodad Jun 14, 2022
88881bb
Show messages without line/col info
mingodad Jun 15, 2022
db4b015
Fixes to build the playground with C++ compiler
mingodad Jun 15, 2022
6f9c3b1
Applying a fix for false detection of infinite loop and in some cases…
mingodad Jun 15, 2022
4322e52
Fixing the warning about reapeating a repetition
mingodad Jun 15, 2022
95dafb7
Another attempt to show warnings about repetition over repetition
mingodad Jun 16, 2022
ace6f56
Again change the code to catch more repetition over repetition cases
mingodad Jun 16, 2022
696339f
Add the duration of parsing to the profile to help improve grammars
mingodad Jun 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename Node to ChpegNode
ChrisHixon committed May 15, 2022
commit 10c63431d8ace65ca399aad568d92f4755183c7a
14 changes: 7 additions & 7 deletions src/compiler.c
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@

typedef struct _GNode
{
Node *node;
ChpegNode *node;
int type;

int parse_state;
@@ -122,7 +122,7 @@ static void CompilationUnit_print(CompilationUnit *cu, GNode *gnode, const unsig
char *data = NULL;
const char *def_name = Parser_def_name(cu->parser, gnode->type);

Node *node = gnode->node;
ChpegNode *node = gnode->node;
if (node) {
data = esc_bytes(&input[node->offset], node->length, 40);
flags = node->flags;
@@ -157,7 +157,7 @@ static void CompilationUnit_print(CompilationUnit *cu, GNode *gnode, const unsig

static void Compiler_setup_defs(CompilationUnit *cu)
{
Node *p = NULL;
ChpegNode *p = NULL;
int i = 0, j = 0;

cu->bc->num_defs = cu->parser->tree_root->num_children;
@@ -172,7 +172,7 @@ static void Compiler_setup_defs(CompilationUnit *cu)
for (p = cu->parser->tree_root->head, i = 0; p; p = p->next, ++i) {
if (CHPEG_DEFINITION != p->def) { continue; }

Node *tmp = p->head; // Identifier, definition name
ChpegNode *tmp = p->head; // Identifier, definition name
if (NULL == tmp || CHPEG_IDENTIFIER != tmp->def) { continue; }
cu->bc->def_names[i] = (char *)CHPEG_MALLOC(1 + tmp->length);
memcpy(cu->bc->def_names[i], &cu->input[tmp->offset], tmp->length);
@@ -207,7 +207,7 @@ static void Compiler_setup_def_addrs(CompilationUnit *cu)
}
}

static int Compiler_find_def(CompilationUnit *cu, Node *ident)
static int Compiler_find_def(CompilationUnit *cu, ChpegNode *ident)
{
char buf[ident->length + 1];
memcpy(buf, cu->input + ident->offset, ident->length);
@@ -220,13 +220,13 @@ static int Compiler_find_def(CompilationUnit *cu, Node *ident)
return -1;
}

static void Compiler_build_tree(CompilationUnit *cu, Node *np, GNode *gp)
static void Compiler_build_tree(CompilationUnit *cu, ChpegNode *np, GNode *gp)
{
if (NULL == np) { np = cu->parser->tree_root; }
if (NULL == gp) { gp = cu->root; }
gp->node = np;
gp->type = np->def;
for (Node *p = np->head; p; p = p->next) {
for (ChpegNode *p = np->head; p; p = p->next) {
GNode *g = GNode_new();
Compiler_build_tree(cu, p, g);
GNode_push_child(gp, g);
36 changes: 18 additions & 18 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -49,10 +49,10 @@
#endif

//
// Node
// ChpegNode
//

void Node_print(Node *self, Parser *parser, const unsigned char *input, int depth)
void Node_print(ChpegNode *self, Parser *parser, const unsigned char *input, int depth)
{
int flags = self->flags;
char *data = esc_bytes(&input[self->offset], self->length, 40);
@@ -75,17 +75,17 @@ void Node_print(Node *self, Parser *parser, const unsigned char *input, int dept
data ? data : "<NULL>"
);
if (data) { CHPEG_FREE(data); data = NULL; }
for (Node *p = self->head; p; p = p->next) {
for (ChpegNode *p = self->head; p; p = p->next) {
Node_print(p, parser, input, depth + 1);
}
if (depth == 0) {
printf("---------------------------------------------------------------------------------\n");
}
}

Node *Node_new(int def, size_t offset, size_t length, int flags)
ChpegNode *Node_new(int def, size_t offset, size_t length, int flags)
{
Node *self = (Node *)CHPEG_MALLOC(sizeof(Node));
ChpegNode *self = (ChpegNode *)CHPEG_MALLOC(sizeof(ChpegNode));
self->def = def;
self->offset = offset;
self->length = length;
@@ -96,49 +96,49 @@ Node *Node_new(int def, size_t offset, size_t length, int flags)
return self;
}

void Node_free(Node *self)
void Node_free(ChpegNode *self)
{
Node *tmp;
for (Node *p = self->head; p; p = tmp) {
ChpegNode *tmp;
for (ChpegNode *p = self->head; p; p = tmp) {
tmp = p->next;
Node_free(p);
}
self->head = NULL;
CHPEG_FREE(self);
}

Node *Node_push_child(Node *self, int def, size_t offset, size_t length, int flags)
ChpegNode *Node_push_child(ChpegNode *self, int def, size_t offset, size_t length, int flags)
{
Node *node = Node_new(def, offset, length, flags);
ChpegNode *node = Node_new(def, offset, length, flags);
node->next = self->head;
self->head = node;
++(self->num_children);
return node;
}

void Node_pop_child(Node *self)
void Node_pop_child(ChpegNode *self)
{
if (self->head) {
Node *tmp = self->head;
ChpegNode *tmp = self->head;
self->head = self->head->next;
Node_free(tmp);
--(self->num_children);
}
}

// 'Unwrap' a Node, recursively removing unnecessary parent nodes containing only one child.
// 'Unwrap' a ChpegNode, recursively removing unnecessary parent nodes containing only one child.
// In the process, this reverses the reverse node insertion used in tree building, so should only
// be called once on the tree root after a successful parse.
Node *Node_unwrap(Node *self)
ChpegNode *Node_unwrap(ChpegNode *self)
{
if (!(self->flags & (CHPEG_STOP | CHPEG_LEAF)) && self->num_children == 1) {
Node *tmp = Node_unwrap(self->head);
ChpegNode *tmp = Node_unwrap(self->head);
self->head = NULL;
Node_free(self);
return tmp;
}
Node *p = self->head; self->head = NULL;
Node *tmp;
ChpegNode *p = self->head; self->head = NULL;
ChpegNode *tmp;
for (; p; p=tmp) {
tmp = p->next;
p = Node_unwrap(p);
@@ -291,7 +291,7 @@ int Parser_parse(Parser *self, const unsigned char *input, size_t length, size_t
#endif

size_t stack[self->max_stack_size]; int top = -1;
Node *tree_stack[self->max_tree_depth]; int tree_top = -1;
ChpegNode *tree_stack[self->max_tree_depth]; int tree_top = -1;

const int *instructions = self->instructions;

24 changes: 12 additions & 12 deletions src/parser.h
Original file line number Diff line number Diff line change
@@ -24,28 +24,28 @@ struct _CompilationUnit;
struct _Parser;

//
// Syntax tree Node
// Syntax tree ChpegNode
//

typedef struct _Node
typedef struct _ChpegNode
{
int def;
size_t offset;
size_t length;
int flags;
int num_children;
struct _Node *head;
struct _Node *next;
} Node;
struct _ChpegNode *head;
struct _ChpegNode *next;
} ChpegNode;

Node *Node_new(int def, size_t offset, size_t length, int flags);
void Node_free(Node *self);
Node *Node_push_child(Node *self, int def, size_t offset, size_t length, int flags);
void Node_pop_child(Node *self);
Node *Node_unwrap(Node *self);
ChpegNode *Node_new(int def, size_t offset, size_t length, int flags);
void Node_free(ChpegNode *self);
ChpegNode *Node_push_child(ChpegNode *self, int def, size_t offset, size_t length, int flags);
void Node_pop_child(ChpegNode *self);
ChpegNode *Node_unwrap(ChpegNode *self);

// debugging / dev aid. TODO: should disappear based on some DEBUG/NDEBUG macro
void Node_print(Node *self, struct _Parser *parser, const unsigned char *input, int depth);
void Node_print(ChpegNode *self, struct _Parser *parser, const unsigned char *input, int depth);

//
// Parser
@@ -63,7 +63,7 @@ typedef struct _Parser
unsigned char **strings;
int *str_len;

Node *tree_root;
ChpegNode *tree_root;
int max_tree_depth;
int max_stack_size;
size_t error_offset;