diff --git a/src/PhonoChangeApplier.js b/src/PhonoChangeApplier.js index 0b3fe40..1c25671 100644 --- a/src/PhonoChangeApplier.js +++ b/src/PhonoChangeApplier.js @@ -13,13 +13,13 @@ import Latl from './components/Latl'; import LatlOutput from './components/LatlOutput'; import { stateReducer } from './reducers/reducer'; -import { initState } from './reducers/reducer.init'; +import { clearState } from './reducers/reducer.init'; const PhonoChangeApplier = () => { const [ state, dispatch ] = useReducer( stateReducer, {}, - initState + clearState ) const { lexicon, phones, phonemes, epochs, options, features, results, errors, latl, parseResults } = state; diff --git a/src/components/ProtoLang.js b/src/components/ProtoLang.js index c0b7330..e5e2698 100644 --- a/src/components/ProtoLang.js +++ b/src/components/ProtoLang.js @@ -10,6 +10,17 @@ const ProtoLang = ({ lexicon, dispatch }) => { 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 (

Proto Language Lexicon

@@ -21,22 +32,8 @@ const ProtoLang = ({ lexicon, dispatch }) => { rows="10" data-testid="ProtoLang-Lexicon__textarea" value={renderLexicon()} - onChange={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 } - }) - }) - } - }> + onChange={e => handleChange(e)} + >
diff --git a/src/reducers/reducer.init.js b/src/reducers/reducer.init.js index aacb704..5107628 100644 --- a/src/reducers/reducer.init.js +++ b/src/reducers/reducer.init.js @@ -5,6 +5,20 @@ export type initAction = { 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 => { const state = { epochs: [ diff --git a/src/reducers/reducer.latl.js b/src/reducers/reducer.latl.js index 6295001..10cf5d4 100644 --- a/src/reducers/reducer.latl.js +++ b/src/reducers/reducer.latl.js @@ -314,7 +314,9 @@ const parseSlash = (tree, token, index, tokens) => { case 'feature--minus': return tree; case 'lexicon': - return [...tree, { }] + return [...tree, { }]; + case 'main': + return [...tree, { type: 'lexicon', value: []}] default: 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 connectNodes = (tree, node, index, nodes) => { - console.log(tree, node) switch (node.type) { case 'epoch': delete node.type; @@ -463,7 +464,6 @@ export const generateAST = latl => { const tokens = tokenize(latl.trim()); // build tree const tree = buildTree(tokens); - console.log(tree) return tree; } @@ -472,7 +472,6 @@ export const parseLatl = (state, action) => { const latl = state.latl; const AST = generateAST(latl); const features = AST.features; - console.log(AST) if (features) { if (state.features) { state = Object.keys(state.features).reduce((state, feature) => { @@ -517,7 +516,7 @@ const tokenTypes = [ ['slash', `\/`], ['dot', `\\.`], ['underscore', `\\_`], - [`referent`, `[A-Za-z]+[\\w\\-\\_]*`], + [`referent`, `[A-Za-z]+[\u0100-\u03FFA-Za-z0-9\\-\\_]*`], [`phone`, `[\u0100-\u03FFA-Za-z0]+`], ['equal', `=`], [`lineBreak`, `\\n`], diff --git a/src/reducers/reducer.latl.test.js b/src/reducers/reducer.latl.test.js index 9a90514..0f61309 100644 --- a/src/reducers/reducer.latl.test.js +++ b/src/reducers/reducer.latl.test.js @@ -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 = ` ; comment @@ -494,4 +505,16 @@ const lexiconState = { { lexeme: 'sm', epoch: 'PROTO'} ], 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' } \ No newline at end of file