Skip to content

Commit

Permalink
that was difficult
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mock committed Jul 8, 2017
1 parent 2fa4f0a commit e248b37
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions examples/python-ish.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@ let P = require('..');

///////////////////////////////////////////////////////////////////////

let sneak = P((str, i, state) => {
console.log('\n\n\n----');
// console.log(JSON.stringify(str.slice(i)));
console.log(str.slice(i));
console.log(i, state);
return P.makeSuccess(i, null, state);
});

let Pythonish = P.createLanguage({
Program: r =>
r.Statement.many().trim(r._).node('Program'),

Statement: r =>
P.alt(r.Call, r.Block),
P.alt(r.Call, r.Block).trim(r._),

Call: r =>
P.regexp(/[a-z]+/).skip(P.string('()')).skip(r.End).node('Call'),
P.regexp(/[a-z]+/)
.skip(P.string('()'))
.skip(r.Terminator)
.node('Call'),

Block: r =>
P.seqObj(
P.string('block:'),
r.End,
r.Terminator,
r._,
r.IndentMore,
['first', r.Statement],
['rest', r.IndentSame.then(r.Statement).many()],
Expand All @@ -35,7 +47,17 @@ let Pythonish = P.createLanguage({
IndentLess: () => P.indentLess(P.countIndentation),
IndentSame: () => P.indentSame(P.countIndentation),

_: () => P.optWhitespace,
Comment: r =>
P.seq(
P.string('#'),
P.regexp(/[^\r\n]*/),
r.End
),
_: r => r.BlankLine.many(),
BlankLine: r => r.Spaces0.then(P.alt(r.Comment, r.NL)),
Terminator: r =>
r.Spaces0.then(P.alt(r.Comment, r.End)),
Spaces0: () => P.regexp(/[ \t]*/),
CR: () => P.string('\r'),
LF: () => P.string('\n'),
CRLF: () => P.string('\r\n'),
Expand All @@ -45,11 +67,22 @@ let Pythonish = P.createLanguage({

///////////////////////////////////////////////////////////////////////

let SPACE = ' ';

let text = `\
z()
block:
a()
b()
#c0
z() #c1'\r\n\
block: # c2\r
a() #c3
b() # c4
${SPACE}${SPACE}
${SPACE}${SPACE}# comment
block:
c()
d()
Expand All @@ -62,7 +95,7 @@ block:
bb()
bc()
e()
f()
f()#c
`;

function prettyPrint(x) {
Expand All @@ -71,5 +104,7 @@ function prettyPrint(x) {
console.log(s);
}

console.log(new Date().toTimeString());
console.log();
let ast = Pythonish.Program.tryParse(text);
prettyPrint(ast);

0 comments on commit e248b37

Please sign in to comment.