Skip to content

Commit

Permalink
Merge pull request #33 from fink-lang/partial
Browse files Browse the repository at this point in the history
partials
  • Loading branch information
kollhof authored Apr 6, 2020
2 parents ee649e6 + 8ff441c commit bc38d47
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 342 deletions.
637 changes: 309 additions & 328 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
"devDependencies": {
"@fink/cli": "^2.3.0",
"@fink/jest": "^1.1.0",
"@fink/larix": "^4.6.1",
"@fink/larix": "^4.7.0",
"@fink/loxia": "^4.6.3",
"commitizen": "^4.0.3",
"cz-conventional-changelog": "^3.1.0",
"jest-cli": "^25.2.4",
"jest-cli": "^25.2.7",
"npx-run": "^2.1.2",
"semantic-release": "^17.0.4"
},
Expand Down
2 changes: 2 additions & 0 deletions src/lang/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{add_whitespace_tokens} = import './whitespace'
{add_colon} = import './colon'
{add_js_ops} = import './js-compat'
{add_partial} = import './partial'


init_language = fn ctx:
Expand All @@ -38,6 +39,7 @@ init_language = fn ctx:
add_colon

add_identifier
add_partial

add_logical_operators
add_comparison_operators
Expand Down
10 changes: 8 additions & 2 deletions src/lang/literals/regex.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ regex = fn op:

nud: fn: fn ctx:
{start} = curr_loc(ctx)
ind = get_next_line_indentation(ctx)

[text, flags_ctx] = get_text(ctx, '/')
pattern = unindent_text(text.text, ind)
pattern = match true:
text.text.startsWith('\n'):
[, spaces] = text.text.match(rx/^\n+(\s+)/)
ind = spaces.length
unindent_text(text.text.slice(1 + ind), ind)
else:
text.text

[flags, end_ctx] = get_flags(flags_ctx)

{end} = curr_loc(end_ctx)
Expand Down
8 changes: 8 additions & 0 deletions src/lang/literals/regex.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ describe:: 'regex rx/.../', fn:
parse_expr(`rx//`) eq snapshot


it:: 'parses multiline', fn:
parse_expr(`
rx/
foo.+bar
/
`) eq snapshot


it:: 'parses flags: rx//gimsuy', fn:
parse_expr(`rx//gimsuy`) eq snapshot

Expand Down
6 changes: 6 additions & 0 deletions src/lang/literals/regex.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ regex (1:0-1:10)
//gimsuy
`;

exports[`regex rx/.../ parses multiline 1`] = `
regex (1:0-3:1)
/foo.+bar
/
`;

exports[`regex rx/.../ parses regex: rx/.+foo/ 1`] = `
regex (1:0-1:15)
/.+foo/gimsuy
Expand Down
20 changes: 10 additions & 10 deletions src/lang/literals/string.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{symbol} = import '../symbols'

{get_next_line_indentation, indentation} = import '../indentation'
{indentation} = import '../indentation'
{unindent_text} = import '../../string-utils'


Expand Down Expand Up @@ -54,28 +54,28 @@ get_parts = fn ctx, op:

get_unindented_text = fn ctx, op:
{start} = curr_loc(ctx)
ind = get_next_line_indentation(ctx)

[[first, ...rest], next_ctx] = get_parts(ctx, op)

first_part = match true:
ind = match true:
first.value.startsWith('\n'):
{...first, value: unindent_text(first.value.slice(1 + ind), ind)}
else:
first
[, spaces] = first.value.match(rx/^\n+(\s+)/)
spaces.length
else: 0

first_part = match ind:
0: first
else: {...first, value: first.value.slice(1 + ind)}

[...rest_parts] = pipe rest:
[...parts] = pipe [first_part, ...rest]:
map part:
match part:
{type: 'string:text'}:
{...part, value: unindent_text(part.value, ind)}

else:
part

parts = [first_part, ...rest_parts]
{end} = curr_loc(next_ctx)

[{type: 'string', op, parts, loc: {start, end}}, next_ctx]


Expand Down
19 changes: 19 additions & 0 deletions src/lang/partial/index.fnk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{curr_value, curr_loc} = import '@fink/prattler'
{add_non_separating} = import '@fink/prattler/symbols'

{symbol} = import '../symbols'


partial = fn op:
{
...symbol(op),

nud: fn: fn ctx:
loc = curr_loc(ctx)
[{type: 'partial', value: '?', loc}, ctx]
}


add_partial = fn ctx:
pipe ctx:
add_non_separating(partial('?'))
25 changes: 25 additions & 0 deletions src/lang/partial/index.test.fnk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{decribe, it, eq, to_throw, snapshot} = import '../../testing/jest'

{parse_expr} = import '../../'


describe:: 'partial', fn:

it:: 'parses call', fn:
parse_expr(`foo(?)`) eq snapshot
parse_expr(`foo.bar(?)`) eq snapshot


it:: 'parses member', fn:
parse_expr(`?.bar()`) eq snapshot


it:: 'parses binary', fn:
parse_expr(`? > 1`) eq snapshot
parse_expr(`1 < ?`) eq snapshot
parse_expr(`1 + ? * 3`) eq snapshot


it:: 'parses str', fn:
parse_expr(`'foo \${?} bar'`) eq snapshot

46 changes: 46 additions & 0 deletions src/lang/partial/index.test.fnk.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`partial parses binary 1`] = `
comp > (1:0-1:5)
partial (1:0-1:1) ?
number (1:4-1:5) 1
`;

exports[`partial parses binary 2`] = `
comp < (1:0-1:5)
number (1:0-1:1) 1
partial (1:4-1:5) ?
`;

exports[`partial parses binary 3`] = `
arithm + (1:0-1:9)
number (1:0-1:1) 1
arithm * (1:4-1:9)
partial (1:4-1:5) ?
number (1:8-1:9) 3
`;

exports[`partial parses call 1`] = `
call (1:0-1:6)
ident (1:0-1:3) foo
partial (1:4-1:5) ?
`;

exports[`partial parses call 2`] = `
call (1:0-1:10)
member . (1:0-1:7)
ident (1:0-1:3) foo
ident (1:4-1:7) bar
partial (1:8-1:9) ?
`;

exports[`partial parses member 1`] = `partial (1:0-1:1) ?`;

exports[`partial parses str 1`] = `
string ' (1:0-1:14)
\`foo \`
partial (1:7-1:8) ?
\` bar\`
`;

0 comments on commit bc38d47

Please sign in to comment.