construct AST properly for multi set definitions

This commit is contained in:
Sorrel Bri 2020-05-09 15:22:01 -07:00
parent 40aec30537
commit abfe14b410
3 changed files with 55 additions and 35 deletions

View file

@ -25,8 +25,10 @@ var grammar = {
{"name": "main$ebnf$1$subexpression$1", "symbols": ["_", "statement"]}, {"name": "main$ebnf$1$subexpression$1", "symbols": ["_", "statement"]},
{"name": "main$ebnf$1", "symbols": ["main$ebnf$1", "main$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, {"name": "main$ebnf$1", "symbols": ["main$ebnf$1", "main$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "main", "symbols": ["main$ebnf$1", "_"], "postprocess": pipe( {"name": "main", "symbols": ["main$ebnf$1", "_"], "postprocess": pipe(
getTerminal,
clearNull, clearNull,
// recursive call to fix repeat?
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
flag('main'), flag('main'),
getTerminal, getTerminal,
) }, ) },
@ -38,24 +40,34 @@ var grammar = {
{"name": "equal", "symbols": [(lexer.has("equal") ? {type: "equal"} : equal)], "postprocess": remove}, {"name": "equal", "symbols": [(lexer.has("equal") ? {type: "equal"} : equal)], "postprocess": remove},
{"name": "statement", "symbols": ["comment"]}, {"name": "statement", "symbols": ["comment"]},
{"name": "statement", "symbols": ["definition"], "postprocess": pipe( {"name": "statement", "symbols": ["definition"], "postprocess": pipe(
objFromArr d => d.flatMap(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
// recursive call to fit repeat?
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
// may split from other definition statements
d => d.map(t => t && t.length > 1 ? ({ type: 'set', ...objFromArr(t) }) : null)
) }, ) },
{"name": "comment", "symbols": [(lexer.has("comment") ? {type: "comment"} : comment)], "postprocess": pipe(getTerminal, remove)}, {"name": "comment", "symbols": [(lexer.has("comment") ? {type: "comment"} : comment)], "postprocess": pipe(getTerminal, remove)},
{"name": "definition", "symbols": [(lexer.has("kwSet") ? {type: "kwSet"} : kwSet), "__", "setDefinition"], "postprocess": pipe( {"name": "definition$ebnf$1", "symbols": []},
d => ({[d[0].value]: objFromArr(d[2]) }), {"name": "definition$ebnf$1$subexpression$1", "symbols": ["setDefinition", (lexer.has("comma") ? {type: "comma"} : comma), "__"]},
{"name": "definition$ebnf$1", "symbols": ["definition$ebnf$1", "definition$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "definition", "symbols": [(lexer.has("kwSet") ? {type: "kwSet"} : kwSet), "__", "definition$ebnf$1", "setDefinition"], "postprocess": pipe(
// not yet sure why this call is required twice
d => d.map(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
d => d.map(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
d => d.map(u => u && u.length ? u.map(v => v.length ? v.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet')[0] : v) : u),
clearNull,
) }, ) },
{"name": "setDefinition$ebnf$1", "symbols": []}, {"name": "setDefinition", "symbols": [(lexer.has("setIdentifier") ? {type: "setIdentifier"} : setIdentifier), "__", "equal", "__", "setExpression"], "postprocess":
{"name": "setDefinition$ebnf$1$subexpression$1", "symbols": [(lexer.has("setIdentifier") ? {type: "setIdentifier"} : setIdentifier), "__", "equal", "__", "setExpression", (lexer.has("comma") ? {type: "comma"} : comma), "__"]},
{"name": "setDefinition$ebnf$1", "symbols": ["setDefinition$ebnf$1", "setDefinition$ebnf$1$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "setDefinition", "symbols": ["setDefinition$ebnf$1", (lexer.has("setIdentifier") ? {type: "setIdentifier"} : setIdentifier), "__", "equal", "__", "setExpression"], "postprocess":
pipe( pipe(
d => d.filter(t => !!t && t.length !== 0), d => d.filter(t => !!t && t.length !== 0),
d => d.map(t => t.type === 'setIdentifier' ? { setIdentifier: t.toString() } : t), d => d.map(t => t.type === 'setIdentifier' ? { setIdentifier: t.toString() } : t),
d => d.map(t => t && t.length && t[0].hasOwnProperty('setExpression') ? t[0] : t) d => d.map(t => t && t.length && t[0].hasOwnProperty('setExpression') ? t[0] : t),
) )
}, },
{"name": "setExpression", "symbols": [(lexer.has("openSquareBracket") ? {type: "openSquareBracket"} : openSquareBracket), "_", "phoneList", "_", (lexer.has("closeSquareBracket") ? {type: "closeSquareBracket"} : closeSquareBracket)], "postprocess": {"name": "setExpression", "symbols": [(lexer.has("openSquareBracket") ? {type: "openSquareBracket"} : openSquareBracket), "_", "phoneList", "_", (lexer.has("closeSquareBracket") ? {type: "closeSquareBracket"} : closeSquareBracket)], "postprocess":
pipe( pipe(
// filters commas and whitespace
d => d.filter(t => t && t.length), d => d.filter(t => t && t.length),
d => d.map(t => t.map(u => u[0])), d => d.map(t => t.map(u => u[0])),
flag('setExpression') flag('setExpression')

View file

@ -20,8 +20,10 @@
main -> (_ statement):* _ main -> (_ statement):* _
{% pipe( {% pipe(
getTerminal,
clearNull, clearNull,
// recursive call to fix repeat?
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
flag('main'), flag('main'),
getTerminal, getTerminal,
) %} ) %}
@ -37,29 +39,38 @@ equal -> %equal
statement -> comment | definition statement -> comment | definition
{% pipe( {% pipe(
objFromArr d => d.flatMap(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
// recursive call to fit repeat?
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
d => d.map(t => t && t.length === 1 && t[0] ? t[0] : t),
// may split from other definition statements
d => d.map(t => t && t.length > 1 ? ({ type: 'set', ...objFromArr(t) }) : null)
) %} ) %}
comment -> %comment comment -> %comment
{% pipe(getTerminal, remove) %} {% pipe(getTerminal, remove) %}
# SETS # SETS
definition -> %kwSet __ setDefinition definition -> %kwSet __ (setDefinition %comma __):* setDefinition
{% pipe( {% pipe(
d => ({[d[0].value]: objFromArr(d[2]) }), // not yet sure why this call is required twice
d => d.map(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
d => d.map(u => u && u.length ? u.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet') : u),
d => d.map(u => u && u.length ? u.map(v => v.length ? v.filter(t => t && t.type !== 'comma' && t.type !== 'kwSet')[0] : v) : u),
clearNull,
) %} ) %}
# {% flag('definition') %} setDefinition -> %setIdentifier __ equal __ setExpression
setDefinition -> (%setIdentifier __ equal __ setExpression %comma __):* %setIdentifier __ equal __ setExpression
{% {%
pipe( pipe(
d => d.filter(t => !!t && t.length !== 0), d => d.filter(t => !!t && t.length !== 0),
d => d.map(t => t.type === 'setIdentifier' ? { setIdentifier: t.toString() } : t), d => d.map(t => t.type === 'setIdentifier' ? { setIdentifier: t.toString() } : t),
d => d.map(t => t && t.length && t[0].hasOwnProperty('setExpression') ? t[0] : t) d => d.map(t => t && t.length && t[0].hasOwnProperty('setExpression') ? t[0] : t),
) )
%} %}
setExpression -> %openSquareBracket _ phoneList _ %closeSquareBracket setExpression -> %openSquareBracket _ phoneList _ %closeSquareBracket
{% {%
pipe( pipe(
// filters commas and whitespace
d => d.filter(t => t && t.length), d => d.filter(t => t && t.length),
d => d.map(t => t.map(u => u[0])), d => d.map(t => t.map(u => u[0])),
flag('setExpression') flag('setExpression')

View file

@ -33,10 +33,9 @@ export const assertionData = {
AST: { AST: {
main: [ main: [
{ {
set: { type: 'set',
setIdentifier: 'NASAL_PULMONIC_CONSONANTS', setIdentifier: 'NASAL_PULMONIC_CONSONANTS',
setExpression: [ 'm̥', 'm', 'ɱ' ] setExpression: [ 'm̥', 'm', 'ɱ' ]
}
} }
] ]
} }
@ -162,16 +161,14 @@ set NASAL_PULMONIC_CONSONANTS = [ m̥, m, ɱ, n̼, n̥, n, ɳ̊,
AST: { AST: {
main: [ main: [
{ {
set: [ type: 'set',
{ setIdentifier: 'NASAL_PULMONIC_CONSONANTS',
setIdentifier: 'NASAL_PULMONIC_CONSONANTS', setExpression: [ 'm̥', 'm', 'ɱ', 'n̼', 'n̥', 'n', 'ɳ̊', 'ɳ', 'ɲ̊', 'ɲ', 'ŋ', '̊ŋ', 'ɴ' ]
items: [ 'm̥', 'm', 'ɱ', 'n̼', 'n̥', 'n', 'ɳ̊', 'ɳ', 'ɲ̊', 'ɲ', `ŋ`, ' ̊ŋ', 'ɴ' ] },
}, {
{ type: 'set',
setIdentifier: 'STOP_PULMONIC_CONSONANTS', setIdentifier: 'STOP_PULMONIC_CONSONANTS',
items: [ 'p', 'b', 'p̪', 'b̪', 't̼', 'd̼', 't', 'd', 'ʈ', 'ɖ', 'c', 'ɟ', 'k', 'ɡ', 'q', 'ɢ', 'ʡ', 'ʔ' ] setExpression: [ 'p', 'b', 'p̪', 'b̪', 't̼', 'd̼', 't', 'd', 'ʈ', 'ɖ', 'c', 'ɟ', 'k', 'ɡ', 'q', 'ɢ', 'ʡ', 'ʔ' ]
}
]
} }
] ]
} }