Skip to content

Instantly share code, notes, and snippets.

@orodio
Created August 12, 2015 23:33
Show Gist options
  • Save orodio/789ea7d5e3116c30c5ab to your computer and use it in GitHub Desktop.
Save orodio/789ea7d5e3116c30c5ab to your computer and use it in GitHub Desktop.
function value (line) {
const [, t, c, v] = line.split(" ")
return {t, p: {cursor: c.split("::"), value: v}}
}
function header (line) {
const [,...rest] = line.split(" ")
const c = rest.join(" ")
return {t: "header", c}
}
function liChecked (line) {
const [,,...rest] = line.split(" ")
return {t: "list-item", p: { checked: true }, c: rest.join(" ")}
}
function input (line) {
const [, type, name, ...label] = line.split(" ")
return {t: "input", p: {type}, c: label.join(" ")}
}
function watch (line) {
const [, type, cursor, value, ...rest] = line.split(" ")
return {t: "watch", p: {cursor: cursor.split("::")}, c: rest.join(" ")}
}
function toAst(line) {
if (line === "" || line.startsWith("//")) return null
if (line.startsWith("- value")) return value(line)
if (line.startsWith("- watch")) return watch(line)
if (line.startsWith("# ")) return header(line)
if (line.startsWith("* [x]")) return liChecked(line)
if (line.startsWith("~ ")) return input(line)
return line
}
function _Parse(rawLines = [], parsedLines=[]) {
if (!rawLines.length) return parsedLines;
let currentLine = toAst(rawLines.pop().trim());
if (currentLine == null) return _Parse(rawLines, parsedLines)
return _Parse(rawLines, [currentLine, ...parsedLines])
}
function Parse (doc) {
let lines = doc[0].split("\n")
return _Parse(lines)
}
// Symbol | What it is
// - | something to do with data
// ~ | an input
// # | heading
// * | list item
let result = Parse`
- value status default
- value fields::product 162
# This is a heading
* [x] this is a checked list
* [x] woot woot woot
* [x] ehrmagard
~ text full_name Full Name
~ email email Email
~ password password Password
- watch status default button 1
- watch status processing button 2
- watch status success button 2
`
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment