Skip to content

Instantly share code, notes, and snippets.

@jminer
Created March 20, 2012 00:08
Show Gist options
  • Save jminer/2128764 to your computer and use it in GitHub Desktop.
Save jminer/2128764 to your computer and use it in GitHub Desktop.
import pegged.grammar;
import std.stdio;
import std.conv;
mixin(grammar(`
RubySource <- S Expr* End
Until(Expr) <~ (!Expr > .)* Expr
S <- Comment / :(Space / '\t' / EOL)
Comment <- '#' > (!EOL > .)* > (EOL / EOI)
Expr <- FloatLiteral / IntegerLiteral
# OctLiteral comes before DecLiteral
IntegerLiteral <~ HexLiteral / BinLiteral / OctLiteral / DecLiteral
FloatLiteral <~ DecLiteral > '.' > DecLiteral > ([eE] > [+-]? > [0-9]+)?
#FloatLiteral <~ DecLiteral > '.' > DecLiteral > ([eE] > [+-]? > DecLiteral)?
DecLiteral <- [0-9] > ('_'? > [0-9])*
HexLiteral <- '0' > [xX] > [0-9a-fA-F] > ('_'? > [0-9a-fA-F])*
BinLiteral <- '0' > [bB] > [0-1] > ('_'? > [0-1])*
OctLiteral <- '0' DecLiteral
AddOp <- '+'
SubOp <- '-'
MulOp <- '*'
DivOp <- '/'
AndOp1 <- '&&'
AndOp2 <- 'and'
OrOp1 <- '||'
OrOp2 <- 'or'
NotOp1 <- '!'
NotOp2 <- 'not'
`));
/*
5.0
3e2
3E2
3e-2
3e+2
3E-2
5.3e2
0x12_3Fa
0XAfc
*/
void main() {
auto str = `
0XAfc
`;
auto tree = RubySource.parse(str);
writefln("Tree: %s", tree);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment