summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2016-11-21 16:17:05 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2016-11-21 16:17:07 +0900
commit0712c3599bfebc49eda8ab8c49e5536d968d8da7 (patch)
treea76e99ec24aa4bb340e96e53b93addae9da253e7 /examples
parent307d1e8f2b8d73b425518ced60a441aff64ff8a2 (diff)
downloadre2c-0712c3599bfebc49eda8ab8c49e5536d968d8da7.tar.gz
re2c-0712c3599bfebc49eda8ab8c49e5536d968d8da7.tar.bz2
re2c-0712c3599bfebc49eda8ab8c49e5536d968d8da7.zip
Imported Upstream version 0.14
Change-Id: Id2146ce087edd7a4b5d5743f692ea29c2051ccb1 Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/input_custom/fixed.re35
-rw-r--r--examples/input_custom/simple/README20
-rw-r--r--examples/input_custom/simple/default.re24
-rw-r--r--examples/input_custom/simple/fgetc.re43
-rw-r--r--examples/input_custom/simple/istringstream.re27
5 files changed, 149 insertions, 0 deletions
diff --git a/examples/input_custom/fixed.re b/examples/input_custom/fixed.re
new file mode 100644
index 00000000..51f3b2b0
--- /dev/null
+++ b/examples/input_custom/fixed.re
@@ -0,0 +1,35 @@
+// Build with "--input custom" re2c switch.
+//
+// This is an example of handling fixed-length buffer with "--input custom":
+// on each YYPEEK we check for the end of input, thus YYFILL generation
+// can be safely suppressed.
+//
+// Note that YYLIMIT points not to terminating NULL, but to the previous
+// character: we emulate the case when input has no terminating NULL.
+//
+// For a real-life example see https://github.com/sopyer/mjson
+// or mjson.re from re2c test collection.
+
+bool lex (const char * cursor, const char * const limit)
+{
+ const char * marker;
+ const char * ctxmarker;
+# define YYCTYPE char
+# define YYPEEK() (cursor >= limit ? 0 : *cursor)
+# define YYSKIP() ++cursor
+# define YYBACKUP() marker = cursor
+# define YYBACKUPCTX() ctxmarker = cursor
+# define YYRESTORE() cursor = marker
+# define YYRESTORECTX() cursor = ctxmarker
+ /*!re2c
+ re2c:yyfill:enable = 0;
+ "int buffer " / "[" [0-9]+ "]" { return true; }
+ * { return false; }
+ */
+}
+
+int main ()
+{
+ char buffer [] = "int buffer [1024]";
+ return !lex (buffer, buffer + sizeof (buffer) - 1);
+}
diff --git a/examples/input_custom/simple/README b/examples/input_custom/simple/README
new file mode 100644
index 00000000..c0c4d955
--- /dev/null
+++ b/examples/input_custom/simple/README
@@ -0,0 +1,20 @@
+Build with "--input custom" re2c switch.
+
+These are three examples of "--input custom" usage:
+
+- input_custom_default.re:
+ implements default re2c input model (pointers to plain buffer)
+
+- input_custom_fgetc:
+ implements C-style file input (using <stdio.h>)
+
+- input_custom_fgetc:
+ implements std::istringstream input
+
+Note that these examples are very simple and don't need
+to implement YYFILL; the only reason they don't use
+"re2c:yyfill:enable = 0;" is to keep YYLESSTHAN and YYLIMIT
+(for the sake of example).
+
+In real-life programs one will need to care for correct
+end-of-input handling.
diff --git a/examples/input_custom/simple/default.re b/examples/input_custom/simple/default.re
new file mode 100644
index 00000000..94cde7cd
--- /dev/null
+++ b/examples/input_custom/simple/default.re
@@ -0,0 +1,24 @@
+bool lex (const char * cursor, const char * const limit)
+{
+ const char * marker;
+ const char * ctxmarker;
+# define YYCTYPE char
+# define YYPEEK() *cursor
+# define YYSKIP() ++cursor
+# define YYBACKUP() marker = cursor
+# define YYBACKUPCTX() ctxmarker = cursor
+# define YYRESTORE() cursor = marker
+# define YYRESTORECTX() cursor = ctxmarker
+# define YYLESSTHAN(n) limit - cursor < n
+# define YYFILL(n) {}
+ /*!re2c
+ "int buffer " / "[" [0-9]+ "]" { return true; }
+ * { return false; }
+ */
+}
+
+int main ()
+{
+ char buffer [] = "int buffer [1024]";
+ return !lex (buffer, buffer + sizeof (buffer));
+}
diff --git a/examples/input_custom/simple/fgetc.re b/examples/input_custom/simple/fgetc.re
new file mode 100644
index 00000000..d2dffd9a
--- /dev/null
+++ b/examples/input_custom/simple/fgetc.re
@@ -0,0 +1,43 @@
+#include <stdio.h>
+
+char peek (FILE * f)
+{
+ char c = fgetc (f);
+ ungetc (c, f);
+ return c;
+}
+
+bool lex (FILE * f, const long limit)
+{
+ long marker;
+ long ctxmarker;
+# define YYCTYPE char
+# define YYPEEK() peek (f)
+# define YYSKIP() fgetc (f)
+# define YYBACKUP() marker = ftell (f)
+# define YYBACKUPCTX() ctxmarker = ftell (f)
+# define YYRESTORE() fseek (f, marker, SEEK_SET)
+# define YYRESTORECTX() fseek (f, ctxmarker, SEEK_SET)
+# define YYLESSTHAN(n) limit - ftell (f) < n
+# define YYFILL(n) {}
+ /*!re2c
+ "int buffer " / "[" [0-9]+ "]" { return true; }
+ * { return false; }
+ */
+}
+
+int main ()
+{
+ const char buffer [] = "int buffer [1024]";
+ const char fn [] = "input.txt";
+
+ FILE * f = fopen (fn, "w");
+ fwrite (buffer, 1, sizeof (buffer), f);
+ fclose (f);
+
+ f = fopen (fn, "rb");
+ int result = !lex (f, sizeof (buffer));
+ fclose (f);
+
+ return result;
+}
diff --git a/examples/input_custom/simple/istringstream.re b/examples/input_custom/simple/istringstream.re
new file mode 100644
index 00000000..5d702291
--- /dev/null
+++ b/examples/input_custom/simple/istringstream.re
@@ -0,0 +1,27 @@
+#include <sstream>
+
+bool lex (std::istringstream & is, const std::streampos limit)
+{
+ std::streampos marker;
+ std::streampos ctxmarker;
+# define YYCTYPE char
+# define YYPEEK() is.peek ()
+# define YYSKIP() is.ignore ()
+# define YYBACKUP() marker = is.tellg ()
+# define YYBACKUPCTX() ctxmarker = is.tellg ()
+# define YYRESTORE() is.seekg (marker)
+# define YYRESTORECTX() is.seekg (ctxmarker)
+# define YYLESSTHAN(n) limit - is.tellg () < n
+# define YYFILL(n) {}
+ /*!re2c
+ "int buffer " / "[" [0-9]+ "]" { return true; }
+ * { return false; }
+ */
+}
+
+int main ()
+{
+ const char buffer [] = "int buffer [1024]";
+ std::istringstream is (buffer);
+ return !lex (is, sizeof (buffer));
+}