Skip to content

Commit 47c3e2b

Browse files
authored
Merge pull request #81 from JuliaLang/sp/typed-comprehension-begin-handling
Correctly handle begin as kw in typed comprehension
2 parents db88a35 + 08d69c8 commit 47c3e2b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@ name of compatibility, perhaps with a warning.)
565565
arises from `(set! pred char-hex?)` in `parse-number` accepting hex exponent
566566
digits, all of which are detected as invalid except for a trailing `f` when
567567
processed by `isnumtok_base`.
568+
* `begin` and `end` are not parsed as keywords when indexing. Typed comprehensions
569+
initially look the same, but can be distinguished from indexing once we handle
570+
a `for` token; it is safe to treat `begin` and `end` as keywords afterwards. The
571+
reference parser *only* handles this well when there's a newline before `for`:
572+
```julia
573+
Any[foo(i)
574+
for i in x if begin
575+
true
576+
end
577+
]
578+
```
579+
works, while
580+
```julia
581+
Any[foo(i) for i in x if begin
582+
true
583+
end
584+
]
585+
```
586+
does not. JuliaSyntax handles both cases.
568587

569588
## Parsing / AST oddities and warts
570589

src/parser.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,7 @@ end
25262526
# then reconstructing the nested flattens and generators when converting to Expr.
25272527
#
25282528
# [x for a = as for b = bs if cond1 for c = cs if cond2] ==> (comprehension (flatten x (= a as) (filter (= b bs) cond1) (filter (= c cs) cond2)))
2529+
# [x for a = as if begin cond2 end] => (comprehension (generator x (filter (= a as) (block cond2))))
25292530
#
25302531
# flisp: parse-generator
25312532
function parse_generator(ps::ParseState, mark, flatten=false)
@@ -2562,7 +2563,8 @@ end
25622563
# flisp: parse-comprehension
25632564
function parse_comprehension(ps::ParseState, mark, closer)
25642565
ps = ParseState(ps, whitespace_newline=true,
2565-
space_sensitive=false)
2566+
space_sensitive=false,
2567+
end_symbol=false)
25662568
parse_generator(ps, mark)
25672569
bump_closing_token(ps, closer)
25682570
return (K"comprehension", EMPTY_FLAGS)

test/parser.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ tests = [
620620
"[x \n\n for a in as]" => "(comprehension (generator x (= a as)))"
621621
# parse_generator
622622
"[x for a = as for b = bs if cond1 for c = cs if cond2]" => "(comprehension (flatten x (= a as) (filter (= b bs) cond1) (filter (= c cs) cond2)))"
623+
"[x for a = as if begin cond2 end]" => "(comprehension (generator x (filter (= a as) (block cond2))))"
623624
"[(x)for x in xs]" => "(comprehension (generator x (error-t) (= x xs)))"
624625
"(a for x in xs if cond)" => "(generator a (filter (= x xs) cond))"
625626
"(xy for x in xs for y in ys)" => "(flatten xy (= x xs) (= y ys))"

0 commit comments

Comments
 (0)