Created
July 28, 2012 10:42
-
-
Save jakubkulhan/3192844 to your computer and use it in GitHub Desktop.
Python style indentation parser in PEG.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// do not use result cache, nor line and column tracking | |
{ var indentStack = [], indent = ""; } | |
start | |
= INDENT? lines:( blank / line )* | |
{ return lines; } | |
line | |
= SAMEDENT line:(!EOL c:. { return c; })+ EOL? | |
children:( b:blank* INDENT c:( blank / line )* DEDENT { return b.concat(c); })? | |
{ return [line.join(""), children === "" ? [] : children]; } | |
blank | |
= [ \t]* EOL | |
{ return undefined; } | |
EOL | |
= "\r\n" / "\n" / "\r" | |
SAMEDENT | |
= i:[ \t]* &{ return i.join("") === indent; } | |
INDENT | |
= i:[ \t]+ &{ return i.length > indent.length; } | |
{ indentStack.push(indent); indent = i.join(""); pos = offset; } | |
DEDENT | |
= { indent = indentStack.pop(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was also not able to get this running. This example works well.