summaryrefslogtreecommitdiff
path: root/src/reader.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-11 09:58:43 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-11 09:58:43 +0800
commit91016bd3855375796fd1eabd501ebc12491f2655 (patch)
treec4a608966b686c9639a90b92c31c00632532fac8 /src/reader.c
parent949888ad5d0cd0f9f9a87f9938a632b3e32df051 (diff)
Add the framework for character classes.
Now we have the potential to recognize character classes. But the most important task for us now is to experiment with ((B)RN)GLR algorithms, so we leave the character classes at the present state for a moment.
Diffstat (limited to 'src/reader.c')
-rw-r--r--src/reader.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/reader.c b/src/reader.c
index 18435a9..d715726 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -21,14 +21,15 @@
/* Read a string of TNT's into TNTS. TNTS is expected to be already
allocated. */
-static unsigned char
-read_tnt_string(str *s, NUM start, NUM end, TNT *tnts, List *cpa_list)
+static BOOL
+read_tnt_string(const str * const restrict s, NUM start, NUM end,
+ TNT *tnts, List *cpa_list)
{
str_info info;
/* fleprintf("S = %lu, E = %lu\n", start, end); */
- unsigned char readingp = 0, stop = 0;
+ BOOL readingp = 0, stop = 0, double_quote_p = 0;
NUM *string = NULL, string_size = 0, nt_index = 0, tnt_count = 0;
@@ -73,14 +74,15 @@ read_tnt_string(str *s, NUM start, NUM end, TNT *tnts, List *cpa_list)
}
break;
case 34:
+ double_quote_p = 1;
case 39:
/* read a string */
strindex = index + info.step;
strinfo = get_info(s, strindex);
for (;strindex < end &&
- strinfo.value != 34 &&
- strinfo.value != 39 &&
+ ((double_quote_p && strinfo.value != 34) ||
+ (!double_quote_p && strinfo.value != 39)) &&
strinfo.value != 10 &&
strinfo.value != 13;) {
strinfo = get_info(s, strindex);
@@ -88,7 +90,7 @@ read_tnt_string(str *s, NUM start, NUM end, TNT *tnts, List *cpa_list)
if (strinfo.value == 92) {
strindex += info.step;
- unsigned char local_exit = 0;
+ BOOL local_exit = 0;
/* The following errors should not happen. I am just
paranoid. */
@@ -118,6 +120,8 @@ read_tnt_string(str *s, NUM start, NUM end, TNT *tnts, List *cpa_list)
strindex -= strinfo.step;
tnt_count--;
+ double_quote_p = 0;
+
index = strindex;
info = get_info(s, index);
break;
@@ -166,6 +170,7 @@ read_tnt_string(str *s, NUM start, NUM end, TNT *tnts, List *cpa_list)
return 0;
}
+UNUSED
static void
print_int(void *p)
{
@@ -174,7 +179,7 @@ print_int(void *p)
/* Read a grammar from a string in the format of BNF notation. */
Grammar *
-read_grammar_from_bnf(str *s)
+read_grammar_from_bnf(const str * const restrict s)
{
NUM len = str_length(s);
@@ -186,7 +191,7 @@ read_grammar_from_bnf(str *s)
List *line_indices = new_list();
- unsigned char definitionp = 1, readingp = 0;
+ BOOL definitionp = 1, readingp = 0;
/* last index is used to collect strings */
for (NUM index = 0, last_index = 0; index < len;) {
@@ -205,7 +210,7 @@ read_grammar_from_bnf(str *s)
case 9:
readingp = 0;
- if (definitionp) {
+ if (definitionp && readingp) {
fleprintf("%lu, A non-terminal cannot have spaces in "
"the name\n", index);
/* cleanup */
@@ -257,9 +262,9 @@ read_grammar_from_bnf(str *s)
index += info.step;
}
- printf("%s:%d, print indices\n", __FILE__, __LINE__);
- map_list(line_indices, print_int);
- printf("\n");
+ /* printf("%s:%d, print indices\n", __FILE__, __LINE__);
+ * map_list(line_indices, print_int);
+ * printf("\n"); */
/* second round collects all the rules */
@@ -267,7 +272,7 @@ read_grammar_from_bnf(str *s)
NUM line_count = 0;
- unsigned char right_start_p = 0;
+ BOOL right_start_p = 0, double_quote_p = 0;
definitionp = 1, readingp = 0;
@@ -286,6 +291,7 @@ read_grammar_from_bnf(str *s)
switch (info.value) {
/* the quote characters */
case 34: /* double quote */
+ double_quote_p = 1;
case 39: /* single quote */
if (definitionp) {
fleprintf("%lu, A non-terminal cannot have quotes in "
@@ -330,8 +336,8 @@ read_grammar_from_bnf(str *s)
for (;strindex < len &&
strinfo.value != 13 &&
strinfo.value != 10 &&
- strinfo.value != 34 &&
- strinfo.value != 39;) {
+ ((double_quote_p && strinfo.value != 34) ||
+ (!double_quote_p && strinfo.value != 39));) {
strinfo = get_info(s, strindex);
/* escape character */
@@ -339,7 +345,7 @@ read_grammar_from_bnf(str *s)
strindex += strinfo.step;
if (strindex < len) strinfo = get_info(s, strindex);
- unsigned char local_error_p = 0;
+ BOOL local_error_p = 0;
if (strindex >= len) {
local_error_p = 1;
@@ -395,8 +401,8 @@ read_grammar_from_bnf(str *s)
return NULL;
}
} else {
- if (strinfo.value != 34 &&
- strinfo.value != 39) {
+ if ((double_quote_p && strinfo.value != 34) ||
+ (!double_quote_p && strinfo.value != 39)) {
fleprintf0("input ended before string is left\n");
/* cleanup */
map_list(names, destroy_cpa_and_free_all);
@@ -408,15 +414,16 @@ read_grammar_from_bnf(str *s)
destroy_list(rules, 0);
return NULL;
}
-
}
}
+ double_quote_p = 0;
+
break;
/* spaces and tabs are ignored */
case ' ':
case 9:
- if (definitionp) {
+ if (definitionp && readingp) {
fleprintf("%lu, A non-terminal cannot have spaces or "
"tabs in the name\n", index);
/* cleanup */
@@ -435,6 +442,11 @@ read_grammar_from_bnf(str *s)
break;
case '\n':
case 13:
+ if (definitionp && !readingp) {
+ /* empty line */
+ break;
+ }
+
definitionp = 1;
readingp = 0;
line_count++;
@@ -461,13 +473,13 @@ read_grammar_from_bnf(str *s)
return NULL;
}
- printf("%s:%d:%lu, Printing a TNT string: ",
- __FILE__, __LINE__, index);
- for (int i = 0; i < (int) tnt_count;) {
- print_tnt(current_tnt_string+i++);
- if (i<tnt_count) printf(", ");
- }
- printf("\n");
+ /* printf("%s:%d:%lu, Printing a TNT string: ",
+ * __FILE__, __LINE__, index); */
+ /* for (int i = 0; i < (int) tnt_count;) {
+ * print_tnt(current_tnt_string+i++);
+ * if (i<tnt_count) printf(", ");
+ * }
+ * printf("\n"); */
temp_pointers = MYALLOC(void *, tnt_count);