support for epoch and features, lexicon support still buggy

This commit is contained in:
Sorrel Bri 2020-03-03 20:32:54 -08:00
parent 20bede405f
commit c653653ba1
5 changed files with 56 additions and 23 deletions

View file

@ -13,13 +13,13 @@ import Latl from './components/Latl';
import LatlOutput from './components/LatlOutput'; import LatlOutput from './components/LatlOutput';
import { stateReducer } from './reducers/reducer'; import { stateReducer } from './reducers/reducer';
import { initState } from './reducers/reducer.init'; import { clearState } from './reducers/reducer.init';
const PhonoChangeApplier = () => { const PhonoChangeApplier = () => {
const [ state, dispatch ] = useReducer( const [ state, dispatch ] = useReducer(
stateReducer, stateReducer,
{}, {},
initState clearState
) )
const { lexicon, phones, phonemes, epochs, options, features, results, errors, latl, parseResults } = state; const { lexicon, phones, phonemes, epochs, options, features, results, errors, latl, parseResults } = state;

View file

@ -10,6 +10,17 @@ const ProtoLang = ({ lexicon, dispatch }) => {
return lexicon.map(getProperty('lexeme')).join('\n'); return lexicon.map(getProperty('lexeme')).join('\n');
} }
const handleChange = e => {
const value = e.target.value.split(/\n/).map(line => {
const lexeme = line.split('#')[0].trim();
const epoch = line.split('#')[1] || '';
return { lexeme, epoch }
})
dispatch({
type: 'SET_LEXICON',
value
})
}
return ( return (
<div className="ProtoLang" data-testid="ProtoLang"> <div className="ProtoLang" data-testid="ProtoLang">
<h3>Proto Language Lexicon</h3> <h3>Proto Language Lexicon</h3>
@ -21,22 +32,8 @@ const ProtoLang = ({ lexicon, dispatch }) => {
rows="10" rows="10"
data-testid="ProtoLang-Lexicon__textarea" data-testid="ProtoLang-Lexicon__textarea"
value={renderLexicon()} value={renderLexicon()}
onChange={e=> { onChange={e => handleChange(e)}
console.log(e.target.value.split(/\n/).map(line => { >
const lexeme = line.split('#')[0].trim();
const epoch = line.split('#')[1] || '';
return { lexeme, epoch }
}))
dispatch({
type: 'SET_LEXICON',
value: e.target.value.split(/\n/).map(line => {
const lexeme = line.split('#')[0].trim();
const epoch = line.split('#')[1] || '';
return { lexeme, epoch }
})
})
}
}>
</textarea> </textarea>
</form> </form>
</div> </div>

View file

@ -5,6 +5,20 @@ export type initAction = {
type: "INIT" type: "INIT"
} }
export const clearState = () => {
return {
epochs: [],
phones: {},
options: { output: 'default', save: false },
results: [],
errors: {},
features: {},
lexicon: [],
latl: '',
parseResults: ''
}
}
export const initState = (changesArgument: number): stateType => { export const initState = (changesArgument: number): stateType => {
const state = { const state = {
epochs: [ epochs: [

View file

@ -314,7 +314,9 @@ const parseSlash = (tree, token, index, tokens) => {
case 'feature--minus': case 'feature--minus':
return tree; return tree;
case 'lexicon': case 'lexicon':
return [...tree, { }] return [...tree, { }];
case 'main':
return [...tree, { type: 'lexicon', value: []}]
default: default:
return [...tree, 'unexpected slash'] return [...tree, 'unexpected slash']
} }
@ -405,7 +407,6 @@ const generateNode = (tree, token, index, tokens) => {
const addToken = (tree, token, index, tokens) => generateNode(tree, token, index, tokens); const addToken = (tree, token, index, tokens) => generateNode(tree, token, index, tokens);
const connectNodes = (tree, node, index, nodes) => { const connectNodes = (tree, node, index, nodes) => {
console.log(tree, node)
switch (node.type) { switch (node.type) {
case 'epoch': case 'epoch':
delete node.type; delete node.type;
@ -463,7 +464,6 @@ export const generateAST = latl => {
const tokens = tokenize(latl.trim()); const tokens = tokenize(latl.trim());
// build tree // build tree
const tree = buildTree(tokens); const tree = buildTree(tokens);
console.log(tree)
return tree; return tree;
} }
@ -472,7 +472,6 @@ export const parseLatl = (state, action) => {
const latl = state.latl; const latl = state.latl;
const AST = generateAST(latl); const AST = generateAST(latl);
const features = AST.features; const features = AST.features;
console.log(AST)
if (features) { if (features) {
if (state.features) { if (state.features) {
state = Object.keys(state.features).reduce((state, feature) => { state = Object.keys(state.features).reduce((state, feature) => {
@ -517,7 +516,7 @@ const tokenTypes = [
['slash', `\/`], ['slash', `\/`],
['dot', `\\.`], ['dot', `\\.`],
['underscore', `\\_`], ['underscore', `\\_`],
[`referent`, `[A-Za-z]+[\\w\\-\\_]*`], [`referent`, `[A-Za-z]+[\u0100-\u03FFA-Za-z0-9\\-\\_]*`],
[`phone`, `[\u0100-\u03FFA-Za-z0]+`], [`phone`, `[\u0100-\u03FFA-Za-z0]+`],
['equal', `=`], ['equal', `=`],
[`lineBreak`, `\\n`], [`lineBreak`, `\\n`],

View file

@ -88,6 +88,17 @@ describe('LATL', () => {
}) })
it('returns state from well formed latl', () => {
const state = initState();
const setAction = {
type: 'SET_LATL',
value: totalLatl
}
const latlState = stateReducer(state, setAction);
const parseState = parseLatl(latlState, {});
expect(parseState).toStrictEqual(totalLatlState)
})
}) })
const epochDefinitionLatl = ` const epochDefinitionLatl = `
; comment ; comment
@ -495,3 +506,15 @@ const lexiconState = {
], ],
parseResults: 'latl parsed successfully' parseResults: 'latl parsed successfully'
} }
const totalLatl = `${epochDefinitionLatl}\n\n${featureDefinitionLatl}\n\n${lexiconDefinitionLatl}`
const totalLatlState = {
...initState(),
latl: totalLatl,
phonemes: {},
features: featureState.features,
epochs: treeEpoch.epochs,
lexicon: lexiconState.lexicon,
parseResults: 'latl parsed successfully'
}