summaryrefslogtreecommitdiff
path: root/test/recdescent1.rl
diff options
context:
space:
mode:
authorJinkun Jang <jinkun.jang@samsung.com>2013-03-13 01:44:50 +0900
committerJinkun Jang <jinkun.jang@samsung.com>2013-03-13 01:44:50 +0900
commit7e13e5fc62b81ffc5728006ddb560478ac3f1576 (patch)
tree813d127306e4518e2ab20cf03f7dea33d14a4210 /test/recdescent1.rl
parent12056158053532946b53b6249cb0e6cfd4580051 (diff)
downloadragel-7e13e5fc62b81ffc5728006ddb560478ac3f1576.tar.gz
ragel-7e13e5fc62b81ffc5728006ddb560478ac3f1576.tar.bz2
ragel-7e13e5fc62b81ffc5728006ddb560478ac3f1576.zip
Tizen 2.1 base
Diffstat (limited to 'test/recdescent1.rl')
-rw-r--r--test/recdescent1.rl128
1 files changed, 128 insertions, 0 deletions
diff --git a/test/recdescent1.rl b/test/recdescent1.rl
new file mode 100644
index 0000000..1ffca28
--- /dev/null
+++ b/test/recdescent1.rl
@@ -0,0 +1,128 @@
+/*
+ * @LANG: c
+ * Test growable stack.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+%%{
+ machine recdescent;
+
+ prepush {
+ if ( top == stack_size ) {
+ printf( "growing stack\n" );
+ stack_size = top * 2;
+ stack = (int*)realloc( stack, sizeof(int)*stack_size );
+ }
+ }
+
+ postpop {
+ if ( stack_size > (top * 4) ) {
+ stack_size = top * 2;
+ stack = (int*)realloc( stack, sizeof(int)*stack_size );
+ printf( "shrinking stack\n" );
+ }
+ }
+
+ action item_start { item = p; }
+
+ action item_finish
+ {
+ printf( "item: " );
+ fwrite( item, 1, p-item, stdout );
+ printf( "\n" );
+ }
+
+ action call_main
+ {
+ printf( "calling main\n" );
+ fcall main;
+ }
+
+ action return_main
+ {
+ if ( top == 0 ) {
+ printf( "STRAY CLOSE\n" );
+ fbreak;
+ }
+
+ printf( "returning from main\n" );
+ fhold;
+ fret;
+ }
+
+ id = [a-zA-Z_]+;
+ number = [0-9]+;
+ ws = [ \t\n]+;
+
+ main := (
+ ws |
+ ( number | id ) >item_start %item_finish |
+
+ '{' @call_main '}' |
+
+ '}' @return_main
+ )**;
+}%%
+
+%% write data;
+
+void test( char *buf )
+{
+ int cs;
+ int *stack;
+ int top, stack_size;
+ char *p, *pe, *eof, *item = 0;
+
+ int len = strlen( buf );
+
+ %% write init;
+
+ stack_size = 1;
+ stack = (int*)malloc( sizeof(int) * stack_size );
+
+ p = buf;
+ pe = buf + len;
+ eof = pe;
+
+ %% write exec;
+
+ if ( cs == recdescent_error ) {
+ /* Machine failed before finding a token. */
+ printf( "PARSE ERROR\n" );
+ }
+}
+
+int main()
+{
+ test( "88 foo { 99 {{{{}}}}{ } }");
+ test( "76 } sadf");
+ return 0;
+}
+
+#ifdef _____OUTPUT_____
+item: 88
+item: foo
+calling main
+item: 99
+calling main
+growing stack
+calling main
+growing stack
+calling main
+calling main
+growing stack
+returning from main
+returning from main
+returning from main
+returning from main
+shrinking stack
+calling main
+returning from main
+returning from main
+shrinking stack
+item: 76
+STRAY CLOSE
+#endif