~kylep/pipes

72e64be9e7c58ed23263ac5147c14e2ef77214e5 — Kyle Perik 2 months ago 6c45b4b
Simplify define statements
3 files changed, 23 insertions(+), 15 deletions(-)

M README.md
M src/main.js
M src/parser.js
M README.md => README.md +3 -4
@@ 74,12 74,11 @@ Definitions enable you to package a set of rules
into a reusable pattern, making use very succinct.

```
def input {
def input
  ['input', value, state] : value;
}
def log {

def log
  value : ['log', value];
}

input | 'marco' : 'polo' | log;
```

M src/main.js => src/main.js +5 -1
@@ 194,7 194,11 @@ window.onload = () => {
  if (location.hash) {
    programText = atob(location.hash.replace('#', ''));
  }
  compile(programText)
  try {
    compile(programText)
  } catch (e) {
    console.error(e)
  }
  initEditor(programText, compile)
  initDevices()
}

M src/parser.js => src/parser.js +15 -10
@@ 302,16 302,21 @@ function parseGroups (tokenGroups) {
          key: token
        }
      }
      if (typeof token !== 'object' || token.type !== 'brackets') {
        throw new Error(`Unexpected definition: ${ JSON.stringify(token) }`)
      }
      const newDefinitions = {...r.definitions}
      newDefinitions[r.key] = parseGroups(token.tokens).patterns
      return {
        ...r,
        key: null,
        definitions: newDefinitions,
        mode: null
      if (token === ';') {
        const newDefinitions = {...r.definitions}
        newDefinitions[r.key] = parseGroups(r.pendingTokens).patterns
        return {
          ...r,
          key: null,
          pendingTokens: [],
          definitions: newDefinitions,
          mode: null
        }
      } else {
        return {
          ...r,
          pendingTokens: r.pendingTokens.concat([token])
        }
      }
    }
    throw new Error(`Unexpected token for mode: ${JSON.stringify(token)}, ${r.mode}`)