Skip to content

Commit

Permalink
Merge pull request #12 from fink-lang/numbers-and-identifiers
Browse files Browse the repository at this point in the history
feat(numbers,identifiers): add support for floats, hex, oct, bin numbers
  • Loading branch information
kollhof authored Feb 28, 2020
2 parents 7b7ee75 + 33c5c0b commit 3b2f806
Show file tree
Hide file tree
Showing 39 changed files with 262 additions and 209 deletions.
2 changes: 0 additions & 2 deletions src/lang/arithmitic/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../../';


Expand Down
34 changes: 17 additions & 17 deletions src/lang/arithmitic/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@ exports[`arithmitic parses grouped: (a - b) * c 1`] = `
arithm * (1:0-1:11)
group (1:0-1:7)
arithm - (1:1-1:6)
other (1:1-1:2) a
other (1:5-1:6) b
other (1:10-1:11) c
ident (1:1-1:2) a
ident (1:5-1:6) b
ident (1:10-1:11) c
`;

exports[`arithmitic parses single line: a % b / c 1`] = `
arithm / (1:0-1:9)
arithm % (1:0-1:5)
other (1:0-1:1) a
other (1:4-1:5) b
other (1:8-1:9) c
ident (1:0-1:1) a
ident (1:4-1:5) b
ident (1:8-1:9) c
`;

exports[`arithmitic parses single line: a * -b 1`] = `
arithm * (1:0-1:6)
other (1:0-1:1) a
ident (1:0-1:1) a
arithm_prefix - (1:4-1:6)
other (1:5-1:6) b
ident (1:5-1:6) b
`;

exports[`arithmitic parses single line: a * b + c 1`] = `
arithm + (1:0-1:9)
arithm * (1:0-1:5)
other (1:0-1:1) a
other (1:4-1:5) b
other (1:8-1:9) c
ident (1:0-1:1) a
ident (1:4-1:5) b
ident (1:8-1:9) c
`;

exports[`arithmitic parses single line: a + b * c 1`] = `
arithm + (1:0-1:9)
other (1:0-1:1) a
ident (1:0-1:1) a
arithm * (1:4-1:9)
other (1:4-1:5) b
other (1:8-1:9) c
ident (1:4-1:5) b
ident (1:8-1:9) c
`;

exports[`arithmitic parses single line: a ^ b * c 1`] = `
arithm * (1:0-1:9)
arithm_right ^ (1:0-1:5)
other (1:0-1:1) a
other (1:4-1:5) b
other (1:8-1:9) c
ident (1:0-1:1) a
ident (1:4-1:5) b
ident (1:8-1:9) c
`;
3 changes: 0 additions & 3 deletions src/lang/assignment/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../../';
import {strip_block} from '../../string-utils';


describe('assignment', ()=> {
Expand Down
4 changes: 2 additions & 2 deletions src/lang/assignment/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`assignment parses single line: foo = bar 1`] = `
assign = (1:0-1:9)
other (1:0-1:3) foo
other (1:6-1:9) bar
ident (1:0-1:3) foo
ident (1:6-1:9) bar
`;
2 changes: 0 additions & 2 deletions src/lang/async/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../../';


Expand Down
2 changes: 1 addition & 1 deletion src/lang/async/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

exports[`assignment parses single line: foo = bar 1`] = `
await (1:0-1:12)
other (1:6-1:12) foobar
ident (1:6-1:12) foobar
`;
4 changes: 0 additions & 4 deletions src/lang/call/call.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../..';


import {strip_block} from '../../string-utils';


Expand Down
32 changes: 16 additions & 16 deletions src/lang/call/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@

exports[`call() parses dangling comma: foobar(1, 2,) 1`] = `
call (1:0-1:13)
other (1:0-1:6) foobar
other (1:7-1:8) 1
other (1:10-1:11) 2
ident (1:0-1:6) foobar
number (1:7-1:8) 1
number (1:10-1:11) 2
`;

exports[`call() parses empty: foobar() 1`] = `
call (1:0-1:8)
other (1:0-1:6) foobar
ident (1:0-1:6) foobar
`;

exports[`call() parses multiple args: foobar(1, 2) 1`] = `
call (1:0-1:12)
other (1:0-1:6) foobar
other (1:7-1:8) 1
other (1:10-1:11) 2
ident (1:0-1:6) foobar
number (1:7-1:8) 1
number (1:10-1:11) 2
`;

exports[`call() parses single arg: foobar(1) 1`] = `
call (1:0-1:9)
other (1:0-1:6) foobar
other (1:7-1:8) 1
ident (1:0-1:6) foobar
number (1:7-1:8) 1
`;

exports[`call:: ... parses args: foobar:: spam, ni 1`] = `
call (1:0-1:15)
other (1:0-1:6) foobar
other (1:9-1:13) spam
other (1:15-1:17) ni
ident (1:0-1:6) foobar
ident (1:9-1:13) spam
ident (1:15-1:17) ni
`;

exports[`pipe foo: ... pipes 1`] = `
block pipe (1:0-3:4)
other (1:5-1:8) foo
ident (1:5-1:8) foo
:
call (2:2-2:12)
other (2:2-2:5) bar
other (2:6-2:11) shrub
ident (2:2-2:5) bar
ident (2:6-2:11) shrub
other (3:2-3:4) ni
ident (3:2-3:4) ni
`;
8 changes: 4 additions & 4 deletions src/lang/comments/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

exports[`comments parses doc-comment 1`] = `
assign = (4:0-4:9)
other (4:0-4:4) spam
other (4:7-4:9) ni
ident (4:0-4:4) spam
ident (4:7-4:9) ni
`;

exports[`comments parses line comment 1`] = `
assign = (2:0-2:9)
other (2:0-2:4) spam
other (2:7-2:9) ni
ident (2:0-2:4) spam
ident (2:7-2:9) ni
`;
2 changes: 0 additions & 2 deletions src/lang/comparison/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../../';


Expand Down
24 changes: 12 additions & 12 deletions src/lang/comparison/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@

exports[`comparison parses single line: a != b 1`] = `
comp != (1:0-1:6)
other (1:0-1:1) a
other (1:5-1:6) b
ident (1:0-1:1) a
ident (1:5-1:6) b
`;

exports[`comparison parses single line: a < b 1`] = `
comp < (1:0-1:5)
other (1:0-1:1) a
other (1:4-1:5) b
ident (1:0-1:1) a
ident (1:4-1:5) b
`;
exports[`comparison parses single line: a <= b 1`] = `
comp <= (1:0-1:6)
other (1:0-1:1) a
other (1:5-1:6) b
ident (1:0-1:1) a
ident (1:5-1:6) b
`;
exports[`comparison parses single line: a == b 1`] = `
comp == (1:0-1:6)
other (1:0-1:1) a
other (1:5-1:6) b
ident (1:0-1:1) a
ident (1:5-1:6) b
`;
exports[`comparison parses single line: a > b 1`] = `
comp > (1:0-1:5)
other (1:0-1:1) a
other (1:4-1:5) b
ident (1:0-1:1) a
ident (1:4-1:5) b
`;
exports[`comparison parses single line: a >= b 1`] = `
comp >= (1:0-1:6)
other (1:0-1:1) a
other (1:5-1:6) b
ident (1:0-1:1) a
ident (1:5-1:6) b
`;
4 changes: 0 additions & 4 deletions src/lang/conditionals/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../../';


import {strip_block} from '../../string-utils';


Expand Down
10 changes: 5 additions & 5 deletions src/lang/conditionals/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

exports[`match ...: parses single line: match foo: bar 1`] = `
cond match (1:0-3:12)
other (1:6-1:10) item
ident (1:6-1:10) item
:
cond:test:result (2:2-2:10)
other (2:2-2:5) foo
ident (2:2-2:5) foo
block (2:5-2:10)
other (2:7-2:10) bar
ident (2:7-2:10) bar
cond:test:result (3:2-3:12)
other (3:2-3:6) else
ident (3:2-3:6) else
block (3:6-3:12)
other (3:8-3:12) spam
ident (3:8-3:12) spam
`;
14 changes: 7 additions & 7 deletions src/lang/func/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

exports[`func: fn ...: ... parses multi line: fn foo, bar: foo 1`] = `
block fn (1:0-3:5)
other (1:3-1:6) foo
other (1:8-1:11) bar
ident (1:3-1:6) foo
ident (1:8-1:11) bar
:
other (2:2-2:5) foo
other (3:2-3:5) bar
ident (2:2-2:5) foo
ident (3:2-3:5) bar
`;

exports[`func: fn ...: ... parses single line: fn foo, bar: foo 1`] = `
block fn (1:0-1:16)
other (1:3-1:6) foo
other (1:8-1:11) bar
ident (1:3-1:6) foo
ident (1:8-1:11) bar
:
other (1:13-1:16) foo
ident (1:13-1:16) foo
`;
4 changes: 0 additions & 4 deletions src/lang/group/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {other_token} from '@fink/prattler/symbols';

import {parse_expr} from '../..';


import {strip_block} from '../../string-utils';


Expand Down
4 changes: 2 additions & 2 deletions src/lang/group/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`group: (...) parses single line: (foo, bar) 1`] = `
group (1:0-1:10)
other (1:1-1:4) foo
other (1:6-1:9) bar
ident (1:1-1:4) foo
ident (1:6-1:9) bar
`;
49 changes: 46 additions & 3 deletions src/lang/identifier/other.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
curr_value, curr_loc, expression, next_matches, next_loc
curr_value, curr_loc, expression, next_matches, next_loc, next_is,
assert_advance, advance
} from '@fink/prattler';
import {token_error} from '@fink/prattler/errors';
import {other_token, next_lbp} from '@fink/prattler/symbols';

import {symbol} from '../symbols';
Expand All @@ -20,15 +22,56 @@ const infix_led = (lbp)=> (ctx, left)=> {
};


const looks_like_num = (ctx)=> {
const value = curr_value(ctx);
return value.match(/^[0-9]+.*/) !== null;
};


// eslint-disable-next-line max-statements
const number = (ctx)=> {
let value = curr_value(ctx);
const {start} = curr_loc(ctx);

if (next_is(ctx, '.')) {
ctx = advance(ctx);
value += curr_value(ctx);
ctx = advance(ctx);
value += curr_value(ctx);

if (value.endsWith('e')) {
ctx = advance(ctx);
value += curr_value(ctx);

if (curr_value(ctx) === '+' || curr_value(ctx) === '-') {
ctx = advance(ctx);
value += curr_value(ctx);
} else {
throw token_error(
`Expected exponent:`,
ctx.curr_token, ctx
);
}
}
}

const {end} = curr_loc(ctx);
return [{type: 'number', value, loc: {start, end}}, ctx];
};


export const other = ()=> ({
...symbol(other_token),

nud: ()=> (ctx)=> {
const value = curr_value(ctx);
const loc = curr_loc(ctx);

// TODO: use 'other' instead of other_token
return [{type: other_token, value, loc}, ctx];
if (looks_like_num(ctx)) {
return number(ctx);
}

return [{type: 'ident', value, loc}, ctx];
},

led: (lbp)=> infix_led(lbp-1)
Expand Down
Loading

0 comments on commit 3b2f806

Please sign in to comment.