summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2021-01-17 12:18:01 -0500
committerBenjamin Chausse <benjamin@chausse.xyz>2021-01-17 12:18:01 -0500
commit16fc477e1d18fee40f807770f02c77c01d2fe852 (patch)
tree7cd35c767aa4690bfd211516428f58fea685252d
parentfcfa53053d5607b03f75f2f8ced61874161ddc7c (diff)
Pattern matching: check
-rw-r--r--groffdown.c91
-rw-r--r--tree.h79
-rw-r--r--tree.txt10
3 files changed, 92 insertions, 88 deletions
diff --git a/groffdown.c b/groffdown.c
index f0416e9..1a96109 100644
--- a/groffdown.c
+++ b/groffdown.c
@@ -22,56 +22,63 @@
#include <stdio.h>
#include <string.h>
+#include "tree.h"
+#include "preprocessing.h"
+
FILE *fileptr;
char *buffer;
long filelen;
-// Node tree used by the algorithm:
-#include "tree.h"
-node * match(node *n) {
- /* int nomatch = 1; */
- printf("Master node: %c\n", n->pattern );
- for (int i = 0; i < n->childsize-1; i++) {
- printf("Child %d, pattern: %c, identity: %d, position: %d\n",
- i,
- (n->child[i])->pattern,
- (n->child[i])->identity,
- (n->child[i])->pos);
+void printnode(node *n) {
+ printf("
+--------Node:--------
+pattern: %c
+pos: %d
+event: %d
+childsize: %d
+---------------------\n\n",
+ n->pattern,
+ n->pos,
+ n->event,
+ n->childsize
+ );
+}
+
+/*
+ * Checks for pattern matches within all the nodes children
+ * returns the the deepest child for which there is a match
+ * returns 0 if no match was found
+ * if node a->pos, b->pos = -1, 2 (And b is a's child)
+ * b should be changed to 3:
+ * 0(the root) -1(node a) + 3(node b) = 2
+ * 2 being b's actual position relative to root
+ */
+node * checkChildren(node *n, char *b){
+ node *curr;
+ for ( int i = 0; i < n->childsize-1; i++ ) {
+ curr = n->child[i];
+ if ( curr->pattern == b[curr->pos] ){
+ return checkChildren(curr, b+curr->pos);
+ };
};
- printf("\n");
- return (n->child[1]) ;
-};
+ return n;
+}
int main() {
- node *test = match(&n_a);
- test = match(test);
- /* test = match(&test, '*'); */
-
- /* // Importing the file into a char[] {{{ */
- /* // TODO: file should be either piped or passed as an argument */
- /* fileptr = fopen("sample.gd", "rb"); */
- /* fseek(fileptr, 0, SEEK_END); // Jump to EOF */
- /* filelen = ftell(fileptr); // Get byte offset */
- /* rewind(fileptr); // Jump back to beggining */
- /* buffer = (char *)malloc(filelen * sizeof(char)); // Enough memory for the file */
- /* fread(buffer, filelen, 1, fileptr); // Read all the file */
- /* fclose(fileptr); // Close file */
- /* // }}} */
-
- /* // Pattern matching algorithm {{{ */
- /* for (long i = 0; i < filelen; i++) { */
- /* printf("%s", *fileptr); */
- /* fileptr++; */
- /* } */
- /* // }}} */
-
- /* // Node Tree test {{{ */
- /* printf("Pattern: '%c'\n", root.pattern); */
- /* printf("End of test."); */
- /* // }}} */
-
-return 0;
+ char *c = "\\*ello";
+ c ++;
+ node *current = &main_root;
+
+ /* printf("CS: %d\n", n->childsize); */
+ /* printnode(n->child[3]); */
+
+ node *test;
+ test = checkChildren(current, c);
+ printf("Test: %d", test->event);
+
+ return 0;
};
+
diff --git a/tree.h b/tree.h
index 9de31aa..c4f86b9 100644
--- a/tree.h
+++ b/tree.h
@@ -1,7 +1,7 @@
typedef struct singleNode {
char pattern; // Character to match
- int pos; // Position (index) relative to the first node to match
- int identity; // What happens when this treeNode is the last to be matched
+ int pos; // Position of char to match relative to it's parent node.
+ int event; // What happens when this treeNode is the last to be matched
// 0: Invalid match: Match pattern is most likely incomplete (or escaped)
// 1: italics delimiter
// 2: bold delimiter
@@ -17,156 +17,153 @@ typedef struct singleNode {
// 12: automatic date (date will be formatted for groff on compile time)
// 13: manual date
// 14: indent text
- // 15: bold&italics delimiter with a -1 offset
- // 16: bold&italics delimiter with a -2 offset
+ // 15: bold&italics delimiter with a -1 offset
+ // 16: bold&italics delimiter with a -2 offset
+ // 17: asterisk escape sequence
+ // 18: underscore escape sequence
+ // 19: backtick escape sequence
int childsize;
struct singleNode *child[]; // Pointer array to other child nodes
} node ;
-typedef struct nodeRoot {
- int childsize;
- struct singleNode *child[];
-} root ;
-
-
// 8 depth {{{
node n_dfaaaaaa = {
- ':', 7, 11, 0,
+ ':', 1, 11, 0,
{}
};
// }}}
// 7 depth {{{
node n_dfaaaaa = {
- 'r', 6, 0, 1,
+ 'r', 1, 0, 1,
{&n_dfaaaaaa}
};
node n_dfcaaaa = {
- ':', 6, 10, 0,
+ ':', 1, 10, 0,
{}
};
// }}}
// 6 depth {{{
node n_dfaaaa = {
- 'o', 5, 0, 1,
+ 'o', 1, 0, 1,
{&n_dfaaaaa}
};
node n_dfbaaa = {
- ':', 5, 13, 0,
+ ':', 1, 13, 0,
{}
};
node n_dfbaab = {
- '\n', 5, 12, 0,
+ '\n', 1, 12, 0,
{}
};
node n_dfcaaa = {
- 'e', 5, 0, 1,
+ 'e', 1, 0, 1,
{&n_dfcaaaa}
};
// }}}
// 5 depth {{{
node n_dfaaa = {
- 'h', 4, 0, 1,
+ 'h', 2, 0, 1,
{&n_dfaaaa}
};
node n_dfbaa = {
- 'e', 4, 0, 2,
+ 'e', 2, 0, 2,
{&n_dfbaaa, &n_dfbaab}
};
node n_dfcaa = {
- 'l', 4, 0, 1,
+ 'l', 2, 0, 1,
{&n_dfcaaa}
};
// }}}
// 4 depth {{{
node n_dfca = {
- 'i', 2, 0, 1,
+ 'i', 1, 0, 1,
{&n_dfcaa}
};
node n_cbaa = {
- '\n', -1, 5, 0,
+ '\n', -3, 5, 0,
{}
};
node n_deaa = {
- '-', -3, 9, 0,
+ '-', -1, 9, 0,
{}
};
node n_dfaa = {
- 'u', 2, 0, 1,
+ 'u', 1, 0, 1,
{&n_dfaaa}
};
node n_dfba = {
- 'a', 2, 0, 1,
+ 'a', 1, 0, 1,
{&n_dfbaa}
};
// }}}
// 3 depth {{{
node n_dfc = {
- 't', 1, 0, 1,
+ 't', -2, 0, 1,
{&n_dfca}
};
node n_aba = {
- '_', -1, 15, 0,
+ '_', -2, 15, 0,
{}
};
node n_abb = {
- '*', 2, 3, 0,
+ '*', 1, 3, 0,
{}
};
node n_abc = {
- '_', 2, 3, 0,
+ '_', 1, 3, 0,
{}
};
node n_aca = {
- '_', -2, 16, 0,
+ '_', -1, 16, 0,
{}
};
node n_ada = {
- '_', 2, 3, 0,
+ '_', 1, 3, 0,
{}
};
node n_bba = {
- '_', 2, 3, 0,
+ '_', 1, 3, 0,
{}
};
node n_cba = {
- '`', 2, 0, 1,
+ '`', 1, 0, 1,
{&n_cbaa}
};
node n_dea = {
- '-', -2, 0, 1,
+ '-', -1, 0, 1,
{&n_deaa}
};
node n_dfa = {
- 'a', 1, 0, 1,
+ 'a', -2, 0, 1,
{&n_dfaa}
};
node n_dfb = {
- 'd', 1, 0, 1,
+ 'd', -2, 0, 1,
{&n_dfba}
};
// }}}
@@ -178,7 +175,7 @@ node n_df = {
};
node n_aa = {
- '\\', -1, 0, 0,
+ '\\', -1, 17, 0,
{}
};
@@ -198,7 +195,7 @@ node n_ad = {
};
node n_ba = {
- '\\', -1, 0, 0,
+ '\\', -1, 18, 0,
{}
};
@@ -208,7 +205,7 @@ node n_bb = {
};
node n_ca = {
- '\\', -1, 0, 0,
+ '\\', -1, 19, 0,
{}
};
@@ -265,8 +262,8 @@ node n_d = {
};
// }}}
-root tree_root = {
- 4,
+node main_root = {
+ '@', 0, 0, 4,
{&n_a, &n_b, &n_c, &n_d}
};
diff --git a/tree.txt b/tree.txt
index 7ae7bd9..b65a2f8 100644
--- a/tree.txt
+++ b/tree.txt
@@ -1,12 +1,12 @@
This is a visual representation of the node tree used for the pattern matching
alogorithm. It is in this repository as a reference. I'm a human too. I can
-make mistakes. Let's keep this as a debugging tool if I miswrite code.
+make mistakes. Let's keep this as a debugging tool in case I miswrite code.
-| VISUAL TREE | NODE IDs | MATCHED PATTERN | IDENTITY |
+| VISUAL TREE | NODE IDs | MATCHED PATTERN | EVENTS |
.
├── *,0 | (A) | * | italic (1) |
-│   ├── \,-1 | (AA) | \* | void (0) |
+│   ├── \,-1 | (AA) | \* | esc* (17) |
│   ├── *,1 | (AB) | ** | bold (2) |
│   │   ├── _,-1 | (ABA) | _** | boldit(15) |
│   │   ├── *,2 | (ABB) | *** | boldit (3) |
@@ -16,11 +16,11 @@ make mistakes. Let's keep this as a debugging tool if I miswrite code.
│   └── _,1 | (AD) | *_ | void (0) |
│   └── _,2 | (ADA) | *__ | boldit (3) |
├── _,0 | (B) | _ | italic (1) |
-│   ├── \,-1 | (BA) | \_ | void (0) |
+│   ├── \,-1 | (BA) | \_ | esc_ (18) |
│   └── _,1 | (BB) | __ | bold (2) |
│   └── _,2 | (BBA) | ___ | boldit (3) |
├── `,0 | (C) | ` | code (4) |
-│   ├── \,-1 | (CA) | \` | void (0) |
+│   ├── \,-1 | (CA) | \` | esc` (19) |
│   └── `,1 | (CB) | `` | void (0) |
│   └── `,2 | (CBA) | ``` | void (0) |
│   └── ¶,-1 | (CBAA) | ¶``` | codeB (5) |