Skip to content

Instantly share code, notes, and snippets.

@Corvimae
Created January 4, 2021 20:08
Show Gist options
  • Save Corvimae/f7df51e9c60ff0d59b75fcd71052f461 to your computer and use it in GitHub Desktop.
Save Corvimae/f7df51e9c60ff0d59b75fcd71052f461 to your computer and use it in GitHub Desktop.
conditional grammar
Expression 'expression'
= _ term:Term? _ {
return term
}
Term 'term'
= BinaryOrExpression
/ StatExpression
Literal
= IntegerLiteral
Factor
= Range
/ "(" _ term: Term _ ")" { return term; }
Stat 'stat'
= 'hp'
/ 'health'
/ 'atk'
/ 'attack'
/ 'def'
/ 'defense'
/ 'spa'
/ 'spatk'
/ 'spattack'
/ 'specialattack'
/ 'spd'
/ 'spdef'
/ 'spdefense'
/ 'specialdefense'
/ 'spe'
/ 'speed'
/ 'startingLevel'
StatExpression 'statExpression'
= _ stat: Stat _ '=' _ expression: Factor {
return {
type: 'statExpression',
stat,
expression,
};
}
BinaryOrExpression
= head: BinaryAndExpression
tail:(_ '||' _ BinaryOrExpression)* {
return tail.reduce((result, element) => ({
type: "LogicalExpression",
operator: '||',
left: result,
right: element[3]
}), head);
}
BinaryAndExpression
= head: Factor
tail:(_ '&&' _ BinaryAndExpression)* {
return tail.reduce((result, element) => ({
type: "LogicalExpression",
operator: '&&',
left: result,
right: element[3]
}), head);
}
IntegerLiteral 'integer'
= _ [0-9]+ {
return parseInt(text(), 10);
}
Range =
IVRange
/ RangeSegment
IVRange 'ivRange'
= _ negative: RangeSegment _ '/' _ neutral: RangeSegment _ '/' _ positive: RangeSegment _ {
return {
type: 'ivRange',
negative,
neutral,
positive,
};
}
RangeSegment
= BoundedRange
/ UnboundedRange
/ IntegerLiteral
UnboundedRange
= value: IntegerLiteral _ operator: ('-' / '+') {
return {
type: 'unboundedRange',
value,
operator,
};
}
BoundedRange
= from: IntegerLiteral _ ('–' / '-' / '—') _ to: IntegerLiteral {
if (to < from) throw new Error('BoundedRange: upper limit must be greater than or equal to lower limit');
return {
type: 'boundedRange',
from,
to,
};
}
_ "whitespace"
= [ \t\n\r]*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment