@kiniro/parser
Advanced tools
Comparing version 1.1.1 to 1.1.2
{ | ||
"name": "@kiniro/parser", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Parser for @kiniro/lang", | ||
@@ -5,0 +5,0 @@ "main": "src/parser.js", |
@@ -139,3 +139,3 @@ // Generated automatically by nearley, version 2.15.1 | ||
{"name": "expr", "symbols": ["expr$macrocall$37"], "postprocess": extract([0, 1])}, | ||
{"name": "expr", "symbols": ["expr", (lexer.has("kw_dot") ? {type: "kw_dot"} : kw_dot), "symbol"], "postprocess": x => ({ type: 'prop', expr: x[0], prop: x[2] })}, | ||
{"name": "expr", "symbols": ["expr", (lexer.has("kw_dot") ? {type: "kw_dot"} : kw_dot), "prop"], "postprocess": x => ({ type: 'prop', expr: x[0], prop: x[2] })}, | ||
{"name": "expr", "symbols": ["symbol"], "postprocess": id}, | ||
@@ -150,2 +150,18 @@ {"name": "expr$ebnf$1", "symbols": []}, | ||
{"name": "expr", "symbols": [(lexer.has("string") ? {type: "string"} : string)], "postprocess": id}, | ||
{"name": "prop$subexpression$1", "symbols": ["symbol"]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_begin") ? {type: "kw_begin"} : kw_begin)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_apply") ? {type: "kw_apply"} : kw_apply)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_def") ? {type: "kw_def"} : kw_def)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_let") ? {type: "kw_let"} : kw_let)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_if") ? {type: "kw_if"} : kw_if)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_cond") ? {type: "kw_cond"} : kw_cond)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_throw") ? {type: "kw_throw"} : kw_throw)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_try") ? {type: "kw_try"} : kw_try)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_set") ? {type: "kw_set"} : kw_set)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_par") ? {type: "kw_par"} : kw_par)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_async") ? {type: "kw_async"} : kw_async)]}, | ||
{"name": "prop$subexpression$1", "symbols": [(lexer.has("kw_await") ? {type: "kw_await"} : kw_await)]}, | ||
{"name": "prop", "symbols": ["prop$subexpression$1"], "postprocess": x => { x = id(id(x)); | ||
x.type = 'symbol'; | ||
return x; } }, | ||
{"name": "symbol", "symbols": [(lexer.has("symbol") ? {type: "symbol"} : symbol)], "postprocess": id}, | ||
@@ -152,0 +168,0 @@ {"name": "object_key$ebnf$1", "symbols": [/[a-zA-Z0-9_-]/]}, |
@@ -16,5 +16,40 @@ const assert = require('assert'); | ||
describe('parser', () => { | ||
const simplify = (ast, dropPositions = false) => { | ||
const simplify = ast => { | ||
if (ast instanceof Array) { | ||
return ast.map(simplify); | ||
} else { | ||
if (ast.value && ast.type) { | ||
if (dropPositions) { | ||
with (ast) { | ||
return { value, type }; | ||
} | ||
} else { | ||
if (ast.line && ast.col) { | ||
with (ast) { | ||
return { value, type, line, col }; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (typeof ast == 'string') { | ||
return ast; | ||
} | ||
Object.keys(ast).forEach(key => { | ||
ast[key] = simplify(ast[key]); | ||
}); | ||
return ast; | ||
}; | ||
return simplify(ast); | ||
}; | ||
const symbol = value => ({ type: 'symbol', value }); | ||
const prop = (expr, prop) => ({ type: 'prop', expr, prop }); | ||
const kw = name => ({ type: 'symbol', value: name }); | ||
describe('parser', () => { | ||
[ | ||
@@ -68,2 +103,76 @@ ['(a b c)', ['a', 'b', 'c']], | ||
}); | ||
describe('prop', () => { | ||
const a = symbol('a'); | ||
const b = symbol('b'); | ||
const c = symbol('c'); | ||
const d = symbol('d'); | ||
it('parses correctly', () => { | ||
assert.deepStrictEqual([ | ||
'a', | ||
'a.b', | ||
'a.b.c', | ||
'a.b.c.d', | ||
].map(input => simplify(parse(input)[0], true)), [ | ||
a, | ||
prop(a, b), | ||
prop(prop(a, b), c), | ||
prop(prop(prop(a, b), c), d), | ||
]); | ||
}); | ||
describe('allows keywords as properties', () => { | ||
const mkAssertion = (input, expected) => | ||
assert.deepStrictEqual(simplify(parse(input)[0], true), expected); | ||
it('begin', () => { | ||
mkAssertion('a.begin', prop(a, kw('begin'))); | ||
}); | ||
it('apply', () => { | ||
mkAssertion('a.apply', prop(a, kw('apply'))); | ||
}); | ||
it('def', () => { | ||
mkAssertion('a.def', prop(a, kw('def'))); | ||
}); | ||
it('let', () => { | ||
mkAssertion('a.let', prop(a, kw('let'))); | ||
}); | ||
it('if', () => { | ||
mkAssertion('a.if', prop(a, kw('if'))); | ||
}); | ||
it('cond', () => { | ||
mkAssertion('a.cond', prop(a, kw('cond'))); | ||
}); | ||
it('throw', () => { | ||
mkAssertion('a.throw', prop(a, kw('throw'))); | ||
}); | ||
it('try', () => { | ||
mkAssertion('a.try', prop(a, kw('try'))); | ||
}); | ||
it('set', () => { | ||
mkAssertion('a.set', prop(a, kw('set'))); | ||
}); | ||
it('par', () => { | ||
mkAssertion('a.par', prop(a, kw('par'))); | ||
}); | ||
it('async', () => { | ||
mkAssertion('a.async', prop(a, kw('async'))); | ||
}); | ||
it('await', () => { | ||
mkAssertion('a.await', prop(a, kw('await'))); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38613
546