I've just debugged a few simple regexes through my regex->parser parser
and as usual every 6 months or so, it's time to show off.
The latest version is greatly simplified compared to the previous. I've squeezed
it all down to 2 "modules" where the headers have to be included in a
particular order ... but I've enforced that with header guards ... but I'm
using "McIllroy convention" for the guards so they go on the outside.
I'll just show the testing file and the output. That ought to give a sense
of what it's trying to do. For more, see
https://github.com/luser-dr00g/pcomb
or
https://github.com/luser-dr00g/pcomb/archive/1c6d8a346483f522c8fea8da2ee26314bad5bbf4.zip
for this exact contemporaneous version that works /on my machine/.
$ make count
wc pc11*[ch]
359 1567 9185 pc11object.c
92 459 3106 pc11object.h
398 1531 9652 pc11parser.c
54 186 1237 pc11parser.h
70 278 1860 pc11test.c
973 4021 25040 total
$ cat pc11test.c
#include "pc11parser.h"
static int test_basics();
static int test_parsers();
static int test_regex();
int main( void ){
return test_basics()
|| test_parsers()
|| test_regex();
}
static int
test_basics(){
puts( "test_basics" );
list ch = chars_from_str( "abcdef" );
print( ch ), puts("");
print_list( ch ), puts("");
drop( 6, ch );
print( ch ), puts("");
print_list( ch ), puts("");
drop( 7, ch );
print( ch ), puts("");
print_list( ch ), puts("");
puts("");
return 0;
}
static int
test_parsers(){
puts( "test_parsers" );
list ch = chars_from_str( "a b c d 1 2 3 4" );
parser p = result_is( Int(42) );
print_list( parse( p, ch ) ), puts("");
parser q = fails();
print_list( parse( q, ch ) ), puts("");
parser r = item();
print_list( parse( r, ch ) ), puts("");
parser s = either( alpha(), item() );
print_list( parse( s, ch ) ), puts("");
parser t = literal( Int('a') );
print_list( parse( t, ch ) ), puts("");
puts("");
return 0;
}
static int
test_regex(){
puts( "test_regex" );
parser a = regex( "." );
print_list( a ), puts("");
print_list( parse( a, chars_from_str( "a" ) ) ), puts("");
print_list( parse( a, chars_from_str( "." ) ) ), puts("");
print_list( parse( a, chars_from_str( "\\." ) ) ), puts("");
puts("");
parser b = regex( "\\." );
print_list( b ), puts("");
print_list( parse( b, chars_from_str( "a" ) ) ), puts("");
print_list( parse( b, chars_from_str( "." ) ) ), puts("");
print_list( parse( b, chars_from_str( "\\." ) ) ), puts("");
puts("");
parser c = regex( "\\\\." );
print_list( c ), puts("");
print_list( parse( c, chars_from_str( "a" ) ) ), puts("");
print_list( parse( c, chars_from_str( "." ) ) ), puts("");
print_list( parse( c, chars_from_str( "\\." ) ) ), puts("");
puts("");
return 0;
}
$ make
cc -std=c99 -g -Wall -Wpedantic -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-switch -Wno-return-type -Wunused-variable -c -o pc11object.o pc11object.c
cc -std=c99 -g -Wall -Wpedantic -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-switch -Wno-return-type -Wunused-variable -c -o pc11parser.o pc11parser.c
cc -std=c99 -g -Wall -Wpedantic -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-switch -Wno-return-type -Wunused-variable -c -o pc11test.o pc11test.c
cc -std=c99 -g -Wall -Wpedantic -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-switch -Wno-return-type -Wunused-variable -o pc11 pc11object.o pc11parser.o pc11test.o
./pc11
test_basics
...(force_chars_from_string)
...(force_chars_from_string)
('a' .('b' .('c' .('d' .('e' .('f' ....(force_chars_from_string) ))))))
('a' 'b' 'c' 'd' 'e' 'f' ...(force_chars_from_string) )
('a' .('b' .('c' .('d' .('e' .('f' .(EOF .() )))))))
('a' 'b' 'c' 'd' 'e' 'f' EOF )
test_parsers
(OK ((VALUE '*' ) ) ...(force_chars_from_string) )
(FAIL () ...(force_chars_from_string) )
(OK 'a' ...(force_chars_from_string) )
(OK 'a' ...(force_chars_from_string) )
(OK 'a' ...(force_chars_from_string) )
test_regex
Parser(parse_satisfy)
(OK 'a' ...(force_chars_from_string) )
(OK '.' ...(force_chars_from_string) )
(OK '\' ...(force_chars_from_string) )
Parser(parse_satisfy)
(FAIL ("predicate not satisfied" Oper(is_literal) ) 'a' ...(force_chars_from_string) )
(OK '.' ...(force_chars_from_string) )
(FAIL ("predicate not satisfied" Oper(is_literal) ) '\' ...(force_chars_from_string) )
Parser(parse_sequence)
(FAIL ("predicate not satisfied" Oper(is_literal) ) 'a' ...(force_chars_from_string) )
(FAIL ("predicate not satisfied" Oper(is_literal) ) '.' ...(force_chars_from_string) )
(OK ('\' '.' ) ...(force_chars_from_string) )