Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active July 12, 2018 21:58
Show Gist options
  • Select an option

  • Save aziis98/f93ff34505c466f00326452719198359 to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/f93ff34505c466f00326452719198359 to your computer and use it in GitHub Desktop.
Grammar for my config language
// Aziis98 Data Grammar
// ==========================
//
{
function Symbol(text) {
this.__type__ = "Symbol";
this.text = text;
}
}
start
= root:PropertyList
Value
= value:(Integer / String / Identifier / List / Object) { return value }
Object
= "{" nl pl:PropertyList "}" {
return pl
}
PropertyList
= head:(_ KeyValue)? tail:(nl _ KeyValue)* blank {
if (head) {
return [head[1]].concat(tail.map(prop => prop[2]))
.reduce((prop, acc) => Object.assign(acc, prop), {});
}
else {
return { /* empty object */ };
}
}
KeyValue
= KeyValueAssing / KeyList / KeyObject
KeyValueAssing
= key:Key _ "="? _ value:Value { return { [key]: value } }
KeyList
= key:Key _ list:List { return { [key]: list } }
KeyObject
= key:Key _ object:Object { return { [key]: object } }
List
= "[" blank values:( Value blank? ","? blank? )* "]" {
return values.map(value => value[0])
}
Key
= id:Identifier { return id.text }
Identifier "identifier"
= Word ( _ Word)* { return new Symbol(text().trim()) }
Word
= [^0-9 \t\n\r={}\[\],]+
Integer "integer"
= [0-9]+ { return parseInt(text(), 10); }
String "string"
= '"' string:[^\n"]* '"' { return string.join('') }
blank
= [ \t\n\r]+ / EOF
nl
= [ \t]*[\n\r] blank
_ "whitespace"
= [ \t]*
EOF
= !.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment