src/grammar.peg
{
function Step(name, qualifiers) {
this.name = name;
var that = this;
qualifiers.forEach(function(q) {
q.augment(that);
})
}
function IndexQualifier(index) {
this.index = index;
this.augment = function (step) {
step.index = this.index;
}
}
function QueryQualifier(text) {
this.text = text;
this.augment = function (step) {
step.query = this.text;
}
}
function Path(left, right) {
this.steps = [];
function addSteps(source) {
if (source.steps) {
this.steps = this.steps.concat(source.steps);
} else {
this.steps.push(source);
}
}
addSteps.apply(this, [left]);
addSteps.apply(this, [right]);
}
function Instruction(path, invocation) {
this.path = path.steps || [ path ];
this.invocation = invocation || {};
}
function Invocation(commands) {
var that = this;
commands.forEach(function(q) {
q.augment(that);
})
}
function With(name) {
this.name = name;
this.augment = function (invocation) {
invocation.with = name;
}
}
function As(name) {
this.name = name;
this.augment = function (invocation) {
invocation.as = name;
}
}
function Emits(code) {
this.code = code;
this.augment = function(invocation) {
invocation.emits = code;
}
}
}
start
= instruction
instruction
= path:path commands:command* { return new Instruction(path, new Invocation(commands)); }
path
= left:step "." right:path { return new Path(left, right); }
/ step
step
= name:identifier qualifiers:qualifier* { return new Step(name, qualifiers); }
qualifier
= queryQualifier
/ indexQualifier
queryQualifier
= "?{" query:queryText "}" { return new QueryQualifier(query); }
indexQualifier
= "[" index:integer "]" { return new IndexQualifier(index); }
queryText "string"
= chars: [^{}]* { return chars.join(""); }
command
= withCommand
/ asCommand
/ emitsCommand
withCommand
= " with "i name:identifier { return new With(name); }
asCommand
= " as "i name:identifier { return new As(name); }
emitsCommand
= " emits "i responseCode:integer { return new Emits(responseCode); }
integer
= digits:[0-9]+ { return parseInt(digits.join(""), 10); }
identifier
= $([_a-zA-Z]+[-_a-zA-Z0-9]*)