-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2bcfb1
commit 9b03774
Showing
15 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist | ||
coverage | ||
node_modules |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as StateMachine } from './state_machine'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getStateByHandle, StateHandle } from './states'; | ||
|
||
export default class StateMachine { | ||
private currentState = getStateByHandle(StateHandle.Empty); | ||
|
||
public handleCharacter(character: string) { | ||
const [outputCharacter, nextStateHandle] = this.performTransition(character); | ||
|
||
if (nextStateHandle) { | ||
this.currentState = getStateByHandle(nextStateHandle); | ||
} | ||
|
||
return outputCharacter; | ||
} | ||
|
||
private performTransition(character: string) { | ||
const currentState = this.currentState; | ||
|
||
switch (character) { | ||
case ' ': | ||
return currentState.onWhitespace(character); | ||
default: | ||
return currentState.onCharacter(character); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import baseState, { State, StateHandle } from 'src/state_machine/states/state'; | ||
|
||
const emptyState: State = { | ||
...baseState, | ||
stateHandle: StateHandle.Empty, | ||
|
||
onCharacter(character: string) { | ||
return [character, StateHandle.InsideToken]; | ||
}, | ||
}; | ||
|
||
export default emptyState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StateHandle } from './state'; | ||
|
||
import emptyState from './empty'; | ||
import insideTokenState from './inside_token'; | ||
|
||
const ALL_STATES = [emptyState, insideTokenState]; | ||
|
||
export default function getStateByHandle(stateHandle: StateHandle) { | ||
for (const state of ALL_STATES) { | ||
if (state.stateHandle === stateHandle) { | ||
return state; | ||
} | ||
} | ||
|
||
// istanbul ignore next line - shouldn't really happen | ||
throw new Error(`State with handle = ${stateHandle} cannot be found`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { State, StateHandle } from './state'; | ||
export { default as getStateByHandle } from './factory'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import baseState, { State, StateHandle } from 'src/state_machine/states/state'; | ||
|
||
const insiteTokenState: State = { | ||
...baseState, | ||
stateHandle: StateHandle.InsideToken, | ||
|
||
onCharacter(character: string) { | ||
return [character, null]; | ||
}, | ||
onWhitespace() { | ||
return [null, StateHandle.Empty]; | ||
}, | ||
}; | ||
|
||
export default insiteTokenState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type HandlerFunction = (character: string) => [string | null, StateHandle | null]; | ||
|
||
export interface State { | ||
stateHandle: StateHandle; | ||
onCharacter: HandlerFunction; | ||
onWhitespace: HandlerFunction; | ||
onDoubleQuote: HandlerFunction; | ||
onSingleQuote: HandlerFunction; | ||
onEscapeCharacter: HandlerFunction; | ||
} | ||
|
||
export enum StateHandle { | ||
Empty, | ||
InsideToken, | ||
} | ||
|
||
const defaultHandler: HandlerFunction = (_character: string) => [null, null]; | ||
|
||
const baseState = { | ||
onCharacter: defaultHandler, | ||
onWhitespace: defaultHandler, | ||
onDoubleQuote: defaultHandler, | ||
onSingleQuote: defaultHandler, | ||
onEscapeCharacter: defaultHandler, | ||
}; | ||
|
||
export default baseState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters