From 73d0bdf8c771116da360cf8cf4b075f4d8431db6 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Sun, 27 Jan 2013 12:48:45 -0500 Subject: test case for nested streams --- test/include1.in | 14 ++++++++ test/include1.lm | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/include1a.in | 2 ++ test/include1b.in | 2 ++ test/include1c.in | 2 ++ 5 files changed, 118 insertions(+) create mode 100644 test/include1.in create mode 100644 test/include1.lm create mode 100644 test/include1a.in create mode 100644 test/include1b.in create mode 100644 test/include1c.in diff --git a/test/include1.in b/test/include1.in new file mode 100644 index 00000000..9c7aa806 --- /dev/null +++ b/test/include1.in @@ -0,0 +1,14 @@ + +hello; + +#include "include1a.in" + +there; + +#include "include1b.in" + +dude; + +#include "include1c.in" + +and dudettes; diff --git a/test/include1.lm b/test/include1.lm new file mode 100644 index 00000000..03a3b046 --- /dev/null +++ b/test/include1.lm @@ -0,0 +1,98 @@ +namespace string + lex + literal '"' + token data /[^"\\]+/ + token escape /'\\' any/ + end + + def string_data + [data] + | [escape] + + def string + ['"' string_data* '"'] + + str unquote( S: string ) + { + match S ['"' DL: string_data* '"'] + for E: escape in DL + E.data = 'x' + return $DL + } + +end string + +namespace hash + + lex + literal 'define', 'include' + literal '#', '\n' ni + + token id /[a-zA-Z_][a-zA-Z_0-9]*/ + token number /[0-9]+/ + + ignore /[ \t]/ + end + + def hash + ['#' 'define' Id: id number '\n'] + | ['#' 'include' Inc: string::string '\n'] + +end hash + +token rest_of_line /[^\n]* '\n'/ + +namespace lang + + lex + ignore /space/ + literal '*', '(', ')', ';' + token id /[a-zA-Z_][a-zA-Z_0-9]*/ + token number /[0-9]+/ + + token hash /'#'/ { + parse_stop H: hash::hash[ input ] + if ( H.tree ) { + if ( H.tree.Inc ) { + FN: str = unquote( H.tree.Inc ) + print( 'opening ' FN '\n' ) + IS: stream = open( FN 'r' ) + if ( ! IS ) { + print( 'ERROR: failed to open ' FN '\n' ) + exit(1) + } + input.push( IS ) + } + } + else { + parse_stop L: rest_of_line[ input ] + if ! L.tree { + print( "ERROR: stuck: " L.error ) + exit(1) + } + print( "ERROR: failed to parse # directive: " L.tree ) + } + } + end + + def item + [id] + | ['(' item* ')'] + + def statement + [item* ';'] + + def start + [statement*] + +end lang + +parse Input: lang::start[ stdin ] + +if ! Input.tree + print( Input.error '\n' ) +else { + #print( Input.tree '\n' ) + S: lang::start = Input.tree + print( Input.tree '\n' ) +} diff --git a/test/include1a.in b/test/include1a.in new file mode 100644 index 00000000..26da0afa --- /dev/null +++ b/test/include1a.in @@ -0,0 +1,2 @@ +a; +b; diff --git a/test/include1b.in b/test/include1b.in new file mode 100644 index 00000000..6c574323 --- /dev/null +++ b/test/include1b.in @@ -0,0 +1,2 @@ +c; +d; diff --git a/test/include1c.in b/test/include1c.in new file mode 100644 index 00000000..5373832d --- /dev/null +++ b/test/include1c.in @@ -0,0 +1,2 @@ +e; +f; -- cgit v1.2.1