Merge pull request #1446 from AlexKnauth/cdot-left-assoc

make cdot reader left associative
This commit is contained in:
Jay McCarthy 2016-09-01 20:40:56 -04:00 committed by GitHub
commit 5b1658c6b4
3 changed files with 73 additions and 37 deletions

View File

@ -955,13 +955,19 @@ equivalent prefix as discussed in @secref["parse-number"]. If these
numbers are followed by a @litchar{.} intended to be read as a C-style numbers are followed by a @litchar{.} intended to be read as a C-style
infix dot, then there must be separating whitespace. infix dot, then there must be separating whitespace.
Finally, after reading any value, @racket[_x], the reader will seek Finally, after reading any datum @racket[_x], the reader will seek
over whitespace until it reaches a non-whitespace character. If the through whitespace and look for zero or more sequences of a
character is not @litchar{.}, then the value, @racket[_x], is returned @litchar{.} followed by another datum @racket[_y]. It will then group
as usual. If the character is @litchar{.}, then another value, @racket[_x] and @racket[_y] together in a @racket[#%dot] form so that
@racket[_y], is read and the result @racket[(list '#%dot _x _y)] is @racket[_x.y] reads equal to @racket[(#%dot _x _y)].
returned. In @racket[read-syntax] mode, the @racket['#%dot] symbol has
the source location information of the @litchar{.} character and the If @racket[_x.y] has another @litchar{.} after it, the reader will
accumulate more @litchar{.}-separated datums, grouping them from
left-to-right. For example, @racket[_x.y.z] reads equal to
@racket[(#%dot (#%dot _x _y) _z)].
In @racket[read-syntax] mode, the @racket[#%dot] symbol has the
source location information of the @litchar{.} character and the
entire list has the source location information spanning from the entire list has the source location information spanning from the
start of @racket[_x] to the end of @racket[_y]. start of @racket[_x] to the end of @racket[_y].

View File

@ -1241,6 +1241,27 @@
(test 9t0 readstr "#o11.0t0") (test 9t0 readstr "#o11.0t0")
(test 17t0 readstr "#x11.0t0")) (test 17t0 readstr "#x11.0t0"))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; read-cdot
(parameterize ([read-cdot #true])
(test '(#%dot a b) readstr "a.b")
(test '(#%dot (#%dot a b) c) readstr "a.b.c")
(test '(#%dot (#%dot (#%dot a b) c) d) readstr "a.b.c.d")
(test '(#%dot a (#%dot b c)) readstr "a.(#%dot b c)")
(test '(#%dot a (m b c)) readstr "a.(m b c)")
(test '(#%dot (#%dot a (m b c)) (n d e)) readstr "a.(m b c).(n d e)")
(test '(#%dot (#%dot (#%dot (#%dot (#%dot a (m b c)) x) (n d e)) y) z)
readstr "a.(m b c).x.(n d e).y.z")
(test '(#%dot (f a) b) readstr "(f a).b")
(test '(#%dot (#%dot (f a) b) c) readstr "(f a).b.c")
(test '(#%dot (#%dot (#%dot (f a) b) c) d) readstr "(f a).b.c.d")
(test '(#%dot (f a) (#%dot b c)) readstr "(f a).(#%dot b c)")
(test '(#%dot (f a) (m b c)) readstr "(f a).(m b c)")
(test '(#%dot (#%dot (f a) (m b c)) (n d e)) readstr "(f a).(m b c).(n d e)")
(test '(#%dot (#%dot (#%dot (#%dot (#%dot (f a) (m b c)) x) (n d e)) y) z)
readstr "(f a).(m b c).x.(n d e).y.z"))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; srcloc->string ;; srcloc->string

View File

@ -2035,6 +2035,10 @@ read_inner_inner(Scheme_Object *port, Scheme_Object *stxsrc, Scheme_Hash_Table *
if (!read_cdot) { return ret; } if (!read_cdot) { return ret; }
// read in zero or more . sequences in a left-associative way
// X.Y should be read as (#%dot X Y)
// X.Y.Z should be read as (#%dot (#%dot X Y) Z)
while ( 1 ) {
found_dot = 0; found_dot = 0;
while ( 1 ) { while ( 1 ) {
next = scheme_peekc_special_ok(port); next = scheme_peekc_special_ok(port);
@ -2048,14 +2052,17 @@ read_inner_inner(Scheme_Object *port, Scheme_Object *stxsrc, Scheme_Hash_Table *
break; break;
} }
if ( found_dot ) { if ( !found_dot ) {
return ret;
} else {
Scheme_Object *dot, *next; Scheme_Object *dot, *next;
scheme_tell_all(port, &dline, &dcol, &dpos); scheme_tell_all(port, &dline, &dcol, &dpos);
dot = dot_symbol; dot = dot_symbol;
if (stxsrc) { if (stxsrc) {
dot = scheme_make_stx_w_offset(dot, dline, dcol, dpos, SPAN(port,dpos), stxsrc, STX_SRCTAG); dot = scheme_make_stx_w_offset(dot, dline, dcol, dpos, SPAN(port,dpos), stxsrc, STX_SRCTAG);
} }
next = read_inner_inner(port, stxsrc, ht, indentation, params, comment_mode, pre_char, table, get_info); next = read_inner_inner_inner(port, stxsrc, ht, indentation, params, comment_mode, pre_char, table, get_info);
if (SCHEME_EOFP(next)) { if (SCHEME_EOFP(next)) {
scheme_read_err(port, stxsrc, dline, dcol, dpos, 1, EOF, indentation, scheme_read_err(port, stxsrc, dline, dcol, dpos, 1, EOF, indentation,
"read: expected a datum after cdot, found end-of-file"); "read: expected a datum after cdot, found end-of-file");
@ -2068,7 +2075,9 @@ read_inner_inner(Scheme_Object *port, Scheme_Object *stxsrc, Scheme_Hash_Table *
} }
} }
return ret; // look for more dots after this
continue;
}
} }
static Scheme_Object * static Scheme_Object *