Skip to content

Instantly share code, notes, and snippets.

@DanielVF
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save DanielVF/9883117 to your computer and use it in GitHub Desktop.

Select an option

Save DanielVF/9883117 to your computer and use it in GitHub Desktop.
peg.js parser for kerbal config files

This is a parser definition for peg.js, for parsing Kerbal Space Program config files.

Before use:

  • strip everything after comments
  • strip leading and trailing whitespace
  • remove multiple newlines in a row ([\r\n]+) and replace with single new lines
  • strip remove any leading blank line
  • add trailing blank line

This creates key value array pairs, like:

[
  ["key", "value"],
  ["key2", "value"],
  ["LISTKEY", [
     ["key", "value"],
     ["key2", "value"]
  ]
]

Keys are always strings. Values are either strings or arrays.

start
= nodes
nodes
= node*
node
= list
/ stringkeyvalue
list
= whitespace key:keyword newline? whitespace "{" newline? nodes:nodes whitespace? "}" newline { return [key,nodes] }
stringkeyvalue
= whitespace key:keyword whitespace "=" whitespace value:string newline { return [key,value]; }
keyword
= str:[A-Za-z0-9_.]* { return str.join("")}
string
= str:[^\r\n]* { return str.join("")}
whitespace
= [ \t]*
newline
= [\r\n]+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment