Last active
March 7, 2016 02:37
-
-
Save hovissimo/4c6da3ce84899cefbf44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import ExtendableError from 'es6-error'; | |
export class KSPDeserializer { | |
hello() { | |
return "World"; | |
} | |
} | |
export function *yield_lines(string) { | |
yield* string.split('\n'); | |
} | |
function read(lines) { | |
} | |
export class KSPNode { | |
constructor(name, lines) { | |
this.name = name; | |
for (var line of lines) { | |
let match; | |
for (var [pattern, handler] of this.rules()) { | |
match = pattern.exec(line); | |
if (match) { | |
let status = handler(match); | |
if (status && status.done) { return; } // The handler has told us that we've finished constructing this node. | |
break; | |
} | |
} | |
if (!match) { | |
// We finished looping through the rules without finding a match. Implies an error on this line. | |
throw new UnrecognizedSyntaxError(); | |
} | |
} | |
// else: This constructor should return before we get to the end of the loop. | |
throw new UnclosedNodeError("Abnormally reached end of file while waiting for a closing brace."); | |
} | |
handle_node_line() { } | |
handle_value_pair_line() { } | |
handle_open_brace() { } | |
handle_close_brace() { | |
return {done:true}; | |
} | |
noop() { } | |
add_node(node) { | |
//add the passed node to this node as a child | |
} | |
add_value_pair(name, value) { | |
// add a value-pair to this node | |
} | |
rules() { | |
return [ | |
[ /^\t*([^\s{}]+)\s*$/, this.handle_node_line ], | |
[ /\t*([^\s{}]+) = (.*?)\s*$/, this.handle_value_pair_line ], | |
[ /\t*{\s*$/, this.handle_open_brace ], | |
[ /\t*}\s*$/, this.handle_close_brace ], | |
]; | |
} | |
} | |
export class UnrecognizedSyntaxError extends ExtendableError { } | |
export class UnclosedNodeError extends ExtendableError { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment