1 | #include "enum.hfa" |
---|
2 | #include "fstream.hfa" |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #pragma GCC visibility push(default) |
---|
6 | |
---|
7 | forall( E | Serial( E ) ) { |
---|
8 | E fromInt( unsigned i ) { |
---|
9 | E upper = upperBound(); |
---|
10 | E lower = lowerBound(); |
---|
11 | // It is okay to overflow as overflow will be theoretically caught by the other bound |
---|
12 | if ( i < fromInstance( lower ) || i > fromInstance( upper ) ) |
---|
13 | abort( "call to fromInt has index %d outside enumeration range %d-%d", |
---|
14 | i, fromInstance( lower ), fromInstance( upper ) ); |
---|
15 | return fromInt_unsafe( i ); |
---|
16 | } |
---|
17 | |
---|
18 | E succ( E e ) { |
---|
19 | E upper = upperBound(); |
---|
20 | if ( fromInstance( e ) >= fromInstance( upper ) ) |
---|
21 | abort( "call to succ() exceeds enumeration upper bound of %d", fromInstance( upper ) ); |
---|
22 | return succ_unsafe(e); |
---|
23 | } |
---|
24 | |
---|
25 | E pred( E e ) { |
---|
26 | E lower = lowerBound(); |
---|
27 | if ( fromInstance(e) <= fromInstance(lower ) ) |
---|
28 | abort( "call to pred() exceeds enumeration lower bound of %d", fromInstance( lower ) ); |
---|
29 | return pred_unsafe(e); |
---|
30 | } |
---|
31 | |
---|
32 | int Countof( __attribute__((unused)) E e ) { |
---|
33 | E upper = upperBound(); |
---|
34 | E lower = lowerBound(); |
---|
35 | return fromInstance( upper ) + fromInstance( lower ) + 1; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial( E ) ) |
---|
40 | istype & ?|?( istype & is, E & e ) { |
---|
41 | // fprintf( stderr, "here0\n" ); |
---|
42 | if ( eof( is ) ) throwResume ExceptionInst( missing_data ); |
---|
43 | |
---|
44 | // Match longest input enumerator string to enumerator labels, where enumerator names are unique. |
---|
45 | |
---|
46 | int N = countof( E ), lnths[N], max = 0; |
---|
47 | // printf( "N %d\n", N ); |
---|
48 | int r = 0; |
---|
49 | // for ( s; E : r; 0~@ ) { |
---|
50 | for ( s; E ) { // scan string rows gathering lengths |
---|
51 | lnths[r] = strlen( label( s ) ); |
---|
52 | if ( lnths[r] > max ) max = lnths[r]; |
---|
53 | // fprintf( stderr, "%s %d %d\n", label( s ), lnths[r], max ); |
---|
54 | r += 1; |
---|
55 | } // for |
---|
56 | |
---|
57 | int mcol = -1; // last match column |
---|
58 | char ch, curr = '\0', prev = '\0'; |
---|
59 | |
---|
60 | fmt( is, " " ); // skip optional whitespace |
---|
61 | for ( c; max ) { // scan columns of the label matix (some columns missing) |
---|
62 | int args = fmt( is, "%c", &ch ); // read character |
---|
63 | // fprintf( stderr, "fmt args: %d eof: %d\n", args, eof(is) ); |
---|
64 | if ( eof( is ) ) { |
---|
65 | // fprintf( stderr, "Eof1\n" ); |
---|
66 | if ( c == 0 ) return is; // no characters read ? |
---|
67 | clear( is ); // => read something => reset EOF => detect again on next read |
---|
68 | // fprintf( stderr, "Eof2\n" ); |
---|
69 | break; |
---|
70 | } // if |
---|
71 | if ( args != 1 ) throwResume ExceptionInst( missing_data ); // may be unnecessary since reading single character |
---|
72 | |
---|
73 | // printf( "read '%c'\n", ch ); |
---|
74 | for ( r; N ) { // scan enumeration strings for matching character in current column |
---|
75 | // printf( "%d %d %d\n", c, r, lnths[r] ); |
---|
76 | if ( c < lnths[r] ) { // string long enough for this column check ? |
---|
77 | char match = label( fromInt( r ) )[c]; // optimization |
---|
78 | // printf( "%c '%c'\n", match, ch ); |
---|
79 | // Stop on first match, could be other matches. |
---|
80 | if ( (match == ch) && (c == 0 || curr == label( fromInt( r ) )[c - 1]) ) { |
---|
81 | // printf( "match %d %d %d '%c' '%c' '%c' '%c' 'c'\n", c, r, lnths[r], match, ch, prev, label( fromInt( r ) )[c - 1] ); |
---|
82 | mcol = c; // matching column |
---|
83 | prev = curr; // last matching character |
---|
84 | curr = ch; // current matching character |
---|
85 | break; |
---|
86 | } // if |
---|
87 | } // if |
---|
88 | } else { |
---|
89 | // fprintf( stderr, "finished mcol: %d ch: '%c' curr: '%c' prev: '%c'\n", mcol, ch, curr, prev ); |
---|
90 | ungetc( ch, is ); // push back last unmatching character |
---|
91 | if ( mcol == -1 ) throwResume ExceptionInst( missing_data ); // no matching character in first column |
---|
92 | break; |
---|
93 | } // for |
---|
94 | // printf( "\n" ); |
---|
95 | // } else { |
---|
96 | // fprintf( stderr, "finished2 %d\n", mcol ); |
---|
97 | } // for |
---|
98 | |
---|
99 | for ( c; N ) { // scan enumeration strings of length "mcol" for match |
---|
100 | if ( mcol == lnths[c] - 1 ) { |
---|
101 | char match = label( fromInt( c ) )[mcol]; // optimization |
---|
102 | // printf( "finished1 mcol: %d c: %d lnth: %d match: '%c' curr: '%c' prev: '%c'\n", mcol, c, lnths[c], match, curr, prev ); |
---|
103 | if ( (match == curr) && (mcol == 0 || prev == label( fromInt( c ) )[mcol - 1]) ) { |
---|
104 | e = fromInt( c ); |
---|
105 | break; |
---|
106 | } // if |
---|
107 | } // if |
---|
108 | } else { |
---|
109 | // fprintf( stderr, "finished3 %d\n", mcol ); |
---|
110 | throwResume ExceptionInst( missing_data ); // no match in this column |
---|
111 | } // for |
---|
112 | return is; |
---|
113 | } |
---|
114 | |
---|
115 | forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { |
---|
116 | ostype & ?|?( ostype & os, E e ) { |
---|
117 | return os | label( e ); |
---|
118 | } |
---|
119 | OSTYPE_VOID_IMPL( E ) |
---|
120 | } |
---|