|
| 1 | +import Head from 'next/head'; |
| 2 | +import { parse } from '@iter-tools/regex'; |
| 3 | +// @ts-ignore-error |
| 4 | +import { Engine } from '@iter-tools/regex/internal/engine'; |
| 5 | +import { Box, Button, CheckBox, Header, Main, Markdown, TextInput } from 'grommet'; |
| 6 | + |
| 7 | +import { Grid, BlockQuote } from 'grommet-icons'; |
| 8 | +import { PureComponent } from 'react'; |
| 9 | +import { forkerate } from 'iter-tools-es'; |
| 10 | +import { Inspector } from '../components/inspector'; |
| 11 | + |
| 12 | +type InspectorState = { |
| 13 | + index: number; |
| 14 | + width: number; |
| 15 | + engine: any; |
| 16 | + forkr: any; |
| 17 | + done: boolean; |
| 18 | + matches: Array<Array<string | undefined>>; |
| 19 | +}; |
| 20 | + |
| 21 | +const welcomeText = ` |
| 22 | +
|
| 23 | +Welcome to the [@iter-tools/regex](https://github.com/iter-tools/regex) playground! |
| 24 | + |
| 25 | +This playground allows you to step through the incremental process by which \`@iter-tools/regex\` |
| 26 | +does matching. |
| 27 | +
|
| 28 | +To get started, enter a pattern and some text to match against, press "execute" to setup the engine, |
| 29 | +then press the step button repeatedly to see how matching progresses.`; |
| 30 | + |
| 31 | +const Welcome = () => { |
| 32 | + return ( |
| 33 | + <Main pad="medium"> |
| 34 | + <Markdown components={{ p: { props: { fill: true } } }}>{welcomeText}</Markdown> |
| 35 | + </Main> |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +export default class App extends PureComponent<never, InspectorState> { |
| 40 | + state: InspectorState = { |
| 41 | + index: null!, |
| 42 | + width: null!, |
| 43 | + engine: null, |
| 44 | + forkr: null, |
| 45 | + done: null!, |
| 46 | + matches: null!, |
| 47 | + }; |
| 48 | + |
| 49 | + dispatch = (action) => { |
| 50 | + switch (action.type) { |
| 51 | + case 'step': |
| 52 | + this.step(); |
| 53 | + break; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + execute = () => { |
| 58 | + const { pattern, text, ...flags } = Object.fromEntries(new FormData(document.forms[0])); |
| 59 | + this.setState({ |
| 60 | + index: 0, |
| 61 | + width: 0, |
| 62 | + forkr: forkerate(text as string), |
| 63 | + engine: new Engine(parse(pattern as string, Object.keys(flags).join(''))), |
| 64 | + done: false, |
| 65 | + matches: [], |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + step = () => { |
| 70 | + const { index, width, forkr, engine, matches } = this.state; |
| 71 | + |
| 72 | + if (width === 0) { |
| 73 | + const { value, done } = engine.step0(index === 0, forkr.done, forkr.index, forkr.value); |
| 74 | + |
| 75 | + if (value !== null) { |
| 76 | + } |
| 77 | + |
| 78 | + this.setState({ width: 1, matches: value ? [...matches, ...value] : matches, done }); |
| 79 | + } else { |
| 80 | + engine.step1(forkr.value); |
| 81 | + |
| 82 | + forkr.advance(); |
| 83 | + this.setState({ width: 0, index: index + 1 }); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + render() { |
| 88 | + const { engine } = this.state; |
| 89 | + const content = engine ? <Inspector {...this.state} dispatch={this.dispatch} /> : <Welcome />; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Box |
| 93 | + flex |
| 94 | + margin={{ horizontal: 'auto', vertical: '0' }} |
| 95 | + width={{ max: '100%' }} |
| 96 | + height={{ min: '100vh' }} |
| 97 | + background="light-2" |
| 98 | + > |
| 99 | + <Head> |
| 100 | + <title>Regex playground</title> |
| 101 | + <link rel="icon" href="/favicon.ico" /> |
| 102 | + </Head> |
| 103 | + <form id="inputs"> |
| 104 | + <Header background="white" pad="medium"> |
| 105 | + <TextInput name="pattern" icon={<Grid />} placeholder="pattern" /> |
| 106 | + <TextInput name="text" icon={<BlockQuote />} placeholder="text" /> |
| 107 | + <CheckBox name="g" label="g" /> |
| 108 | + <CheckBox name="i" label="i" /> |
| 109 | + <CheckBox name="s" label="s" /> |
| 110 | + <CheckBox name="m" label="m" /> |
| 111 | + <CheckBox name="y" label="y" /> |
| 112 | + <Button primary label="execute" onClick={this.execute} /> |
| 113 | + </Header> |
| 114 | + </form> |
| 115 | + {content} |
| 116 | + </Box> |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments