Skip to content

Commit c8f4e7f

Browse files
authored
Merge pull request #75 from fink-lang/features
features
2 parents 8c24cf6 + 52c84c3 commit c8f4e7f

File tree

9 files changed

+214
-30
lines changed

9 files changed

+214
-30
lines changed

src/lang/block/expr.fnk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ next_is_end_of_block = fn ctx:
4141
next_is_unindented ?: true
4242
# TODO: should be responsibility of enclosing block
4343
next_is ?, ':': true
44+
next_is ?, '/>': true
4445
else: false
4546

4647

@@ -175,7 +176,8 @@ single_expression = fn ctx, lbp=0:
175176
next_is_new_expr ?:
176177
[expr, next_ctx]
177178

178-
next_is_call_arg expr, lbp, ?:
179+
180+
not ctx.jsx and next_is_call_arg expr, lbp, ?:
179181
indented_block = fn ctx, block_expr=single_expression:
180182
expressions ctx, end_of_block_indent, block_expr
181183

src/lang/block/init.fnk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
terminated_block = fn ctx, end_symbol, block_expr=single_expression:
12-
expressions ctx, end_symbol, block_expr
12+
expressions {...ctx, jsx: false}, end_symbol, block_expr
1313

1414

1515

src/lang/call/init.fnk

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
{add_operator} = import '@fink/prattler/symbols.fnk'
2+
13
{add_named_block} = import '../block/init.fnk'
4+
{single_expression} = import '../block/expr.fnk'
5+
{infix} = import '../symbols.fnk'
6+
7+
8+
9+
infix_pipe = fn op:
10+
dict:
11+
...infix op, 'call'
12+
13+
led: fn lbp: fn ctx, left:
14+
{loc: {start}} = left
15+
[callee, next_ctx] = single_expression ctx, lbp
16+
{loc: {end}} = callee
17+
[{type: 'call', callee, args: [left], op, loc: {start,end}}, next_ctx]
218

319

420

521
add_pipe = fn ctx:
622
pipe ctx:
723
add_named_block 'pipe'
24+
add_operator infix_pipe '|'
825

926

1027

src/lang/call/init.test.fnk

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ describe 'pipe foo: ...', fn:
1414
'
1515
to_match_snapshot
1616

17+
18+
19+
describe 'small pipe |', fn:
20+
it 'pipes', fn:
21+
expect
22+
parse '
23+
foo = 134 | is_int
24+
'
25+
to_match_snapshot
26+
27+
28+
it 'handles precedence', fn:
29+
expect
30+
parse '
31+
foo = "foo" | matches rx"[a-z]", ?
32+
bar = [spam | ham, shrub | ni]
33+
'
34+
to_match_snapshot

src/lang/call/init.test.fnk.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,35 @@ module (1:0-4:0)
1010
ident (2:6-2:11) shrub
1111
ident (3:2-3:4) ni
1212
`;
13+
14+
exports[`small pipe | handles precedence 1`] = `
15+
module (1:0-3:0)
16+
assign = (1:0-1:34)
17+
ident (1:0-1:3) foo
18+
call | (1:6-1:34)
19+
call (1:14-1:34)
20+
ident (1:14-1:21) matches
21+
string " (1:22-1:31)
22+
rx'[a-z]'
23+
partial (1:33-1:34) ?
24+
string " (1:6-1:11)
25+
'foo'
26+
assign = (2:0-2:30)
27+
ident (2:0-2:3) bar
28+
list (2:6-2:30)
29+
call | (2:7-2:17)
30+
ident (2:14-2:17) ham
31+
ident (2:7-2:11) spam
32+
call | (2:19-2:29)
33+
ident (2:27-2:29) ni
34+
ident (2:19-2:24) shrub
35+
`;
36+
37+
exports[`small pipe | pipes 1`] = `
38+
module (1:0-2:0)
39+
assign = (1:0-1:18)
40+
ident (1:0-1:3) foo
41+
call | (1:6-1:18)
42+
ident (1:12-1:18) is_int
43+
number (1:6-1:9) 134
44+
`;

src/lang/comparison/init.fnk

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ lt = fn op:
1414
...comp op
1515

1616
lbp: fn lbp: fn ctx:
17-
1817
match ctx:
1918
# TODO: this is a prattler internal interface
2019
{tokenizer: {partial_token: {value: starts_with ?, ' '}}}:
@@ -24,6 +23,16 @@ lt = fn op:
2423
0
2524

2625

26+
gt = fn op:
27+
dict:
28+
...comp op
29+
30+
lbp: fn lbp: fn ctx:
31+
match ctx:
32+
{jsx: true}: 0
33+
else: lbp
34+
35+
2736

2837
# TODO just enforce space before and after e.g. < to allow jsx overloading?
2938
add_comparison_operators = fn ctx:
@@ -32,5 +41,5 @@ add_comparison_operators = fn ctx:
3241
add_operator comp '!='
3342
add_operator comp '>='
3443
add_operator comp '<='
35-
add_operator comp '>'
44+
add_operator gt '>'
3645
add_operator lt '<'

src/lang/jsx/init.test.fnk

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{describe, it, expect, to_match_snapshot} = import '@fink/jest/test.fnk'
22

3-
{parse_expr, parse} = import '../../parser.fnk'
3+
{parse_expr} = import '../../parser.fnk'
4+
45

56

67
describe 'JSX', fn:
@@ -23,9 +24,9 @@ describe 'JSX', fn:
2324
to_match_snapshot
2425

2526

26-
it 'parses self closing elem with str attr: <Foobar spam=\'ni\' />', fn:
27+
it "parses self closing elem with str attr: <Foobar spam='ni' />", fn:
2728
expect
28-
parse_expr '<Foobar spam="ni" />'
29+
parse_expr "<Foobar spam='ni' />"
2930
to_match_snapshot
3031

3132

@@ -70,16 +71,46 @@ describe 'JSX', fn:
7071
'
7172
to_match_snapshot
7273

74+
expect
75+
parse_expr '<></>'
76+
to_match_snapshot
77+
7378
expect
7479
parse_expr '
75-
<></>
80+
<div>
81+
<></>
82+
</div>
7683
'
7784
to_match_snapshot
7885

7986

8087

81-
describe 'JSX parse failures', fn:
82-
it 'errors with invalid attr value', fn:
88+
describe 'JSX extensions', fn:
89+
it 'parses spread', fn:
90+
expect
91+
parse_expr '<Foobar spam ...ham shrub />'
92+
to_match_snapshot
93+
94+
95+
it "parses template str attr", fn:
96+
expect
97+
parse_expr "<Foobar spam='ni: \${1 + 2}' />"
98+
to_match_snapshot
99+
100+
101+
it "parses fink expr as attr values", fn:
102+
expect
103+
parse_expr "<Foobar spam=ham shrub=1234 ni=-123> foo </Foobar>"
104+
to_match_snapshot
105+
106+
107+
it "parses fink expr with gt comparison", fn:
108+
expect
109+
parse_expr "<Foobar spam=(foo > 123) shrub=1234> ni </Foobar>"
110+
to_match_snapshot
111+
112+
113+
it "parses expr group with call for attr value", fn:
83114
expect
84-
parse '<Foobar spam=123 />', 'test.fnk'
115+
parse_expr "<Foobar spam=(ham ni) shrub=1234 />"
85116
to_match_snapshot

src/lang/jsx/init.test.fnk.snap

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`JSX parse failures errors with invalid attr value 1`] = `
4-
module (1:0-1:19)
5-
jsx:elem (1:0-1:19) Foobar
6-
jsx:attr (1:8-1:13) spam
7-
::error::
8-
jsx:attr (1:13-1:16) 123
9-
10-
error:
11-
test.fnk:1:13
12-
1| <Foobar spam=123 />
13-
^
3+
exports[`JSX extensions parses expr group with call for attr value 1`] = `
4+
jsx:elem (1:0-1:35) Foobar
5+
jsx:attr (1:8-1:21) spam
6+
group (1:13-1:21)
7+
call (1:14-1:20)
8+
ident (1:14-1:17) ham
9+
ident (1:18-1:20) ni
10+
jsx:attr (1:22-1:32) shrub
11+
number (1:28-1:32) 1234
12+
`;
1413

15-
Expected one of \`{,",'\` but found \`123\`.
14+
exports[`JSX extensions parses fink expr as attr values 1`] = `
15+
jsx:elem (1:0-1:50) Foobar
16+
jsx:attr (1:8-1:16) spam
17+
ident (1:13-1:16) ham
18+
jsx:attr (1:17-1:27) shrub
19+
number (1:23-1:27) 1234
20+
jsx:attr (1:28-1:35) ni
21+
arithm:prefix - (1:31-1:35)
22+
number (1:32-1:35) 123
23+
:
24+
jsx:text (1:36-1:41)
25+
" foo "
26+
`;
1627

28+
exports[`JSX extensions parses fink expr with gt comparison 1`] = `
29+
jsx:elem (1:0-1:49) Foobar
30+
jsx:attr (1:8-1:24) spam
31+
group (1:13-1:24)
32+
comp > (1:14-1:23)
33+
ident (1:14-1:17) foo
34+
number (1:20-1:23) 123
35+
jsx:attr (1:25-1:35) shrub
36+
number (1:31-1:35) 1234
37+
:
38+
jsx:text (1:36-1:40)
39+
" ni "
40+
`;
1741

42+
exports[`JSX extensions parses spread 1`] = `
43+
jsx:elem (1:0-1:28) Foobar
44+
jsx:attr (1:8-1:12) spam
45+
spread ... (1:13-1:19)
46+
ident (1:16-1:19) ham
47+
jsx:attr (1:20-1:25) shrub
48+
`;
1849

50+
exports[`JSX extensions parses template str attr 1`] = `
51+
jsx:elem (1:0-1:30) Foobar
52+
jsx:attr (1:8-1:27) spam
53+
string ' (1:13-1:27)
54+
'ni: '
55+
arithm + (1:20-1:25)
56+
number (1:20-1:21) 1
57+
number (1:24-1:25) 2
58+
''
1959
`;
2060

2161
exports[`JSX parses elem with children: <Foobar><Spam /></Foobar> 1`] = `
@@ -61,6 +101,16 @@ jsx:frag (1:0-3:3)
61101

62102
exports[`JSX parses fragment 2`] = `jsx:frag (1:0-1:5) `;
63103

104+
exports[`JSX parses fragment 3`] = `
105+
jsx:elem (1:0-3:6) div
106+
:
107+
jsx:text (1:5-2:2)
108+
"\\n "
109+
jsx:frag (2:2-2:7)
110+
jsx:text (2:7-3:0)
111+
"\\n"
112+
`;
113+
64114
exports[`JSX parses self closing elem with expr attr: <Foobar spam={ni} /> 1`] = `
65115
jsx:elem (1:0-1:20) Foobar
66116
jsx:attr (1:8-1:17) spam
@@ -77,14 +127,14 @@ jsx:elem (1:0-1:15) Foobar
77127
exports[`JSX parses self closing elem with str attr: <Foobar spam="ni" /> 1`] = `
78128
jsx:elem (1:0-1:20) Foobar
79129
jsx:attr (1:8-1:17) spam
80-
jsx:string (1:14-1:16)
130+
string " (1:13-1:17)
81131
'ni'
82132
`;
83133

84134
exports[`JSX parses self closing elem with str attr: <Foobar spam='ni' /> 1`] = `
85135
jsx:elem (1:0-1:20) Foobar
86136
jsx:attr (1:8-1:17) spam
87-
jsx:string (1:14-1:16)
137+
string ' (1:13-1:17)
88138
'ni'
89139
`;
90140

0 commit comments

Comments
 (0)