Added scanner module and fixed BNF dependency on it
authorThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Wed, 26 Oct 2005 12:38:47 +0000 (12:38 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@dcc.ufmg.br>
Wed, 26 Oct 2005 12:38:47 +0000 (12:38 +0000)
Scanner module was added, with no default scanner. BNF dependency on it
was fixed, providing its own scanner function.

git-archimport-id: cascardo@tlscascardo--private/libgrammatic--dev--0.1--patch-22

bnf.c
scanner.c [new file with mode: 0644]
scanner.h [new file with mode: 0644]

diff --git a/bnf.c b/bnf.c
index 8d960f8..4ee7241 100644 (file)
--- a/bnf.c
+++ b/bnf.c
@@ -6,7 +6,16 @@
 #include <unistd.h>
 #include <stdlib.h>
 
-static gint scanner_next (scanner_t* scanner, GString** val)
+typedef enum
+  {
+    NONE = 0,
+    EQUAL = 1,
+    ID = 2,
+    STRING = 3,
+    EOL = 4
+  } token_t;
+
+static gint bnf_scanner_next (scanner_t* scanner, GString** val)
 {
 
   int state;
@@ -249,7 +258,7 @@ Grammar* grammar_load (char* filename)
 
   scanner = scanner_new (read, fd);
 
-  parser = rdp_new (scanner_next, scanner, BNF_GRAMMAR);
+  parser = rdp_new (bnf_scanner_next, scanner, BNF_GRAMMAR);
   grammar = (Grammar*) parser;
 
   rule = grammar_rule_new (grammar, symbol_new (FALSE, BNF_GRAMMAR));
diff --git a/scanner.c b/scanner.c
new file mode 100644 (file)
index 0000000..1307bca
--- /dev/null
+++ b/scanner.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <scanner.h>
+
+static void string_free (gpointer data)
+{
+  g_string_free ((GString*) data, TRUE);
+}
+
+scanner_t* scanner_new (readcb cb, gpointer data)
+{
+
+  scanner_t* scanner;
+
+  scanner = g_malloc (sizeof (scanner_t));
+  scanner->cb = cb;
+  scanner->data = data;
+  scanner->buffer = g_string_sized_new (256);
+  scanner->reserved = g_hash_table_new_full (g_string_hash, g_string_equal,
+                                            string_free, NULL);
+
+  return scanner;
+
+}
+
+void scanner_delete (scanner_t* scanner)
+{
+  g_hash_table_destroy (scanner->reserved);
+  g_string_free (scanner->buffer, TRUE);
+  g_free (scanner);
+}
diff --git a/scanner.h b/scanner.h
new file mode 100644 (file)
index 0000000..514d9a0
--- /dev/null
+++ b/scanner.h
@@ -0,0 +1,19 @@
+#ifndef SCANNER_H
+#define SCANNER_H
+
+#include <glib.h>
+
+typedef gint (*readcb) (gpointer, gchar*, gint);
+
+typedef struct
+{
+  readcb cb;
+  gpointer data;
+  GString* buffer;
+  GHashTable* reserved;
+} scanner_t;
+
+scanner_t* scanner_new (readcb, gpointer);
+void scanner_delete (scanner_t*);
+
+#endif